1'''
2DIO - DO_blinky_pins.py with asynchronous mode.
3
4This example illustrates the process of writing a high or low signal to a DO pin from EthanD.
5
6To begin with, it demonstrates the steps required to open the DO pin.
7Next, in each loop, a different voltage output is applied, resulting in a blinking effect.
8Lastly, it concludes by closing the DO pin.
9
10-------------------------------------------------------------------------------------
11Please change correct serial number or IP and port number BEFORE you run example code.
12
13For other examples please check:
14 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
15See README.md file to get detailed usage of this example.
16
17Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
18'''
19
20## WPC
21from wpcsys import pywpc
22
23## Python
24import asyncio
25import sys
26sys.path.insert(0, 'src/')
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.EthanD()
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 port = 0 ## Depend on your device
48 pinindex = [1, 3, 5, 7]
49
50 ## Get firmware model & version
51 driver_info = await dev.Sys_getDriverInfo_async()
52 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
53
54 ## Open pins with digital output
55 err = await dev.DO_openPins_async(port, pinindex)
56 print(f"DO_openPins_async in port {port}, status: {err}")
57
58 ## Toggle digital state for 10 times. Each times delay for 0.5 second
59 for i in range(10):
60 state = await dev.DO_togglePins_async(port, pinindex)
61 print(state)
62
63 ## Wait for 0.5 second to see led status
64 await asyncio.sleep(0.5) ## delay [sec]
65
66 ## Close pins with digital output
67 err = await dev.DO_closePins_async(port, pinindex)
68 print(f"DO_closePins_async in port {port}, status: {err}")
69 except Exception as err:
70 pywpc.printGenericError(err)
71
72 finally:
73 ## Disconnect device
74 dev.disconnect()
75
76 ## Release device handle
77 dev.close()
78
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
86
87if __name__ == '__main__':
88 asyncio.run(main()) ## Use terminal
89 # await main() ## Use Jupyter or IPython(>=7.0)
90 # main_for_spyder() ## Use Spyder