Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizable modbus timeout #178

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ void setup()
// use Serial (port 0); initialize Modbus communication baud rate
Serial.begin(19200);

// communicate with Modbus slave ID 2 over Serial (port 0)
node.begin(2, Serial);
// communicate with Modbus slave ID 2 over Serial (port 0), with a 1-second timeout
node.begin(2, Serial, 1000);
}


Expand Down
6 changes: 4 additions & 2 deletions src/ModbusMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ Call once class has been instantiated, typically within setup().

@param slave Modbus slave ID (1..255)
@param &serial reference to serial port object (Serial, Serial1, ... Serial3)
@param u16MBResponseTimeout Modbus timeout in milliseconds (default 2000)
@ingroup setup
*/
void ModbusMaster::begin(uint8_t slave, Stream &serial)
void ModbusMaster::begin(uint8_t slave, Stream &serial, uint16_t u16MBResponseTimeout = ModbusMaster::ku16MBResponseTimeout)
{
// txBuffer = (uint16_t*) calloc(ku8MaxBufferSize, sizeof(uint16_t));
_u8MBSlave = slave;
_serial = &serial;
_u16MBResponseTimeout = u16MBResponseTimeout;
_u8TransmitBufferIndex = 0;
u16TransmitBufferLength = 0;

Expand Down Expand Up @@ -797,7 +799,7 @@ uint8_t ModbusMaster::ModbusMasterTransaction(uint8_t u8MBFunction)
break;
}
}
if ((millis() - u32StartTime) > ku16MBResponseTimeout)
if ((millis() - u32StartTime) > _u16MBResponseTimeout)
{
u8MBStatus = ku8MBResponseTimedOut;
}
Expand Down
7 changes: 4 additions & 3 deletions src/ModbusMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ModbusMaster
public:
ModbusMaster();

void begin(uint8_t, Stream &serial);
void begin(uint8_t, Stream &serial, uint16_t u16MBResponseTimeout);
void idle(void (*)());
void preTransmission(void (*)());
void postTransmission(void (*)());
Expand Down Expand Up @@ -219,6 +219,7 @@ class ModbusMaster

private:
Stream* _serial; ///< reference to serial port object
uint16_t _u16MBResponseTimeout; ///< Modbus timeout [milliseconds]
uint8_t _u8MBSlave; ///< Modbus slave (1..255) initialized in begin()
static const uint8_t ku8MaxBufferSize = 64; ///< size of response/transmit buffers
uint16_t _u16ReadAddress; ///< slave register from which to read
Expand Down Expand Up @@ -248,8 +249,8 @@ class ModbusMaster
static const uint8_t ku8MBMaskWriteRegister = 0x16; ///< Modbus function 0x16 Mask Write Register
static const uint8_t ku8MBReadWriteMultipleRegisters = 0x17; ///< Modbus function 0x17 Read Write Multiple Registers

// Modbus timeout [milliseconds]
static const uint16_t ku16MBResponseTimeout = 2000; ///< Modbus timeout [milliseconds]
// Default Modbus timeout [milliseconds]
static const uint16_t ku16MBResponseTimeout = 2000; ///< Default Modbus timeout [milliseconds]

// master function that conducts Modbus transactions
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction);
Expand Down