Hi,
I've been battling SoftwareSerial for a few weeks now. And I think I tried everything I can throw at it. But intermittently (about 35-50% of the time) I am not able to receive any messages I'm sending over.
I'm using MAX485 for RS-485 communication. I managed to reduce my code to reveal the problem. This code works just fine:
if (dev.available() && dev.read() == 2) {
// Stuff to do some calculation while not meaning anything
if (timeout < millis()) {}
int timeoutTime = millis() + timeout;
byte Checksummm = byte(0x00);
char c = dev.read();
digitalWrite(8, HIGH);
dev.print(c);
delay(50);
digitalWrite(8, LOW);
}
This expects transmission to begin with a certain character, and then read 1 more character after that, sending it back.
Now if I modify to wait for timeout, or end of transmission (marked by a new line), then it works only half the time.
if (dev.available() && dev.read() == 2) {
int timeoutTime = millis() + timeout;
lastMessage = "";
while (timeoutTime > millis()) {
char c = (char)dev.read();
if (c == byte(0x0A)) {
break;
}
lastMessage += c;
}
digitalWrite(8, HIGH);
dev.print(lastMessage);
delay(50);
digitalWrite(8, LOW);
}
pin 8 controls MAX485 pins 2 & 3.
When it stops working, it's quite persistent for a number of seconds/minutes. It's never just 1 message that's dropped. It's always over a certain period of time. And every time I reset the board, it starts working just fine again (for an unpredictable period of time).
Any ideas as to what might be the culprit?
PS: I can't seem to get hardware serial to work, and tried AltSoftSerial with same results (can't get it to work). I can receive, but when transmitting, get nothing on the other end. But that's an entirely different topic, which I can post if I can't get SoftwareSerial to work.
Thanks for any help and/or pointers to try out!