AIO one channel loopback

  1'''
  2AIO - AIO_one_channel_loopback.py with asynchronous mode.
  3
  4This example demonstrates the process of AIO loopback with specific channels of WifiDAQE3AOD.
  5It involves using AO pins to send signals and AI pins to receive signals on a single device, commonly referred to as "loopback".
  6The AI and AO pins are connected using a wire.
  7
  8Initially, the example demonstrates the steps required to open the AI and AO.
  9Next, it reads AI data and displays its corresponding values.
 10Following that, it writes digital signals to the AO pins and reads AI on-demand data once again.
 11Lastly, it closes the AO and AI ports.
 12
 13-------------------------------------------------------------------------------------
 14Please change correct serial number or IP and port number BEFORE you run example code.
 15
 16For other examples please check:
 17    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 18See README.md file to get detailed usage of this example.
 19
 20Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 21'''
 22
 23## WPC
 24from wpcsys import pywpc
 25
 26## Python
 27import asyncio
 28import sys
 29sys.path.insert(0, 'src/')
 30
 31
 32async def main():
 33    ## Get Python driver version
 34    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 35
 36    ## Create device handle
 37    dev = pywpc.WifiDAQE3AOD()
 38
 39    ## Connect to device
 40    try:
 41        dev.connect("192.168.5.38")  ## Depend on your device
 42    except Exception as err:
 43        pywpc.printGenericError(err)
 44        ## Release device handle
 45        dev.close()
 46        return
 47
 48    try:
 49        ## Parameters setting
 50        port = 0  ## Depend on your device
 51        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
 52        
 53
 54        ## Get firmware model & version
 55        driver_info = await dev.Sys_getDriverInfo_async()
 56        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 57
 58        ## Open AI
 59        err = await dev.AI_open_async(port)
 60        print(f"AI_open_async in port {port}, status: {err}")
 61        
 62
 63        ## Open AO
 64        err = await dev.AO_open_async(port)
 65        print(f"AO_open_async in port {port}, status: {err}")
 66
 67        ## Read data acquisition
 68        ai_list = await dev.AI_readOnDemand_async(port)
 69        print(f"AI data in port {port}: {ai_list}")
 70
 71        ## Write AO vaule in channel 0
 72        err = await dev.AO_writeOneChannel_async(port, 0, ao_value_list[0])
 73        print(f"In port {port} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
 74
 75        ## Write AO vaule in channel 1
 76        err = await dev.AO_writeOneChannel_async(port, 1, ao_value_list[1])
 77        print(f"In port {port} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
 78
 79        ## Write AO vaule in channel 2
 80        err = await dev.AO_writeOneChannel_async(port, 2, ao_value_list[2])
 81        print(f"In port {port} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
 82
 83        ## Write AO vaule in channel 3
 84        err = await dev.AO_writeOneChannel_async(port, 3, ao_value_list[3])
 85        print(f"In port {port} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
 86
 87        ## Read data acquisition
 88        ai_list = await dev.AI_readOnDemand_async(port)
 89        print(f"AI data in port {port}: {ai_list}")
 90
 91        ## Close AI
 92        err = await dev.AI_close_async(port)
 93        print(f"AI_close_async in port {port}, status: {err}")
 94
 95        ## Close AO
 96        err = await dev.AO_close_async(port)
 97        print(f"AO_close_async in port {port}, status: {err}")
 98    except Exception as err:
 99        pywpc.printGenericError(err)
100
101    finally:
102        ## Disconnect device
103        dev.disconnect()
104
105        ## Release device handle
106        dev.close()
107
108
109def main_for_spyder(*args):
110    if asyncio.get_event_loop().is_running():
111        return asyncio.create_task(main(*args)).result()
112    else:
113        return asyncio.run(main(*args))
114
115
116if __name__ == '__main__':
117    asyncio.run(main())  ## Use terminal
118    # await main()  ## Use Jupyter or IPython(>=7.0)
119    # main_for_spyder()  ## Use Spyder