-
Notifications
You must be signed in to change notification settings - Fork 161
Description
In cases where the SoftwareSerial.h library causes inaccurate measurement of time intervals or the program introduces interference in the form of interrupt handling for other processes while handling the hardware Serial port, it happens that after starting to receive a message, the "while (state == IN_PACKET)" loop executes before the entire message is received. In such cases, a PACKET_LENGTH_ERROR error is generated, followed by a CRC_ERROR. As a result, one correct message is interpreted as two short, erroneous messages. To minimize the risk of such errors, I suggest adding the condition if (!serial.available()) delay(2) at the end of the "while (state == IN_PACKET)" loop. The number of milliseconds of delay depends on the transmission speed. I used 2 ms for a speed of 9600 bps. Additionally, I noticed that when calculating the value of the "interval" variable, the number of bits in the word, parity, and start/stop are not taken into account. At low transmission speeds, this may affect the number of errors.
Consider implementing this modification to minimize the number of interface errors. The code fragment after the proposed change would look like this:
case IN_PACKET:
// tight loop until finished reading or error
{
// Is there a byte?
while (serial.available()) {
// Yes, collect it
buffer[bufferPtr++] = serial.read();
// Mark time of last byte
lastMicros = micros();
// Buffer full?
if (bufferPtr >= BUFBLOCKSIZE) {
// Yes. Something fishy here - bail out!
rv.push_back(PACKET_LENGTH_ERROR);
state = FINISHED;
break;
}
}
// No more byte read
if (state == IN_PACKET) {
// Are we past the interval gap?
if (micros() - lastMicros >= interval) {
// Yes, terminate reading
LOG_V("%c/%ldus without data after %u\n", (const char)caller, micros() - lastMicros, bufferPtr);
state = DATA_READ;
break;
}
}
//************************************************************************************************** */
if (!serial.available()) delay(2); // Avoid false Errors it is my proposal
//************************************************************************************************** */
}
Best Regards