AIO all channels loopback

  1'''
  2AIO - AIO_all_channels_loopback.py with synchronous mode.
  3
  4This example demonstrates the process of AIO loopback across all channels of STEM.
  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
 13If your product is "STEM", please invoke the function `Sys_setAIOMode` and `AI_enableCS`.
 14Example: AI_enableCS is {0, 2}
 15Subsequently, the returned value of AI_readOnDemand and AI_readStreaming will be displayed as follows.
 16data:
 17          CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7
 18          |                                     |                                      |
 19          |---------------- CS0-----------------|---------------- CS2------------------|
 20[sample0]
 21[sample1]
 22   .
 23   .
 24   .
 25[sampleN]
 26
 27-------------------------------------------------------------------------------------
 28Please change correct serial number or IP and port number BEFORE you run example code.
 29
 30For other examples please check:
 31    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 32See README.md file to get detailed usage of this example.
 33
 34Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
 35'''
 36
 37## WPC
 38from wpcsys import pywpc
 39
 40
 41
 42def main():
 43    ## Get Python driver version
 44    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 45
 46    ## Create device handle
 47    dev = pywpc.STEM()
 48
 49    ## Connect to device
 50    try:
 51        dev.connect("192.168.1.110")  ## Depend on your device
 52    except Exception as err:
 53        pywpc.printGenericError(err)
 54        ## Release device handle
 55        dev.close()
 56        return
 57
 58    try:
 59        ## Parameters setting
 60        slot = 1  ## Connect AIO module to slot
 61        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
 62        timeout = 3  ## [sec]
 63        chip_select = [0]
 64
 65        ## Get firmware model & version
 66        driver_info = dev.Sys_getDriverInfo(timeout)
 67        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 68
 69        ## Get slot mode
 70        slot_mode = dev.Sys_getMode(slot, timeout)
 71        print("Slot mode:", slot_mode)
 72
 73        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 74        if slot_mode != "AIO":
 75            err = dev.Sys_setAIOMode(slot, timeout)
 76            print(f"Sys_setAIOMode in slot {slot}, status: {err}")
 77
 78        ## Get slot mode
 79        slot_mode = dev.Sys_getMode(slot, timeout)
 80        print("Slot mode:", slot_mode)
 81
 82        ## Open AI
 83        err = dev.AI_open(slot, timeout)
 84        print(f"AI_open in slot {slot}, status: {err}")
 85
 86        ## Enable CS
 87        err = dev.AI_enableCS(slot, chip_select, timeout)
 88        print(f"AI_enableCS in slot {slot}, status: {err}")
 89
 90        ## Open AO
 91        err = dev.AO_open(slot, timeout)
 92        print(f"AO_open in slot {slot}, status: {err}")
 93
 94        ## Read data acquisition
 95        ai_list = dev.AI_readOnDemand(slot, timeout)
 96        print(f"AI data in slot {slot}: {ai_list}")
 97
 98        ## Write AO value simultaneously
 99        err = dev.AO_writeAllChannels(slot, ao_value_list, timeout)
100        print(f"In slot {slot} the AO value is {ao_value_list}, status: {err}")
101
102        ## Read data acquisition
103        ai_list = dev.AI_readOnDemand(slot, timeout)
104        print(f"AI data in slot {slot}: {ai_list}")
105
106        ## Close AI
107        err = dev.AI_close(slot, timeout)
108        print(f"AI_close in slot {slot}, status: {err}")
109
110        ## Close AO
111        err = dev.AO_close(slot, timeout)
112        print(f"AO_close in slot {slot}, status: {err}")
113    except Exception as err:
114        pywpc.printGenericError(err)
115
116    finally:
117        ## Disconnect device
118        dev.disconnect()
119
120        ## Release device handle
121        dev.close()
122
123
124if __name__ == '__main__':
125    main()