Skip to content

Commit f3a9bdc

Browse files
committed
Merge pull request #2 from romainreignier/improved_datarate
Thanks, I didn't notice the int thing 👍
2 parents 3589a31 + bc0f5c0 commit f3a9bdc

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/ModbusSlave.cpp

+16-27
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,23 @@ Modbus::Modbus(uint8_t _unitID, int _ctrlPin) {
3939
*
4040
* @param boud the serial port boud rate.
4141
*/
42-
void Modbus::begin(int boud) {
42+
void Modbus::begin(unsigned long boud) {
4343
// set control pin
4444
if (ctrlPin) {
4545
pinMode(ctrlPin, OUTPUT);
4646
}
4747

4848
// open port and set the timeout for 3.5 chars.
4949
Serial.begin(boud);
50-
Serial.flush();
50+
Serial.setTimeout(0);
5151

52-
// set the T35 interframe timout to 5 milliseconds.
53-
timeout = 5;
52+
// set the T35 interframe timeout
53+
if (boud > 19200) {
54+
timeout = 1750;
55+
}
56+
else {
57+
timeout = 35000000 / boud; // 1T * 3.5 = T3.5
58+
}
5459

5560
// init last received values
5661
last_receive_len = 0;
@@ -86,20 +91,6 @@ uint16_t Modbus::calcCRC(uint8_t *buf, int length) {
8691
return crc;
8792
}
8893

89-
/**
90-
* Read all bytes available in serial port.
91-
*/
92-
int serialRead(uint8_t *buffer, uint8_t len) {
93-
uint8_t size = 0;
94-
95-
while (Serial.available() && size < len) {
96-
buffer[size] = Serial.read();
97-
size++;
98-
}
99-
100-
return size;
101-
}
102-
10394
/**
10495
* wait for end of frame, parse request and answer it.
10596
*/
@@ -123,18 +114,18 @@ int Modbus::poll() {
123114
// if we have new data, update last received time and length.
124115
if (available_len != last_receive_len) {
125116
last_receive_len = available_len;
126-
last_receive_time = millis();
117+
last_receive_time = micros();
127118

128119
return 0;
129120
}
130121

131-
// if no new data, wait for T35 milliseconds.
132-
if (millis() < (last_receive_time + timeout)) {
122+
// if no new data, wait for T35 microseconds.
123+
if (micros() < (last_receive_time + timeout)) {
133124
return 0;
134125
}
135126

136127
// we waited for the inter-frame timeout, read the frame.
137-
lengthIn = serialRead(bufIn, MAX_BUFFER);
128+
lengthIn = Serial.readBytes(bufIn, MAX_BUFFER);
138129
last_receive_len = 0;
139130
last_receive_time = 0;
140131
} else {
@@ -267,18 +258,16 @@ int Modbus::poll() {
267258
// send buffer
268259
Serial.write(bufOut, lengthOut);
269260

270-
// wait until serial port register (UCSRnA)
271-
// transfer complete bit (TXCn) is on
272-
// and then set rs485 control pin to read
261+
// wait for the transmission of outgoing data
262+
// to complete and then set rs485 control pin to read
273263
// [ on SoftwareSerial use delay ? ]
274-
while (!(UCSR0A & (1 << TXC0)));
264+
Serial.flush();
275265
digitalWrite(ctrlPin, LOW);
276266
} else {
277267
// just send the buffer.
278268
Serial.write(bufOut, lengthOut);
279269
}
280270

281-
Serial.flush();
282271
return lengthOut;
283272
}
284273

src/ModbusSlave.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef void(*CallBackFunc)(uint8_t, uint16_t, uint16_t);
5050
class Modbus {
5151
public:
5252
Modbus(uint8_t unitID, int ctrlPin);
53-
void begin(int boud);
53+
void begin(unsigned long boud);
5454
int poll();
5555
uint16_t readRegisterFromBuffer(int offset);
5656
void writeCoilToBuffer(int offset, uint16_t state);

0 commit comments

Comments
 (0)