DIO loopback port

 1'''
 2DIO - DIO_loopback_port.py with synchronous 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 time
23
24## WPC
25
26from wpcsys import pywpc
27
28
29def 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        timeout = 3 ## second
51
52        ## Get firmware model & version
53        driver_info = dev.Sys_getDriverInfo(timeout)
54        print("Model name: " + driver_info[0])
55        print("Firmware version: " + driver_info[-1])
56
57        ## Open DO port with digital output
58        err = dev.DO_openPort(DO_port, timeout)
59        print(f"DO_openPort in DO_port {DO_port}, status: {err}")
60
61        ## Open DI port with digital input
62        err = dev.DI_openPort(DI_port, timeout)
63        print(f"DI_openPort in DI_port {DI_port}, status: {err}")
64
65        ## Write DO port to high or low
66        err = dev.DO_writePort(DO_port, DO_value, timeout)
67        print(f"DO_writePort in DO_port {DO_port}, status: {err}")
68
69        ## Read DI port state
70        state_list = dev.DI_readPort(DI_port, timeout)
71        print(f"state_list in DI_port {DI_port}: {state_list}")
72
73        ## Close DO port with digital output
74        err = dev.DO_closePort(DO_port, timeout)
75        print(f"DO_closePort in DO_port {DO_port}, status: {err}")
76
77        ## Close DI port with digital input
78        err = dev.DI_closePort(DI_port, timeout)
79        print(f"DI_closePort in DI_port {DI_port}, status: {err}")
80    except Exception as err:
81        pywpc.printGenericError(err)
82
83    ## Disconnect device
84    dev.disconnect()
85
86    ## Release device handle
87    dev.close()
88
89    return
90
91if __name__ == '__main__':
92    main()