1'''
2DIO - DIO_loopback_pins.py with synchronous mode.
3
4This example demonstrates the process of DIO loopback using pins from STEM.
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
11If your product is "STEM", please invoke the function `Sys_setDIOMode`.
12
13The DIO ports 0 to 1 are assigned to slot 1, while ports 2 to 3 are assigned to slot 2.
14---------------------------
15| Slot 1 port 1 & 0 |
16| Slot 2 port 3 & 2 |
17| Slot 3 port 5 & 4 |
18| Slot 4 port 7 & 6 |
19---------------------------
20
21-------------------------------------------------------------------------------------
22Please change correct serial number or IP and port number BEFORE you run example code.
23
24For other examples please check:
25 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
26See README.md file to get detailed usage of this example.
27
28Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
29'''
30
31## WPC
32from wpcsys import pywpc
33
34
35def main():
36 ## Get Python driver version
37 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
38
39 ## Create device handle
40 dev = pywpc.STEM()
41
42 ## Connect to device
43 try:
44 dev.connect("192.168.1.110") ## Depend on your device
45 except Exception as err:
46 pywpc.printGenericError(err)
47 ## Release device handle
48 dev.close()
49 return
50
51 try:
52 ## Parameters setting
53 slot = 1 ## Connect DIO module to slot
54 timeout = 3 ## [sec]
55 DO_port = 0
56 DI_port = 1
57 DO_pins = [0, 1, 2, 3]
58 DI_pins = [4, 5, 6, 7]
59 DO_value = [1, 0, 1, 0]
60
61 ## Get firmware model & version
62 driver_info = dev.Sys_getDriverInfo(timeout)
63 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
64
65 ## Get slot mode
66 slot_mode = dev.Sys_getMode(slot, timeout)
67 print("Slot mode:", slot_mode)
68
69 ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
70 if slot_mode != "DIO":
71 err = dev.Sys_setDIOMode(slot, timeout)
72 print(f"Sys_setDIOMode in slot {slot}, status: {err}")
73
74 ## Get slot mode
75 slot_mode = dev.Sys_getMode(slot, timeout)
76 print("Slot mode:", slot_mode)
77
78 ## Get DIO start up information
79 info = dev.DIO_loadStartup(DO_port, timeout)
80 print(f"Enable: {info[0]}")
81 print(f"Direction: {info[1]}")
82 print(f"State: {info[2]}")
83
84 ## Write pins to high or low
85 err = dev.DO_writePins(DO_port, DO_pins, DO_value, timeout)
86 print(f"DO_writePins in DO_port {DO_port}, status: {err}")
87
88 ## Read pins state
89 state_list = dev.DI_readPins(DI_port, DI_pins, timeout)
90 print(f"state_list in DI_port {DI_port}: {state_list}")
91 except Exception as err:
92 pywpc.printGenericError(err)
93
94 finally:
95 ## Disconnect device
96 dev.disconnect()
97
98 ## Release device handle
99 dev.close()
100
101
102if __name__ == '__main__':
103 main()