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

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

Committer:
AkinoriHashimoto
Date:
Tue Oct 20 01:32:17 2015 +0000
Revision:
0:812e6b59aa54
Child:
1:5ed110051e39
beta publish.

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 //extern void sendHC05(string);
AkinoriHashimoto 0:812e6b59aa54 5
AkinoriHashimoto 0:812e6b59aa54 6
AkinoriHashimoto 0:812e6b59aa54 7
AkinoriHashimoto 0:812e6b59aa54 8 const int RN41::NG= 0;
AkinoriHashimoto 0:812e6b59aa54 9 const int RN41::OK= 1;
AkinoriHashimoto 0:812e6b59aa54 10
AkinoriHashimoto 0:812e6b59aa54 11 // PRIVATE
AkinoriHashimoto 0:812e6b59aa54 12 const int RN41::findCmp12= 100;
AkinoriHashimoto 0:812e6b59aa54 13 const int RN41::findCmp1= 101;
AkinoriHashimoto 0:812e6b59aa54 14 const int RN41::findCmp2= 102;
AkinoriHashimoto 0:812e6b59aa54 15
AkinoriHashimoto 0:812e6b59aa54 16 // PUBLIC
AkinoriHashimoto 0:812e6b59aa54 17 const int RN41::ERR_AddrUnder12= 210;
AkinoriHashimoto 0:812e6b59aa54 18 const int RN41::ERR_AddrOver12= 211;
AkinoriHashimoto 0:812e6b59aa54 19 const int RN41::ERR_Connect= 220;
AkinoriHashimoto 0:812e6b59aa54 20 const int RN41::FAIL_Connect= 221;
AkinoriHashimoto 0:812e6b59aa54 21 const int RN41::ERR_EnterCmdMode= 230;
AkinoriHashimoto 0:812e6b59aa54 22 const int RN41::ERR_Disconnect= 240;
AkinoriHashimoto 0:812e6b59aa54 23
AkinoriHashimoto 0:812e6b59aa54 24 const int RN41::ERR_NameSize= 310;
AkinoriHashimoto 0:812e6b59aa54 25
AkinoriHashimoto 0:812e6b59aa54 26 /*
AkinoriHashimoto 0:812e6b59aa54 27 const int RN41::None= 10;
AkinoriHashimoto 0:812e6b59aa54 28 const int RN41::Odd= 11;
AkinoriHashimoto 0:812e6b59aa54 29 const int RN41::Even= 12;
AkinoriHashimoto 0:812e6b59aa54 30 */
AkinoriHashimoto 0:812e6b59aa54 31
AkinoriHashimoto 0:812e6b59aa54 32
AkinoriHashimoto 0:812e6b59aa54 33
AkinoriHashimoto 0:812e6b59aa54 34 RN41::RN41(PinName TX, PinName RX, int baudrate, int bit, int parity, int stop, bool CRLN)
AkinoriHashimoto 0:812e6b59aa54 35 : rn41(TX, RX)
AkinoriHashimoto 0:812e6b59aa54 36 {
AkinoriHashimoto 0:812e6b59aa54 37 rn41.baud(baudrate);
AkinoriHashimoto 0:812e6b59aa54 38 /*
AkinoriHashimoto 0:812e6b59aa54 39 Parity _parity= SerialBase::None; // enum
AkinoriHashimoto 0:812e6b59aa54 40 if(parity == 'O')
AkinoriHashimoto 0:812e6b59aa54 41 _parity= SerialBase::Odd;
AkinoriHashimoto 0:812e6b59aa54 42 if(parity == 'E')
AkinoriHashimoto 0:812e6b59aa54 43 _parity= SerialBase::Even;
AkinoriHashimoto 0:812e6b59aa54 44 rn41.format(bit, _parity, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:812e6b59aa54 45 */
AkinoriHashimoto 0:812e6b59aa54 46
AkinoriHashimoto 0:812e6b59aa54 47 // 力技
AkinoriHashimoto 0:812e6b59aa54 48 if (parity == SerialBase::Odd)
AkinoriHashimoto 0:812e6b59aa54 49 rn41.format(bit, SerialBase::Odd, stop);
AkinoriHashimoto 0:812e6b59aa54 50 else if (parity == SerialBase::Even)
AkinoriHashimoto 0:812e6b59aa54 51 rn41.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:812e6b59aa54 52 else// if (parity == SerialBase::None)
AkinoriHashimoto 0:812e6b59aa54 53 rn41.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:812e6b59aa54 54
AkinoriHashimoto 0:812e6b59aa54 55 CR= '\r';
AkinoriHashimoto 0:812e6b59aa54 56 if(CRLN)
AkinoriHashimoto 0:812e6b59aa54 57 CR= "\r\n";
AkinoriHashimoto 0:812e6b59aa54 58 }
AkinoriHashimoto 0:812e6b59aa54 59
AkinoriHashimoto 0:812e6b59aa54 60
AkinoriHashimoto 0:812e6b59aa54 61 int RN41::connect(string addr)
AkinoriHashimoto 0:812e6b59aa54 62 {
AkinoriHashimoto 0:812e6b59aa54 63 timerLocal.reset();
AkinoriHashimoto 0:812e6b59aa54 64 timerLocal.start();
AkinoriHashimoto 0:812e6b59aa54 65
AkinoriHashimoto 0:812e6b59aa54 66 addr= toAlpanumeric(addr, true);
AkinoriHashimoto 0:812e6b59aa54 67 int idx= addr.size();
AkinoriHashimoto 0:812e6b59aa54 68 // ex. 0006:66:72:6e05\r ->000666726e05.
AkinoriHashimoto 0:812e6b59aa54 69 if(idx < 12)
AkinoriHashimoto 0:812e6b59aa54 70 return ERR_AddrUnder12;
AkinoriHashimoto 0:812e6b59aa54 71 if(idx > 12)
AkinoriHashimoto 0:812e6b59aa54 72 return ERR_AddrOver12;
AkinoriHashimoto 0:812e6b59aa54 73
AkinoriHashimoto 0:812e6b59aa54 74 if(!enterCMD())
AkinoriHashimoto 0:812e6b59aa54 75 return ERR_EnterCmdMode; // CMD入れず
AkinoriHashimoto 0:812e6b59aa54 76 // sendHC05("test 100");
AkinoriHashimoto 0:812e6b59aa54 77 if(!disconnect())
AkinoriHashimoto 0:812e6b59aa54 78 return ERR_Disconnect;
AkinoriHashimoto 0:812e6b59aa54 79 if(!enterCMD())
AkinoriHashimoto 0:812e6b59aa54 80 return ERR_EnterCmdMode; // CMD入れず
AkinoriHashimoto 0:812e6b59aa54 81
AkinoriHashimoto 0:812e6b59aa54 82 // BT addr check success;
AkinoriHashimoto 0:812e6b59aa54 83 sendCMD("C,"+ addr);
AkinoriHashimoto 0:812e6b59aa54 84 int status;
AkinoriHashimoto 0:812e6b59aa54 85
AkinoriHashimoto 0:812e6b59aa54 86 // wait(1.0);read();sendHC05("--"+ rxStr+ "**");
AkinoriHashimoto 0:812e6b59aa54 87
AkinoriHashimoto 0:812e6b59aa54 88 status= chkReply("TRYING\r", 1000, "ERR\r");
AkinoriHashimoto 0:812e6b59aa54 89 if(status == findCmp2)
AkinoriHashimoto 0:812e6b59aa54 90 return ERR_Connect;
AkinoriHashimoto 0:812e6b59aa54 91 else if(status == findCmp1) {
AkinoriHashimoto 0:812e6b59aa54 92 // Trying to connect to remote Bluetooth dev.
AkinoriHashimoto 0:812e6b59aa54 93
AkinoriHashimoto 0:812e6b59aa54 94 // wait(7.0); read(); sendHC05(rxStr);
AkinoriHashimoto 0:812e6b59aa54 95 timerLocal.reset();
AkinoriHashimoto 0:812e6b59aa54 96 timerLocal.start();
AkinoriHashimoto 0:812e6b59aa54 97 status= chkReply("CONNECT,", 10000, "CONNECT failed\r"); // wait 10s
AkinoriHashimoto 0:812e6b59aa54 98
AkinoriHashimoto 0:812e6b59aa54 99 // sendHC05("Time of Create-Connection: "+ I2A(timerLocal.read_ms())+ " ms.");
AkinoriHashimoto 0:812e6b59aa54 100
AkinoriHashimoto 0:812e6b59aa54 101 if(status == findCmp1)
AkinoriHashimoto 0:812e6b59aa54 102 return OK;
AkinoriHashimoto 0:812e6b59aa54 103 else if(status == findCmp2)
AkinoriHashimoto 0:812e6b59aa54 104 return FAIL_Connect;
AkinoriHashimoto 0:812e6b59aa54 105 } else {
AkinoriHashimoto 0:812e6b59aa54 106 // sendHC05("connect:"+ I2A(status));
AkinoriHashimoto 0:812e6b59aa54 107 }
AkinoriHashimoto 0:812e6b59aa54 108 return 1234;
AkinoriHashimoto 0:812e6b59aa54 109 }
AkinoriHashimoto 0:812e6b59aa54 110
AkinoriHashimoto 0:812e6b59aa54 111
AkinoriHashimoto 0:812e6b59aa54 112
AkinoriHashimoto 0:812e6b59aa54 113
AkinoriHashimoto 0:812e6b59aa54 114 string RN41::getLine()
AkinoriHashimoto 0:812e6b59aa54 115 {
AkinoriHashimoto 0:812e6b59aa54 116 read();
AkinoriHashimoto 0:812e6b59aa54 117 // CRまでを返す
AkinoriHashimoto 0:812e6b59aa54 118
AkinoriHashimoto 0:812e6b59aa54 119 //sendHC05("debug 10");
AkinoriHashimoto 0:812e6b59aa54 120 if(str4GetLine.size()==0 && rxStr.size()==0)
AkinoriHashimoto 0:812e6b59aa54 121 return "";
AkinoriHashimoto 0:812e6b59aa54 122
AkinoriHashimoto 0:812e6b59aa54 123 // trying->Connect待ちをTickerにすると、そこんとの除外処理が必須。
AkinoriHashimoto 0:812e6b59aa54 124 // sendHC05("debug 20");
AkinoriHashimoto 0:812e6b59aa54 125
AkinoriHashimoto 0:812e6b59aa54 126 // rxStr.size() > 0
AkinoriHashimoto 0:812e6b59aa54 127 int idx= rxStr.rfind(CR);
AkinoriHashimoto 0:812e6b59aa54 128 // sendHC05("rxStr: "+ rxStr); sendHC05("idxCR: "+ I2A(idx));
AkinoriHashimoto 0:812e6b59aa54 129 if(idx == string::npos) // not find CR!
AkinoriHashimoto 0:812e6b59aa54 130 return str4GetLine; // ダメじゃね??
AkinoriHashimoto 0:812e6b59aa54 131
AkinoriHashimoto 0:812e6b59aa54 132 // sendHC05("debug 30");
AkinoriHashimoto 0:812e6b59aa54 133 // rxStr has CR.
AkinoriHashimoto 0:812e6b59aa54 134 idx += CR.size(); // \rなら+1、\r\nは+2
AkinoriHashimoto 0:812e6b59aa54 135 string rtn= str4GetLine;
AkinoriHashimoto 0:812e6b59aa54 136 rtn += rxStr.substr(0, idx); // 切り取り
AkinoriHashimoto 0:812e6b59aa54 137 rxStr= rxStr.substr(idx);
AkinoriHashimoto 0:812e6b59aa54 138 // sendHC05("debug 40");
AkinoriHashimoto 0:812e6b59aa54 139 return rtn;
AkinoriHashimoto 0:812e6b59aa54 140 }
AkinoriHashimoto 0:812e6b59aa54 141
AkinoriHashimoto 0:812e6b59aa54 142
AkinoriHashimoto 0:812e6b59aa54 143
AkinoriHashimoto 0:812e6b59aa54 144 int RN41::setDev_Name(string str, bool usingAddr)
AkinoriHashimoto 0:812e6b59aa54 145 {
AkinoriHashimoto 0:812e6b59aa54 146 int strSize= str.size();
AkinoriHashimoto 0:812e6b59aa54 147 if((strSize==0) || (20<=strSize) || (usingAddr && 15<=strSize))
AkinoriHashimoto 0:812e6b59aa54 148 return ERR_NameSize;
AkinoriHashimoto 0:812e6b59aa54 149
AkinoriHashimoto 0:812e6b59aa54 150 // 特殊文字チェックは不要っぽい
AkinoriHashimoto 0:812e6b59aa54 151
AkinoriHashimoto 0:812e6b59aa54 152 string cmd;
AkinoriHashimoto 0:812e6b59aa54 153 if(usingAddr)
AkinoriHashimoto 0:812e6b59aa54 154 cmd= "S-,";
AkinoriHashimoto 0:812e6b59aa54 155 else
AkinoriHashimoto 0:812e6b59aa54 156 cmd= "SN,";
AkinoriHashimoto 0:812e6b59aa54 157
AkinoriHashimoto 0:812e6b59aa54 158 if(!enterCMD())
AkinoriHashimoto 0:812e6b59aa54 159 return ERR_EnterCmdMode; // CMD入れず
AkinoriHashimoto 0:812e6b59aa54 160
AkinoriHashimoto 0:812e6b59aa54 161
AkinoriHashimoto 0:812e6b59aa54 162 sendCMD(cmd+ str);
AkinoriHashimoto 0:812e6b59aa54 163 int status;
AkinoriHashimoto 0:812e6b59aa54 164 // wait(1.0);read();sendHC05("--"+ rxStr+ "**");
AkinoriHashimoto 0:812e6b59aa54 165 status= chkReply("AOK\r", 1000);
AkinoriHashimoto 0:812e6b59aa54 166 if(status == findCmp1)
AkinoriHashimoto 0:812e6b59aa54 167 return OK;
AkinoriHashimoto 0:812e6b59aa54 168
AkinoriHashimoto 0:812e6b59aa54 169 return NG;
AkinoriHashimoto 0:812e6b59aa54 170 }
AkinoriHashimoto 0:812e6b59aa54 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 0:812e6b59aa54 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
AkinoriHashimoto 0:812e6b59aa54 189
AkinoriHashimoto 0:812e6b59aa54 190 bool RN41::enterCMD()
AkinoriHashimoto 0:812e6b59aa54 191 {
AkinoriHashimoto 0:812e6b59aa54 192 sendCMD("$$$", false);
AkinoriHashimoto 0:812e6b59aa54 193 if(chkReply("CMD\r", 500) == findCmp1) { // 500ms
AkinoriHashimoto 0:812e6b59aa54 194 // sendHC05("returned CMD.");
AkinoriHashimoto 0:812e6b59aa54 195 return true;
AkinoriHashimoto 0:812e6b59aa54 196 }
AkinoriHashimoto 0:812e6b59aa54 197
AkinoriHashimoto 0:812e6b59aa54 198 sendCMD("\r", false); // 既にCMDモード
AkinoriHashimoto 0:812e6b59aa54 199 if(chkReply("?\r", 500) == findCmp1) {
AkinoriHashimoto 0:812e6b59aa54 200 // sendHC05("Already CmdMode.");
AkinoriHashimoto 0:812e6b59aa54 201 return true;
AkinoriHashimoto 0:812e6b59aa54 202 }
AkinoriHashimoto 0:812e6b59aa54 203 return false;
AkinoriHashimoto 0:812e6b59aa54 204 }
AkinoriHashimoto 0:812e6b59aa54 205
AkinoriHashimoto 0:812e6b59aa54 206
AkinoriHashimoto 0:812e6b59aa54 207
AkinoriHashimoto 0:812e6b59aa54 208 bool RN41::disconnect()
AkinoriHashimoto 0:812e6b59aa54 209 {
AkinoriHashimoto 0:812e6b59aa54 210 // if(!enterCMD()) return false; // CMD入れず
AkinoriHashimoto 0:812e6b59aa54 211
AkinoriHashimoto 0:812e6b59aa54 212 sendCMD("K,"); // rtn KILL ? / ERR ?
AkinoriHashimoto 0:812e6b59aa54 213 int status;
AkinoriHashimoto 0:812e6b59aa54 214 // wait(6.0); read(); sendHC05("in disconnect; \t"+ rxStr);
AkinoriHashimoto 0:812e6b59aa54 215
AkinoriHashimoto 0:812e6b59aa54 216 status= chkReply("KILL\r", 500, "ERR\r");
AkinoriHashimoto 0:812e6b59aa54 217 if(status == findCmp2) { // 既に切断されている
AkinoriHashimoto 0:812e6b59aa54 218 sendCMD("---"); // exit CMD mode.
AkinoriHashimoto 0:812e6b59aa54 219 if(chkReply("END\r", 500) == findCmp1)
AkinoriHashimoto 0:812e6b59aa54 220 return true;
AkinoriHashimoto 0:812e6b59aa54 221 else// { leds.ON(4);
AkinoriHashimoto 0:812e6b59aa54 222 return false; // どういう状況か不明
AkinoriHashimoto 0:812e6b59aa54 223 } else if(status == findCmp1) {
AkinoriHashimoto 0:812e6b59aa54 224 // if(chkReply("DISCONNECT", 2000) == findCmp1)
AkinoriHashimoto 0:812e6b59aa54 225 if(chkReply_woCR("DISCONNECT", 5000)) // CR無いかも?
AkinoriHashimoto 0:812e6b59aa54 226 return true;
AkinoriHashimoto 0:812e6b59aa54 227 else //{ leds.ON(3);
AkinoriHashimoto 0:812e6b59aa54 228 return false; // どういう状況か不明
AkinoriHashimoto 0:812e6b59aa54 229 }// else { return false; }
AkinoriHashimoto 0:812e6b59aa54 230 return false;
AkinoriHashimoto 0:812e6b59aa54 231 }
AkinoriHashimoto 0:812e6b59aa54 232
AkinoriHashimoto 0:812e6b59aa54 233
AkinoriHashimoto 0:812e6b59aa54 234
AkinoriHashimoto 0:812e6b59aa54 235
AkinoriHashimoto 0:812e6b59aa54 236 int RN41::chkReply(string cmp1, int timeout, string cmp2)
AkinoriHashimoto 0:812e6b59aa54 237 {
AkinoriHashimoto 0:812e6b59aa54 238 timerLocal.reset();
AkinoriHashimoto 0:812e6b59aa54 239 timerLocal.start();
AkinoriHashimoto 0:812e6b59aa54 240
AkinoriHashimoto 0:812e6b59aa54 241 int cmp2Size= cmp2.size();
AkinoriHashimoto 0:812e6b59aa54 242 int idxCR, idx1, idx2;//= string::npos;
AkinoriHashimoto 0:812e6b59aa54 243 bool find1, find2;
AkinoriHashimoto 0:812e6b59aa54 244 for(;;) {
AkinoriHashimoto 0:812e6b59aa54 245 if(timerLocal.read_ms() > timeout)
AkinoriHashimoto 0:812e6b59aa54 246 return NG;
AkinoriHashimoto 0:812e6b59aa54 247
AkinoriHashimoto 0:812e6b59aa54 248 read();
AkinoriHashimoto 0:812e6b59aa54 249 idxCR= rxStr.find_first_of('\r');
AkinoriHashimoto 0:812e6b59aa54 250 if(idxCR != string::npos) { // CR found.
AkinoriHashimoto 0:812e6b59aa54 251 idx1= idx2= string::npos; // -1
AkinoriHashimoto 0:812e6b59aa54 252 find1= find2= false;
AkinoriHashimoto 0:812e6b59aa54 253 idx1= rxStr.find(cmp1);
AkinoriHashimoto 0:812e6b59aa54 254 if(cmp2Size != 0)
AkinoriHashimoto 0:812e6b59aa54 255 idx2= rxStr.find(cmp2);
AkinoriHashimoto 0:812e6b59aa54 256
AkinoriHashimoto 0:812e6b59aa54 257 // idx1: 0< or npos, idx2: 0< or npos(notfined or size0)
AkinoriHashimoto 0:812e6b59aa54 258 int idxCut;
AkinoriHashimoto 0:812e6b59aa54 259 //idx1(~CR) -> idx2(~CR)
AkinoriHashimoto 0:812e6b59aa54 260 if(idx1 != string::npos)
AkinoriHashimoto 0:812e6b59aa54 261 if(idx1 < idxCR)
AkinoriHashimoto 0:812e6b59aa54 262 find1= true;
AkinoriHashimoto 0:812e6b59aa54 263 if(idx2 != string::npos)
AkinoriHashimoto 0:812e6b59aa54 264 if(idx2 < idxCR)
AkinoriHashimoto 0:812e6b59aa54 265 find2= true;
AkinoriHashimoto 0:812e6b59aa54 266
AkinoriHashimoto 0:812e6b59aa54 267 //sendHC05("++"+ rxStr+ "**");
AkinoriHashimoto 0:812e6b59aa54 268 //sendHC05("idx1:"+cmp1+":"+ I2A(idx1)+ "\tidx2:"+cmp2+":"+ I2A(idx2)+ "\tidxCR:"+ I2A(idxCR));
AkinoriHashimoto 0:812e6b59aa54 269
AkinoriHashimoto 0:812e6b59aa54 270 // CRまで切り取り。findならば切り捨て、見つからなければコピー後切り捨て
AkinoriHashimoto 0:812e6b59aa54 271 idxCut= idxCR+ 1;
AkinoriHashimoto 0:812e6b59aa54 272 if(rxStr[idxCut] == '\n')
AkinoriHashimoto 0:812e6b59aa54 273 idxCut++;
AkinoriHashimoto 0:812e6b59aa54 274 // 切り捨てはidxからの切り取りを上書き。コピーは文字数なので、idxが-1まで。
AkinoriHashimoto 0:812e6b59aa54 275 if(!(find1 || find2))
AkinoriHashimoto 0:812e6b59aa54 276 str4GetLine= rxStr.substr(0, idxCut);
AkinoriHashimoto 0:812e6b59aa54 277 rxStr= rxStr.substr(idxCut); // 切り取りは共通。
AkinoriHashimoto 0:812e6b59aa54 278
AkinoriHashimoto 0:812e6b59aa54 279 if(find1 && find2) // find1有、find2有
AkinoriHashimoto 0:812e6b59aa54 280 return findCmp12;
AkinoriHashimoto 0:812e6b59aa54 281 if(find1)
AkinoriHashimoto 0:812e6b59aa54 282 return findCmp1;
AkinoriHashimoto 0:812e6b59aa54 283 if(find2)
AkinoriHashimoto 0:812e6b59aa54 284 return findCmp2;
AkinoriHashimoto 0:812e6b59aa54 285 // CR内にCMP無い場合は、ループへ
AkinoriHashimoto 0:812e6b59aa54 286 }
AkinoriHashimoto 0:812e6b59aa54 287 } // end for
AkinoriHashimoto 0:812e6b59aa54 288 }
AkinoriHashimoto 0:812e6b59aa54 289
AkinoriHashimoto 0:812e6b59aa54 290
AkinoriHashimoto 0:812e6b59aa54 291 bool RN41::chkReply_woCR(string cmp, int timeout)
AkinoriHashimoto 0:812e6b59aa54 292 {
AkinoriHashimoto 0:812e6b59aa54 293 timerLocal.reset();
AkinoriHashimoto 0:812e6b59aa54 294 timerLocal.start();
AkinoriHashimoto 0:812e6b59aa54 295
AkinoriHashimoto 0:812e6b59aa54 296 int idx;
AkinoriHashimoto 0:812e6b59aa54 297 for(;;) {
AkinoriHashimoto 0:812e6b59aa54 298 if(timerLocal.read_ms() > timeout) {
AkinoriHashimoto 0:812e6b59aa54 299 // sendHC05("in chkReply; "+ rxStr);
AkinoriHashimoto 0:812e6b59aa54 300 return false;
AkinoriHashimoto 0:812e6b59aa54 301 }
AkinoriHashimoto 0:812e6b59aa54 302 read();
AkinoriHashimoto 0:812e6b59aa54 303 idx= rxStr.find(cmp);
AkinoriHashimoto 0:812e6b59aa54 304 if(idx != string::npos) {
AkinoriHashimoto 0:812e6b59aa54 305 // めんどいから消去する。
AkinoriHashimoto 0:812e6b59aa54 306 idx= rxStr.find_first_of('\r');
AkinoriHashimoto 0:812e6b59aa54 307 if(idx == string::npos)
AkinoriHashimoto 0:812e6b59aa54 308 rxStr= "";
AkinoriHashimoto 0:812e6b59aa54 309 else {
AkinoriHashimoto 0:812e6b59aa54 310 idx++;
AkinoriHashimoto 0:812e6b59aa54 311 if(rxStr[idx] == '\n')
AkinoriHashimoto 0:812e6b59aa54 312 idx++;
AkinoriHashimoto 0:812e6b59aa54 313 rxStr= rxStr.substr(idx);
AkinoriHashimoto 0:812e6b59aa54 314 }
AkinoriHashimoto 0:812e6b59aa54 315 // rxStr= "";
AkinoriHashimoto 0:812e6b59aa54 316 return true;
AkinoriHashimoto 0:812e6b59aa54 317 }
AkinoriHashimoto 0:812e6b59aa54 318 }
AkinoriHashimoto 0:812e6b59aa54 319 }
AkinoriHashimoto 0:812e6b59aa54 320
AkinoriHashimoto 0:812e6b59aa54 321
AkinoriHashimoto 0:812e6b59aa54 322
AkinoriHashimoto 0:812e6b59aa54 323
AkinoriHashimoto 0:812e6b59aa54 324 void RN41::read()
AkinoriHashimoto 0:812e6b59aa54 325 {
AkinoriHashimoto 0:812e6b59aa54 326 if(!rn41.readable())
AkinoriHashimoto 0:812e6b59aa54 327 return;
AkinoriHashimoto 0:812e6b59aa54 328
AkinoriHashimoto 0:812e6b59aa54 329 // Bufferを吸い尽くす
AkinoriHashimoto 0:812e6b59aa54 330 // char ch;
AkinoriHashimoto 0:812e6b59aa54 331 for(;;) {
AkinoriHashimoto 0:812e6b59aa54 332 // char ch= rn41.getc(); // 1文字取る
AkinoriHashimoto 0:812e6b59aa54 333 // ch= rn41.getc(); // 1文字取る
AkinoriHashimoto 0:812e6b59aa54 334 // rxStr += ch; // 上書き前に退避
AkinoriHashimoto 0:812e6b59aa54 335 rxStr += (char)rn41.getc();
AkinoriHashimoto 0:812e6b59aa54 336 if(!rn41.readable())
AkinoriHashimoto 0:812e6b59aa54 337 return; //break; // Bufferになくなった
AkinoriHashimoto 0:812e6b59aa54 338 } // end for
AkinoriHashimoto 0:812e6b59aa54 339 }