1'''
2DIO - DO_write_pins.py with asynchronous mode.
3
4This example illustrates the process of writing a high or low signal to a DO pin from STEM.
5
6To begin with, it demonstrates the steps required to open the DO pin.
7Next, voltage output is written to the DO pin.
8Lastly, it concludes by closing the DO pin.
9
10If your product is "STEM", please invoke the function `Sys_setDIOMode_async`.
11
12The DIO ports 0 to 1 are assigned to slot 1, while ports 2 to 3 are assigned to slot 2.
13---------------------------
14| Slot 1 port 1 & 0 |
15| Slot 2 port 3 & 2 |
16| Slot 3 port 5 & 4 |
17| Slot 4 port 7 & 6 |
18---------------------------
19
20-------------------------------------------------------------------------------------
21Please change correct serial number or IP and port number BEFORE you run example code.
22
23For other examples please check:
24 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
25See README.md file to get detailed usage of this example.
26
27Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
28'''
29
30## WPC
31from wpcsys import pywpc
32
33## Python
34import asyncio
35import sys
36sys.path.insert(0, 'src/')
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 pin_index = [0, 1, 2, 3]
60 DO_value = [1, 0, 1, 0]
61
62 ## Get firmware model & version
63 driver_info = await dev.Sys_getDriverInfo_async()
64 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
65
66 ## Get slot mode
67 slot_mode = await dev.Sys_getMode_async(slot)
68 print("Slot mode:", slot_mode)
69
70 ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
71 if slot_mode != "DIO":
72 err = await dev.Sys_setDIOMode_async(slot)
73 print(f"Sys_setDIOMode_async in slot {slot}, status: {err}")
74
75 ## Get slot mode
76 slot_mode = await dev.Sys_getMode_async(slot)
77 print("Slot mode:", slot_mode)
78
79 ## Get DIO start up information
80 info = await dev.DIO_loadStartup_async(DO_port)
81 print(f"Enable: {info[0]}")
82 print(f"Direction: {info[1]}")
83 print(f"State: {info[2]}")
84
85 ## Write pins to high or low
86 err = await dev.DO_writePins_async(DO_port, pin_index, DO_value)
87 print(f"DO_writePins_async in DO_port {DO_port}, status: {err}")
88 except Exception as err:
89 pywpc.printGenericError(err)
90
91 finally:
92 ## Disconnect device
93 dev.disconnect()
94
95 ## Release device handle
96 dev.close()
97
98
99def main_for_spyder(*args):
100 if asyncio.get_event_loop().is_running():
101 return asyncio.create_task(main(*args)).result()
102 else:
103 return asyncio.run(main(*args))
104
105
106if __name__ == '__main__':
107 asyncio.run(main()) ## Use terminal
108 # await main() ## Use Jupyter or IPython(>=7.0)
109 # main_for_spyder() ## Use Spyder