Test Example-9b: Data Exchange between Raspberry Pi Pico and Python

Test Example-9b: Data Exchange between Raspberry Pi Pico and Python

CASP User Data Input Output (UDIO) is a light weight communication protocol used to transfer data between the target (such as a micro-controller board or an SBC) and the host computer. Unlike CASP GPIO protocol, CASP UDIO transfers user data (bytes, integers and floats) instead of GPIO data. This example demonstrates how to exchange data between Raspberry Pi Pico board and Python running on host computer using UDIO protocol through serial communication.

Pre-Requisites

  • Raspberry Pi Pico board with a USB cable.
  • CASP software is installed along with CASP Python Libraries.
  • Raspberry Pi Pico BSP is installed.
  • The project files are located at CASP installed directory ‘CASP/support/examples/casp_udio_examples/udio_led_control_pico’. These are also available for download at this link.

Model Details

CASP model that runs on the target board:

  • Reads ADC pin value and transfer the value to the host as a UDI 32-bit integer data at byte index 0.
  • Controls the frequency of the on-board LED based on the frequency value received from host at byte index 0 as a UDO 32-bit floating point value.
  • Controls PWM pin based on the amplitude value received from the host at byte index 4 as a UDO 32-bit integer value.

On the host side, a simple Python script is written to communicate with the target board and access the above data.

Upload Target Model to Raspberry Pi Pico

  • Ensure, the board is connected to the computer.
  • Run CASP and open the target project ‘CASP/support/examples/casp_udio_examples/udio_led_control_pico/control_led_udio_target_pico/control_led_udio_target_pico.prj’ from CASP installed directory.
  • Open ‘Setup Simulation’ window and change the ‘Target Programmer Port’ to which the board is connected.
  • Click Run button to program the board.

Accessing UDIO data from Python on Host PC

  • Close existing CASP program.
  • Open terminal in any directory.
  • Start Python by typing ‘Python’ or ‘Python3’ in the terminal. Type following commands in Python to access UDIO values from the target board.
> import casp_udio as udio #import CASP UDIO module.

> udio.Init() #initialize CASP GPIO client process on host computer.

> udio.SerialPorts() #display available serial ports (used to identify the serial port to which the board is connected).

> udio.ConnectSerial("COM35", 115200, True, 1, 16, 16) #starts communicating with the target board using GPIO protocol on COM35. Change the serial port as per your setup. The last two arguments (16, 16) represent UDI and UDO bytes respectively.

> udio.ReadI(0) #reads UDI 32-bit integer value at index-0. This value represents ADC value in the above target model.

> udio.WriteF(0, 1) #writes UDO 32-bit float value at index-0. This value represents LED blink frequency in the above target model.

> udio.WriteI(1, 200) #writes UDO 32-bit integer value at index-1. This value represents PWM magnitude in the above target model.

Below is the screen shot of above commands. You may now try to write your own logic with this data.

Related Links