1'''
2DIO - DIO_loopback_port.py with asynchronous mode.
3
4This example demonstrates the process of DIO loopback using port from EthanD.
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-2024 WPC Systems Ltd. All rights reserved.
19'''
20
21## Python
22import asyncio
23
24## WPC
25
26from wpcsys import pywpc
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 DO_port = 0 ## Depend on your device
48 DI_port = 1
49 DO_value = [1, 0, 1, 0]
50
51 ## Get firmware model & version
52 driver_info = await dev.Sys_getDriverInfo_async()
53 print("Model name: " + driver_info[0])
54 print("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 ## Disconnect device
83 dev.disconnect()
84
85 ## Release device handle
86 dev.close()
87
88 return
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))
95if __name__ == '__main__':
96 asyncio.run(main()) ## Use terminal
97 # await main() ## Use Jupyter or IPython(>=7.0)
98 # main_for_spyder() ## Use Spyder