DIO loopback pins

 1'''
 2DIO - DIO_loopback_pins.py with synchronous mode.
 3
 4This example demonstrates the process of DIO loopback using pins from USBDAQF1RD.
 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-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.USBDAQF1RD()
35
36    ## Connect to device
37    try:
38        dev.connect("default") ## 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        timeout = 3 ## second
48        DO_port = 0 ## Depend on your device
49        DI_port = 1
50        DO_pins = [0, 1, 2, 3]
51        DI_pins = [0, 1, 2, 3]
52        DO_value = [1, 0, 1, 0]
53
54        ## Get firmware model & version
55        driver_info = dev.Sys_getDriverInfo(timeout)
56        print("Model name: " + driver_info[0])
57        print("Firmware version: " + driver_info[-1])
58
59        ## Open pins with digital output
60        err = dev.DO_openPins(DO_port, DO_pins, timeout)
61        print(f"DO_openPins in DO_port {DO_port}, status: {err}")
62
63        ## Write pins to high or low
64        err = dev.DO_writePins(DO_port, DO_pins, DO_value, timeout)
65        print(f"DO_writePins in {DO_port}, status: {err}")
66
67        ## Open pins with digital iutput
68        err = dev.DI_openPins(DI_port, DI_pins, timeout)
69        print(f"DI_openPins in DI_port {DI_port}, status: {err}")
70
71        ## Read pins state
72        state_list = dev.DI_readPins(DI_port, DI_pins, timeout)
73        print(f"state_list in DI_port {DI_port}: {state_list}")
74
75        ## Close pins with digital output
76        err = dev.DO_closePins(DO_port, DO_pins, timeout)
77        print(f"DO_closePins in DO_port {DO_port}, status: {err}")
78
79        ## Close pins with digital input
80        err = dev.DI_closePins(DI_port, DI_pins, timeout)
81        print(f"DI_closePins in DI_port {DI_port}, status: {err}")
82    except Exception as err:
83        pywpc.printGenericError(err)
84
85    ## Disconnect device
86    dev.disconnect()
87
88    ## Release device handle
89    dev.close()
90
91    return
92
93if __name__ == '__main__':
94    main()