DIO loopback port

 1'''
 2DIO - DIO_loopback_port.py with synchronous mode.
 3
 4This example demonstrates the process of DIO loopback using port from USBDAQF1AOD.
 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
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.USBDAQF1AOD()
31
32    ## Connect to device
33    try:
34        dev.connect("default")  ## 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        DO_port = 0  ## Depend on your device
44        DI_port = 1
45        DO_value = [1, 0, 1, 0]
46        timeout = 3  ## [sec]
47
48        ## Get firmware model & version
49        driver_info = dev.Sys_getDriverInfo(timeout)
50        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
51
52        ## Open DO port with digital output
53        err = dev.DO_openPort(DO_port, timeout)
54        print(f"DO_openPort in DO_port {DO_port}, status: {err}")
55
56        ## Open DI port with digital input
57        err = dev.DI_openPort(DI_port, timeout)
58        print(f"DI_openPort in DI_port {DI_port}, status: {err}")
59
60        ## Write DO port to high or low
61        err = dev.DO_writePort(DO_port, DO_value, timeout)
62        print(f"DO_writePort in DO_port {DO_port}, status: {err}")
63
64        ## Read DI port state
65        state_list = dev.DI_readPort(DI_port, timeout)
66        print(f"state_list in DI_port {DI_port}: {state_list}")
67
68        ## Close DO port with digital output
69        err = dev.DO_closePort(DO_port, timeout)
70        print(f"DO_closePort in DO_port {DO_port}, status: {err}")
71
72        ## Close DI port with digital input
73        err = dev.DI_closePort(DI_port, timeout)
74        print(f"DI_closePort in DI_port {DI_port}, status: {err}")
75    except Exception as err:
76        pywpc.printGenericError(err)
77
78    finally:
79        ## Disconnect device
80        dev.disconnect()
81
82        ## Release device handle
83        dev.close()
84
85
86if __name__ == '__main__':
87    main()