当前位置: 首页>>代码示例>>C++>>正文


C++ Serial::readable方法代码示例

本文整理汇总了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++) {
//.........这里部分代码省略.........
开发者ID:bharad6,项目名称:arm_hab_controller,代码行数:101,代码来源:main.cpp

示例2: get_command

unsigned char get_command(Serial cmdLink) {
    while (! cmdLink.readable());  
    return cmdLink.getByte();
}
开发者ID:bgamari,项目名称:libmanyuc,代码行数:4,代码来源:auto.cpp


注:本文中的Serial::readable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。