DIO loopback pins

  1'''
  2DIO - DIO_loopback_pins.py with asynchronous mode.
  3
  4This example demonstrates the process of DIO loopback using pins from USBDAQF1D.
  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-2024 WPC Systems Ltd. All rights reserved.
 19'''
 20
 21## Python
 22import asyncio
 23
 24## WPC
 25
 26from wpcsys import pywpc
 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        DO_port = 0 ## Depend on your device
 48        DI_port = 1
 49        DO_pins = [0, 1, 2, 3]
 50        DI_pins = [0, 1, 2, 3]
 51        DO_value = [1, 0, 1, 0]
 52
 53        ## Get firmware model & version
 54        driver_info = await dev.Sys_getDriverInfo_async()
 55        print("Model name: " + driver_info[0])
 56        print("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    ## Disconnect device
 85    dev.disconnect()
 86
 87    ## Release device handle
 88    dev.close()
 89
 90    return
 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))
 97if __name__ == '__main__':
 98    asyncio.run(main()) ## Use terminal
 99    # await main() ## Use Jupyter or IPython(>=7.0)
100    # main_for_spyder() ## Use Spyder