DIO loopback pins

  1'''
  2DIO - DIO_loopback_pins.py with asynchronous mode.
  3
  4This example demonstrates the process of DIO loopback using pins from USBDAQF1TD.
  5It involves using DO pins to send signals and DI pins to receive signals on a single device, commonly known as "loopback".
  6
  7To begin with, it illustrates the steps required to open the DO and DI pins.
  8Next, it performs the operation of writing to a DO pin and reading from a DI pin.
  9Lastly, it concludes by closing the DO and DI pins.
 10
 11-------------------------------------------------------------------------------------
 12Please change correct serial number or IP and port number BEFORE you run example code.
 13
 14For other examples please check:
 15    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 16See README.md file to get detailed usage of this example.
 17
 18Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 19'''
 20
 21## WPC
 22from wpcsys import pywpc
 23
 24## Python
 25import asyncio
 26import sys
 27sys.path.insert(0, 'src/')
 28
 29
 30async def main():
 31    ## Get Python driver version
 32    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 33
 34    ## Create device handle
 35    dev = pywpc.USBDAQF1TD()
 36
 37    ## Connect to device
 38    try:
 39        dev.connect("default")  ## Depend on your device
 40    except Exception as err:
 41        pywpc.printGenericError(err)
 42        ## Release device handle
 43        dev.close()
 44        return
 45
 46    try:
 47        ## Parameters setting
 48        DO_port = 0  ## Depend on your device
 49        DI_port = 1
 50        DO_pins = [0, 1, 2, 3]
 51        DI_pins = [0, 1, 2, 3]
 52        DO_value = [1, 0, 1, 0]
 53
 54        ## Get firmware model & version
 55        driver_info = await dev.Sys_getDriverInfo_async()
 56        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 57
 58        ## Open pins with digital output
 59        err = await dev.DO_openPins_async(DO_port, DO_pins)
 60        print(f"DO_openPins_async in DO_port {DO_port}, status: {err}")
 61
 62        ## Write pins to high or low
 63        err = await dev.DO_writePins_async(DO_port, DO_pins, DO_value)
 64        print(f"DO_writePins_async in DO_port {DO_port}, status: {err}")
 65
 66        ## Open pins with digital iutput
 67        err = await dev.DI_openPins_async(DI_port, DI_pins)
 68        print(f"DI_openPins_async in DI_port {DI_port}, status: {err}")
 69
 70        ## Read pins state
 71        state_list = await dev.DI_readPins_async(DI_port, DI_pins)
 72        print(state_list)
 73
 74        ## Close pins with digital output
 75        err = await dev.DO_closePins_async(DO_port, DO_pins)
 76        print(f"DO_closePins_async in DO_port {DO_port}, status: {err}")
 77
 78        ## Close pins with digital input
 79        err = await dev.DI_closePins_async(DI_port, DI_pins)
 80        print(f"DI_closePins_async in DI_port {DI_port}, status: {err}")
 81    except Exception as err:
 82        pywpc.printGenericError(err)
 83
 84    finally:
 85        ## Disconnect device
 86        dev.disconnect()
 87
 88        ## Release device handle
 89        dev.close()
 90
 91
 92def main_for_spyder(*args):
 93    if asyncio.get_event_loop().is_running():
 94        return asyncio.create_task(main(*args)).result()
 95    else:
 96        return asyncio.run(main(*args))
 97
 98
 99if __name__ == '__main__':
100    asyncio.run(main())  ## Use terminal
101    # await main()  ## Use Jupyter or IPython(>=7.0)
102    # main_for_spyder()  ## Use Spyder