HAL UART Modbus RTU Example

Demonstrates advance usage of UART HAL.

Overview

This example demonstrates:

  1. How to implement a protocol where the number of bytes to be received is not known ahead of time. Since the SYSCFG UART HAL interrupt-mode RX API transfers a fixed number of bytes per call, this example issues single-byte RX requests in a loop and uses a one-shot software timer to detect the MODBUS RTU inter-frame silence period, which marks the end of a variable-length frame.

  2. A simple MODBUS RTU slave, supporting a single holding register (address 0x0000) via the Read Holding Registers (0x03) and Write Single Register (0x06) function codes.

  3. How to use the SYSCFG-generated UART HAL driver. The HAL driver internally uses UART driverlib APIs for the actual register-level operations.

Hardware Setup

On the AM13E230X LaunchPad, the UART is routed through the on-board XDS110 debug probe. Connect the LaunchPad to your PC using the same USB cable used for JTAG debugging. The XDS110 provides a virtual COM port for UART communication - no additional hardware required.

Expected Output

Open a serial terminal on the virtual COM port to view the LOG messages.

Serial Terminal Configuration

  • Baud Rate: 9600

  • Data Bits: 8

  • Parity: None

  • Stop Bits: 1

  • Flow Control: None

Testing with the Included Python Script

A bare-minimum MODBUS RTU test script, test_modbus_rtu.py, is included alongside this example. It sends a Write Single Register request followed by a Read Holding Registers request to the target, and verifies the written value is correctly read back.

Requires pyserial (pip install pyserial).

Example usage on Linux/macOS:

python3 test_modbus_rtu.py /dev/ttyACM0 1234

Example usage on Windows (use the appropriate COM port):

python3 test_modbus_rtu.py COM5 1234

The script prints PASS and exits with status code 0 on success, or prints FAIL: and exits with a non-zero status code on any communication error or value mismatch.