本文整理汇总了C++中Serial::readable方法的典型用法代码示例。如果您正苦于以下问题:C++ Serial::readable方法的具体用法?C++ Serial::readable怎么用?C++ Serial::readable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Serial
的用法示例。
在下文中一共展示了Serial::readable方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rxInterruptLoop
/* This function is invoked whenever new data is received. Such new data
includes both the actual message sent from as well as all control messages,
such as "CSQ:"
Currently, this is the function in which you may read out the actual (SBDRB)
message and perform appropriate tasks.
*/
void rxInterruptLoop(const void *_serial, const void *_sbd) {
Serial *serial = (Serial *)_serial;
IridiumSBD2 *sbd = (IridiumSBD2 *)_sbd;
if (millis_time() - sbd->getStartTime() > 1000UL * sbd->getDuration()) {
// Last message received timeout, and need to reset state
// drop message
return;
}
bool terminated = false; /* This is used to check if the current set of recvd
bytes is the end of a full control message, which
will trigger subsequent control actions.
*/
while (serial->readable()) { // serial == nss (main.cpp)
char c = (char) serial->getc();
sbd->console(c);
char *prompt = sbd->getPrompt();
char *terminator = sbd->getTerminator();
if (prompt) {
int matchPromptPos = sbd->getMatchPromptPos();
switch(sbd->getPromptState()) {
case 0: // matching prompt
if (c == prompt[matchPromptPos]) {
matchPromptPos++;
sbd->setMatchPromptPos(matchPromptPos);
if (prompt[matchPromptPos] == '\0') {
sbd->setPromptState(1);
}
} else {
matchPromptPos = c == prompt[0] ? 1 : 0; /* try to match prompt,
if current char matches,
then move on to next char
to match
*/
sbd->setMatchPromptPos(matchPromptPos);
}
break;
case 1: // gathering reponse from end of prompt to first
int responseSize = sbd->getResponseSize();
if (responseSize > 0) {
if (c == '\r' || responseSize < 2) {
sbd->setPromptState(2);
} else {
(sbd->getRxBuffer())->insert(c);
// rxBuffer (only put in actual response,
// no prompt/terminator in buffer
responseSize--;
sbd->setResponseSize(responseSize);
}
}
break;
}
}
if (sbd->getCompletionNum() == processSBDRBResponsNum) {
(sbd->getRxBuffer())->insert(c);
messageBuffer.insert(c);
}
// If there is no prompt, then just keep trying to match the terminator
// until either all terminator characters are
// matched (return true), or no more serial to read (return false)
int matchTerminatorPos = sbd->getMatchTerminatorPos();
if (terminator) {
if (c == terminator[matchTerminatorPos]) {
matchTerminatorPos++;
sbd->setMatchTerminatorPos(matchTerminatorPos);
if (terminator[matchTerminatorPos] == '\0') {
terminated = true;
}
} else {
matchTerminatorPos = c == terminator[0] ? 1 : 0;
sbd->setMatchTerminatorPos(matchTerminatorPos);
}
}
} // while (serial.available())
if (sbd->checkCompletion(terminated) && sbd->getCompletionNum() == processSBDRBResponsNum) {
char c;
int nBytes = sbd->getResponseSize() - 4; /* -4 because we are currently
seeing the four bytes missing
on most cases, though not always
consistent.
*/
/* NOTE: THIS IS WHERE YOU MAY DECIDE WHAT ACTIONS/TASKS TO ENQUE FOR
RECEIVING DIFFERENT TYPES OF MESSAGES FROM SBDRB */
if (nBytes > 0) {
printf("I RECEIVED A MESSAGE :) \n");
}
for (int i = 0; i < nBytes; i++) {
//.........这里部分代码省略.........
示例2: get_command
unsigned char get_command(Serial cmdLink) {
while (! cmdLink.readable());
return cmdLink.getByte();
}