Skip to content

Commit 314baac

Browse files
rtu layer: fix parsing of serial stop bits values
Up until now, the stop bits value was hard coded to be 2 if the parity mode was set to none or 1 if the parity mode was set to even or odd. So a setting of "115200E1" would result in a stop bit value of 1. But "115200E2" would result also in a stop bit value of 1. And "115200N1" would result in a stop bit value of 2. This is not a restriction of libmodbus. The library supports all these modes just fine. In fact they are using 115200N1 in the documentation for modbus_new_rtu(). Rather this is a bug in the string parsing that libmodbuspp introduces in its constructor. libmodbus has no string parsing in modbus_new_rtu() and takes in the arguments as parameters directly. Fixes: b0f3dc4
1 parent ddebf7c commit 314baac

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/rtulayer.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* along with the libmodbuspp Library; if not, see <http://www.gnu.org/licenses/>.
1616
*/
1717
#include <modbuspp/message.h>
18+
#include <iostream>
1819
#include "rtulayer_p.h"
1920
#include "config.h"
2021

@@ -239,6 +240,7 @@ namespace Modbus {
239240
b = std::stoi (settings);
240241
}
241242
catch (...) {
243+
std::cerr << "ERROR in parsing RtuLayer settings string \"" << settings << "\": Invalid baud. Setting to 19200. " << std::endl;
242244
b = 19200;
243245
}
244246
return b;
@@ -256,17 +258,29 @@ namespace Modbus {
256258
return c;
257259
}
258260
}
261+
262+
std::cerr << "ERROR in parsing RtuLayer settings string \"" << settings << "\": Invalid parity. Setting to N. " << std::endl;
263+
259264
return p;
260265
}
261266

262267
// ---------------------------------------------------------------------------
263268
// static
264269
int RtuLayer::stop (const std::string & settings) {
270+
size_t s = settings.length();
265271

266-
if (parity (settings) == 'N') {
267-
268-
return 2;
272+
if (s >= 3) {
273+
char c = settings[s - 1];
274+
switch (c) {
275+
case '1':
276+
return 1;
277+
case '2':
278+
return 2;
279+
}
269280
}
281+
282+
std::cerr << "ERROR in parsing RtuLayer settings string \"" << settings << "\": Invalid stop bit. Setting to 1. " << std::endl;
283+
270284
return 1;
271285
}
272286

0 commit comments

Comments
 (0)