RN41/42 control lib. RN41/42 is bluetooth module with class 1 or 2.

Dependencies:   StrLib myTimer RingBuffer

Committer:
AkinoriHashimoto
Date:
Fri Nov 20 07:15:40 2015 +0000
Revision:
5:63925964cbd5
Parent:
4:8b74db22e4c6
add reboot().

Who changed what in which revision?

UserRevisionLine numberNew 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 5:63925964cbd5 161 bool RN41::reboot()
AkinoriHashimoto 5:63925964cbd5 162 {
AkinoriHashimoto 5:63925964cbd5 163 if(!enterCMD())
AkinoriHashimoto 5:63925964cbd5 164 return ; // CMD入れず
AkinoriHashimoto 5:63925964cbd5 165 sendCMD("R,1");
AkinoriHashimoto 5:63925964cbd5 166
AkinoriHashimoto 5:63925964cbd5 167 // bool2 find= chkReply("Reboot!\r", 100);
AkinoriHashimoto 5:63925964cbd5 168
AkinoriHashimoto 5:63925964cbd5 169 return chkReply("Reboot!\r", 100).b1;
AkinoriHashimoto 5:63925964cbd5 170 }
AkinoriHashimoto 5:63925964cbd5 171
AkinoriHashimoto 0:812e6b59aa54 172
AkinoriHashimoto 0:812e6b59aa54 173 void RN41::sendCMD(string str, bool addCR)
AkinoriHashimoto 0:812e6b59aa54 174 {
AkinoriHashimoto 0:812e6b59aa54 175 if(addCR)
AkinoriHashimoto 0:812e6b59aa54 176 str += '\r';
AkinoriHashimoto 0:812e6b59aa54 177 rn41.printf(str.c_str());
AkinoriHashimoto 0:812e6b59aa54 178 return;
AkinoriHashimoto 0:812e6b59aa54 179 }
AkinoriHashimoto 1:5ed110051e39 180 void RN41::sendLine(string str, bool addCR)
AkinoriHashimoto 0:812e6b59aa54 181 {
AkinoriHashimoto 0:812e6b59aa54 182 if(addCR)
AkinoriHashimoto 0:812e6b59aa54 183 str += "\r\n";
AkinoriHashimoto 0:812e6b59aa54 184 rn41.printf(str.c_str());
AkinoriHashimoto 0:812e6b59aa54 185 return;
AkinoriHashimoto 0:812e6b59aa54 186 }
AkinoriHashimoto 0:812e6b59aa54 187
AkinoriHashimoto 0:812e6b59aa54 188 bool RN41::enterCMD()
AkinoriHashimoto 0:812e6b59aa54 189 {
AkinoriHashimoto 0:812e6b59aa54 190 sendCMD("$$$", false);
AkinoriHashimoto 2:3a28bc9332b6 191 if(chkReply("CMD\r", 500).b1) // 500ms
AkinoriHashimoto 0:812e6b59aa54 192 return true;
AkinoriHashimoto 1:5ed110051e39 193
AkinoriHashimoto 0:812e6b59aa54 194 sendCMD("\r", false); // 既にCMDモード
AkinoriHashimoto 2:3a28bc9332b6 195 if(chkReply("?\r", 500).b1)
AkinoriHashimoto 0:812e6b59aa54 196 return true;
AkinoriHashimoto 3:2a87c102ddd3 197
AkinoriHashimoto 0:812e6b59aa54 198 return false;
AkinoriHashimoto 0:812e6b59aa54 199 }
AkinoriHashimoto 0:812e6b59aa54 200
AkinoriHashimoto 0:812e6b59aa54 201 bool RN41::disconnect()
AkinoriHashimoto 0:812e6b59aa54 202 {
AkinoriHashimoto 1:5ed110051e39 203 if(!enterCMD())
AkinoriHashimoto 1:5ed110051e39 204 return false; // CMD入れず
AkinoriHashimoto 0:812e6b59aa54 205 sendCMD("K,"); // rtn KILL ? / ERR ?
AkinoriHashimoto 0:812e6b59aa54 206
AkinoriHashimoto 2:3a28bc9332b6 207 bool2 find;
AkinoriHashimoto 2:3a28bc9332b6 208 find= chkReply("KILL\r", 500, "ERR\r");
AkinoriHashimoto 2:3a28bc9332b6 209 if(find.b2) { // 既に切断されている
AkinoriHashimoto 0:812e6b59aa54 210 sendCMD("---"); // exit CMD mode.
AkinoriHashimoto 2:3a28bc9332b6 211 return chkReply("END\r", 500).b1;
AkinoriHashimoto 2:3a28bc9332b6 212 // return true;else// { leds.ON(4);return false; // どういう状況か不明
AkinoriHashimoto 1:5ed110051e39 213 }
AkinoriHashimoto 2:3a28bc9332b6 214 // 切断中
AkinoriHashimoto 2:3a28bc9332b6 215 return chkReply("DISCONNECT", 5000, "", false).b1; // CR無いかも?
AkinoriHashimoto 0:812e6b59aa54 216 }
AkinoriHashimoto 0:812e6b59aa54 217
AkinoriHashimoto 2:3a28bc9332b6 218
AkinoriHashimoto 2:3a28bc9332b6 219 RN41::bool2 RN41::chkReply(string cmp1, int timeout, string cmp2, bool wCR) // withCR
AkinoriHashimoto 2:3a28bc9332b6 220 {
AkinoriHashimoto 2:3a28bc9332b6 221 // BT外側から文字列が到着しないと仮定。
AkinoriHashimoto 2:3a28bc9332b6 222 // 目的単語は、取り逃していないと仮定。
AkinoriHashimoto 2:3a28bc9332b6 223
AkinoriHashimoto 2:3a28bc9332b6 224 if(rxStr[0] == '\n')
AkinoriHashimoto 2:3a28bc9332b6 225 rxStr= rxStr.substr(1);
AkinoriHashimoto 2:3a28bc9332b6 226
AkinoriHashimoto 2:3a28bc9332b6 227 timer.start(true); // for Timeout
AkinoriHashimoto 2:3a28bc9332b6 228 int idxCR;//, idx1, idx2;//, idxCut;
AkinoriHashimoto 2:3a28bc9332b6 229 // bool2 find;//1, find2;
AkinoriHashimoto 2:3a28bc9332b6 230 bool2 find, tmpFind;
AkinoriHashimoto 2:3a28bc9332b6 231 string tmpStr;
AkinoriHashimoto 2:3a28bc9332b6 232
AkinoriHashimoto 2:3a28bc9332b6 233
AkinoriHashimoto 2:3a28bc9332b6 234 find.b1= find.b2= false; // rxStr全てを確認するために、trueのみ上書きしていく。
AkinoriHashimoto 2:3a28bc9332b6 235 while(true) {
AkinoriHashimoto 2:3a28bc9332b6 236 _read();
AkinoriHashimoto 2:3a28bc9332b6 237 // rxStrを確認する。
AkinoriHashimoto 2:3a28bc9332b6 238 idxCR= rxStr.find('\r');
AkinoriHashimoto 3:2a87c102ddd3 239
AkinoriHashimoto 2:3a28bc9332b6 240 if(idxCR != string::npos) { // CR found.
AkinoriHashimoto 2:3a28bc9332b6 241 // 通常通信、目的語(wCR)、目的語(woCR)+通常通信。の3パターンのみのはず。
AkinoriHashimoto 3:2a87c102ddd3 242 tmpFind= chkReply_process_wCR(cmp1, cmp2);
AkinoriHashimoto 2:3a28bc9332b6 243 if(tmpFind.b1)
AkinoriHashimoto 2:3a28bc9332b6 244 find.b1= true;
AkinoriHashimoto 2:3a28bc9332b6 245 if(tmpFind.b2)
AkinoriHashimoto 2:3a28bc9332b6 246 find.b2= true;
AkinoriHashimoto 3:2a87c102ddd3 247 // CRがrxStrになくなるまで繰り返す。
AkinoriHashimoto 2:3a28bc9332b6 248 } else {
AkinoriHashimoto 2:3a28bc9332b6 249 // CRなし
AkinoriHashimoto 3:2a87c102ddd3 250 tmpFind= chkReply_process_woCR(cmp1, cmp2, wCR);
AkinoriHashimoto 2:3a28bc9332b6 251 // この時点で、tmpとfindの両方がTrueの場合、目的のリプが2回出てきた。おかしい。
AkinoriHashimoto 2:3a28bc9332b6 252 // とりあ、そのケースは無視。
AkinoriHashimoto 2:3a28bc9332b6 253 if(tmpFind.b1)
AkinoriHashimoto 2:3a28bc9332b6 254 find.b1= true;
AkinoriHashimoto 2:3a28bc9332b6 255 if(tmpFind.b2)
AkinoriHashimoto 2:3a28bc9332b6 256 find.b2= true;
AkinoriHashimoto 2:3a28bc9332b6 257
AkinoriHashimoto 2:3a28bc9332b6 258 bool flagRtn= false;
AkinoriHashimoto 2:3a28bc9332b6 259 if(find.b1||find.b2)
AkinoriHashimoto 2:3a28bc9332b6 260 flagRtn= true;
AkinoriHashimoto 2:3a28bc9332b6 261
AkinoriHashimoto 3:2a87c102ddd3 262 if(wCR && (tmpFind.b1||tmpFind.b2))
AkinoriHashimoto 3:2a87c102ddd3 263 flagRtn= false; // CRが出てくるまで待つ
AkinoriHashimoto 2:3a28bc9332b6 264
AkinoriHashimoto 2:3a28bc9332b6 265 if(flagRtn)
AkinoriHashimoto 2:3a28bc9332b6 266 return find;
AkinoriHashimoto 2:3a28bc9332b6 267 }
AkinoriHashimoto 2:3a28bc9332b6 268
AkinoriHashimoto 2:3a28bc9332b6 269 if(timer.read_ms(false, false, false) > timeout)
AkinoriHashimoto 2:3a28bc9332b6 270 return find;
AkinoriHashimoto 3:2a87c102ddd3 271 wait_ms(1); // 1ms waiting.
AkinoriHashimoto 2:3a28bc9332b6 272 } // end while
AkinoriHashimoto 2:3a28bc9332b6 273 }
AkinoriHashimoto 2:3a28bc9332b6 274
AkinoriHashimoto 3:2a87c102ddd3 275 RN41::bool2 RN41::chkReply_process_wCR(string cmp1, string cmp2)
AkinoriHashimoto 3:2a87c102ddd3 276 {
AkinoriHashimoto 3:2a87c102ddd3 277 int idx= rxStr.find('\r');
AkinoriHashimoto 3:2a87c102ddd3 278 if(rxStr[++idx] == '\n')
AkinoriHashimoto 3:2a87c102ddd3 279 idx++;
AkinoriHashimoto 3:2a87c102ddd3 280 // rxStr[idx]は次の文字列のindex
AkinoriHashimoto 3:2a87c102ddd3 281
AkinoriHashimoto 3:2a87c102ddd3 282 // tmpに切り取って確認する。
AkinoriHashimoto 3:2a87c102ddd3 283 string tmpStr= rxStr.substr(0, idx); // substr(idx, count)
AkinoriHashimoto 3:2a87c102ddd3 284 rxStr= rxStr.substr(idx);
AkinoriHashimoto 3:2a87c102ddd3 285
AkinoriHashimoto 3:2a87c102ddd3 286 b2i2 tmpFind= chkReply_find(tmpStr, cmp1, cmp2);
AkinoriHashimoto 4:8b74db22e4c6 287
AkinoriHashimoto 3:2a87c102ddd3 288 if(!(tmpFind.b1 || tmpFind.b2)) // 通常通信
AkinoriHashimoto 3:2a87c102ddd3 289 rxStrStored += tmpStr;
AkinoriHashimoto 3:2a87c102ddd3 290 // cmpと合致していれば、保存しておく必要性がない
AkinoriHashimoto 4:8b74db22e4c6 291
AkinoriHashimoto 4:8b74db22e4c6 292 /* if(tmpFind.b1)
AkinoriHashimoto 4:8b74db22e4c6 293 rtn.b1= true;
AkinoriHashimoto 4:8b74db22e4c6 294 if(tmpFind.b2)
AkinoriHashimoto 4:8b74db22e4c6 295 rtn.b2= true;*/
AkinoriHashimoto 4:8b74db22e4c6 296 bool2 rtn;
AkinoriHashimoto 4:8b74db22e4c6 297 rtn.b1= tmpFind.b1;
AkinoriHashimoto 4:8b74db22e4c6 298 rtn.b2= tmpFind.b2;
AkinoriHashimoto 3:2a87c102ddd3 299 return rtn;
AkinoriHashimoto 3:2a87c102ddd3 300 }
AkinoriHashimoto 3:2a87c102ddd3 301 RN41::bool2 RN41::chkReply_process_woCR(string cmp1, string cmp2, bool wCR)
AkinoriHashimoto 2:3a28bc9332b6 302 {
AkinoriHashimoto 3:2a87c102ddd3 303 b2i2 tmpFind= chkReply_find(rxStr, cmp1, cmp2);
AkinoriHashimoto 3:2a87c102ddd3 304 // この時点で、tmpとfindの両方がTrueの場合、目的のリプが2回出てきた。おかしい。
AkinoriHashimoto 3:2a87c102ddd3 305 // とりあ、そのケースは無視。
AkinoriHashimoto 3:2a87c102ddd3 306
AkinoriHashimoto 3:2a87c102ddd3 307 // 文字列rxStr処理
AkinoriHashimoto 3:2a87c102ddd3 308 if(!wCR && (tmpFind.b1||tmpFind.b2)) {
AkinoriHashimoto 3:2a87c102ddd3 309 int idx1= 0, idx2= 0;
AkinoriHashimoto 3:2a87c102ddd3 310 if(tmpFind.b1)
AkinoriHashimoto 3:2a87c102ddd3 311 idx1= tmpFind.i1+ cmp1.size();
AkinoriHashimoto 3:2a87c102ddd3 312 if(tmpFind.b2)
AkinoriHashimoto 3:2a87c102ddd3 313 idx2= tmpFind.i2+ cmp2.size();
AkinoriHashimoto 3:2a87c102ddd3 314
AkinoriHashimoto 3:2a87c102ddd3 315 if(idx1 < idx2)
AkinoriHashimoto 3:2a87c102ddd3 316 idx1= idx2;
AkinoriHashimoto 3:2a87c102ddd3 317 if(idx1 > 0)
AkinoriHashimoto 3:2a87c102ddd3 318 rxStr= rxStr.substr(idx1);
AkinoriHashimoto 3:2a87c102ddd3 319 }
AkinoriHashimoto 3:2a87c102ddd3 320
AkinoriHashimoto 3:2a87c102ddd3 321 bool2 rtn;
AkinoriHashimoto 3:2a87c102ddd3 322 rtn.b1= tmpFind.b1;
AkinoriHashimoto 3:2a87c102ddd3 323 rtn.b2= tmpFind.b2;
AkinoriHashimoto 3:2a87c102ddd3 324 return rtn;
AkinoriHashimoto 3:2a87c102ddd3 325 }
AkinoriHashimoto 3:2a87c102ddd3 326 RN41::b2i2 RN41::chkReply_find(string trg, string cmp1, string cmp2) //, bool wCR) // withCR
AkinoriHashimoto 3:2a87c102ddd3 327 {
AkinoriHashimoto 3:2a87c102ddd3 328 b2i2 tmp;
AkinoriHashimoto 2:3a28bc9332b6 329 tmp.b1= tmp.b2= false;
AkinoriHashimoto 3:2a87c102ddd3 330 tmp.i1= tmp.i2= string::npos;
AkinoriHashimoto 2:3a28bc9332b6 331
AkinoriHashimoto 3:2a87c102ddd3 332 tmp.i1= trg.find(cmp1);
AkinoriHashimoto 3:2a87c102ddd3 333 if( (tmp.i1= trg.find(cmp1)) != string::npos)
AkinoriHashimoto 2:3a28bc9332b6 334 tmp.b1= true;
AkinoriHashimoto 2:3a28bc9332b6 335 if(!cmp2.empty())
AkinoriHashimoto 3:2a87c102ddd3 336 if((tmp.i2= trg.find(cmp2)) != string::npos)
AkinoriHashimoto 2:3a28bc9332b6 337 tmp.b2= true;
AkinoriHashimoto 2:3a28bc9332b6 338 return tmp;
AkinoriHashimoto 2:3a28bc9332b6 339 }
AkinoriHashimoto 2:3a28bc9332b6 340
AkinoriHashimoto 2:3a28bc9332b6 341
AkinoriHashimoto 2:3a28bc9332b6 342 void RN41::_readIrq(void)
AkinoriHashimoto 0:812e6b59aa54 343 {
AkinoriHashimoto 3:2a87c102ddd3 344 while(rn41.readable())
AkinoriHashimoto 3:2a87c102ddd3 345 ringBuf.set((char)rn41.getc());
AkinoriHashimoto 2:3a28bc9332b6 346 return;
AkinoriHashimoto 2:3a28bc9332b6 347 }
AkinoriHashimoto 2:3a28bc9332b6 348 void RN41::_read()
AkinoriHashimoto 2:3a28bc9332b6 349 {
AkinoriHashimoto 0:812e6b59aa54 350 // Bufferを吸い尽くす
AkinoriHashimoto 3:2a87c102ddd3 351 this->_readIrq(); // ringBufにSerialバッファからコピーされた。
AkinoriHashimoto 3:2a87c102ddd3 352 rxStr += ringBuf.get();
AkinoriHashimoto 3:2a87c102ddd3 353 return;
AkinoriHashimoto 1:5ed110051e39 354 }
AkinoriHashimoto 1:5ed110051e39 355
AkinoriHashimoto 1:5ed110051e39 356 // EOF