DO blinky port

  1'''
  2DIO - DO_blinky_port.py with synchronous mode.
  3
  4This example illustrates the process of writing a high or low signal to a DO port from STEM.
  5
  6To begin with, it demonstrates the steps required to open the DO port.
  7Next, in each loop, a different voltage output is applied, resulting in a blinking effect.
  8Lastly, it concludes by closing the DO port.
  9
 10If your product is "STEM", please invoke the function `Sys_setDIOMode`.
 11
 12The DIO ports 0 to 1 are assigned to slot 1, while ports 2 to 3 are assigned to slot 2.
 13---------------------------
 14|  Slot 1    port 1 & 0   |
 15|  Slot 2    port 3 & 2   |
 16|  Slot 3    port 5 & 4   |
 17|  Slot 4    port 7 & 6   |
 18---------------------------
 19
 20-------------------------------------------------------------------------------------
 21Please change correct serial number or IP and port number BEFORE you run example code.
 22
 23For other examples please check:
 24    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 25See README.md file to get detailed usage of this example.
 26
 27Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 28'''
 29
 30## WPC
 31from wpcsys import pywpc
 32
 33## Python
 34import time
 35
 36
 37def main():
 38    ## Get Python driver version
 39    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 40
 41    ## Create device handle
 42    dev = pywpc.STEM()
 43
 44    ## Connect to device
 45    try:
 46        dev.connect("192.168.1.110")  ## Depend on your device
 47    except Exception as err:
 48        pywpc.printGenericError(err)
 49        ## Release device handle
 50        dev.close()
 51        return
 52
 53    try:
 54        ## Parameters setting
 55        slot = 1  ## Connect DIO module to slot
 56        DO_port = 0
 57        timeout = 3  ## [sec]
 58
 59        ## Get firmware model & version
 60        driver_info = dev.Sys_getDriverInfo(timeout)
 61        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 62
 63        ## Get slot mode
 64        slot_mode = dev.Sys_getMode(slot, timeout)
 65        print("Slot mode:", slot_mode)
 66
 67        ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
 68        if slot_mode != "DIO":
 69            err = dev.Sys_setDIOMode(slot, timeout)
 70            print(f"Sys_setDIOMode in slot {slot}, status: {err}")
 71
 72        ## Get slot mode
 73        slot_mode = dev.Sys_getMode(slot, timeout)
 74        print("Slot mode:", slot_mode)
 75
 76        ## Get DIO start up information
 77        info = dev.DIO_loadStartup(DO_port, timeout)
 78        print(f"Enable: {info[0]}")
 79        print(f"Direction: {info[1]}")
 80        print(f"State: {info[2]}")
 81
 82        ## Toggle digital state for 10 times. Each times delay for 0.5 second
 83        for i in range(10):
 84            state = dev.DO_togglePort(DO_port, timeout)
 85            print(state)
 86
 87            ## Wait for 0.5 second to see led status
 88            time.sleep(0.5)  ## delay [sec]
 89    except Exception as err:
 90        pywpc.printGenericError(err)
 91
 92    finally:
 93        ## Disconnect device
 94        dev.disconnect()
 95
 96        ## Release device handle
 97        dev.close()
 98
 99
100if __name__ == '__main__':
101    main()