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