1'''
2DIO - DIO_loopback_pins.py with synchronous mode.
3
4This example demonstrates the process of DIO loopback using pins from EthanEXD.
5It involves using DO pins to send signals and DI pins 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 pins.
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 pins.
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
25def main():
26 ## Get Python driver version
27 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
28
29 ## Create device handle
30 dev = pywpc.EthanEXD()
31
32 ## Connect to device
33 try:
34 dev.connect("192.168.1.110") ## Depend on your device
35 except Exception as err:
36 pywpc.printGenericError(err)
37 ## Release device handle
38 dev.close()
39 return
40
41 try:
42 ## Parameters setting
43 timeout = 3 ## [sec]
44 DO_port = 0 ## Depend on your device
45 DI_port = 2
46 DO_pins = [0, 1, 2, 3]
47 DI_pins = [0, 1, 2, 3]
48 DO_value = [1, 0, 1, 0]
49
50 ## Get firmware model & version
51 driver_info = dev.Sys_getDriverInfo(timeout)
52 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
53
54 ## Open pins with digital output
55 err = dev.DO_openPins(DO_port, DO_pins, timeout)
56 print(f"DO_openPins in DO_port {DO_port}, status: {err}")
57
58 ## Write pins to high or low
59 err = dev.DO_writePins(DO_port, DO_pins, DO_value, timeout)
60 print(f"DO_writePins in {DO_port}, status: {err}")
61
62 ## Open pins with digital iutput
63 err = dev.DI_openPins(DI_port, DI_pins, timeout)
64 print(f"DI_openPins in DI_port {DI_port}, status: {err}")
65
66 ## Read pins state
67 state_list = dev.DI_readPins(DI_port, DI_pins, timeout)
68 print(f"state_list in DI_port {DI_port}: {state_list}")
69
70 ## Close pins with digital output
71 err = dev.DO_closePins(DO_port, DO_pins, timeout)
72 print(f"DO_closePins in DO_port {DO_port}, status: {err}")
73
74 ## Close pins with digital input
75 err = dev.DI_closePins(DI_port, DI_pins, timeout)
76 print(f"DI_closePins in DI_port {DI_port}, status: {err}")
77 except Exception as err:
78 pywpc.printGenericError(err)
79
80 finally:
81 ## Disconnect device
82 dev.disconnect()
83
84 ## Release device handle
85 dev.close()
86
87
88if __name__ == '__main__':
89 main()