UART read

  1'''
  2UART - UART_read.py with synchronous mode.
  3
  4This example demonstrates how to read data from another device with UART interface from USBDAQF1AD.
  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 time
 25
 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.USBDAQF1AD()
 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  ## [sec]
 51        read_bytes = 20
 52        delay = 0.005  ## [sec]
 53
 54        ## Get firmware model & version
 55        driver_info = dev.Sys_getDriverInfo(timeout)
 56        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 57
 58        ## Open UART
 59        err = dev.UART_open(port, timeout)
 60        print(f"UART_open in port {port}, status: {err}")
 61
 62        ## Set UART port and set baudrate to 9600
 63        err = dev.UART_setBaudRate(port, baudrate, timeout)
 64        print(f"UART_setBaudRate in port {port}, status: {err}")
 65
 66        ## Set UART port and set data bit to 8-bit data
 67        err = dev.UART_setDataBit(port, data_bit_mode, timeout)
 68        print(f"UART_setDataBit in port {port}, status: {err}")
 69
 70        ## Set UART port and set parity to None
 71        err = dev.UART_setParity(port, parity_mode, timeout)
 72        print(f"UART_setParity in port {port}, status: {err}")
 73
 74        ## Set UART port and set stop bit to 8-bit data
 75        err = dev.UART_setNumStopBit(port, stop_bit_mode, timeout)
 76        print(f"UART_setNumStopBit 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        time.sleep(10)  ## delay [sec]
 83
 84        ## Set UART port and read 20 bytes
 85        data = dev.UART_read(port, read_bytes, delay=delay)
 86        print(f"data in port {port}: {data}")
 87
 88        ## Close UART
 89        err = dev.UART_close(port, timeout)
 90        print(f"UART_close in port {port}, status: {err}")
 91    except Exception as err:
 92        pywpc.printGenericError(err)
 93
 94    finally:
 95        ## Disconnect device
 96        dev.disconnect()
 97
 98        ## Release device handle
 99        dev.close()
100
101
102if __name__ == '__main__':
103    main()