AO write all channels

  1'''
  2AO - AO_write_all_channels.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 simultaneously 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 value simultaneously
 73        err = await dev.AO_writeAllChannels_async(slot, ao_value_list)
 74        print(f"In slot {slot} the AO value is {ao_value_list}, status: {err}")
 75
 76        ## Close AO
 77        err = await dev.AO_close_async(slot)
 78        print(f"AO_close_async in slot {slot}, status: {err}")
 79    except Exception as err:
 80        pywpc.printGenericError(err)
 81
 82    finally:
 83        ## Disconnect device
 84        dev.disconnect()
 85
 86        ## Release device handle
 87        dev.close()
 88
 89
 90def main_for_spyder(*args):
 91    if asyncio.get_event_loop().is_running():
 92        return asyncio.create_task(main(*args)).result()
 93    else:
 94        return asyncio.run(main(*args))
 95
 96
 97if __name__ == '__main__':
 98    asyncio.run(main())  ## Use terminal
 99    # await main()  ## Use Jupyter or IPython(>=7.0)
100    # main_for_spyder()  ## Use Spyder