1'''
2AIO - AIO_all_channels_loopback.py with synchronous 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` and `AI_enableCS`.
14Example: AI_enableCS is {0, 2}
15Subsequently, the returned value of AI_readOnDemand and AI_readStreaming 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 time
39
40## WPC
41
42from wpcsys import pywpc
43
44
45def 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 timeout = 3 ## second
66 chip_select = [0]
67
68 ## Get firmware model & version
69 driver_info = dev.Sys_getDriverInfo(timeout)
70 print("Model name: " + driver_info[0])
71 print("Firmware version: " + driver_info[-1])
72
73 ## Get slot mode
74 slot_mode = dev.Sys_getMode(slot, timeout)
75 print("Slot mode:", slot_mode)
76
77 ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
78 if slot_mode != "AIO":
79 err = dev.Sys_setAIOMode(slot, timeout)
80 print(f"Sys_setAIOMode in slot {slot}, status: {err}")
81
82 ## Get slot mode
83 slot_mode = dev.Sys_getMode(slot, timeout)
84 print("Slot mode:", slot_mode)
85
86 ## Open AI
87 err = dev.AI_open(slot, timeout)
88 print(f"AI_open in slot {slot}, status: {err}")
89
90 ## Enable CS
91 err = dev.AI_enableCS(slot, chip_select, timeout)
92 print(f"AI_enableCS in slot {slot}, status: {err}")
93
94 ## Open AO
95 err = dev.AO_open(slot, timeout)
96 print(f"AO_open in slot {slot}, status: {err}")
97
98 ## Read data acquisition
99 ai_list = dev.AI_readOnDemand(slot, timeout)
100 print(f"AI data in slot {slot}: {ai_list}")
101
102 ## Write AO value simultaneously
103 err = dev.AO_writeAllChannels(slot, ao_value_list, timeout)
104 print(f"In slot {slot} the AO value is {ao_value_list}, status: {err}")
105
106 ## Read data acquisition
107 ai_list = dev.AI_readOnDemand(slot, timeout)
108 print(f"AI data in slot {slot}: {ai_list}")
109
110 ## Close AI
111 err = dev.AI_close(slot, timeout)
112 print(f"AI_close in slot {slot}, status: {err}")
113
114 ## Close AO
115 err = dev.AO_close(slot, timeout)
116 print(f"AO_close in slot {slot}, status: {err}")
117 except Exception as err:
118 pywpc.printGenericError(err)
119
120 ## Disconnect device
121 dev.disconnect()
122
123 ## Release device handle
124 dev.close()
125
126 return
127
128if __name__ == '__main__':
129 main()