Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
setBaudrate function added, fixed PR remarks
  • Loading branch information
brainelectronics committed May 13, 2020
commit 309b5f1028f642e6c3f8ff3d80c57648c75be453
16 changes: 10 additions & 6 deletions src/ModbusRTU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,27 @@ uint16_t ModbusRTU::crc16(uint8_t address, uint8_t* frame, uint8_t pduLen) {
return (CRCHi << 8) | CRCLo;
}

void ModbusRTU::setBaudrate(uint32 baud = -1) {
_baudrate = baud;
}

bool ModbusRTU::begin(Stream* port) {
_port = port;
_t = 2;
return true;
}

bool ModbusRTU::begin(HardwareSerial* port, uint32_t thisBaudrate, int16_t txPin) {
bool ModbusRTU::begin(HardwareSerial* port, int16_t txPin) {
uint32_t baud = 0;
if (thisBaudrate > 0) {
baud = thisBaudrate;
if (_baudrate > 0) {
baud = _baudrate;
}
else {
#if defined(ESP32) || defined(ESP8266)
// baudRate() only available with ESP32+ESP8266
baud = port->baudRate();
#else
#warning "Using default baudrate if not specified"
#warning "Using default 9600 baud as not specified with setBaudrate()"
baud = 9600;
#endif
}
Expand Down Expand Up @@ -104,7 +108,7 @@ bool ModbusRTU::rawSend(uint8_t slaveId, uint8_t* frame, uint8_t len) {
}
_port->write(slaveId); //Send slaveId
_port->write(frame, len); // Send PDU
_port->write(newCrc >> 8); //Send CRC
_port->write(newCrc >> 8); //Send CRC
_port->write(newCrc & 0xFF);//Send CRC
_port->flush();
if (_txPin >= 0)
Expand Down Expand Up @@ -137,7 +141,7 @@ void ModbusRTU::task() {
}
if (_len == 0) { // No data
if (isMaster) cleanup();
return;
return;
}
if (millis() - t < _t) return; // Wait data whitespace

Expand Down
2 changes: 2 additions & 0 deletions src/ModbusRTU.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ModbusRTU : public Modbus {
protected:
Stream* _port;
int16_t _txPin = -1;
uint32_t _baudrate = -1;
unsigned int _t; // inter-frame delay in mS
uint32_t t = 0;
bool isMaster = false;
Expand All @@ -43,6 +44,7 @@ class ModbusRTU : public Modbus {
bool cleanup(); // Free clients if not connected and remove timedout transactions and transaction with forced events
uint16_t crc16(uint8_t address, uint8_t* frame, uint8_t pdulen);
public:
void setBaudrate(uint32 baud = -1);
#if defined(ESP8266)
bool begin(SoftwareSerial* port, int16_t txPin=-1);
#endif
Expand Down