Skip to content

Commit

Permalink
fix: Fixes #5
Browse files Browse the repository at this point in the history
This issue was due to a bad management of the dynamic memory.
The implementation of the Modbus class has been revised to correct this problem, but also that of writeMultipleCoils() which was not working correctly.
The Reading methods have been reviewed and optimized.
  • Loading branch information
epsilonrt committed May 27, 2023
1 parent 1780535 commit 8da5063
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 289 deletions.
5 changes: 5 additions & 0 deletions issues/modbus-arduino-Issue5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
71 changes: 71 additions & 0 deletions issues/modbus-arduino-Issue5/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
default_envs = mkrvidor4000

[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
lib_extra_dirs = ../../..
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3

[env:mkrvidor4000]
platform = atmelsam
board = mkrvidor4000
framework = arduino
lib_extra_dirs = ../../..
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3

[env:mkrvidor4000-debug]
platform = atmelsam
framework = arduino
board = mkrvidor4000
debug_tool = atmel-ice


; Debugger (gdb) support
; https://docs.arduino.cc/tutorials/mkr-wifi-1010/mkr-jlink-setup
; https://gojimmypi.blogspot.com/2018/12/swd-debugging-arduino-mkr-wifi-1010.html

build_type = debug
lib_extra_dirs = ../../..
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3

; activate Dual USB just as README says
build_flags =
-DDEBUG # Comment out to disable debugging.
;debug_build_flags = -O0 -g2 -ggdb2
debug_build_flags = -O0 -ggdb3 -g3

[env:esp32]
framework = arduino
platform = espressif32
; change for your board : https://registry.platformio.org/platforms/platformio/espressif32/boards
board = esp32doit-devkit-v1
;board_build.f_cpu = 240000000L
lib_extra_dirs = ../../..
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3
;upload_port = COM9

[env:esp32-debug]
; https://dzone.com/articles/eclipse-jtag-debugging-the-esp32-with-a-segger-j-l
; https://docs.platformio.org/en/latest/tutorials/espressif32/arduino_debugging_unit_testing.html
; https://community.platformio.org/t/esp32-and-segger-jlink-tip-for-interface-setup-configuration/25964
framework = arduino
platform = espressif32
; change for your board : https://registry.platformio.org/platforms/platformio/espressif32/boards
board = esp32doit-devkit-v1
;board_build.f_cpu = 240000000L
lib_extra_dirs = ../../..
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3
upload_port = COM1
debug_tool = jlink
41 changes: 41 additions & 0 deletions issues/modbus-arduino-Issue5/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <ModbusSerial.h>

// Used Pins
const byte LedPin = LED_BUILTIN;
const byte TxenPin = -1; // -1 disables the feature, change that if you are using an RS485 driver, this pin would be connected to the DE and /RE pins of the driver.

#define MySerial Serial1 // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
const unsigned long Baudrate = 38400;

const byte SlaveId = 10;

const byte FirstReg = 0;
const byte Lamp1Coil = FirstReg;
const byte NofRegs = 32;

ModbusSerial mb(MySerial, SlaveId, TxenPin);

void setup() {

// MySerial.begin(Baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard
// prefer the line below instead if possible
MySerial.begin (Baudrate, MB_PARITY_EVEN);

mb.config(Baudrate);

for (byte i = 0; i < NofRegs; i++) {

mb.addHreg(FirstReg + i, i);
mb.addIreg(FirstReg + i, i);
mb.addCoil(FirstReg + i, i % 2);
mb.addIsts(FirstReg + i, (i + 1) % 2);
}

pinMode(LedPin, OUTPUT);
}

void loop() {

mb.task();
digitalWrite(LedPin, !mb.Coil(Lamp1Coil));
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Modbus-Arduino",
"version": "1.1.2",
"version": "1.2.0",
"keywords": "modbus, server, slave, rtu, tcp",
"description": "A library that allows your Arduino to communicate via Modbus protocol, acting as a slave. Application layer library (OSI 7), used by all implementations over serial line and TCP/IP.",
"homepage": "https://epsilonrt.github.io/modbus-arduino",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Modbus-Arduino
version=1.1.2
version=1.2.0
author=Pascal Jean aka epsilonrt,André Sarmento Barbosa
maintainer=epsilonrt
sentence=A library that allows your Arduino to communicate via Modbus protocol, acting as a slave.
Expand Down
Loading

0 comments on commit 8da5063

Please sign in to comment.