AIO one channel loopback

  1'''
  2AIO - AIO_one_channel_loopback.py with synchronous 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-2025 WPC Systems Ltd. All rights reserved.
 21'''
 22
 23## WPC
 24from wpcsys import pywpc
 25
 26
 27def main():
 28    ## Get Python driver version
 29    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 30
 31    ## Create device handle
 32    dev = pywpc.USBDAQF1AOD()
 33
 34    ## Connect to device
 35    try:
 36        dev.connect("default")  ## Depend on your device
 37    except Exception as err:
 38        pywpc.printGenericError(err)
 39        ## Release device handle
 40        dev.close()
 41        return
 42
 43    try:
 44        ## Parameters setting
 45        port = 0  ## Depend on your device
 46        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
 47        timeout = 3  ## [sec]
 48        channel = 8
 49        ## Get firmware model & version
 50        driver_info = dev.Sys_getDriverInfo(timeout)
 51        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 52
 53        ## Open AI
 54        err = dev.AI_open(port, timeout)
 55        print(f"AI_open in port {port}, status: {err}")
 56        
 57        ## Set AI channel
 58        err = dev.AI_enableChannel(port, channel, timeout)
 59        print(f"AI_enableChannel in port {port}, status: {err}")
 60
 61        ## Open AO
 62        err = dev.AO_open(port, timeout)
 63        print(f"AO_open in port {port}, status: {err}")
 64
 65        ## Read data acquisition
 66        ai_list = dev.AI_readOnDemand(port, timeout)
 67        print(f"AI data in port {port}: {ai_list}")
 68
 69        ## Write AO vaule in channel 0
 70        err = dev.AO_writeOneChannel(port, 0, ao_value_list[0], timeout)
 71        print(f"In port {port} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
 72
 73        ## Write AO vaule in channel 1
 74        err = dev.AO_writeOneChannel(port, 1, ao_value_list[1], timeout)
 75        print(f"In port {port} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
 76
 77        ## Write AO vaule in channel 2
 78        err = dev.AO_writeOneChannel(port, 2, ao_value_list[2], timeout)
 79        print(f"In port {port} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
 80
 81        ## Write AO vaule in channel 3
 82        err = dev.AO_writeOneChannel(port, 3, ao_value_list[3], timeout)
 83        print(f"In port {port} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
 84
 85        ## Read data acquisition
 86        ai_list = dev.AI_readOnDemand(port, timeout)
 87        print(f"AI data in port {port}: {ai_list}")
 88
 89        ## Close AI
 90        err = dev.AI_close(port, timeout)
 91        print(f"AI_close in port {port}, status: {err}")
 92
 93        ## Close AO
 94        err = dev.AO_close(port, timeout)
 95        print(f"AO_close in port {port}, status: {err}")
 96    except Exception as err:
 97        pywpc.printGenericError(err)
 98
 99    finally:
100        ## Disconnect device
101        dev.disconnect()
102
103        ## Release device handle
104        dev.close()
105
106
107if __name__ == '__main__':
108    main()