AO write one channel

 1'''
 2AO - AO_write_one_channel.py with synchronous mode.
 3
 4This example demonstrates the process of writing AO signal of STEM.
 5To begin with, it demonstrates the steps to open AO.
 6Next, it outlines the procedure for writing digital signals with channel to the AO pins.
 7Finally, it concludes by explaining how to close AO.
 8
 9If your product is "STEM", please invoke the function `Sys_setAIOMode`.
10
11-------------------------------------------------------------------------------------
12Please change correct serial number or IP and port number BEFORE you run example code.
13
14For other examples please check:
15    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
16See README.md file to get detailed usage of this example.
17
18Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
19'''
20
21## WPC
22from wpcsys import pywpc
23
24
25def main():
26    ## Get Python driver version
27    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
28
29    ## Create device handle
30    dev = pywpc.STEM()
31
32    ## Connect to device
33    try:
34        dev.connect("192.168.1.110")  ## Depend on your device
35    except Exception as err:
36        pywpc.printGenericError(err)
37        ## Release device handle
38        dev.close()
39        return
40
41    try:
42        ## Parameters setting
43        slot = 1  ## Connect AIO module to slot
44        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
45        timeout = 3  ## [sec]
46
47        ## Get firmware model & version
48        driver_info = dev.Sys_getDriverInfo(timeout)
49        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
50
51        ## Get slot mode
52        slot_mode = dev.Sys_getMode(slot, timeout)
53        print("Slot mode:", slot_mode)
54
55        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
56        if slot_mode != "AIO":
57            err = dev.Sys_setAIOMode(slot, timeout)
58            print(f"Sys_setAIOMode in slot {slot}, status: {err}")
59
60        ## Get slot mode
61        slot_mode = dev.Sys_getMode(slot, timeout)
62        print("Slot mode:", slot_mode)
63
64        ## Open AO
65        err = dev.AO_open(slot, timeout)
66        print(f"AO_open in slot {slot}, status: {err}")
67
68        ## Write AO vaule in channel 0
69        err = dev.AO_writeOneChannel(slot, 0, ao_value_list[0], timeout)
70        print(f"In slot {slot} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
71
72        ## Write AO vaule in channel 1
73        err = dev.AO_writeOneChannel(slot, 1, ao_value_list[1], timeout)
74        print(f"In slot {slot} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
75
76        ## Write AO vaule in channel 2
77        err = dev.AO_writeOneChannel(slot, 2, ao_value_list[2], timeout)
78        print(f"In slot {slot} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
79
80        ## Write AO vaule in channel 3
81        err = dev.AO_writeOneChannel(slot, 3, ao_value_list[3], timeout)
82        print(f"In slot {slot} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
83
84        ## Close AO
85        err = dev.AO_close(slot)
86        print(f"AO_close in slot {slot}, status: {err}")
87    except Exception as err:
88        pywpc.printGenericError(err)
89
90    finally:
91        ## Disconnect device
92        dev.disconnect()
93
94        ## Release device handle
95        dev.close()
96
97
98if __name__ == '__main__':
99    main()