1'''
2AO - AO_write_one_channel.py with asynchronous mode.
3
4This example demonstrates the process of writing AO signal of WifiDAQE3AOD.
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-2024 WPC Systems Ltd. All rights reserved.
17'''
18
19## Python
20import asyncio
21
22## WPC
23
24from wpcsys import pywpc
25
26
27async def main():
28 ## Get Python driver version
29 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
30
31 ## Create device handle
32 dev = pywpc.WifiDAQE3AOD()
33
34 ## Connect to device
35 try:
36 dev.connect("192.168.5.38") ## Depend on your device
37 except Exception as err:
38 pywpc.printGenericError(err)
39 ## Release device handle
40 dev.close()
41 return
42
43 try:
44 ## Parameters setting
45 port = 0 ## Depend on your device
46 ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
47
48 ## Get firmware model & version
49 driver_info = await dev.Sys_getDriverInfo_async()
50 print("Model name: " + driver_info[0])
51 print("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 ## Disconnect device
80 dev.disconnect()
81
82 ## Release device handle
83 dev.close()
84
85 return
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))
92if __name__ == '__main__':
93 asyncio.run(main()) ## Use terminal
94 # await main() ## Use Jupyter or IPython(>=7.0)
95 # main_for_spyder() ## Use Spyder