1'''
2AIO - AIO_all_channels_loopback.py with asynchronous mode.
3
4This example demonstrates the process of AIO loopback across all 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 value simultaneously
102 err = await dev.AO_writeAllChannels_async(slot, ao_value_list)
103 print(f"In slot {slot} the AO value is {ao_value_list}, status: {err}")
104
105 ## Read data acquisition
106 ai_list = await dev.AI_readOnDemand_async(slot)
107 print(f"AI data in slot {slot}: {ai_list}")
108
109 ## Close AI
110 err = await dev.AI_close_async(slot)
111 print(f"AI_close_async in slot {slot}, status: {err}")
112
113 ## Close AO
114 err = await dev.AO_close_async(slot)
115 print(f"AO_close_async in slot {slot}, status: {err}")
116 except Exception as err:
117 pywpc.printGenericError(err)
118
119 finally:
120 ## Disconnect device
121 dev.disconnect()
122
123 ## Release device handle
124 dev.close()
125
126
127def main_for_spyder(*args):
128 if asyncio.get_event_loop().is_running():
129 return asyncio.create_task(main(*args)).result()
130 else:
131 return asyncio.run(main(*args))
132
133
134if __name__ == '__main__':
135 asyncio.run(main()) ## Use terminal
136 # await main() ## Use Jupyter or IPython(>=7.0)
137 # main_for_spyder() ## Use Spyder