1'''
2DIO - DIO_loopback_port.py with asynchronous mode.
3
4This example demonstrates the process of DIO loopback using port from WifiDAQE3AOD.
5It involves using DO port to send signals and DI port to receive signals on a single device, commonly known as "loopback".
6
7To begin with, it illustrates the steps required to open the DO and DI port.
8Next, it performs the operation of writing to a DO pin and reading from a DI pin.
9Lastly, it concludes by closing the DO and DI port.
10
11-------------------------------------------------------------------------------------
12Please change correct serial number or IP and port number BEFORE you run example code.
13
14For other examples please check:
15 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
16See README.md file to get detailed usage of this example.
17
18Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
19'''
20
21## WPC
22from wpcsys import pywpc
23
24## Python
25import asyncio
26import sys
27sys.path.insert(0, 'src/')
28
29
30async def main():
31 ## Get Python driver version
32 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
33
34 ## Create device handle
35 dev = pywpc.WifiDAQE3AOD()
36
37 ## Connect to device
38 try:
39 dev.connect("192.168.5.38") ## Depend on your device
40 except Exception as err:
41 pywpc.printGenericError(err)
42 ## Release device handle
43 dev.close()
44 return
45
46 try:
47 ## Parameters setting
48 DO_port = 0 ## Depend on your device
49 DI_port = 3
50 DO_value = [1, 0, 1, 0]
51
52 ## Get firmware model & version
53 driver_info = await dev.Sys_getDriverInfo_async()
54 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
55
56 ## Open DO port with digital output
57 err = await dev.DO_openPort_async(DO_port)
58 print(f"DO_openPort_async in DO_port {DO_port}, status: {err}")
59
60 ## Open DI port with digital input
61 err = await dev.DI_openPort_async(DI_port)
62 print(f"DI_openPort_async in DI_port {DI_port}, status: {err}")
63
64 ## Write DO port to high or low
65 err = await dev.DO_writePort_async(DO_port, DO_value)
66 print(f"DO_writePort_async in DO_port {DO_port}, status: {err}")
67
68 ## Read DI port state
69 state_list = await dev.DI_readPort_async(DI_port)
70 print(f"state_list{state_list}")
71
72 ## Close DO port with digital output
73 err = await dev.DO_closePort_async(DO_port)
74 print(f"DO_closePort_async in DO_port {DO_port}, status: {err}")
75
76 ## Close DI port with digital input
77 err = await dev.DI_closePort_async(DI_port)
78 print(f"DI_closePort_async in DI_port {DI_port}, status: {err}")
79 except Exception as err:
80 pywpc.printGenericError(err)
81
82 finally:
83 ## Disconnect device
84 dev.disconnect()
85
86 ## Release device handle
87 dev.close()
88
89
90def main_for_spyder(*args):
91 if asyncio.get_event_loop().is_running():
92 return asyncio.create_task(main(*args)).result()
93 else:
94 return asyncio.run(main(*args))
95
96
97if __name__ == '__main__':
98 asyncio.run(main()) ## Use terminal
99 # await main() ## Use Jupyter or IPython(>=7.0)
100 # main_for_spyder() ## Use Spyder