1'''
2UART - UART_read.py with asynchronous mode.
3
4This example demonstrates how to read data from another device with UART interface from USBDAQF1RD.
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-2025 WPC Systems Ltd. All rights reserved.
18'''
19
20## WPC
21from wpcsys import pywpc
22
23## Python
24import asyncio
25import sys
26sys.path.insert(0, 'src/')
27
28
29async def main():
30 ## Get Python driver version
31 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
32
33 ## Create device handle
34 dev = pywpc.USBDAQF1RD()
35
36 ## Connect to device
37 try:
38 dev.connect("default") ## Depend on your device
39 except Exception as err:
40 pywpc.printGenericError(err)
41 ## Release device handle
42 dev.close()
43 return
44
45 try:
46 ## Parameters setting
47 port = 2 ## Depend on your device
48 baudrate = 9600
49 data_bit_mode = 0 ## 0 : 8-bit data, 1 : 9-bit data.
50 parity_mode = 0 ## 0 : None, 2 : Even parity, 3 : Odd parity.
51 stop_bit_mode = 0 ## 0 : 1 bit, 1 : 0.5 bits, 2 : 2 bits, 3 : 1.5 bits
52 read_bytes = 20
53 delay = 0.005 ## [sec]
54
55 ## Get firmware model & version
56 driver_info = await dev.Sys_getDriverInfo_async()
57 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
58
59 ## Open UART
60 err = await dev.UART_open_async(port)
61 print(f"UART_open_async in port {port}, status: {err}")
62
63 ## Set UART port and set baudrate to 9600
64 err = await dev.UART_setBaudRate_async(port, baudrate)
65 print(f"UART_setBaudRate_async in port {port}, status: {err}")
66
67 ## Set UART port and set data bit to 8-bit data
68 err = await dev.UART_setDataBit_async(port, data_bit_mode)
69 print(f"UART_setDataBit_async in port {port}, status: {err}")
70
71 ## Set UART port and set parity to None
72 err = await dev.UART_setParity_async(port, parity_mode)
73 print(f"UART_setParity_async in port {port}, status: {err}")
74
75 ## Set UART port and set stop bit to 8-bit data
76 err = await dev.UART_setNumStopBit_async(port, stop_bit_mode)
77 print(f"UART_setNumStopBit_async 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 await asyncio.sleep(10) ## delay [sec]
84
85 ## Set UART port and read 20 bytes
86 data = await dev.UART_read_async(port, read_bytes, delay=delay)
87 print(f"data in port {port}: {data}")
88
89 ## Close UART
90 err = await dev.UART_close_async(port)
91 print(f"UART_close_async in port {port}, status: {err}")
92 except Exception as err:
93 pywpc.printGenericError(err)
94
95 finally:
96 ## Disconnect device
97 dev.disconnect()
98
99 ## Release device handle
100 dev.close()
101
102
103def main_for_spyder(*args):
104 if asyncio.get_event_loop().is_running():
105 return asyncio.create_task(main(*args)).result()
106 else:
107 return asyncio.run(main(*args))
108
109
110if __name__ == '__main__':
111 asyncio.run(main()) ## Use terminal
112 # await main() ## Use Jupyter or IPython(>=7.0)
113 # main_for_spyder() ## Use Spyder