DIO loopback port

  1'''
  2DIO - DIO_loopback_port.py with synchronous 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`.
 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-2024 WPC Systems Ltd. All rights reserved.
 29'''
 30
 31## Python
 32import time
 33
 34## WPC
 35
 36from wpcsys import pywpc
 37
 38
 39def main():
 40    ## Get Python driver version
 41    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 42
 43    ## Create device handle
 44    dev = pywpc.STEM()
 45
 46    ## Connect to device
 47    try:
 48        dev.connect("192.168.1.110") ## Depend on your device
 49    except Exception as err:
 50        pywpc.printGenericError(err)
 51        ## Release device handle
 52        dev.close()
 53        return
 54
 55    try:
 56        ## Parameters setting
 57        slot = 1 ## Connect DIO module to slot
 58        DO_port = 0
 59        DI_port = 1
 60        DO_value = [1, 0, 1, 0]
 61        timeout = 3 ## second
 62
 63        ## Get firmware model & version
 64        driver_info = dev.Sys_getDriverInfo(timeout)
 65        print("Model name: " + driver_info[0])
 66        print("Firmware version: " + driver_info[-1])
 67
 68        ## Get slot mode
 69        slot_mode = dev.Sys_getMode(slot, timeout)
 70        print("Slot mode:", slot_mode)
 71
 72        ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
 73        if slot_mode != "DIO":
 74            err = dev.Sys_setDIOMode(slot, timeout)
 75            print(f"Sys_setDIOMode in slot {slot}, status: {err}")
 76
 77        ## Get slot mode
 78        slot_mode = dev.Sys_getMode(slot, timeout)
 79        print("Slot mode:", slot_mode)
 80
 81        ## Get DIO start up information
 82        info = dev.DIO_loadStartup(DO_port, timeout)
 83        print("Enable:   ", info[0])
 84        print("Direction:", info[1])
 85        print("State:    ", info[2])
 86
 87        ## Write DO port to high or low
 88        err = dev.DO_writePort(DO_port, DO_value, timeout)
 89        print(f"DO_writePort in DO_port {DO_port}, status: {err}")
 90
 91        ## Read DI port state
 92        state_list = dev.DI_readPort(DI_port, timeout)
 93        print(f"state_list in DI_port {DI_port}: {state_list}")
 94    except Exception as err:
 95        pywpc.printGenericError(err)
 96
 97    ## Disconnect device
 98    dev.disconnect()
 99
100    ## Release device handle
101    dev.close()
102
103    return
104
105if __name__ == '__main__':
106    main()