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-2024 WPC Systems Ltd. All rights reserved.
35'''
36
37## Python
38import asyncio
39
40## WPC
41
42from wpcsys import pywpc
43
44
45async def main():
46 ## Get Python driver version
47 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
48
49 ## Create device handle
50 dev = pywpc.STEM()
51
52 ## Connect to device
53 try:
54 dev.connect("192.168.1.110") ## Depend on your device
55 except Exception as err:
56 pywpc.printGenericError(err)
57 ## Release device handle
58 dev.close()
59 return
60
61 try:
62 ## Parameters setting
63 slot = 1 ## Connect AIO module to slot
64 ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
65 chip_select = [0]
66
67 ## Get firmware model & version
68 driver_info = await dev.Sys_getDriverInfo_async()
69 print("Model name: " + driver_info[0])
70 print("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 ## Disconnect device
132 dev.disconnect()
133
134 ## Release device handle
135 dev.close()
136
137 return
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))
144if __name__ == '__main__':
145 asyncio.run(main()) ## Use terminal
146 # await main() ## Use Jupyter or IPython(>=7.0)
147 # main_for_spyder() ## Use Spyder