AIO all channels loopback

  1'''
  2AIO - AIO_all_channels_loopback.py with asynchronous 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_async` and `AI_enableCS_async`.
 14Example: AI_enableCS_async is {0, 2}
 15Subsequently, the returned value of AI_readOnDemand_async and AI_readStreaming_async 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 asyncio
 39
 40## WPC
 41
 42from wpcsys import pywpc
 43
 44
 45async def 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        ao_value_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]
 65        chip_select = [0]
 66
 67        ## Get firmware model & version
 68        driver_info = await dev.Sys_getDriverInfo_async()
 69        print("Model name: " + driver_info[0])
 70        print("Firmware version: " + driver_info[-1])
 71
 72        ## Get slot mode
 73        slot_mode = await dev.Sys_getMode_async(slot)
 74        print("slot mode:", slot_mode)
 75
 76        ## If the slot mode is not set to "AIO", set the slot mode to "AIO"
 77        if slot_mode != "AIO":
 78            err = await dev.Sys_setAIOMode_async(slot)
 79            print(f"Sys_setAIOMode_async in slot {slot}, status: {err}")
 80
 81        ## Get slot mode
 82        slot_mode = await dev.Sys_getMode_async(slot)
 83        print("slot mode:", slot_mode)
 84
 85        ## Open AI
 86        err = await dev.AI_open_async(slot)
 87        print(f"AI_open_async in slot {slot}, status: {err}")
 88
 89        ## Enable CS
 90        err = await dev.AI_enableCS_async(slot, chip_select)
 91        print(f"AI_enableCS_async in slot {slot}, status: {err}")
 92
 93        ## Open AO
 94        err = await dev.AO_open_async(slot)
 95        print(f"AO_open_async in slot {slot}, status: {err}")
 96
 97        ## Read data acquisition
 98        ai_list = await dev.AI_readOnDemand_async(slot)
 99        print(f"AI data in slot {slot}: {ai_list}")
100
101        ## Write AO value simultaneously
102        err = await dev.AO_writeAllChannels_async(slot, ao_value_list)
103        print(f"In slot {slot} the AO value is {ao_value_list}, status: {err}")
104
105        ## Read data acquisition
106        ai_list = await dev.AI_readOnDemand_async(slot)
107        print(f"AI data in slot {slot}: {ai_list}")
108
109        ## Close AI
110        err = await dev.AI_close_async(slot)
111        print(f"AI_close_async in slot {slot}, status: {err}")
112
113        ## Close AO
114        err = await dev.AO_close_async(slot)
115        print(f"AO_close_async in slot {slot}, status: {err}")
116    except Exception as err:
117        pywpc.printGenericError(err)
118
119    ## Disconnect device
120    dev.disconnect()
121
122    ## Release device handle
123    dev.close()
124
125    return
126
127def main_for_spyder(*args):
128    if asyncio.get_event_loop().is_running():
129        return asyncio.create_task(main(*args)).result()
130    else:
131        return asyncio.run(main(*args))
132if __name__ == '__main__':
133    asyncio.run(main()) ## Use terminal
134    # await main() ## Use Jupyter or IPython(>=7.0)
135    # main_for_spyder() ## Use Spyder