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