[XBee] Added multiple attempts when connecting
This commit is contained in:
parent
5b16b2b10f
commit
2fbb6afcf4
4 changed files with 35 additions and 14 deletions
|
@ -174,6 +174,7 @@ ERR_CMD_MODE_FAILED LITERAL1
|
|||
ERR_FRAME_MALFORMED LITERAL1
|
||||
ERR_FRAME_INCORRECT_CHECKSUM LITERAL1
|
||||
ERR_FRAME_UNEXPECTED_ID LITERAL1
|
||||
ERR_FRAME_NO_RESPONSE LITERAL1
|
||||
|
||||
ASCII LITERAL1
|
||||
ASCII_EXTENDED LITERAL1
|
||||
|
|
|
@ -111,6 +111,7 @@
|
|||
#define ERR_FRAME_MALFORMED -302
|
||||
#define ERR_FRAME_INCORRECT_CHECKSUM -303
|
||||
#define ERR_FRAME_UNEXPECTED_ID -304
|
||||
#define ERR_FRAME_NO_RESPONSE -305
|
||||
|
||||
// RTTY status codes
|
||||
#define ERR_INVALID_RTTY_SHIFT -401
|
||||
|
|
|
@ -27,17 +27,35 @@ int16_t XBee::begin(long speed) {
|
|||
// empty UART buffer (garbage data)
|
||||
_mod->ATemptyBuffer();
|
||||
|
||||
// send test frame (get baudrate setting)
|
||||
uint8_t frameID = _frameID++;
|
||||
sendApiFrame(XBEE_API_FRAME_AT_COMMAND_QUEUE, frameID, "BD");
|
||||
|
||||
// get response code
|
||||
int16_t state = readApiFrame(frameID, 4);
|
||||
if(state != ERR_NONE) {
|
||||
return(state);
|
||||
// try to find the XBee
|
||||
bool flagFound = false;
|
||||
uint8_t i = 0;
|
||||
while((i < 10) && !flagFound) {
|
||||
// send test frame (get baudrate setting)
|
||||
uint8_t frameID = _frameID++;
|
||||
sendApiFrame(XBEE_API_FRAME_AT_COMMAND_QUEUE, frameID, "BD");
|
||||
int16_t state = readApiFrame(frameID, 4, 2000);
|
||||
|
||||
if(state == ERR_NONE) {
|
||||
flagFound = true;
|
||||
} else {
|
||||
DEBUG_PRINT_STR("XBee not found! (");
|
||||
DEBUG_PRINT(i + 1);
|
||||
DEBUG_PRINT_STR(" of 10 tries) STATE == ");
|
||||
DEBUG_PRINTLN(state);
|
||||
DEBUG_PRINTLN_STR("Resetting ...");
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
return(state);
|
||||
if(!flagFound) {
|
||||
DEBUG_PRINTLN_STR("No XBee found!");
|
||||
return(ERR_CMD_MODE_FAILED);
|
||||
} else {
|
||||
DEBUG_PRINTLN_STR("Found XBee!");
|
||||
}
|
||||
|
||||
return(ERR_NONE);
|
||||
}
|
||||
|
||||
void XBee::reset() {
|
||||
|
@ -352,13 +370,13 @@ void XBee::sendApiFrame(uint8_t type, uint8_t id, uint8_t* data, uint16_t length
|
|||
delete[] frame;
|
||||
}
|
||||
|
||||
int16_t XBee::readApiFrame(uint8_t frameID, uint8_t codePos) {
|
||||
int16_t XBee::readApiFrame(uint8_t frameID, uint8_t codePos, uint16_t timeout) {
|
||||
// TODO: modemStatus frames may be sent at any time, interfering with frame parsing. Add check to make sure this does not happen.
|
||||
|
||||
// get number of bytes in response (must be enough to read the length field
|
||||
uint16_t numBytes = getNumBytes(5000, 3);
|
||||
uint16_t numBytes = getNumBytes(timeout/2, 3);
|
||||
if(numBytes == 0) {
|
||||
return(ERR_FRAME_MALFORMED);
|
||||
return(ERR_FRAME_NO_RESPONSE);
|
||||
}
|
||||
|
||||
// checksum byte is not included in length field
|
||||
|
@ -367,7 +385,8 @@ int16_t XBee::readApiFrame(uint8_t frameID, uint8_t codePos) {
|
|||
// wait until all response bytes are available (5s timeout)
|
||||
uint32_t start = millis();
|
||||
while(_mod->ModuleSerial->available() < (int16_t)numBytes) {
|
||||
if(millis() - start >= 5000) {
|
||||
if(millis() - start >= timeout/2) {
|
||||
Serial.println(_mod->ModuleSerial->available());
|
||||
return(ERR_FRAME_MALFORMED);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ class XBee {
|
|||
|
||||
void sendApiFrame(uint8_t type, uint8_t id, const char* data);
|
||||
void sendApiFrame(uint8_t type, uint8_t id, uint8_t* data, uint16_t length);
|
||||
int16_t readApiFrame(uint8_t frameID, uint8_t codePos);
|
||||
int16_t readApiFrame(uint8_t frameID, uint8_t codePos, uint16_t timeout = 5000);
|
||||
|
||||
uint16_t getNumBytes(uint32_t timeout = 10000, size_t minBytes = 10);
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue