1'''
2UART - UART_read.py with synchronous mode.
3
4This example demonstrates how to read data from another device with UART interface from USBDAQF1AOD.
5
6First, it shows how to open UART port and configure UART parameters.
7Second, read bytes from another device.
8Last, close UART port.
9
10-------------------------------------------------------------------------------------
11Please change correct serial number or IP and port number BEFORE you run example code.
12
13For other examples please check:
14 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
15See README.md file to get detailed usage of this example.
16
17Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
18'''
19
20## Python
21import time
22
23## WPC
24
25from wpcsys import pywpc
26
27def main():
28 ## Get Python driver version
29 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
30
31 ## Create device handle
32 dev = pywpc.USBDAQF1AOD()
33
34 ## Connect to device
35 try:
36 dev.connect("default") ## Depend on your device
37 except Exception as err:
38 pywpc.printGenericError(err)
39 ## Release device handle
40 dev.close()
41 return
42
43 try:
44 ## Parameters setting
45 port = 2 ## Depend on your device
46 baudrate = 9600
47 data_bit_mode = 0 ## 0 : 8-bit data, 1 : 9-bit data.
48 parity_mode = 0 ## 0 : None, 2 : Even parity, 3 : Odd parity.
49 stop_bit_mode = 0 ## 0 : 1 bit, 1 : 0.5 bits, 2 : 2 bits, 3 : 1.5 bits
50 timeout = 3 ## second
51 read_bytes = 20
52 delay = 0.005 ## second
53
54 ## Get firmware model & version
55 driver_info = dev.Sys_getDriverInfo(timeout)
56 print("Model name: " + driver_info[0])
57 print("Firmware version: " + driver_info[-1])
58
59 ## Open UART
60 err = dev.UART_open(port, timeout)
61 print(f"UART_open in port {port}, status: {err}")
62
63 ## Set UART port and set baudrate to 9600
64 err = dev.UART_setBaudRate(port, baudrate, timeout)
65 print(f"UART_setBaudRate in port {port}, status: {err}")
66
67 ## Set UART port and set data bit to 8-bit data
68 err = dev.UART_setDataBit(port, data_bit_mode, timeout)
69 print(f"UART_setDataBit in port {port}, status: {err}")
70
71 ## Set UART port and set parity to None
72 err = dev.UART_setParity(port, parity_mode, timeout)
73 print(f"UART_setParity in port {port}, status: {err}")
74
75 ## Set UART port and set stop bit to 8-bit data
76 err = dev.UART_setNumStopBit(port, stop_bit_mode, timeout)
77 print(f"UART_setNumStopBit in port {port}, status: {err}")
78
79 ## Print informaion
80 print("Wait for 10 seconds to receive data from other devices")
81
82 ## Wait
83 time.sleep(10) ## delay [s]
84
85 ## Set UART port and read 20 bytes
86 data = dev.UART_read(port, read_bytes, delay=delay)
87 print(f"data in port {port}: {data}")
88
89 ## Close UART
90 err = dev.UART_close(port, timeout)
91 print(f"UART_close in port {port}, status: {err}")
92 except Exception as err:
93 pywpc.printGenericError(err)
94
95 ## Disconnect device
96 dev.disconnect()
97
98 ## Release device handle
99 dev.close()
100
101 return
102
103if __name__ == '__main__':
104 main()