AO write one channel

  1'''
  2AO - AO_write_one_channel.py with asynchronous 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_async`.
 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## Python
 25import asyncio
 26import sys
 27sys.path.insert(0, 'src/')
 28
 29
 30async def main():
 31    ## Get Python driver version
 32    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 33
 34    ## Create device handle
 35    dev = pywpc.STEM()
 36
 37    ## Connect to device
 38    try:
 39        dev.connect("192.168.1.110")  ## Depend on your device
 40    except Exception as err:
 41        pywpc.printGenericError(err)
 42        ## Release device handle
 43        dev.close()
 44        return
 45
 46    try:
 47        ## Parameters setting
 48        slot = 1  ## Connect AIO module to slot
 49        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
 50
 51        ## Get firmware model & version
 52        driver_info = await dev.Sys_getDriverInfo_async()
 53        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 54
 55        ## Get slot mode
 56        slot_mode = await dev.Sys_getMode_async(slot)
 57        print("Slot mode:", slot_mode)
 58
 59        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 60        if slot_mode != "AIO":
 61            err = await dev.Sys_setAIOMode_async(slot)
 62            print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
 63
 64        ## Get slot mode
 65        slot_mode = await dev.Sys_getMode_async(slot)
 66        print("Slot mode:", slot_mode)
 67
 68        ## Open AO
 69        err = await dev.AO_open_async(slot)
 70        print(f"AO_open_async in slot {slot}, status: {err}")
 71
 72        ## Write AO vaule in channel 0
 73        err = await dev.AO_writeOneChannel_async(slot, 0, ao_value_list[0])
 74        print(f"In slot {slot} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
 75
 76        ## Write AO vaule in channel 1
 77        err = await dev.AO_writeOneChannel_async(slot, 1, ao_value_list[1])
 78        print(f"In slot {slot} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
 79
 80        ## Write AO vaule in channel 2
 81        err = await dev.AO_writeOneChannel_async(slot, 2, ao_value_list[2])
 82        print(f"In slot {slot} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
 83
 84        ## Write AO vaule in channel 3
 85        err = await dev.AO_writeOneChannel_async(slot, 3, ao_value_list[3])
 86        print(f"In slot {slot} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
 87
 88        ## Close AO
 89        err = await dev.AO_close_async(slot)
 90        print(f"AO_close_async in slot {slot}, status: {err}")
 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
102def main_for_spyder(*args):
103    if asyncio.get_event_loop().is_running():
104        return asyncio.create_task(main(*args)).result()
105    else:
106        return asyncio.run(main(*args))
107
108
109if __name__ == '__main__':
110    asyncio.run(main())  ## Use terminal
111    # await main()  ## Use Jupyter or IPython(>=7.0)
112    # main_for_spyder()  ## Use Spyder