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 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
 41def main():
 42    ## Get Python driver version
 43    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 44
 45    ## Create device handle
 46    dev = pywpc.STEM()
 47
 48    ## Connect to device
 49    try:
 50        dev.connect("192.168.1.110")  ## Depend on your device
 51    except Exception as err:
 52        pywpc.printGenericError(err)
 53        ## Release device handle
 54        dev.close()
 55        return
 56
 57    try:
 58        ## Parameters setting
 59        slot = 1  ## Connect AIO module to slot
 60        chip_select = [0]
 61        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
 62        timeout = 3  ## [sec]
 63
 64        ## Get firmware model & version
 65        driver_info = dev.Sys_getDriverInfo(timeout)
 66        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 67
 68        ## Get slot mode
 69        slot_mode = dev.Sys_getMode(slot, timeout)
 70        print("Slot mode:", slot_mode)
 71
 72        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 73        if slot_mode != "AIO":
 74            err = dev.Sys_setAIOMode(slot, timeout)
 75            print(f"Sys_setAIOMode in slot {slot}, status: {err}")
 76
 77        ## Get slot mode
 78        slot_mode = dev.Sys_getMode(slot, timeout)
 79        print("Slot mode:", slot_mode)
 80
 81        ## Open AI
 82        err = dev.AI_open(slot, timeout)
 83        print(f"AI_open in slot {slot}, status: {err}")
 84
 85        ## Enable CS
 86        err = dev.AI_enableCS(slot, chip_select, timeout)
 87        print(f"AI_enableCS in slot {slot}, status: {err}")
 88
 89        ## Open AO
 90        err = dev.AO_open(slot, timeout)
 91        print(f"AO_open in slot {slot}, status: {err}")
 92
 93        ## Read data acquisition
 94        ai_list = dev.AI_readOnDemand(slot, timeout)
 95        print(f"AI data in slot {slot}: {ai_list}")
 96
 97        ## Write AO vaule in channel 0
 98        err = dev.AO_writeOneChannel(slot, 0, ao_value_list[0], timeout)
 99        print(f"In slot {slot} channel 0, the AO value is {ao_value_list[0]}, status: {err}")
100
101        ## Write AO vaule in channel 1
102        err = dev.AO_writeOneChannel(slot, 1, ao_value_list[1], timeout)
103        print(f"In slot {slot} channel 1, the AO value is {ao_value_list[1]}, status: {err}")
104
105        ## Write AO vaule in channel 2
106        err = dev.AO_writeOneChannel(slot, 2, ao_value_list[2], timeout)
107        print(f"In slot {slot} channel 2, the AO value is {ao_value_list[2]}, status: {err}")
108
109        ## Write AO vaule in channel 3
110        err = dev.AO_writeOneChannel(slot, 3, ao_value_list[3], timeout)
111        print(f"In slot {slot} channel 3, the AO value is {ao_value_list[3]}, status: {err}")
112
113        ## Read data acquisition
114        ai_list = dev.AI_readOnDemand(slot, timeout)
115        print(f"AI data in slot {slot}: {ai_list}")
116
117        ## Close AI
118        err = dev.AI_close(slot, timeout)
119        print(f"AI_close in slot {slot}, status: {err}")
120
121        ## Close AO
122        err = dev.AO_close(slot, timeout)
123        print(f"AO_close in slot {slot}, status: {err}")
124    except Exception as err:
125        pywpc.printGenericError(err)
126
127    finally:
128        ## Disconnect device
129        dev.disconnect()
130
131        ## Release device handle
132        dev.close()
133
134
135if __name__ == '__main__':
136    main()