Skip to content

Commit 8da5063

Browse files
committed
fix: Fixes #5
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.
1 parent 1780535 commit 8da5063

File tree

7 files changed

+284
-289
lines changed

7 files changed

+284
-289
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[platformio]
12+
default_envs = mkrvidor4000
13+
14+
[env:teensy41]
15+
platform = teensy
16+
board = teensy41
17+
framework = arduino
18+
lib_extra_dirs = ../../..
19+
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3
20+
21+
[env:mkrvidor4000]
22+
platform = atmelsam
23+
board = mkrvidor4000
24+
framework = arduino
25+
lib_extra_dirs = ../../..
26+
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3
27+
28+
[env:mkrvidor4000-debug]
29+
platform = atmelsam
30+
framework = arduino
31+
board = mkrvidor4000
32+
debug_tool = atmel-ice
33+
34+
35+
; Debugger (gdb) support
36+
; https://docs.arduino.cc/tutorials/mkr-wifi-1010/mkr-jlink-setup
37+
; https://gojimmypi.blogspot.com/2018/12/swd-debugging-arduino-mkr-wifi-1010.html
38+
39+
build_type = debug
40+
lib_extra_dirs = ../../..
41+
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3
42+
43+
; activate Dual USB just as README says
44+
build_flags =
45+
-DDEBUG # Comment out to disable debugging.
46+
;debug_build_flags = -O0 -g2 -ggdb2
47+
debug_build_flags = -O0 -ggdb3 -g3
48+
49+
[env:esp32]
50+
framework = arduino
51+
platform = espressif32
52+
; change for your board : https://registry.platformio.org/platforms/platformio/espressif32/boards
53+
board = esp32doit-devkit-v1
54+
;board_build.f_cpu = 240000000L
55+
lib_extra_dirs = ../../..
56+
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3
57+
;upload_port = COM9
58+
59+
[env:esp32-debug]
60+
; https://dzone.com/articles/eclipse-jtag-debugging-the-esp32-with-a-segger-j-l
61+
; https://docs.platformio.org/en/latest/tutorials/espressif32/arduino_debugging_unit_testing.html
62+
; https://community.platformio.org/t/esp32-and-segger-jlink-tip-for-interface-setup-configuration/25964
63+
framework = arduino
64+
platform = espressif32
65+
; change for your board : https://registry.platformio.org/platforms/platformio/espressif32/boards
66+
board = esp32doit-devkit-v1
67+
;board_build.f_cpu = 240000000L
68+
lib_extra_dirs = ../../..
69+
;lib_deps = epsilonrt/Modbus-Serial@^2.0.3
70+
upload_port = COM1
71+
debug_tool = jlink
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <ModbusSerial.h>
2+
3+
// Used Pins
4+
const byte LedPin = LED_BUILTIN;
5+
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.
6+
7+
#define MySerial Serial1 // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
8+
const unsigned long Baudrate = 38400;
9+
10+
const byte SlaveId = 10;
11+
12+
const byte FirstReg = 0;
13+
const byte Lamp1Coil = FirstReg;
14+
const byte NofRegs = 32;
15+
16+
ModbusSerial mb(MySerial, SlaveId, TxenPin);
17+
18+
void setup() {
19+
20+
// MySerial.begin(Baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard
21+
// prefer the line below instead if possible
22+
MySerial.begin (Baudrate, MB_PARITY_EVEN);
23+
24+
mb.config(Baudrate);
25+
26+
for (byte i = 0; i < NofRegs; i++) {
27+
28+
mb.addHreg(FirstReg + i, i);
29+
mb.addIreg(FirstReg + i, i);
30+
mb.addCoil(FirstReg + i, i % 2);
31+
mb.addIsts(FirstReg + i, (i + 1) % 2);
32+
}
33+
34+
pinMode(LedPin, OUTPUT);
35+
}
36+
37+
void loop() {
38+
39+
mb.task();
40+
digitalWrite(LedPin, !mb.Coil(Lamp1Coil));
41+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Modbus-Arduino",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"keywords": "modbus, server, slave, rtu, tcp",
55
"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.",
66
"homepage": "https://epsilonrt.github.io/modbus-arduino",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Modbus-Arduino
2-
version=1.1.2
2+
version=1.2.0
33
author=Pascal Jean aka epsilonrt,André Sarmento Barbosa
44
maintainer=epsilonrt
55
sentence=A library that allows your Arduino to communicate via Modbus protocol, acting as a slave.

0 commit comments

Comments
 (0)