DO blinky pins

  1'''
  2DIO - DO_blinky_pins.py with synchronous mode.
  3
  4This example illustrates the process of writing a high or low signal to a DO pin from STEM.
  5
  6To begin with, it demonstrates the steps required to open the DO pin.
  7Next, in each loop, a different voltage output is applied, resulting in a blinking effect.
  8Lastly, it concludes by closing the DO pin.
  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
 37
 38def main():
 39    ## Get Python driver version
 40    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 41
 42    ## Create device handle
 43    dev = pywpc.STEM()
 44
 45    ## Connect to device
 46    try:
 47        dev.connect("192.168.1.110")  ## Depend on your device
 48    except Exception as err:
 49        pywpc.printGenericError(err)
 50        ## Release device handle
 51        dev.close()
 52        return
 53
 54    try:
 55        ## Parameters setting
 56        slot = 1  ## Connect DIO module to slot
 57        DO_port = 1
 58        pinindex = [1, 3, 5, 7]
 59        timeout = 3  ## [sec]
 60
 61        ## Get firmware model & version
 62        driver_info = dev.Sys_getDriverInfo(timeout)
 63        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 64
 65        ## Get slot mode
 66        slot_mode = dev.Sys_getMode(slot, timeout)
 67        print("Slot mode:", slot_mode)
 68
 69        ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
 70        if slot_mode != "DIO":
 71            err = dev.Sys_setDIOMode(slot, timeout)
 72            print(f"Sys_setDIOMode in slot {slot}, status: {err}")
 73
 74        ## Get slot mode
 75        slot_mode = dev.Sys_getMode(slot, timeout)
 76        print("Slot mode:", slot_mode)
 77
 78        ## Get DIO start up information
 79        info = dev.DIO_loadStartup(DO_port, timeout)
 80        print(f"Enable: {info[0]}")
 81        print(f"Direction: {info[1]}")
 82        print(f"State: {info[2]}")
 83
 84        ## Toggle digital state for 10 times. Each times delay for 0.5 second
 85        for i in range(10):
 86            state = dev.DO_togglePins(DO_port, pinindex, timeout)
 87            print(state)
 88
 89            ## Wait for 0.5 second to see led status
 90            time.sleep(0.5)  ## delay [sec]
 91    except Exception as err:
 92        pywpc.printGenericError(err)
 93
 94    finally:
 95        ## Disconnect device
 96        dev.disconnect()
 97
 98        ## Release device handle
 99        dev.close()
100
101
102if __name__ == '__main__':
103    main()