1'''
2UART - UART_read.py with asynchronous mode.
3
4This example demonstrates how to read data from another device with UART interface from USBDAQF1TD.
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 asyncio
22
23## WPC
24
25from wpcsys import pywpc
26
27async def main():
28 ## Get Python driver version
29 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
30
31 ## Create device handle
32 dev = pywpc.USBDAQF1TD()
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 read_bytes = 20
51 delay = 0.005 ## second
52
53 ## Get firmware model & version
54 driver_info = await dev.Sys_getDriverInfo_async()
55 print("Model name: " + driver_info[0])
56 print("Firmware version: " + driver_info[-1])
57
58 ## Open UART
59 err = await dev.UART_open_async(port)
60 print(f"UART_open_async in port {port}, status: {err}")
61
62 ## Set UART port and set baudrate to 9600
63 err = await dev.UART_setBaudRate_async(port, baudrate)
64 print(f"UART_setBaudRate_async in port {port}, status: {err}")
65
66 ## Set UART port and set data bit to 8-bit data
67 err = await dev.UART_setDataBit_async(port, data_bit_mode)
68 print(f"UART_setDataBit_async in port {port}, status: {err}")
69
70 ## Set UART port and set parity to None
71 err = await dev.UART_setParity_async(port, parity_mode)
72 print(f"UART_setParity_async in port {port}, status: {err}")
73
74 ## Set UART port and set stop bit to 8-bit data
75 err = await dev.UART_setNumStopBit_async(port, stop_bit_mode)
76 print(f"UART_setNumStopBit_async in port {port}, status: {err}")
77
78 ## Print informaion
79 print("Wait for 10 seconds to receive data from other devices")
80
81 ## Wait
82 await asyncio.sleep(10) ## delay [s]
83
84 ## Set UART port and read 20 bytes
85 data = await dev.UART_read_async(port, read_bytes, delay=delay)
86 print(f"data in port {port}: {data}")
87
88 ## Close UART
89 err = await dev.UART_close_async(port)
90 print(f"UART_close_async in port {port}, status: {err}")
91 except Exception as err:
92 pywpc.printGenericError(err)
93
94 ## Disconnect device
95 dev.disconnect()
96
97 ## Release device handle
98 dev.close()
99
100 return
101
102def main_for_spyder(*args):
103 if asyncio.get_event_loop().is_running():
104 return asyncio.create_task(main(*args)).result()
105 else:
106 return asyncio.run(main(*args))
107
108if __name__ == '__main__':
109 asyncio.run(main()) ## Use terminal
110 # await main() ## Use Jupyter or IPython(>=7.0)
111 # main_for_spyder() ## Use Spyder