1'''
2DIO - DO_blinky_port.py with asynchronous mode.
3
4This example illustrates the process of writing a high or low signal to a DO port from STEM.
5
6To begin with, it demonstrates the steps required to open the DO port.
7Next, in each loop, a different voltage output is applied, resulting in a blinking effect.
8Lastly, it concludes by closing the DO port.
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-2024 WPC Systems Ltd. All rights reserved.
28'''
29
30## Python
31import asyncio
32
33## WPC
34
35from wpcsys import pywpc
36
37
38async def main():
39 ## Get Python driver version
40 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
41
42 ## Create device handle
43 dev = pywpc.STEM()
44
45 ## Connect to device
46 try:
47 dev.connect("192.168.1.110") ## Depend on your device
48 except Exception as err:
49 pywpc.printGenericError(err)
50 ## Release device handle
51 dev.close()
52 return
53
54 try:
55 ## Parameters setting
56 slot = 1 ## Connect DIO module to slot
57 DO_port = 0
58
59 ## Get firmware model & version
60 driver_info = await dev.Sys_getDriverInfo_async()
61 print("Model name: " + driver_info[0])
62 print("Firmware version: " + driver_info[-1])
63
64 ## Get slot mode
65 slot_mode = await dev.Sys_getMode_async(slot)
66 print("Slot mode:", slot_mode)
67
68 ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
69 if slot_mode != "DIO":
70 err = await dev.Sys_setDIOMode_async(slot)
71 print(f"Sys_setDIOMode_async in slot {slot}, status: {err}")
72
73 ## Get slot mode
74 slot_mode = await dev.Sys_getMode_async(slot)
75 print("Slot mode:", slot_mode)
76
77 ## Get DIO start up information
78 info = await dev.DIO_loadStartup_async(DO_port)
79 print("Enable: ", info[0])
80 print("Direction:", info[1])
81 print("State: ", info[2])
82
83 ## Toggle digital state for 10 times. Each times delay for 0.5 second
84 for i in range(10):
85 state = await dev.DO_togglePort_async(DO_port)
86 print(state)
87
88 ## Wait for 0.5 second to see led status
89 await asyncio.sleep(0.5) ## delay [s]
90 except Exception as err:
91 pywpc.printGenericError(err)
92
93 ## Disconnect device
94 dev.disconnect()
95
96 ## Release device handle
97 dev.close()
98
99 return
100
101def main_for_spyder(*args):
102 if asyncio.get_event_loop().is_running():
103 return asyncio.create_task(main(*args)).result()
104 else:
105 return asyncio.run(main(*args))
106if __name__ == '__main__':
107 asyncio.run(main()) ## Use terminal
108 # await main() ## Use Jupyter or IPython(>=7.0)
109 # main_for_spyder() ## Use Spyder