1'''
2DIO - DO_blinky_port.py with asynchronous mode.
3
4This example illustrates the process of writing a high or low signal to a DO port from USBDAQF1DSNK.
5
6To begin with, it demonstrates the steps required to open the DO port.
7Next, in each loop, a different voltage output is applied, resulting in a blinking effect.
8Lastly, it concludes by closing the DO port.
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-2024 WPC Systems Ltd. All rights reserved.
18'''
19
20## Python
21import asyncio
22
23## WPC
24
25from wpcsys import pywpc
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.USBDAQF1DSNK()
34
35 ## Connect to device
36 try:
37 dev.connect("default") ## 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 = 2 ## Depend on your device
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 port with digital output
54 err = await dev.DO_openPort_async(port)
55 print(f"DO_openPort_async in port {port}, status: {err}")
56
57 ## Toggle digital state for 10 times. Each times delay for 0.5 second
58 for i in range(10):
59 state = await dev.DO_togglePort_async(port)
60 print(state)
61
62 ## Wait for 0.5 second to see led status
63 await asyncio.sleep(0.5) ## delay [s]
64
65 ## Close port with digital output
66 err = await dev.DO_closePort_async(port)
67 print(f"DO_closePort_async in port {port}, status: {err}")
68 except Exception as err:
69 pywpc.printGenericError(err)
70
71 ## Disconnect device
72 dev.disconnect()
73
74 ## Release device handle
75 dev.close()
76
77 return
78
79def main_for_spyder(*args):
80 if asyncio.get_event_loop().is_running():
81 return asyncio.create_task(main(*args)).result()
82 else:
83 return asyncio.run(main(*args))
84if __name__ == '__main__':
85 asyncio.run(main()) ## Use terminal
86 # await main() ## Use Jupyter or IPython(>=7.0)
87 # main_for_spyder() ## Use Spyder