1'''
2DIO - DIO_loopback_pins.py with asynchronous 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_async`.
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-2024 WPC Systems Ltd. All rights reserved.
29'''
30
31## Python
32import asyncio
33
34## WPC
35
36from wpcsys import pywpc
37
38
39async def main():
40 ## Get Python driver version
41 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
42
43 ## Create device handle
44 dev = pywpc.STEM()
45
46 ## Connect to device
47 try:
48 dev.connect("192.168.1.110") ## Depend on your device
49 except Exception as err:
50 pywpc.printGenericError(err)
51 ## Release device handle
52 dev.close()
53 return
54
55 try:
56 ## Parameters setting
57 slot = 1 ## Connect DIO module to slot
58 DO_port = 0
59 DI_port = 1
60 DO_pins = [0, 1, 2, 3]
61 DI_pins = [4, 5, 6, 7]
62 DO_value = [1, 0, 1, 0]
63
64 ## Get firmware model & version
65 driver_info = await dev.Sys_getDriverInfo_async()
66 print("Model name: " + driver_info[0])
67 print("Firmware version: " + driver_info[-1])
68
69 ## Get slot mode
70 slot_mode = await dev.Sys_getMode_async(slot)
71 print("Slot mode:", slot_mode)
72
73 ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
74 if slot_mode != "DIO":
75 err = await dev.Sys_setDIOMode_async(slot)
76 print(f"Sys_setDIOMode_async in slot {slot}, status: {err}")
77
78 ## Get slot mode
79 slot_mode = await dev.Sys_getMode_async(slot)
80 print("Slot mode:", slot_mode)
81
82 ## Get DIO start up information
83 info = await dev.DIO_loadStartup_async(DO_port)
84 print("Enable: ", info[0])
85 print("Direction:", info[1])
86 print("State: ", info[2])
87
88 ## Write pins to high or low
89 err = await dev.DO_writePins_async(DO_port, DO_pins, DO_value)
90 print(f"DO_writePins_async in DO_port {DO_port}, status: {err}")
91
92 ## Read pins state
93 state_list = await dev.DI_readPins_async(DI_port, DI_pins)
94 print(f"state_list_async in DI_port {DI_port}: {state_list}")
95 except Exception as err:
96 pywpc.printGenericError(err)
97
98 ## Disconnect device
99 dev.disconnect()
100
101 ## Release device handle
102 dev.close()
103
104 return
105
106def main_for_spyder(*args):
107 if asyncio.get_event_loop().is_running():
108 return asyncio.create_task(main(*args)).result()
109 else:
110 return asyncio.run(main(*args))
111if __name__ == '__main__':
112 asyncio.run(main()) ## Use terminal
113 # await main() ## Use Jupyter or IPython(>=7.0)
114 # main_for_spyder() ## Use Spyder