1'''
2AO - AO_write_all_channels.py with asynchronous mode.
3
4This example demonstrates the process of writing AO signal of STEM.
5To begin with, it demonstrates the steps to open AO.
6Next, it outlines the procedure for writing digital signals simultaneously to the AO pins.
7Finally, it concludes by explaining how to close AO.
8
9If your product is "STEM", please invoke the function `Sys_setAIOMode_async`.
10
11-------------------------------------------------------------------------------------
12Please change correct serial number or IP and port number BEFORE you run example code.
13
14For other examples please check:
15 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
16See README.md file to get detailed usage of this example.
17
18Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
19'''
20
21## Python
22import asyncio
23
24## WPC
25
26from wpcsys import pywpc
27
28
29async def main():
30 ## Get Python driver version
31 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
32
33 ## Create device handle
34 dev = pywpc.STEM()
35
36 ## Connect to device
37 try:
38 dev.connect("192.168.1.110") ## Depend on your device
39 except Exception as err:
40 pywpc.printGenericError(err)
41 ## Release device handle
42 dev.close()
43 return
44
45 try:
46 ## Parameters setting
47 slot = 1 ## Connect AIO module to slot
48 ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
49
50 ## Get firmware model & version
51 driver_info = await dev.Sys_getDriverInfo_async()
52 print("Model name: " + driver_info[0])
53 print("Firmware version: " + driver_info[-1])
54
55 ## Get slot mode
56 slot_mode = await dev.Sys_getMode_async(slot)
57 print("Slot mode:", slot_mode)
58
59 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
60 if slot_mode != "AIO":
61 err = await dev.Sys_setAIOMode_async(slot)
62 print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
63
64 ## Get slot mode
65 slot_mode = await dev.Sys_getMode_async(slot)
66 print("Slot mode:", slot_mode)
67
68 ## Open AO
69 err = await dev.AO_open_async(slot)
70 print(f"AO_open_async in slot {slot}, status: {err}")
71
72 ## Write AO value simultaneously
73 err = await dev.AO_writeAllChannels_async(slot, ao_value_list)
74 print(f"In slot {slot} the AO value is {ao_value_list}, status: {err}")
75
76 ## Close AO
77 err = await dev.AO_close_async(slot)
78 print(f"AO_close_async in slot {slot}, status: {err}")
79 except Exception as err:
80 pywpc.printGenericError(err)
81
82 ## Disconnect device
83 dev.disconnect()
84
85 ## Release device handle
86 dev.close()
87
88 return
89
90def main_for_spyder(*args):
91 if asyncio.get_event_loop().is_running():
92 return asyncio.create_task(main(*args)).result()
93 else:
94 return asyncio.run(main(*args))
95if __name__ == '__main__':
96 asyncio.run(main()) ## Use terminal
97 # await main() ## Use Jupyter or IPython(>=7.0)
98 # main_for_spyder() ## Use Spyder