1'''
2AO - AO_write_one_channel.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 with channel 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 vaule in channel 0
73 err = await dev.AO_writeOneChannel_async(slot, 0, ao_value_list[0])
74 print(f"In slot {slot} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
75
76 ## Write AO vaule in channel 1
77 err = await dev.AO_writeOneChannel_async(slot, 1, ao_value_list[1])
78 print(f"In slot {slot} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
79
80 ## Write AO vaule in channel 2
81 err = await dev.AO_writeOneChannel_async(slot, 2, ao_value_list[2])
82 print(f"In slot {slot} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
83
84 ## Write AO vaule in channel 3
85 err = await dev.AO_writeOneChannel_async(slot, 3, ao_value_list[3])
86 print(f"In slot {slot} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
87
88 ## Close AO
89 err = await dev.AO_close_async(slot)
90 print(f"AO_close_async in slot {slot}, status: {err}")
91 except Exception as err:
92 pywpc.printGenericError(err)
93
94 ## Disconnect device
95 dev.disconnect()
96
97 ## Release device handle
98 dev.close()
99
100 return
101
102def main_for_spyder(*args):
103 if asyncio.get_event_loop().is_running():
104 return asyncio.create_task(main(*args)).result()
105 else:
106 return asyncio.run(main(*args))
107if __name__ == '__main__':
108 asyncio.run(main()) ## Use terminal
109 # await main() ## Use Jupyter or IPython(>=7.0)
110 # main_for_spyder() ## Use Spyder