An Engineer's Blog

Back

STM32F4 USB OTG and Modbus RTUBlur image

Overview

A take-home assignment for an interview asked me to talk to PLC-style machines and a HRI interface inside a high voltage control box. Those boxes run Modbus RTU, so I built both sides of the conversation: an STM32F4 Discovery board as a Modbus RTU slave over its USB OTG port, and a Python harness on the PC doing the asking.

One minute demo: board first, then the two Python scripts on the PC.

The setup needs no USB-to-serial adapter and no RS-485 dongle: the board’s own USB OTG port shows up on the PC as a virtual COM port, and Modbus RTU runs over it directly.

The firmware side

The board runs a small RTOS whose blinker thread doubles as a status light: a different blink period when USB_OTG is active, duty cycle 50% either way. You can tell from across the desk whether the connection is up.

Two things come out of the USB port:

  1. A plain byte stream with board status, the words process: on and process: off among them.
  2. Modbus RTU frames when a master asks. The slave supports a 16-bit CRC, function code 1 (Read Coils) and function code 5 (Write Single Coil). Anything else gets a Modbus exception instead of silence.

The PC side with Python

Two short scripts drive the demo. usb_ser.py opens the virtual COM port, decodes the raw byte stream, and appends it to a log file:

Decoded Byte: 1947fKM process: off
Decoded Byte: 1947fKM process: on
text

mb_tester.py uses PyModBus ↗ as the Modbus master. With pymodbus debug logging on you can see what goes over the wire:

DEBUG:pymodbus:Running Current transaction state: IDLE
DEBUG:pymodbus:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus:GET: Read Coils Response
DEBUG:pymodbus:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
text

The demo reads coils, writes a single coil on and off, then pokes an address that is not implemented. The board answers with exception code 2, illegal data address, the correct way for a slave to say “ask me something I have”.

Why this simple demo is enough

Read some coils, write one coil, log a few bytes: that is genuinely the whole vocabulary, and industrial machines do not speak a bigger one. Every Modbus device, from a cheap power meter to a factory PLC, exposes coils for on/off facts (motor running, fault latched, door closed) and registers for numbers (temperature, vibration, current draw). Read Coils and Write Single Coil cover what a maintenance system does: read the machine’s state, occasionally tell it something. A real PLC just has more addresses and more data behind each one. The 16-bit CRC, the request/response states, and the exception replies are identical in the field.

Predictive maintenance is a boring loop: poll the machine on a schedule, log what comes back, watch the trend lines. A bearing does not fail silently; current draw creeps up, vibration climbs, a coil that used to answer starts throwing exceptions. The process: on/off bytes in the first script are the smallest possible version of that loop. Point the same PyModBus client at a real machine, run it from cron, feed the log to anything that charts trends, and the desk demo scales to a control box.

The exception path matters most. A slave that goes quiet on a bad request stalls the whole polling loop; one that answers with exception code 2 lets the poller log it and move on. Where the poller might be the only thing watching, that reply keeps the trend lines continuous.

References

STM32F4 USB OTG and Modbus RTU
https://tin.ng/blog/2023-04-06--stm32f4-usb-otg-modbus-rtu
Author Tin Nguyen
Published at April 6, 2023
Comment seems to stuck. Try to refresh?✨