Based on mbusd project
-
Tested on ESP32 IDF v4.1
-
Clone this repository
git clone https://github.com/ololoshka2871/esp32-modbusTCP2RTU-gateway-lib.git
OR as git submodulegit submodule add https://github.com/ololoshka2871/esp32-modbusTCP2RTU-gateway-lib.git
to yourcomponents
folder -
Add dependency to library to your main module: in
main/CMakeLists.txt
add lineidf_component_register( ... +++ DEPENDS esp32-modbusTCP2RTU-gateway-lib )
-
Add code to start gateway (example)
#include "mbGateway.h" ... // After network up... mb_gateway_config gateway_config = MB_GATEWAY_CONFIG_DEFAULT(1, // UART1 57600, // speed 4, // TX pin 2, // RX pin 16 // RE/DE pin ); // also you can modify defaults in gateway_config mb_gateway_configure(&gateway_config); // prepare gateway to satrt mb_gateway_start(); // new thread will be created
-
Build, flash and run your application
Esp32 VFS drivers has several limitations. So it needs some fixes to get linux library mbusd to work:
- UARTs represents in ESP32 VFS via
/dev/uart/<UART_number>
- mbusd uses ioctl() to direct control RTS pin. It's unsupported by esp32-idf VFS, so
this function disabled at all, but insted of this I use
UART_MODE_RS485_HALF_DUPLEX
mode see documentation - It needs to initalise UART driver BEFORE access it via VFS, see
mb_gateway_configure()
- lwip socket [does not support buffer resize](see http://lwip.100.n7.nabble.com/Setting-TCP-socket-s-send-amp-receive-buffer-size-td10263.html), so, skip buffers modification
- esp32 VFS
select()
function for files, what represents UART devices work incorrectly: if UART file descriptor added toselect()
wait for transmitter ready set,select()
allways ends with timeout. To avoid it I create hookmy_select()
, what skips waiting for UART file descriptor ready to write waiting (assumes it allways ready). - Override mbusd's logging to accept ESP32-idf logging style see esp_log.c
VCC ---------------+
|
+-------x-------+
RXD <------| R |
| B|-----------<> B
TXD ------>| D ADM483/ |
ESP32 | ADM3072 | RS485 bus side
RTS --+--->| DE |
| | A|-----------<> A
+----| /RE |
+-------x-------+
|
GND
You mast select ESP32 UART pins RXD, TXD and RTS in struct mb_gateway_config