I want to connect 2 MCU (ESP32 with STM) via Uart using Modbus. I saw implementations with a RS485-modul, but that is kinda overkill for my Project. I simply want to connect TX/RX Pins and communicate via Modbus.
The implementations you saw were for Modbus over RS-485. Using RS-485 allows you to connect more than 2 devices together - aka a multi-drop bus.
If you simply want ONLY 2 devices to communicate then you can normally use the TX/RX pins providing that they are not being used for other purposes such as a serial-USB interface. This is ok for short distances, but for longer distances you really should consider some sort of line driver.
Well i have to implement Modbus. I do an internship for a company and they told me to implement Modbus as a communication between 2 MCU. I researched about my topic. As far as i saw, there is no library for modbus communicatino without rs-xxx modules.
I think i have to deal with every single packet which is about to be send via the serial line.
Or do you have any recommendations? It sounds like you are more experienced with modbus on Arduino
As you are only using 2 devices, you can skip over all the RS-485 module stuff.
You can connect the UARTS so MCU #1 Tx -> MCU #2 Rx and MCU #1 Rx -> MCU #2 Tx.
The Modbus libraries will usually reference signals called RE (received enable) & DE (transmit enable). These 2 signals are used to enable/disable the Tx & Rx interface inside a MAX485 (for example). As you are using direct comms, you can either remove (or comment out) the code that drives these pins, or declare a couple of unused pins as being connected to RE & DE instead.
The rest of the Modbus library code should function as is.
I'm not sure what you mean by this? Are you listening in to the Modbus messages? I think there are 2 Modbus protocols over serial. There's an ASCII version (which uses printable characters) and a binary version, which I think is called Modbus-RTU.
Note that in order to have a debug serial console output as well as a Modbus, you need 2 serial interfaces. Either 2 hardware UARTS or 1 hardware and 1 software UART. You shouldn't use the same serial port for both Modbus and debug.
for this example code provided by the Library creater:
RS485_HalfDuplex.pde - example using ModbusMaster library to communicate
with EPSolar LS2024B controller using a half-duplex RS485 transceiver.
This example is tested against an EPSolar LS2024B solar charge controller.
See here for protocol specs:
Author:: Marius Kintel <marius at kintel dot net>
Copyright:: 2009-2016 Doc Walker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
We're using a MAX485-compatible RS485 Transceiver.
Rx/Tx is hooked up to the hardware serial port at 'Serial'.
The Data Enable and Receiver Enable pins are hooked up as follows:
#define MAX485_DE 3
#define MAX485_RE_NEG 2
// instantiate ModbusMaster object
// Init in receive mode
// Modbus communication runs at 115200 baud
// Modbus slave ID 1
// Callbacks allow us to configure the RS485 transceiver correctly
bool state = true;
// Toggle the coil at address 0x0002 (Manual Load Control)
result = node.writeSingleCoil(0x0002, state);
state = !state;
// Read 16 registers starting at 0x3100)
result = node.readInputRegisters(0x3100, 16);
if (result == node.ku8MBSuccess)
node.getResponseBuffer(0x0E) << 16)/100.0f);
I send the Message to a USB-Serial-interface so i can see the incoming message via PUTTY.