Skip to content

Commit 760b1e3

Browse files
committed
Feature: class ModbusRTUDelay allows for calculation of minimum inter-frame breaks.
More information can be found in https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf, 2.5.1.1 MODBUS Message RTU Framing.
1 parent 5afa436 commit 760b1e3

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

examples/RTU/ModbusRTUClientParameters/ModbusRTUClientParameters.ino

+1-11
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,13 @@
1616

1717
constexpr auto baudrate { 19200 };
1818

19-
// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
20-
//
21-
// MODBUS over serial line specification and implementation guide V1.02
22-
// Paragraph 2.5.1.1 MODBUS Message RTU Framing
23-
// https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
24-
constexpr auto bitduration { 1.f / baudrate };
25-
constexpr auto wordlen { 9.6f }; // try also with 10.0f
26-
constexpr auto preDelayBR { bitduration * wordlen * 3.5f * 1e6 };
27-
constexpr auto postDelayBR { bitduration * wordlen * 3.5f * 1e6 };
28-
2919
void setup() {
3020
Serial.begin(9600);
3121
while (!Serial);
3222

3323
Serial.println("Modbus RTU Client Toggle w/ Parameters");
3424

35-
RS485.setDelays(preDelayBR, postDelayBR);
25+
RS485.setDelays(ModbusRTUDelay::preDelay(baudrate), ModbusRTUDelay::postDelay(baudrate));
3626

3727
// start the Modbus RTU client in 8E1 mode
3828
if (!ModbusRTUClient.begin(baudrate, SERIAL_8E1)) {

keywords.txt

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#######################################
88

99
ArduinoModbus KEYWORD1
10+
ModbusRTUDelay KEYWORD1
1011
ModbusRTUClient KEYWORD1
1112
ModbusRTUServer KEYWORD1
1213
ModbusRTUClient KEYWORD1
@@ -21,6 +22,9 @@ poll KEYWORD2
2122
end KEYWORD2
2223
setTimeout KEYWORD2
2324

25+
preDelay KEYWORD2
26+
postDelay KEYWORD2
27+
2428
beginTransmission KEYWORD2
2529
write KEYWORD2
2630
endTransmission KEYWORD2

src/ArduinoModbus.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef _ARDUINO_MODBUS_H_INCLUDED
2121
#define _ARDUINO_MODBUS_H_INCLUDED
2222

23+
#include "ModbusRTUDelay.h"
2324
#include "ModbusRTUClient.h"
2425
#include "ModbusRTUServer.h"
2526

src/ModbusRTUDelay.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
This file is part of the ArduinoModbus library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _MODBUS_RTU_DELAY_H_INCLUDED
21+
#define _MODBUS_RTU_DELAY_H_INCLUDED
22+
23+
class ModbusRTUDelay
24+
{
25+
public:
26+
/* Do not allow construction or copying. */
27+
ModbusRTUDelay() = delete;
28+
ModbusRTUDelay(ModbusRTUDelay const &) = delete;
29+
30+
/* Calculate __minimum__ preDelay and postDelay in
31+
* microseconds as per Modbus RTU Specification.
32+
*
33+
* MODBUS over serial line specification and implementation guide V1.02
34+
* Paragraph 2.5.1.1 MODBUS Message RTU Framing
35+
* https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
36+
*/
37+
static unsigned long const preDelay(unsigned long const baudrate)
38+
{
39+
double const bit_duration = 1. / baudrate;
40+
double const word_len = 9.6f; // try also with 10.0f
41+
double const pre_delay_us = bit_duration * word_len * 3.5f * 1e6;
42+
return static_cast<unsigned long>(pre_delay_us);
43+
}
44+
static unsigned long const postDelay(unsigned long const baudrate)
45+
{
46+
return preDelay(baudrate);
47+
}
48+
};
49+
50+
#endif /* _MODBUS_RTU_DELAY_H_INCLUDED */

0 commit comments

Comments
 (0)