DO write port

 1'''
 2DIO - DO_write_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, voltage output is written to the DO port.
 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
34def main():
35    ## Get Python driver version
36    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
37
38    ## Create device handle
39    dev = pywpc.STEM()
40
41    ## Connect to device
42    try:
43        dev.connect("192.168.1.110")  ## Depend on your device
44    except Exception as err:
45        pywpc.printGenericError(err)
46        ## Release device handle
47        dev.close()
48        return
49
50    try:
51        ## Parameters setting
52        slot = 1  ## Connect DIO module to slot
53        DO_port = 0
54        DO_value = [1, 0, 1, 0]
55        timeout = 3  ## [sec]
56
57        ## Get firmware model & version
58        driver_info = dev.Sys_getDriverInfo(timeout)
59        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
60
61        ## Get slot mode
62        slot_mode = dev.Sys_getMode(slot, timeout)
63        print("Slot mode:", slot_mode)
64
65        ## If the slot mode is not set to "DIO", set the slot mode to "DIO"
66        if slot_mode != "DIO":
67            err = dev.Sys_setDIOMode(slot, timeout)
68            print(f"Sys_setDIOMode in slot {slot}, status: {err}")
69
70        ## Get slot mode
71        slot_mode = dev.Sys_getMode(slot, timeout)
72        print("Slot mode:", slot_mode)
73
74        ## Get DIO start up information
75        info = dev.DIO_loadStartup(DO_port, timeout)
76        print(f"Enable: {info[0]}")
77        print(f"Direction: {info[1]}")
78        print(f"State: {info[2]}")
79
80        ## Write port to high or low
81        err = dev.DO_writePort(DO_port, DO_value, timeout)
82        print(f"DO_writePort in DO_port {DO_port}, status: {err}")
83    except Exception as err:
84        pywpc.printGenericError(err)
85
86    finally:
87        ## Disconnect device
88        dev.disconnect()
89
90        ## Release device handle
91        dev.close()
92
93
94if __name__ == '__main__':
95    main()