Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/modbuspp/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#pragma once

#include <stdexcept>
#include <cstdio> // printf
#include <cstring> // memcpy ...
#include <array>
Expand Down
11 changes: 5 additions & 6 deletions include/modbuspp/rtulayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,22 @@ namespace Modbus {

/**
* @brief Extracts the baudrate from a settings string.
* @return the baudrate found. if no value is found, returns the default
* value, ie 19200.
* @return the baudrate found. If no valid value is found, an exception is thrown.
*/
static int baud (const std::string & settings);

/**
* @brief Extracts the parity from a settings string.
* @return the parity found. if no value is found, returns the default
* value, ie E for Even parity.
* @return the parity found. If no valid value is found, an exception is thrown.
*/
static char parity (const std::string & settings);

/**
* @brief Return the stop bits from a settings string.
*
* @return the number returned is determined based on the parity found.
* If the parity is None, this function returns 2, otherwise returns 1.
* @return the number of stop bits.
* It is parsed from the last character of the settings string.
* If the last character is neither '1' or '2' an exception is thrown.
*/
static int stop (const std::string & settings);

Expand Down
32 changes: 20 additions & 12 deletions src/rtulayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,40 +234,48 @@ namespace Modbus {
// ---------------------------------------------------------------------------
// static
int RtuLayer::baud (const std::string & settings) {
int b;
try {
b = std::stoi (settings);
return std::stoi (settings);
}
catch (...) {
b = 19200;
throw std::invalid_argument ("RtuLayer settings\"" + settings + "\" has an invalid baud rate setting.");
}
return b;
}

// ---------------------------------------------------------------------------
// static
char RtuLayer::parity (const std::string & settings) {
char p = 'N';
size_t s = settings.length();

if (s >= 2) {
char c = settings[s - 2];
if ( (c == 'E') || (c == 'O')) {
return c;
switch (c) {
case 'N':
case 'E':
case 'O':
return c;
}
}
return p;

throw std::invalid_argument ("RtuLayer settings\"" + settings + "\" has an invalid parity setting.");
}

// ---------------------------------------------------------------------------
// static
int RtuLayer::stop (const std::string & settings) {
size_t s = settings.length();

if (parity (settings) == 'N') {

return 2;
if (s >= 3) {
char c = settings[s - 1];
switch (c) {
case '1':
return 1;
case '2':
return 2;
}
}
return 1;

throw std::invalid_argument ("RtuLayer settings\"" + settings + "\" has an invalid stop bit setting.");
}

// ---------------------------------------------------------------------------
Expand Down