1'''
2UART - UART_write.py with synchronous mode.
3
4This example demonstrates how to write data to another device with UART interface from USBDAQF1AOD.
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-2025 WPC Systems Ltd. All rights reserved.
18'''
19
20## WPC
21from wpcsys import pywpc
22
23
24def main():
25 ## Get Python driver version
26 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
27
28 ## Create device handle
29 dev = pywpc.USBDAQF1AOD()
30
31 ## Connect to device
32 try:
33 dev.connect("default") ## Depend on your device
34 except Exception as err:
35 pywpc.printGenericError(err)
36 ## Release device handle
37 dev.close()
38 return
39
40 try:
41 ## Parameters setting
42 port = 2 ## Depend on your device
43 baudrate = 9600
44 data_bit_mode = 0 ## 0 : 8-bit data, 1 : 9-bit data.
45 parity_mode = 0 ## 0 : None, 2 : Even parity, 3 : Odd parity.
46 stop_bit_mode = 0 ## 0 : 1 bit, 1 : 0.5 bits, 2 : 2 bits, 3 : 1.5 bits
47 timeout = 3 ## [sec]
48
49 ## Get firmware model & version
50 driver_info = dev.Sys_getDriverInfo(timeout)
51 print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
52
53 ## Open UART
54 err = dev.UART_open(port, timeout)
55 print(f"UART_open in port {port}, status: {err}")
56
57 ## Set UART port and set baudrate to 9600
58 err = dev.UART_setBaudRate(port, baudrate, timeout)
59 print(f"UART_setBaudRate in port {port}, status: {err}")
60
61 ## Set UART port and set data bit to 8-bit data
62 err = dev.UART_setDataBit(port, data_bit_mode, timeout)
63 print(f"UART_setDataBit in port {port}, status: {err}")
64
65 ## Set UART port and set parity to None
66 err = dev.UART_setParity(port, parity_mode, timeout)
67 print(f"UART_setParity in port {port}, status: {err}")
68
69 ## Set UART port and set stop bit to 1 bit
70 err = dev.UART_setNumStopBit(port, stop_bit_mode, timeout)
71 print(f"UART_setNumStopBit in port {port}, status: {err}")
72
73 ## Set UART port and and write "12345" to device in string format
74 err = dev.UART_write(port, "12345", timeout)
75 print(f"UART_write in port {port}, status: {err}")
76
77 ## Set UART port and and write "chunglee people" to device
78 err = dev.UART_write(port, "chunglee people", timeout)
79 print(f"UART_write in port {port}, status: {err}")
80
81 ## Set UART port and and write "12345" to device in list format
82 err = dev.UART_write(port, ["1", "2", "3", "4", "5"], timeout)
83 print(f"UART_write in port {port}, status: {err}")
84
85 ## Close UART
86 err = dev.UART_close(port, timeout)
87 print(f"UART_close in port {port}, status: {err}")
88 except Exception as err:
89 pywpc.printGenericError(err)
90
91 finally:
92 ## Disconnect device
93 dev.disconnect()
94
95 ## Release device handle
96 dev.close()
97
98
99if __name__ == '__main__':
100 main()