1'''
2UART - UART_write.py with asynchronous mode.
3
4This example demonstrates how to write data to another device with UART interface from USBDAQF1TD.
5
6First, it shows how to open UART port and configure UART parameters.
7Second, write bytes to 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
51 ## Get firmware model & version
52 driver_info = await dev.Sys_getDriverInfo_async()
53 print("Model name: " + driver_info[0])
54 print("Firmware version: " + driver_info[-1])
55
56 ## Open UART
57 err = await dev.UART_open_async(port)
58 print(f"UART_open_async in port {port}, status: {err}")
59
60 ## Set UART port and set baudrate to 9600
61 err = await dev.UART_setBaudRate_async(port, baudrate)
62 print(f"UART_setBaudRate_async in port {port}, status: {err}")
63
64 ## Set UART port and set data bit to 8-bit data
65 err = await dev.UART_setDataBit_async(port, data_bit_mode)
66 print(f"UART_setDataBit_async in port {port}, status: {err}")
67
68 ## Set UART port and set parity to None
69 err = await dev.UART_setParity_async(port, parity_mode)
70 print(f"UART_setParity_async in port {port}, status: {err}")
71
72 ## Set UART port and set stop bit to 1 bit
73 err = await dev.UART_setNumStopBit_async(port, stop_bit_mode)
74 print(f"UART_setNumStopBit_async in port {port}, status: {err}")
75
76 ## Set UART port and and write "12345" to device in string format
77 err = await dev.UART_write_async(port, "12345")
78 print(f"UART_write_async in port {port}, status: {err}")
79
80 ## Set UART port and and write "chunglee people" to device
81 err = await dev.UART_write_async(port, "chunglee people")
82 print(f"UART_write_async in port {port}, status: {err}")
83
84 ## Set UART port and and write "12345" to device in list format
85 err = await dev.UART_write_async(port, ["1","2","3","4","5"])
86 print(f"UART_write_async in port {port}, status: {err}")
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