DIO loopback port

  1'''
  2DIO - DIO_loopback_port.py with asynchronous mode.
  3
  4This example demonstrates the process of DIO loopback using port from STEM.
  5It involves using DO port to send signals and DI port 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 port.
  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 port.
 10
 11If your product is "STEM", please invoke the function `Sys_setDIOMode_async`.
 12
 13The DIO ports 0 to 1 are assigned to slot 1, while ports 2 to 3 are assigned to slot 2.
 14---------------------------
 15|  Slot 1    port 1 & 0   |
 16|  Slot 2    port 3 & 2   |
 17|  Slot 3    port 5 & 4   |
 18|  Slot 4    port 7 & 6   |
 19---------------------------
 20
 21-------------------------------------------------------------------------------------
 22Please change correct serial number or IP and port number BEFORE you run example code.
 23
 24For other examples please check:
 25    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 26See README.md file to get detailed usage of this example.
 27
 28Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 29'''
 30
 31## WPC
 32from wpcsys import pywpc
 33
 34## Python
 35import asyncio
 36import sys
 37sys.path.insert(0, 'src/')
 38
 39
 40async def main():
 41    ## Get Python driver version
 42    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 43
 44    ## Create device handle
 45    dev = pywpc.STEM()
 46
 47    ## Connect to device
 48    try:
 49        dev.connect("192.168.1.110")  ## Depend on your device
 50    except Exception as err:
 51        pywpc.printGenericError(err)
 52        ## Release device handle
 53        dev.close()
 54        return
 55
 56    try:
 57        ## Parameters setting
 58        slot = 1  ## Connect DIO module to slot
 59        DO_port = 0
 60        DI_port = 1
 61        DO_value = [1, 0, 1, 0]
 62
 63        ## Get firmware model & version
 64        driver_info = await dev.Sys_getDriverInfo_async()
 65        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 66
 67        ## Get slot mode
 68        slot_mode = await dev.Sys_getMode_async(slot)
 69        print("slot mode:", slot_mode)
 70
 71        ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
 72        if slot_mode != "DIO":
 73            err = await dev.Sys_setDIOMode_async(slot)
 74            print(f"Sys_setDIOMode_async in slot {slot}, status: {err}")
 75
 76        ## Get slot mode
 77        slot_mode = await dev.Sys_getMode_async(slot)
 78        print("slot mode:", slot_mode)
 79
 80        ## Get DIO start up information
 81        info = await dev.DIO_loadStartup_async(DO_port)
 82        print(f"Enable: {info[0]}")
 83        print(f"Direction: {info[1]}")
 84        print(f"State: {info[2]}")
 85
 86        ## Write DO port to high or low
 87        err = await dev.DO_writePort_async(DO_port, DO_value)
 88        print(f"DO_writePort_async in DO_port {DO_port}, status: {err}")
 89
 90        ## Read DI port state
 91        state_list = await dev.DI_readPort_async(DI_port)
 92        print(f"state_list in DI_port {DI_port}: {state_list}")
 93    except Exception as err:
 94        pywpc.printGenericError(err)
 95
 96    finally:
 97        ## Disconnect device
 98        dev.disconnect()
 99
100        ## Release device handle
101        dev.close()
102
103
104def main_for_spyder(*args):
105    if asyncio.get_event_loop().is_running():
106        return asyncio.create_task(main(*args)).result()
107    else:
108        return asyncio.run(main(*args))
109
110
111if __name__ == '__main__':
112    asyncio.run(main())  ## Use terminal
113    # await main()  ## Use Jupyter or IPython(>=7.0)
114    # main_for_spyder()  ## Use Spyder