This repository contain working example Modbus RTU Slave implementation for rp2040 (Raspberry Pi Pico) with C++ SDK
This is a fork from https://github.com/gleboss-redfab/rp2040-modbus_example
From the fork base class modbusManager, modbus.hpp, I create a subclass ModbusPico.
This way I just need to modify ModbusPico class to add function.
On the current example the modbus layout has 8 outputs, 8 inputs, one wire DS18B20 line and analog ADC inputs.
-
add two BME280/BMP280 sensors on i2c gpio10 and gpio11
input register address
2200 SENSOR1 type, 0 = none/error, 0x58= BMP280, 0x60=BME280
2201 SENSOR2 type, 0 = none/error, 0x58= BMP280, 0x60=BME280
22.. future usage
2210..19 SENSOR1 RANGE
2220..29 SENSOR2 RANGE
2230... future usageFor BME280 offset 0..1 signed 32bits temper temperature * 100 in celsius
offset 2..3 unsigned 32bits Humidity * 1024 in %
offset 4..5 unsigned 32bits Pressure * 100 in HPa
offset 6..9 invalidFor BMP280 offfset 2..3 for humidity will be set to zero.
N.B. if no sensor the data will be zero
Check readBME280.py for info
- Modbus RTU Slave
- RS-485 support
- C++ style class
- Registers layout
- Separate setup read-only and read-write registers
- UART 0 and 1 support
- it works out of box and tested
- Tune VS Code for Raspberry Pico development (instruction)
- You can use 2nd Raspberry Pico as a bootloader and debuger (instruction)
- Modify launch.json and setting.json files in .vscode according to your environment
- Start the debugging session
- Use Modbus Master emulator for connection (ModBus Poll for example)
- Change UART parameters in main.cpp and registers layout in modbus.cpp for your purpose
- Change stored data inside Modbus Manager class
- Change Switch-case optins according to your data and registers in mb_read_holding_register() and mb_write_single_register()
- Follow the installation step from the PDF "Raspberry Pi Pico c/C++ SDK". https://www.raspberrypi.com/documentation/microcontrollers/c_sdk.html
- clone the github. git clone https://github.com/danjperron/rp2040-modbus_example.git
- Enter the rp2040-modbus_example folder. cd rp2040-modbus_example
- Create the build folder. mkdir build
- Enter the build folder. cd build
- Create the cmake environnement. cmake ..
- Compile. make
- To transfer use openocd. https://iosoft.blog/2019/01/28/raspberry-pi-openocd