int SWread(byte rxPin){ byte val = 0; // unsigned long beginTime = millis(); while (digitalRead(rxPin)/* && (millis() - beginTime < 100)*/); //wait for start bit if (digitalRead(rxPin) == LOW) { delayMicroseconds(halfBit4800Delay); for (int offset = 0; offset < 8; offset++) { delayMicroseconds(bit4800Delay); val |= digitalRead(rxPin) << offset; } //wait for stop bit + extra delayMicroseconds(bit4800Delay); delayMicroseconds(bit4800Delay); return val; }}
The function works under normal conditions without the commented code (the whole beginTime bit near the beginning). However, if the GPS device is disconnected, simulating a failure of the GPS module, the code hangs at the point of the commented code, since it's waiting for the proper sequence. Thus, I added the time-out code that's now commented, so that if no information is recieved in 100 milliseconds, it just moves on. My problem is that now (with the commented code uncommented) the function doesn't receive data properly, even when I can otherwise verify that the GPS device is working.Any thoughts on why this may be, and what a better way to create a timeout like this might be? My intuition is that the extra code is creating timing issues, but I'm not sure how I would work around that..
You need to be a member of diydrones to add comments!
Replies
The problem is that I need to use pins besides the native serial pins to get GPS data (currently I'm using 6 and 7). Normally, when everything's fine and dandy, the SoftwareSerial library works fine; the code I posted above is almost exactly the read() function from that library. However, the problem is that when the source of serial information, the GPS module, is disconnected, that function hangs infinitely, because it's waiting for input. I want to deal with this because I still want my autopilot to be conscious, even if the GPS module fails.
I tried to solve this problem by adding the commented code on these lines:
// unsigned long beginTime = millis();
while (digitalRead(rxPin)/* && (millis() - beginTime < 100)*/);
...so that if the code is waiting more than 100 milliseconds, it just moves on. But, this seems to create timing issues or something, because using this code, I get garbled nonsense input from the GPS module.
Serial pins 0 and 1 can deal with this because they apparently have dedicated serial hardware. However, I'm not sure how to deal with it when I'm doing the serial communication myself. Is there maybe some type of buffer chip that would send a special signal if it was empty, instead of nothing?
Take a look at the code in
hardware/libraries/SoftwareSerial/SoftwareSerial.cpp
I think this is trying to do what you are trying to do.
Regards
Phil Wilshire