UART write

  1'''
  2UART - UART_write.py with asynchronous 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-2025 WPC Systems Ltd. All rights reserved.
 18'''
 19
 20## WPC
 21from wpcsys import pywpc
 22
 23## Python
 24import asyncio
 25import sys
 26sys.path.insert(0, 'src/')
 27
 28
 29async def main():
 30    ## Get Python driver version
 31    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 32
 33    ## Create device handle
 34    dev = pywpc.USBDAQF1D()
 35
 36    ## Connect to device
 37    try:
 38        dev.connect("default")  ## Depend on your device
 39    except Exception as err:
 40        pywpc.printGenericError(err)
 41        ## Release device handle
 42        dev.close()
 43        return
 44
 45    try:
 46        ## Parameters setting
 47        port = 2  ## Depend on your device
 48        baudrate = 9600
 49        data_bit_mode = 0  ## 0 : 8-bit data, 1 : 9-bit data.
 50        parity_mode = 0  ## 0 : None, 2 : Even parity, 3 : Odd parity.
 51        stop_bit_mode = 0  ## 0 : 1 bit, 1 : 0.5 bits, 2 : 2 bits, 3 : 1.5 bits
 52
 53        ## Get firmware model & version
 54        driver_info = await dev.Sys_getDriverInfo_async()
 55        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 56
 57        ## Open UART
 58        err = await dev.UART_open_async(port)
 59        print(f"UART_open_async in port {port}, status: {err}")
 60
 61        ## Set UART port and set baudrate to 9600
 62        err = await dev.UART_setBaudRate_async(port, baudrate)
 63        print(f"UART_setBaudRate_async in port {port}, status: {err}")
 64
 65        ## Set UART port and set data bit to 8-bit data
 66        err = await dev.UART_setDataBit_async(port, data_bit_mode)
 67        print(f"UART_setDataBit_async in port {port}, status: {err}")
 68
 69        ## Set UART port and set parity to None
 70        err = await dev.UART_setParity_async(port, parity_mode)
 71        print(f"UART_setParity_async in port {port}, status: {err}")
 72
 73        ## Set UART port and set stop bit to 1 bit
 74        err = await dev.UART_setNumStopBit_async(port, stop_bit_mode)
 75        print(f"UART_setNumStopBit_async in port {port}, status: {err}")
 76
 77        ## Set UART port and and write "12345" to device in string format
 78        err = await dev.UART_write_async(port, "12345")
 79        print(f"UART_write_async in port {port}, status: {err}")
 80
 81        ## Set UART port and and write "chunglee people" to device
 82        err = await dev.UART_write_async(port, "chunglee people")
 83        print(f"UART_write_async in port {port}, status: {err}")
 84
 85        ## Set UART port and and write "12345" to device in list format
 86        err = await dev.UART_write_async(port, ["1", "2", "3", "4", "5"])
 87        print(f"UART_write_async in port {port}, status: {err}")
 88
 89        ## Close UART
 90        err = await dev.UART_close_async(port)
 91        print(f"UART_close_async in port {port}, status: {err}")
 92    except Exception as err:
 93        pywpc.printGenericError(err)
 94
 95    finally:
 96        ## Disconnect device
 97        dev.disconnect()
 98
 99        ## Release device handle
100        dev.close()
101
102
103def main_for_spyder(*args):
104    if asyncio.get_event_loop().is_running():
105        return asyncio.create_task(main(*args)).result()
106    else:
107        return asyncio.run(main(*args))
108
109
110if __name__ == '__main__':
111    asyncio.run(main())  ## Use terminal
112    # await main()  ## Use Jupyter or IPython(>=7.0)
113    # main_for_spyder()  ## Use Spyder