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 USBDAQF1AOD.
  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-2024 WPC Systems Ltd. All rights reserved.
 21'''
 22
 23## Python
 24import asyncio
 25
 26## WPC
 27
 28from wpcsys import pywpc
 29
 30
 31async def main():
 32    ## Get Python driver version
 33    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 34
 35    ## Create device handle
 36    dev = pywpc.USBDAQF1AOD()
 37
 38    ## Connect to device
 39    try:
 40        dev.connect("default") ## Depend on your device
 41    except Exception as err:
 42        pywpc.printGenericError(err)
 43        ## Release device handle
 44        dev.close()
 45        return
 46
 47    try:
 48        ## Parameters setting
 49        port = 0 ## Depend on your device
 50        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
 51        channel = 8
 52
 53        ## Get firmware model & version
 54        driver_info = await dev.Sys_getDriverInfo_async()
 55        print("Model name: " + driver_info[0])
 56        print("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        ## Set AI channel
 63        err = await dev.AI_enableChannel_async(port, channel)
 64        print(f"AI_enableChannel_async in port {port}, status: {err}")
 65
 66        ## Open AO
 67        err = await dev.AO_open_async(port)
 68        print(f"AO_open_async in port {port}, status: {err}")
 69
 70        ## Read data acquisition
 71        ai_list = await dev.AI_readOnDemand_async(port)
 72        print(f"AI data in port {port}: {ai_list}")
 73
 74        ## Write AO vaule in channel 0
 75        err = await dev.AO_writeOneChannel_async(port, 0, ao_value_list[0])
 76        print(f"In port {port} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
 77
 78        ## Write AO vaule in channel 1
 79        err = await dev.AO_writeOneChannel_async(port, 1, ao_value_list[1])
 80        print(f"In port {port} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
 81
 82        ## Write AO vaule in channel 2
 83        err = await dev.AO_writeOneChannel_async(port, 2, ao_value_list[2])
 84        print(f"In port {port} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
 85
 86        ## Write AO vaule in channel 3
 87        err = await dev.AO_writeOneChannel_async(port, 3, ao_value_list[3])
 88        print(f"In port {port} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
 89
 90        ## Read data acquisition
 91        ai_list = await dev.AI_readOnDemand_async(port)
 92        print(f"AI data in port {port}: {ai_list}")
 93
 94        ## Close AI
 95        err = await dev.AI_close_async(port)
 96        print(f"AI_close_async in port {port}, status: {err}")
 97
 98        ## Close AO
 99        err = await dev.AO_close_async(port)
100        print(f"AO_close_async in port {port}, status: {err}")
101    except Exception as err:
102        pywpc.printGenericError(err)
103
104    ## Disconnect device
105    dev.disconnect()
106
107    ## Release device handle
108    dev.close()
109
110    return
111
112def main_for_spyder(*args):
113    if asyncio.get_event_loop().is_running():
114        return asyncio.create_task(main(*args)).result()
115    else:
116        return asyncio.run(main(*args))
117if __name__ == '__main__':
118    asyncio.run(main()) ## Use terminal
119    # await main() ## Use Jupyter or IPython(>=7.0)
120    # main_for_spyder() ## Use Spyder