An STM32 HAL example of communicating with an energy meter and a display over the Modbus protocol. Both Modbus devices come from F&F. For the relevant datasheets visit LE-01M and MR-LED-T.
The main goal for this example is to let you get familiar with the Modbus communication protocol. Here the RTU (Remote Terminal Unit) transmission mode and the RS-485 in the physical layer are used. Reading and writing multiple registers is exercised. The example also encourages you to play with the CRC (Cyclic Redundancy Check) peripheral on STM32 microcontrollers - yes, you don't have to bother the main core to calculate it 🙂
Modbus was introduced in 1979 and is still popular in industrial environments - you will find plenty of devices that use it and a rich selection of monitoring/debugging tools to help you make the system run.
Don't worry 🙂 Just hit Alt-K to generate /Drivers/CMCIS/ and /Drivers/STM32G4xx_HAL_Driver/ based on the .ioc file. After a couple of seconds your project will be ready for building.
- OLED: stm32-ssd1306 (MIT license)
- KAmod USB RS485 ISO - USB - RS485 converter with galvanic isolation (KAMAMI)
- USB - RS232/RS485/UART(TTL) converter - FT232RL (Waveshare)
- USB TO RS485 Bidirectional Converter (Waveshare)
- Serial Server RS232/485 To WiFi and Ethernet, Modbus Gateway
- Simply Modbus Master
- CAS Modbus Scanner
- Serial Port Monitor / Modbus RTU Sniffer / Modbus Protocol Analyzer
- Android Modbus Monitor + USB OTG cable
- QModMaster
- YAT - Yet Another Terminal :: Serial Communication :: Engineer/Test/Debug - any serial terminal will do the job as far as you are OK with raw unstructured data.
A communication test with the energy meter:
A communication test with the display:
- RS-485 (Wikipedia)
- What is RS-485? (Texas Instruments)
- Modbus (Wikipedia)
- Modbus Technical Resources (Modbus Organization)
- How does Modbus Communication Protocol Work?
- MODBUS PROTOCOL 101
- Modbus #1. Read Holding and Input Registers
- STM32 Master Reads Coils and inputs
- Master Writes single Coil and Holding Register
- Master Writes Multiple Coils and Registers
- Modbus #4. STM32 as Slave || Read Holding and Input Registers
- Modbus #5. STM32 as Slave || Read Coils and Inputs
- Modbus #6. STM32 as Slave || Write Registers
- Modbus #7. STM32 as Slave || Writing Coils
- Cyclic redundancy check (Wikipedia)
- Checksums and Hamming distance (Ben Eater)
- How do CRCs work? (Ben Eater)
- Hardware build: CRC calculation (Ben Eater)
- AN4187 Application note: Using the CRC peripheral on STM32 microcontrollers (STMicroelectronics)
- CRC Calculator (Javascript) and many other CRC online calculators - you can use them to verify the configuration of the STM32 CRC peripheral.
Modbus CRC:
CubeMX CRC peripheral configuration:
Create your own home laboratory/workshop/garage! Get inspired by ControllersTech, DroneBot Workshop, Andreas Spiess, GreatScott!, ElectroBOOM, Phil's Lab, atomic14, That Project, Paul McWhorter, and many other professional hobbyists sharing their awesome projects and tutorials! Shout-out/kudos to all of them!
Warning
Automation and control engineering - do try this at home ❗
190+ challenges to start from: Control Engineering for Hobbyists at the Warsaw University of Technology.
Stay tuned!