UART write

  1'''
  2UART - UART_write.py with synchronous mode.
  3
  4This example demonstrates how to write data to another device with UART interface from USBDAQF1D.
  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 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.USBDAQF1D()
 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
 52        ## Get firmware model & version
 53        driver_info = dev.Sys_getDriverInfo(timeout)
 54        print("Model name: " + driver_info[0])
 55        print("Firmware version: " + driver_info[-1])
 56
 57        ## Open UART
 58        err = dev.UART_open(port, timeout)
 59        print(f"UART_open in port {port}, status: {err}")
 60
 61        ## Set UART port and set baudrate to 9600
 62        err = dev.UART_setBaudRate(port, baudrate, timeout)
 63        print(f"UART_setBaudRate in port {port}, status: {err}")
 64
 65        ## Set UART port and set data bit to 8-bit data
 66        err = dev.UART_setDataBit(port, data_bit_mode, timeout)
 67        print(f"UART_setDataBit in port {port}, status: {err}")
 68
 69        ## Set UART port and set parity to None
 70        err = dev.UART_setParity(port, parity_mode, timeout)
 71        print(f"UART_setParity in port {port}, status: {err}")
 72
 73        ## Set UART port and set stop bit to 1 bit
 74        err = dev.UART_setNumStopBit(port, stop_bit_mode, timeout)
 75        print(f"UART_setNumStopBit in port {port}, status: {err}")
 76
 77        ## Set UART port and and write "12345" to device in string format
 78        err = dev.UART_write(port, "12345", timeout)
 79        print(f"UART_write in port {port}, status: {err}")
 80
 81        ## Set UART port and and write "chunglee people" to device
 82        err = dev.UART_write(port, "chunglee people", timeout)
 83        print(f"UART_write in port {port}, status: {err}")
 84
 85        ## Set UART port and and write "12345" to device in list format
 86        err = dev.UART_write(port, ["1","2","3","4","5"], timeout)
 87        print(f"UART_write in port {port}, status: {err}")
 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()