1'''
2Relay - Relay_set_channel.py with asynchronous mode.
3
4This example demonstrates how to write channel from EthanL.
5
6-------------------------------------------------------------------------------------
7Please change correct serial number or IP and port number BEFORE you run example code.
8
9For other examples please check:
10 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
11See README.md file to get detailed usage of this example.
12
13Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
14'''
15
16## Python
17import asyncio
18
19## WPC
20
21from wpcsys import pywpc
22
23async def main():
24 ## Get Python driver version
25 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
26
27 ## Create device handle
28 dev = pywpc.EthanL()
29
30 ## Connect to device
31 try:
32 dev.connect("192.168.1.110") ## Depend on your device
33 except Exception as err:
34 pywpc.printGenericError(err)
35 ## Release device handle
36 dev.close()
37 return
38
39 try:
40 ## Parameters setting
41 port = 0 ## Depend on your device
42 DO_port = 0
43
44 ## Get firmware model & version
45 driver_info = await dev.Sys_getDriverInfo_async()
46 print("Model name: " + driver_info[0])
47 print("Firmware version: " + driver_info[-1])
48
49 ## Open relay
50 err = await dev.Relay_open_async(port)
51 print(f"Relay_open_async in port {port}, status: {err}")
52
53 ## Toggle digital state for 10 times. Each times delay for 0.5 second
54 for i in range(10):
55 if i%2 == 0:
56 value = [0, 0, 0, 0, 0, 0]
57 else:
58 value = [1, 1, 1, 1, 1, 1]
59
60 await dev.DO_writePort_async(DO_port, value)
61 print(f'Port: {DO_port}, digital state= {value}')
62
63 ## Wait
64 await asyncio.sleep(0.5) ## delay [s]
65
66 ## Close relay
67 err = await dev.Relay_close_async(port)
68 print(f"Relay_close_async in port {port}, status: {err}")
69 except Exception as err:
70 pywpc.printGenericError(err)
71
72 ## Disconnect device
73 dev.disconnect()
74
75 ## Release device handle
76 dev.close()
77
78 return
79
80def main_for_spyder(*args):
81 if asyncio.get_event_loop().is_running():
82 return asyncio.create_task(main(*args)).result()
83 else:
84 return asyncio.run(main(*args))
85
86if __name__ == '__main__':
87 asyncio.run(main()) ## Use terminal
88 # await main() ## Use Jupyter or IPython(>=7.0)
89 # main_for_spyder() ## Use Spyder