1'''
2AI - AI_N_samples_once.py with synchronous mode.
3
4This example demonstrates the process of obtaining AI data in N-sample mode.
5Additionally, it gets AI data with points in once from EthanA.
6
7To begin with, it demonstrates the steps to open the AI and configure the AI parameters.
8Next, it outlines the procedure for reading the streaming AI data.
9Finally, it concludes by explaining how to close the AI.
10
11-------------------------------------------------------------------------------------
12Please change correct serial number or IP and port number BEFORE you run example code.
13
14For other examples please check:
15 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
16See README.md file to get detailed usage of this example.
17
18Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
19'''
20
21## WPC
22from wpcsys import pywpc
23
24
25def main():
26 ## Get Python driver version
27 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
28
29 ## Create device handle
30 dev = pywpc.EthanA()
31
32 ## Connect to device
33 try:
34 dev.connect("192.168.1.110") ## Depend on your device
35 except Exception as err:
36 pywpc.printGenericError(err)
37 ## Release device handle
38 dev.close()
39 return
40
41 try:
42 ## Parameters setting
43 port = 0 ## Depend on your device
44 mode = 1 ## 0: On demand, 1: N-samples, 2: Continuous
45 channel = 8
46 sampling_rate = 1000
47 samples = 200
48 read_points = 200
49 read_delay = 3 ## [sec]
50 timeout = 3 ## [sec]
51
52 ## Get firmware model & version
53 driver_info = dev.Sys_getDriverInfo(timeout)
54 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
55
56 ## Open AI
57 err = dev.AI_open(port, timeout)
58 print(f"AI_open in port {port}, status: {err}")
59
60
61 ## Set AI acquisition mode to N-samples mode (1)
62 err = dev.AI_setMode(port, mode, timeout)
63 print(f"AI_setMode {mode} in port {port}, status: {err}")
64
65 ## Set AI sampling rate
66 err = dev.AI_setSamplingRate(port, sampling_rate, timeout)
67 print(f"AI_setSamplingRate {sampling_rate} in port {port}, status: {err}")
68
69 ## Set AI # of samples
70 err = dev.AI_setNumSamples(port, samples, timeout)
71 print(f"AI_setNumSamples {samples} in port {port}, status: {err}")
72
73 ## Open AI streaming
74 err = dev.AI_openStreaming(port, timeout)
75 print(f"AI_openStreaming in port {port}, status: {err}")
76
77 ## Start AI streaming
78 err = dev.AI_startStreaming(port, timeout)
79 print(f"AI_startStreaming in port {port}, status: {err}")
80
81 ## Read AI
82 ai_2Dlist = dev.AI_readStreaming(port, read_points, read_delay)
83 print(f"Number of samples: {len(ai_2Dlist)}")
84
85 ok = True
86 for i, ai_list in enumerate(ai_2Dlist):
87 ## Check for any missing data
88 if len(ai_list) != channel:
89 print(i, ai_list)
90 ok = False
91 if ok:
92 print('Sample length OK')
93 else:
94 print('Error: at least 1 sample has wrong length')
95
96 ## Close AI streaming
97 err = dev.AI_closeStreaming(port, timeout)
98 print(f"AI_closeStreaming in port {port}, status: {err}")
99
100 ## Close AI
101 err = dev.AI_close(port, timeout)
102 print(f"AI_close in port {port}, status: {err}")
103 except Exception as err:
104 pywpc.printGenericError(err)
105
106 finally:
107 ## Disconnect device
108 dev.disconnect()
109
110 ## Release device handle
111 dev.close()
112
113
114if __name__ == '__main__':
115 main()