1'''
2AIO - AIO_one_channel_loopback.py with asynchronous mode.
3
4This example demonstrates the process of AIO loopback with specific channels of STEM.
5It involves using AO pins to send signals and AI pins to receive signals on a single device, commonly referred to as "loopback".
6The AI and AO pins are connected using a wire.
7
8Initially, the example demonstrates the steps required to open the AI and AO.
9Next, it reads AI data and displays its corresponding values.
10Following that, it writes digital signals to the AO pins and reads AI on-demand data once again.
11Lastly, it closes the AO and AI ports.
12
13If your product is "STEM", please invoke the function `Sys_setAIOMode_async` and `AI_enableCS_async`.
14Example: AI_enableCS_async is {0, 2}
15Subsequently, the returned value of AI_readOnDemand_async and AI_readStreaming_async will be displayed as follows.
16data:
17 CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7
18 | | |
19 |---------------- CS0-----------------|---------------- CS2------------------|
20[sample0]
21[sample1]
22 .
23 .
24 .
25[sampleN]
26
27-------------------------------------------------------------------------------------
28Please change correct serial number or IP and port number BEFORE you run example code.
29
30For other examples please check:
31 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
32See README.md file to get detailed usage of this example.
33
34Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
35'''
36
37## WPC
38from wpcsys import pywpc
39
40## Python
41import asyncio
42import sys
43sys.path.insert(0, 'src/')
44
45
46async def main():
47 ## Get Python driver version
48 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
49
50 ## Create device handle
51 dev = pywpc.STEM()
52
53 ## Connect to device
54 try:
55 dev.connect("192.168.1.110") ## Depend on your device
56 except Exception as err:
57 pywpc.printGenericError(err)
58 ## Release device handle
59 dev.close()
60 return
61
62 try:
63 ## Parameters setting
64 slot = 1 ## Connect AIO module to slot
65 ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
66 chip_select = [0]
67
68 ## Get firmware model & version
69 driver_info = await dev.Sys_getDriverInfo_async()
70 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
71
72 ## Get slot mode
73 slot_mode = await dev.Sys_getMode_async(slot)
74 print("Slot mode:", slot_mode)
75
76 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
77 if slot_mode != "AIO":
78 err = await dev.Sys_setAIOMode_async(slot)
79 print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
80
81 ## Get slot mode
82 slot_mode = await dev.Sys_getMode_async(slot)
83 print("Slot mode:", slot_mode)
84
85 ## Open AI
86 err = await dev.AI_open_async(slot)
87 print(f"AI_open_async in slot {slot}, status: {err}")
88
89 ## Enable CS
90 err = await dev.AI_enableCS_async(slot, chip_select)
91 print(f"AI_enableCS_async in slot {slot}, status: {err}")
92
93 ## Open AO
94 err = await dev.AO_open_async(slot)
95 print(f"AO_open_async in slot {slot}, status: {err}")
96
97 ## Read data acquisition
98 ai_list = await dev.AI_readOnDemand_async(slot)
99 print(f"AI data in slot {slot}: {ai_list}")
100
101 ## Write AO vaule in channel 0
102 err = await dev.AO_writeOneChannel_async(slot, 0, ao_value_list[0])
103 print(f"In slot {slot} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
104
105 ## Write AO vaule in channel 1
106 err = await dev.AO_writeOneChannel_async(slot, 1, ao_value_list[1])
107 print(f"In slot {slot} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
108
109 ## Write AO vaule in channel 2
110 err = await dev.AO_writeOneChannel_async(slot, 2, ao_value_list[2])
111 print(f"In slot {slot} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
112
113 ## Write AO vaule in channel 3
114 err = await dev.AO_writeOneChannel_async(slot, 3, ao_value_list[3])
115 print(f"In slot {slot} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
116
117 ## Read data acquisition
118 ai_list = await dev.AI_readOnDemand_async(slot)
119 print(f"AI data in slot {slot}: {ai_list}")
120
121 ## Close AI
122 err = await dev.AI_close_async(slot)
123 print(f"AI_close_async in slot {slot}, status: {err}")
124
125 ## Close AO
126 err = await dev.AO_close_async(slot)
127 print(f"AO_close_async in slot {slot}, status: {err}")
128 except Exception as err:
129 pywpc.printGenericError(err)
130
131 finally:
132 ## Disconnect device
133 dev.disconnect()
134
135 ## Release device handle
136 dev.close()
137
138
139def main_for_spyder(*args):
140 if asyncio.get_event_loop().is_running():
141 return asyncio.create_task(main(*args)).result()
142 else:
143 return asyncio.run(main(*args))
144
145
146if __name__ == '__main__':
147 asyncio.run(main()) ## Use terminal
148 # await main() ## Use Jupyter or IPython(>=7.0)
149 # main_for_spyder() ## Use Spyder