RN41/42 control lib. RN41/42 is bluetooth module with class 1 or 2.
Dependencies: StrLib myTimer RingBuffer
RN41.cpp@4:8b74db22e4c6, 2015-11-16 (annotated)
- Committer:
- AkinoriHashimoto
- Date:
- Mon Nov 16 04:11:50 2015 +0000
- Revision:
- 4:8b74db22e4c6
- Parent:
- 3:2a87c102ddd3
- Child:
- 5:63925964cbd5
debug internal compare.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkinoriHashimoto | 0:812e6b59aa54 | 1 | #include "RN41.h" |
AkinoriHashimoto | 0:812e6b59aa54 | 2 | //#include "SerialBase.h" |
AkinoriHashimoto | 0:812e6b59aa54 | 3 | |
AkinoriHashimoto | 0:812e6b59aa54 | 4 | const int RN41::NG= 0; |
AkinoriHashimoto | 0:812e6b59aa54 | 5 | const int RN41::OK= 1; |
AkinoriHashimoto | 0:812e6b59aa54 | 6 | |
AkinoriHashimoto | 0:812e6b59aa54 | 7 | // PRIVATE |
AkinoriHashimoto | 0:812e6b59aa54 | 8 | const int RN41::findCmp12= 100; |
AkinoriHashimoto | 0:812e6b59aa54 | 9 | const int RN41::findCmp1= 101; |
AkinoriHashimoto | 0:812e6b59aa54 | 10 | const int RN41::findCmp2= 102; |
AkinoriHashimoto | 0:812e6b59aa54 | 11 | |
AkinoriHashimoto | 0:812e6b59aa54 | 12 | // PUBLIC |
AkinoriHashimoto | 1:5ed110051e39 | 13 | const int RN41::ERR_Timeout= 201; |
AkinoriHashimoto | 0:812e6b59aa54 | 14 | const int RN41::ERR_AddrUnder12= 210; |
AkinoriHashimoto | 0:812e6b59aa54 | 15 | const int RN41::ERR_AddrOver12= 211; |
AkinoriHashimoto | 0:812e6b59aa54 | 16 | const int RN41::ERR_Connect= 220; |
AkinoriHashimoto | 0:812e6b59aa54 | 17 | const int RN41::FAIL_Connect= 221; |
AkinoriHashimoto | 0:812e6b59aa54 | 18 | const int RN41::ERR_EnterCmdMode= 230; |
AkinoriHashimoto | 0:812e6b59aa54 | 19 | const int RN41::ERR_Disconnect= 240; |
AkinoriHashimoto | 0:812e6b59aa54 | 20 | |
AkinoriHashimoto | 0:812e6b59aa54 | 21 | const int RN41::ERR_NameSize= 310; |
AkinoriHashimoto | 0:812e6b59aa54 | 22 | |
AkinoriHashimoto | 0:812e6b59aa54 | 23 | /* |
AkinoriHashimoto | 0:812e6b59aa54 | 24 | const int RN41::None= 10; |
AkinoriHashimoto | 0:812e6b59aa54 | 25 | const int RN41::Odd= 11; |
AkinoriHashimoto | 0:812e6b59aa54 | 26 | const int RN41::Even= 12; |
AkinoriHashimoto | 0:812e6b59aa54 | 27 | */ |
AkinoriHashimoto | 0:812e6b59aa54 | 28 | |
AkinoriHashimoto | 0:812e6b59aa54 | 29 | |
AkinoriHashimoto | 0:812e6b59aa54 | 30 | |
AkinoriHashimoto | 0:812e6b59aa54 | 31 | RN41::RN41(PinName TX, PinName RX, int baudrate, int bit, int parity, int stop, bool CRLN) |
AkinoriHashimoto | 0:812e6b59aa54 | 32 | : rn41(TX, RX) |
AkinoriHashimoto | 0:812e6b59aa54 | 33 | { |
AkinoriHashimoto | 1:5ed110051e39 | 34 | this->setDev_UART(baudrate, bit, parity, stop); |
AkinoriHashimoto | 2:3a28bc9332b6 | 35 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 36 | // attach rxIrq. because, rx buf of serial equals to 16 Bytes. |
AkinoriHashimoto | 2:3a28bc9332b6 | 37 | this->rn41.attach(this, &RN41::_readIrq, Serial::RxIrq); |
AkinoriHashimoto | 2:3a28bc9332b6 | 38 | |
AkinoriHashimoto | 1:5ed110051e39 | 39 | cr= '\r'; |
AkinoriHashimoto | 0:812e6b59aa54 | 40 | if(CRLN) |
AkinoriHashimoto | 1:5ed110051e39 | 41 | cr= "\r\n"; |
AkinoriHashimoto | 0:812e6b59aa54 | 42 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 43 | |
AkinoriHashimoto | 0:812e6b59aa54 | 44 | int RN41::connect(string addr) |
AkinoriHashimoto | 0:812e6b59aa54 | 45 | { |
AkinoriHashimoto | 0:812e6b59aa54 | 46 | addr= toAlpanumeric(addr, true); |
AkinoriHashimoto | 0:812e6b59aa54 | 47 | int idx= addr.size(); |
AkinoriHashimoto | 0:812e6b59aa54 | 48 | // ex. 0006:66:72:6e05\r ->000666726e05. |
AkinoriHashimoto | 0:812e6b59aa54 | 49 | if(idx < 12) |
AkinoriHashimoto | 0:812e6b59aa54 | 50 | return ERR_AddrUnder12; |
AkinoriHashimoto | 0:812e6b59aa54 | 51 | if(idx > 12) |
AkinoriHashimoto | 0:812e6b59aa54 | 52 | return ERR_AddrOver12; |
AkinoriHashimoto | 0:812e6b59aa54 | 53 | |
AkinoriHashimoto | 0:812e6b59aa54 | 54 | if(!enterCMD()) |
AkinoriHashimoto | 0:812e6b59aa54 | 55 | return ERR_EnterCmdMode; // CMD入れず |
AkinoriHashimoto | 0:812e6b59aa54 | 56 | if(!disconnect()) |
AkinoriHashimoto | 0:812e6b59aa54 | 57 | return ERR_Disconnect; |
AkinoriHashimoto | 0:812e6b59aa54 | 58 | if(!enterCMD()) |
AkinoriHashimoto | 0:812e6b59aa54 | 59 | return ERR_EnterCmdMode; // CMD入れず |
AkinoriHashimoto | 0:812e6b59aa54 | 60 | |
AkinoriHashimoto | 0:812e6b59aa54 | 61 | sendCMD("C,"+ addr); |
AkinoriHashimoto | 2:3a28bc9332b6 | 62 | bool2 find; |
AkinoriHashimoto | 2:3a28bc9332b6 | 63 | find= chkReply("TRYING\r", 1000, "ERR\r"); // within waiting for Reply |
AkinoriHashimoto | 0:812e6b59aa54 | 64 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 65 | // TRYINGが返って来ない場合 |
AkinoriHashimoto | 2:3a28bc9332b6 | 66 | if(!find.b1) |
AkinoriHashimoto | 0:812e6b59aa54 | 67 | return ERR_Connect; |
AkinoriHashimoto | 2:3a28bc9332b6 | 68 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 69 | // Trying to connect to remote Bluetooth dev. |
AkinoriHashimoto | 2:3a28bc9332b6 | 70 | find= chkReply("CONNECT,", 10000, "CONNECT failed\r"); // wait 10s(max) |
AkinoriHashimoto | 2:3a28bc9332b6 | 71 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 72 | if(find.b1) |
AkinoriHashimoto | 2:3a28bc9332b6 | 73 | return OK; |
AkinoriHashimoto | 2:3a28bc9332b6 | 74 | if(find.b2) |
AkinoriHashimoto | 2:3a28bc9332b6 | 75 | return FAIL_Connect; |
AkinoriHashimoto | 2:3a28bc9332b6 | 76 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 77 | return ERR_Timeout; |
AkinoriHashimoto | 0:812e6b59aa54 | 78 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 79 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 80 | string RN41::read() |
AkinoriHashimoto | 2:3a28bc9332b6 | 81 | { |
AkinoriHashimoto | 2:3a28bc9332b6 | 82 | _read(); |
AkinoriHashimoto | 2:3a28bc9332b6 | 83 | if(rxStrStored.empty() && rxStr.empty()) |
AkinoriHashimoto | 2:3a28bc9332b6 | 84 | return ""; |
AkinoriHashimoto | 2:3a28bc9332b6 | 85 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 86 | string tmp= rxStrStored+ rxStr; |
AkinoriHashimoto | 3:2a87c102ddd3 | 87 | // string tmp= rxStrStored+ ringBuf.get(); |
AkinoriHashimoto | 2:3a28bc9332b6 | 88 | rxStr.erase(); |
AkinoriHashimoto | 2:3a28bc9332b6 | 89 | rxStrStored.erase(); |
AkinoriHashimoto | 2:3a28bc9332b6 | 90 | return tmp; |
AkinoriHashimoto | 2:3a28bc9332b6 | 91 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 92 | |
AkinoriHashimoto | 0:812e6b59aa54 | 93 | string RN41::getLine() |
AkinoriHashimoto | 0:812e6b59aa54 | 94 | { |
AkinoriHashimoto | 2:3a28bc9332b6 | 95 | _read(); |
AkinoriHashimoto | 3:2a87c102ddd3 | 96 | |
AkinoriHashimoto | 0:812e6b59aa54 | 97 | // CRまでを返す |
AkinoriHashimoto | 2:3a28bc9332b6 | 98 | if(rxStrStored.empty() && rxStr.empty()) |
AkinoriHashimoto | 0:812e6b59aa54 | 99 | return ""; |
AkinoriHashimoto | 0:812e6b59aa54 | 100 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 101 | int idx= rxStr.rfind('\r'); |
AkinoriHashimoto | 3:2a87c102ddd3 | 102 | if(idx != string::npos) { |
AkinoriHashimoto | 3:2a87c102ddd3 | 103 | // CR存在 |
AkinoriHashimoto | 3:2a87c102ddd3 | 104 | if(rxStr[++idx] == '\n') // LF |
AkinoriHashimoto | 2:3a28bc9332b6 | 105 | idx++; |
AkinoriHashimoto | 2:3a28bc9332b6 | 106 | rxStrStored += rxStr.substr(0, idx); // size |
AkinoriHashimoto | 2:3a28bc9332b6 | 107 | rxStr= rxStr.substr(idx); // idx |
AkinoriHashimoto | 2:3a28bc9332b6 | 108 | } |
AkinoriHashimoto | 2:3a28bc9332b6 | 109 | // rxStrはCRなし、rxStrStoredに調整済み |
AkinoriHashimoto | 2:3a28bc9332b6 | 110 | string tmp= rxStrStored; |
AkinoriHashimoto | 2:3a28bc9332b6 | 111 | rxStrStored.erase(); |
AkinoriHashimoto | 2:3a28bc9332b6 | 112 | return tmp; |
AkinoriHashimoto | 0:812e6b59aa54 | 113 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 114 | |
AkinoriHashimoto | 1:5ed110051e39 | 115 | int RN41::setDev_UART(int baud, int bit, int parity, int stop) |
AkinoriHashimoto | 1:5ed110051e39 | 116 | { |
AkinoriHashimoto | 1:5ed110051e39 | 117 | rn41.baud(baud); |
AkinoriHashimoto | 1:5ed110051e39 | 118 | |
AkinoriHashimoto | 1:5ed110051e39 | 119 | // 力技 |
AkinoriHashimoto | 1:5ed110051e39 | 120 | if (parity == SerialBase::Odd) |
AkinoriHashimoto | 1:5ed110051e39 | 121 | rn41.format(bit, SerialBase::Odd, stop); |
AkinoriHashimoto | 1:5ed110051e39 | 122 | else if (parity == SerialBase::Even) |
AkinoriHashimoto | 1:5ed110051e39 | 123 | rn41.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit |
AkinoriHashimoto | 1:5ed110051e39 | 124 | else if (parity == SerialBase::None) |
AkinoriHashimoto | 1:5ed110051e39 | 125 | rn41.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit |
AkinoriHashimoto | 1:5ed110051e39 | 126 | else |
AkinoriHashimoto | 1:5ed110051e39 | 127 | return NG; |
AkinoriHashimoto | 1:5ed110051e39 | 128 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 129 | // 9,600bps -> 1,200B/s -> 833us/B. 0.1文字×13回待ちする(安全率30%)。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 130 | // usWait4readBuf= (int)( (float)800000 / (float)baud ); |
AkinoriHashimoto | 2:3a28bc9332b6 | 131 | // if(usWait4readBuf < 1)usWait4readBuf= 1; |
AkinoriHashimoto | 2:3a28bc9332b6 | 132 | |
AkinoriHashimoto | 1:5ed110051e39 | 133 | return OK; |
AkinoriHashimoto | 1:5ed110051e39 | 134 | } |
AkinoriHashimoto | 1:5ed110051e39 | 135 | |
AkinoriHashimoto | 0:812e6b59aa54 | 136 | |
AkinoriHashimoto | 0:812e6b59aa54 | 137 | int RN41::setDev_Name(string str, bool usingAddr) |
AkinoriHashimoto | 0:812e6b59aa54 | 138 | { |
AkinoriHashimoto | 0:812e6b59aa54 | 139 | int strSize= str.size(); |
AkinoriHashimoto | 0:812e6b59aa54 | 140 | if((strSize==0) || (20<=strSize) || (usingAddr && 15<=strSize)) |
AkinoriHashimoto | 0:812e6b59aa54 | 141 | return ERR_NameSize; |
AkinoriHashimoto | 0:812e6b59aa54 | 142 | |
AkinoriHashimoto | 0:812e6b59aa54 | 143 | // 特殊文字チェックは不要っぽい |
AkinoriHashimoto | 0:812e6b59aa54 | 144 | |
AkinoriHashimoto | 0:812e6b59aa54 | 145 | string cmd; |
AkinoriHashimoto | 0:812e6b59aa54 | 146 | if(usingAddr) |
AkinoriHashimoto | 0:812e6b59aa54 | 147 | cmd= "S-,"; |
AkinoriHashimoto | 0:812e6b59aa54 | 148 | else |
AkinoriHashimoto | 0:812e6b59aa54 | 149 | cmd= "SN,"; |
AkinoriHashimoto | 0:812e6b59aa54 | 150 | |
AkinoriHashimoto | 0:812e6b59aa54 | 151 | if(!enterCMD()) |
AkinoriHashimoto | 0:812e6b59aa54 | 152 | return ERR_EnterCmdMode; // CMD入れず |
AkinoriHashimoto | 0:812e6b59aa54 | 153 | |
AkinoriHashimoto | 0:812e6b59aa54 | 154 | sendCMD(cmd+ str); |
AkinoriHashimoto | 2:3a28bc9332b6 | 155 | if(chkReply("AOK\r", 1000).b1) |
AkinoriHashimoto | 0:812e6b59aa54 | 156 | return OK; |
AkinoriHashimoto | 0:812e6b59aa54 | 157 | |
AkinoriHashimoto | 0:812e6b59aa54 | 158 | return NG; |
AkinoriHashimoto | 0:812e6b59aa54 | 159 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 160 | |
AkinoriHashimoto | 0:812e6b59aa54 | 161 | |
AkinoriHashimoto | 0:812e6b59aa54 | 162 | void RN41::sendCMD(string str, bool addCR) |
AkinoriHashimoto | 0:812e6b59aa54 | 163 | { |
AkinoriHashimoto | 0:812e6b59aa54 | 164 | if(addCR) |
AkinoriHashimoto | 0:812e6b59aa54 | 165 | str += '\r'; |
AkinoriHashimoto | 0:812e6b59aa54 | 166 | rn41.printf(str.c_str()); |
AkinoriHashimoto | 0:812e6b59aa54 | 167 | return; |
AkinoriHashimoto | 0:812e6b59aa54 | 168 | } |
AkinoriHashimoto | 1:5ed110051e39 | 169 | void RN41::sendLine(string str, bool addCR) |
AkinoriHashimoto | 0:812e6b59aa54 | 170 | { |
AkinoriHashimoto | 0:812e6b59aa54 | 171 | if(addCR) |
AkinoriHashimoto | 0:812e6b59aa54 | 172 | str += "\r\n"; |
AkinoriHashimoto | 0:812e6b59aa54 | 173 | rn41.printf(str.c_str()); |
AkinoriHashimoto | 0:812e6b59aa54 | 174 | return; |
AkinoriHashimoto | 0:812e6b59aa54 | 175 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 176 | |
AkinoriHashimoto | 0:812e6b59aa54 | 177 | bool RN41::enterCMD() |
AkinoriHashimoto | 0:812e6b59aa54 | 178 | { |
AkinoriHashimoto | 0:812e6b59aa54 | 179 | sendCMD("$$$", false); |
AkinoriHashimoto | 2:3a28bc9332b6 | 180 | if(chkReply("CMD\r", 500).b1) // 500ms |
AkinoriHashimoto | 0:812e6b59aa54 | 181 | return true; |
AkinoriHashimoto | 1:5ed110051e39 | 182 | |
AkinoriHashimoto | 0:812e6b59aa54 | 183 | sendCMD("\r", false); // 既にCMDモード |
AkinoriHashimoto | 2:3a28bc9332b6 | 184 | if(chkReply("?\r", 500).b1) |
AkinoriHashimoto | 0:812e6b59aa54 | 185 | return true; |
AkinoriHashimoto | 3:2a87c102ddd3 | 186 | |
AkinoriHashimoto | 0:812e6b59aa54 | 187 | return false; |
AkinoriHashimoto | 0:812e6b59aa54 | 188 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 189 | |
AkinoriHashimoto | 0:812e6b59aa54 | 190 | bool RN41::disconnect() |
AkinoriHashimoto | 0:812e6b59aa54 | 191 | { |
AkinoriHashimoto | 1:5ed110051e39 | 192 | if(!enterCMD()) |
AkinoriHashimoto | 1:5ed110051e39 | 193 | return false; // CMD入れず |
AkinoriHashimoto | 0:812e6b59aa54 | 194 | sendCMD("K,"); // rtn KILL ? / ERR ? |
AkinoriHashimoto | 0:812e6b59aa54 | 195 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 196 | bool2 find; |
AkinoriHashimoto | 2:3a28bc9332b6 | 197 | find= chkReply("KILL\r", 500, "ERR\r"); |
AkinoriHashimoto | 2:3a28bc9332b6 | 198 | if(find.b2) { // 既に切断されている |
AkinoriHashimoto | 0:812e6b59aa54 | 199 | sendCMD("---"); // exit CMD mode. |
AkinoriHashimoto | 2:3a28bc9332b6 | 200 | return chkReply("END\r", 500).b1; |
AkinoriHashimoto | 2:3a28bc9332b6 | 201 | // return true;else// { leds.ON(4);return false; // どういう状況か不明 |
AkinoriHashimoto | 1:5ed110051e39 | 202 | } |
AkinoriHashimoto | 2:3a28bc9332b6 | 203 | // 切断中 |
AkinoriHashimoto | 2:3a28bc9332b6 | 204 | return chkReply("DISCONNECT", 5000, "", false).b1; // CR無いかも? |
AkinoriHashimoto | 0:812e6b59aa54 | 205 | } |
AkinoriHashimoto | 0:812e6b59aa54 | 206 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 207 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 208 | RN41::bool2 RN41::chkReply(string cmp1, int timeout, string cmp2, bool wCR) // withCR |
AkinoriHashimoto | 2:3a28bc9332b6 | 209 | { |
AkinoriHashimoto | 2:3a28bc9332b6 | 210 | // BT外側から文字列が到着しないと仮定。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 211 | // 目的単語は、取り逃していないと仮定。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 212 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 213 | if(rxStr[0] == '\n') |
AkinoriHashimoto | 2:3a28bc9332b6 | 214 | rxStr= rxStr.substr(1); |
AkinoriHashimoto | 2:3a28bc9332b6 | 215 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 216 | timer.start(true); // for Timeout |
AkinoriHashimoto | 2:3a28bc9332b6 | 217 | int idxCR;//, idx1, idx2;//, idxCut; |
AkinoriHashimoto | 2:3a28bc9332b6 | 218 | // bool2 find;//1, find2; |
AkinoriHashimoto | 2:3a28bc9332b6 | 219 | bool2 find, tmpFind; |
AkinoriHashimoto | 2:3a28bc9332b6 | 220 | string tmpStr; |
AkinoriHashimoto | 2:3a28bc9332b6 | 221 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 222 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 223 | find.b1= find.b2= false; // rxStr全てを確認するために、trueのみ上書きしていく。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 224 | while(true) { |
AkinoriHashimoto | 2:3a28bc9332b6 | 225 | _read(); |
AkinoriHashimoto | 2:3a28bc9332b6 | 226 | // rxStrを確認する。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 227 | idxCR= rxStr.find('\r'); |
AkinoriHashimoto | 3:2a87c102ddd3 | 228 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 229 | if(idxCR != string::npos) { // CR found. |
AkinoriHashimoto | 2:3a28bc9332b6 | 230 | // 通常通信、目的語(wCR)、目的語(woCR)+通常通信。の3パターンのみのはず。 |
AkinoriHashimoto | 3:2a87c102ddd3 | 231 | tmpFind= chkReply_process_wCR(cmp1, cmp2); |
AkinoriHashimoto | 2:3a28bc9332b6 | 232 | if(tmpFind.b1) |
AkinoriHashimoto | 2:3a28bc9332b6 | 233 | find.b1= true; |
AkinoriHashimoto | 2:3a28bc9332b6 | 234 | if(tmpFind.b2) |
AkinoriHashimoto | 2:3a28bc9332b6 | 235 | find.b2= true; |
AkinoriHashimoto | 3:2a87c102ddd3 | 236 | // CRがrxStrになくなるまで繰り返す。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 237 | } else { |
AkinoriHashimoto | 2:3a28bc9332b6 | 238 | // CRなし |
AkinoriHashimoto | 3:2a87c102ddd3 | 239 | tmpFind= chkReply_process_woCR(cmp1, cmp2, wCR); |
AkinoriHashimoto | 2:3a28bc9332b6 | 240 | // この時点で、tmpとfindの両方がTrueの場合、目的のリプが2回出てきた。おかしい。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 241 | // とりあ、そのケースは無視。 |
AkinoriHashimoto | 2:3a28bc9332b6 | 242 | if(tmpFind.b1) |
AkinoriHashimoto | 2:3a28bc9332b6 | 243 | find.b1= true; |
AkinoriHashimoto | 2:3a28bc9332b6 | 244 | if(tmpFind.b2) |
AkinoriHashimoto | 2:3a28bc9332b6 | 245 | find.b2= true; |
AkinoriHashimoto | 2:3a28bc9332b6 | 246 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 247 | bool flagRtn= false; |
AkinoriHashimoto | 2:3a28bc9332b6 | 248 | if(find.b1||find.b2) |
AkinoriHashimoto | 2:3a28bc9332b6 | 249 | flagRtn= true; |
AkinoriHashimoto | 2:3a28bc9332b6 | 250 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 251 | if(wCR && (tmpFind.b1||tmpFind.b2)) |
AkinoriHashimoto | 3:2a87c102ddd3 | 252 | flagRtn= false; // CRが出てくるまで待つ |
AkinoriHashimoto | 2:3a28bc9332b6 | 253 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 254 | if(flagRtn) |
AkinoriHashimoto | 2:3a28bc9332b6 | 255 | return find; |
AkinoriHashimoto | 2:3a28bc9332b6 | 256 | } |
AkinoriHashimoto | 2:3a28bc9332b6 | 257 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 258 | if(timer.read_ms(false, false, false) > timeout) |
AkinoriHashimoto | 2:3a28bc9332b6 | 259 | return find; |
AkinoriHashimoto | 3:2a87c102ddd3 | 260 | wait_ms(1); // 1ms waiting. |
AkinoriHashimoto | 2:3a28bc9332b6 | 261 | } // end while |
AkinoriHashimoto | 2:3a28bc9332b6 | 262 | } |
AkinoriHashimoto | 2:3a28bc9332b6 | 263 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 264 | RN41::bool2 RN41::chkReply_process_wCR(string cmp1, string cmp2) |
AkinoriHashimoto | 3:2a87c102ddd3 | 265 | { |
AkinoriHashimoto | 3:2a87c102ddd3 | 266 | int idx= rxStr.find('\r'); |
AkinoriHashimoto | 3:2a87c102ddd3 | 267 | if(rxStr[++idx] == '\n') |
AkinoriHashimoto | 3:2a87c102ddd3 | 268 | idx++; |
AkinoriHashimoto | 3:2a87c102ddd3 | 269 | // rxStr[idx]は次の文字列のindex |
AkinoriHashimoto | 3:2a87c102ddd3 | 270 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 271 | // tmpに切り取って確認する。 |
AkinoriHashimoto | 3:2a87c102ddd3 | 272 | string tmpStr= rxStr.substr(0, idx); // substr(idx, count) |
AkinoriHashimoto | 3:2a87c102ddd3 | 273 | rxStr= rxStr.substr(idx); |
AkinoriHashimoto | 3:2a87c102ddd3 | 274 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 275 | b2i2 tmpFind= chkReply_find(tmpStr, cmp1, cmp2); |
AkinoriHashimoto | 4:8b74db22e4c6 | 276 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 277 | if(!(tmpFind.b1 || tmpFind.b2)) // 通常通信 |
AkinoriHashimoto | 3:2a87c102ddd3 | 278 | rxStrStored += tmpStr; |
AkinoriHashimoto | 3:2a87c102ddd3 | 279 | // cmpと合致していれば、保存しておく必要性がない |
AkinoriHashimoto | 4:8b74db22e4c6 | 280 | |
AkinoriHashimoto | 4:8b74db22e4c6 | 281 | /* if(tmpFind.b1) |
AkinoriHashimoto | 4:8b74db22e4c6 | 282 | rtn.b1= true; |
AkinoriHashimoto | 4:8b74db22e4c6 | 283 | if(tmpFind.b2) |
AkinoriHashimoto | 4:8b74db22e4c6 | 284 | rtn.b2= true;*/ |
AkinoriHashimoto | 4:8b74db22e4c6 | 285 | bool2 rtn; |
AkinoriHashimoto | 4:8b74db22e4c6 | 286 | rtn.b1= tmpFind.b1; |
AkinoriHashimoto | 4:8b74db22e4c6 | 287 | rtn.b2= tmpFind.b2; |
AkinoriHashimoto | 3:2a87c102ddd3 | 288 | return rtn; |
AkinoriHashimoto | 3:2a87c102ddd3 | 289 | } |
AkinoriHashimoto | 3:2a87c102ddd3 | 290 | RN41::bool2 RN41::chkReply_process_woCR(string cmp1, string cmp2, bool wCR) |
AkinoriHashimoto | 2:3a28bc9332b6 | 291 | { |
AkinoriHashimoto | 3:2a87c102ddd3 | 292 | b2i2 tmpFind= chkReply_find(rxStr, cmp1, cmp2); |
AkinoriHashimoto | 3:2a87c102ddd3 | 293 | // この時点で、tmpとfindの両方がTrueの場合、目的のリプが2回出てきた。おかしい。 |
AkinoriHashimoto | 3:2a87c102ddd3 | 294 | // とりあ、そのケースは無視。 |
AkinoriHashimoto | 3:2a87c102ddd3 | 295 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 296 | // 文字列rxStr処理 |
AkinoriHashimoto | 3:2a87c102ddd3 | 297 | if(!wCR && (tmpFind.b1||tmpFind.b2)) { |
AkinoriHashimoto | 3:2a87c102ddd3 | 298 | int idx1= 0, idx2= 0; |
AkinoriHashimoto | 3:2a87c102ddd3 | 299 | if(tmpFind.b1) |
AkinoriHashimoto | 3:2a87c102ddd3 | 300 | idx1= tmpFind.i1+ cmp1.size(); |
AkinoriHashimoto | 3:2a87c102ddd3 | 301 | if(tmpFind.b2) |
AkinoriHashimoto | 3:2a87c102ddd3 | 302 | idx2= tmpFind.i2+ cmp2.size(); |
AkinoriHashimoto | 3:2a87c102ddd3 | 303 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 304 | if(idx1 < idx2) |
AkinoriHashimoto | 3:2a87c102ddd3 | 305 | idx1= idx2; |
AkinoriHashimoto | 3:2a87c102ddd3 | 306 | if(idx1 > 0) |
AkinoriHashimoto | 3:2a87c102ddd3 | 307 | rxStr= rxStr.substr(idx1); |
AkinoriHashimoto | 3:2a87c102ddd3 | 308 | } |
AkinoriHashimoto | 3:2a87c102ddd3 | 309 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 310 | bool2 rtn; |
AkinoriHashimoto | 3:2a87c102ddd3 | 311 | rtn.b1= tmpFind.b1; |
AkinoriHashimoto | 3:2a87c102ddd3 | 312 | rtn.b2= tmpFind.b2; |
AkinoriHashimoto | 3:2a87c102ddd3 | 313 | return rtn; |
AkinoriHashimoto | 3:2a87c102ddd3 | 314 | } |
AkinoriHashimoto | 3:2a87c102ddd3 | 315 | RN41::b2i2 RN41::chkReply_find(string trg, string cmp1, string cmp2) //, bool wCR) // withCR |
AkinoriHashimoto | 3:2a87c102ddd3 | 316 | { |
AkinoriHashimoto | 3:2a87c102ddd3 | 317 | b2i2 tmp; |
AkinoriHashimoto | 2:3a28bc9332b6 | 318 | tmp.b1= tmp.b2= false; |
AkinoriHashimoto | 3:2a87c102ddd3 | 319 | tmp.i1= tmp.i2= string::npos; |
AkinoriHashimoto | 2:3a28bc9332b6 | 320 | |
AkinoriHashimoto | 3:2a87c102ddd3 | 321 | tmp.i1= trg.find(cmp1); |
AkinoriHashimoto | 3:2a87c102ddd3 | 322 | if( (tmp.i1= trg.find(cmp1)) != string::npos) |
AkinoriHashimoto | 2:3a28bc9332b6 | 323 | tmp.b1= true; |
AkinoriHashimoto | 2:3a28bc9332b6 | 324 | if(!cmp2.empty()) |
AkinoriHashimoto | 3:2a87c102ddd3 | 325 | if((tmp.i2= trg.find(cmp2)) != string::npos) |
AkinoriHashimoto | 2:3a28bc9332b6 | 326 | tmp.b2= true; |
AkinoriHashimoto | 2:3a28bc9332b6 | 327 | return tmp; |
AkinoriHashimoto | 2:3a28bc9332b6 | 328 | } |
AkinoriHashimoto | 2:3a28bc9332b6 | 329 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 330 | |
AkinoriHashimoto | 2:3a28bc9332b6 | 331 | void RN41::_readIrq(void) |
AkinoriHashimoto | 0:812e6b59aa54 | 332 | { |
AkinoriHashimoto | 3:2a87c102ddd3 | 333 | while(rn41.readable()) |
AkinoriHashimoto | 3:2a87c102ddd3 | 334 | ringBuf.set((char)rn41.getc()); |
AkinoriHashimoto | 2:3a28bc9332b6 | 335 | return; |
AkinoriHashimoto | 2:3a28bc9332b6 | 336 | } |
AkinoriHashimoto | 2:3a28bc9332b6 | 337 | void RN41::_read() |
AkinoriHashimoto | 2:3a28bc9332b6 | 338 | { |
AkinoriHashimoto | 0:812e6b59aa54 | 339 | // Bufferを吸い尽くす |
AkinoriHashimoto | 3:2a87c102ddd3 | 340 | this->_readIrq(); // ringBufにSerialバッファからコピーされた。 |
AkinoriHashimoto | 3:2a87c102ddd3 | 341 | rxStr += ringBuf.get(); |
AkinoriHashimoto | 3:2a87c102ddd3 | 342 | return; |
AkinoriHashimoto | 1:5ed110051e39 | 343 | } |
AkinoriHashimoto | 1:5ed110051e39 | 344 | |
AkinoriHashimoto | 1:5ed110051e39 | 345 | // EOF |