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

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

Committer:
AkinoriHashimoto
Date:
Wed Oct 28 04:49:41 2015 +0000
Revision:
2:3a28bc9332b6
Parent:
1:5ed110051e39
Child:
3:2a87c102ddd3
Adj. attach.; Rx buffer of mbed official Serial is 16 Bytes.; Therefore, this lib copies rx buf to private string with rxIrq attach, and public read() function returns private string.

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 2:3a28bc9332b6 87 rxStr.erase();
AkinoriHashimoto 2:3a28bc9332b6 88 rxStrStored.erase();
AkinoriHashimoto 2:3a28bc9332b6 89 return tmp;
AkinoriHashimoto 2:3a28bc9332b6 90 }
AkinoriHashimoto 0:812e6b59aa54 91
AkinoriHashimoto 0:812e6b59aa54 92 string RN41::getLine()
AkinoriHashimoto 0:812e6b59aa54 93 {
AkinoriHashimoto 2:3a28bc9332b6 94 _read();
AkinoriHashimoto 0:812e6b59aa54 95 // CRまでを返す
AkinoriHashimoto 0:812e6b59aa54 96
AkinoriHashimoto 2:3a28bc9332b6 97 if(rxStrStored.empty() && rxStr.empty())
AkinoriHashimoto 0:812e6b59aa54 98 return "";
AkinoriHashimoto 0:812e6b59aa54 99
AkinoriHashimoto 2:3a28bc9332b6 100 int idx= rxStr.rfind('\r');
AkinoriHashimoto 2:3a28bc9332b6 101 if(idx != string::npos) { // CR存在
AkinoriHashimoto 2:3a28bc9332b6 102 if(rxStr[++idx] == '\n')
AkinoriHashimoto 2:3a28bc9332b6 103 idx++;
AkinoriHashimoto 2:3a28bc9332b6 104 rxStrStored += rxStr.substr(0, idx); // size
AkinoriHashimoto 2:3a28bc9332b6 105 rxStr= rxStr.substr(idx); // idx
AkinoriHashimoto 2:3a28bc9332b6 106 }
AkinoriHashimoto 2:3a28bc9332b6 107 // rxStrはCRなし、rxStrStoredに調整済み
AkinoriHashimoto 0:812e6b59aa54 108
AkinoriHashimoto 2:3a28bc9332b6 109 string tmp= rxStrStored;
AkinoriHashimoto 2:3a28bc9332b6 110 rxStrStored.erase();
AkinoriHashimoto 2:3a28bc9332b6 111 return tmp;
AkinoriHashimoto 0:812e6b59aa54 112 }
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 2:3a28bc9332b6 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
AkinoriHashimoto 2:3a28bc9332b6 228 idxCR= rxStr.find('\r');
AkinoriHashimoto 2:3a28bc9332b6 229 if(idxCR != string::npos) { // CR found.
AkinoriHashimoto 2:3a28bc9332b6 230 // 通常通信、目的語(wCR)、目的語(woCR)+通常通信。の3パターンのみのはず。
AkinoriHashimoto 2:3a28bc9332b6 231
AkinoriHashimoto 2:3a28bc9332b6 232 // tmpに切り取って確認する。
AkinoriHashimoto 2:3a28bc9332b6 233 idxCR++;
AkinoriHashimoto 2:3a28bc9332b6 234 if(rxStr[idxCR] == '\n')
AkinoriHashimoto 2:3a28bc9332b6 235 idxCR++;
AkinoriHashimoto 2:3a28bc9332b6 236 // rxStr[idxCR]は次の文字列のidx
AkinoriHashimoto 2:3a28bc9332b6 237 tmpStr= rxStr.substr(0, idxCR); // substr(idx, count)
AkinoriHashimoto 2:3a28bc9332b6 238 rxStr= rxStr.substr(idxCR);
AkinoriHashimoto 2:3a28bc9332b6 239
AkinoriHashimoto 2:3a28bc9332b6 240 tmpFind= chkReply_line(tmpStr, cmp1, cmp2);
AkinoriHashimoto 2:3a28bc9332b6 241 if(tmpFind.b1)
AkinoriHashimoto 2:3a28bc9332b6 242 find.b1= true;
AkinoriHashimoto 2:3a28bc9332b6 243 if(tmpFind.b2)
AkinoriHashimoto 2:3a28bc9332b6 244 find.b2= true;
AkinoriHashimoto 2:3a28bc9332b6 245 if(!(tmpFind.b1 || tmpFind.b2)) // 通常通信
AkinoriHashimoto 2:3a28bc9332b6 246 rxStrStored += tmpStr;
AkinoriHashimoto 2:3a28bc9332b6 247 } else {
AkinoriHashimoto 2:3a28bc9332b6 248 // CRなし
AkinoriHashimoto 2:3a28bc9332b6 249 tmpFind= chkReply_line(rxStr, cmp1, cmp2);
AkinoriHashimoto 2:3a28bc9332b6 250 // この時点で、tmpとfindの両方がTrueの場合、目的のリプが2回出てきた。おかしい。
AkinoriHashimoto 2:3a28bc9332b6 251 // とりあ、そのケースは無視。
AkinoriHashimoto 2:3a28bc9332b6 252 if(tmpFind.b1)
AkinoriHashimoto 2:3a28bc9332b6 253 find.b1= true;
AkinoriHashimoto 2:3a28bc9332b6 254 if(tmpFind.b2)
AkinoriHashimoto 2:3a28bc9332b6 255 find.b2= true;
AkinoriHashimoto 2:3a28bc9332b6 256
AkinoriHashimoto 2:3a28bc9332b6 257 bool flagRtn= false;
AkinoriHashimoto 2:3a28bc9332b6 258 if(find.b1||find.b2)
AkinoriHashimoto 2:3a28bc9332b6 259 flagRtn= true;
AkinoriHashimoto 2:3a28bc9332b6 260
AkinoriHashimoto 2:3a28bc9332b6 261 // Return準備
AkinoriHashimoto 2:3a28bc9332b6 262 if( wCR && (tmpFind.b1||tmpFind.b2))
AkinoriHashimoto 2:3a28bc9332b6 263 flagRtn= false;
AkinoriHashimoto 2:3a28bc9332b6 264
AkinoriHashimoto 2:3a28bc9332b6 265 if(!wCR && (tmpFind.b1||tmpFind.b2)) {
AkinoriHashimoto 2:3a28bc9332b6 266 int idx1, idx2;
AkinoriHashimoto 2:3a28bc9332b6 267 idx1= rxStr.find(cmp1);
AkinoriHashimoto 2:3a28bc9332b6 268 if(!cmp2.empty())
AkinoriHashimoto 2:3a28bc9332b6 269 idx2= rxStr.find(cmp2);
AkinoriHashimoto 2:3a28bc9332b6 270 else
AkinoriHashimoto 2:3a28bc9332b6 271 idx2= string::npos;
AkinoriHashimoto 2:3a28bc9332b6 272
AkinoriHashimoto 2:3a28bc9332b6 273 if(idx1 != string::npos)
AkinoriHashimoto 2:3a28bc9332b6 274 idx1 += cmp1.size();
AkinoriHashimoto 2:3a28bc9332b6 275 else
AkinoriHashimoto 2:3a28bc9332b6 276 idx1= 0;
AkinoriHashimoto 2:3a28bc9332b6 277 if(idx2 != string::npos)
AkinoriHashimoto 2:3a28bc9332b6 278 idx2 += cmp2.size();
AkinoriHashimoto 2:3a28bc9332b6 279 else
AkinoriHashimoto 2:3a28bc9332b6 280 idx2= 0;
AkinoriHashimoto 2:3a28bc9332b6 281
AkinoriHashimoto 2:3a28bc9332b6 282 if(idx1 < idx2)
AkinoriHashimoto 2:3a28bc9332b6 283 idx1= idx2;
AkinoriHashimoto 2:3a28bc9332b6 284 rxStr= rxStr.substr(idx1);
AkinoriHashimoto 2:3a28bc9332b6 285 }
AkinoriHashimoto 2:3a28bc9332b6 286
AkinoriHashimoto 2:3a28bc9332b6 287 if(flagRtn)
AkinoriHashimoto 2:3a28bc9332b6 288 return find;
AkinoriHashimoto 2:3a28bc9332b6 289 }
AkinoriHashimoto 2:3a28bc9332b6 290
AkinoriHashimoto 2:3a28bc9332b6 291 if(timer.read_ms(false, false, false) > timeout)
AkinoriHashimoto 2:3a28bc9332b6 292 return find;
AkinoriHashimoto 2:3a28bc9332b6 293 wait_ms(2); // 1ms waiting.
AkinoriHashimoto 2:3a28bc9332b6 294 } // end while
AkinoriHashimoto 2:3a28bc9332b6 295 }
AkinoriHashimoto 2:3a28bc9332b6 296
AkinoriHashimoto 2:3a28bc9332b6 297 RN41::bool2 RN41::chkReply_line(string trg, string cmp1, string cmp2) //, bool wCR) // withCR
AkinoriHashimoto 2:3a28bc9332b6 298 {
AkinoriHashimoto 2:3a28bc9332b6 299 bool2 tmp;
AkinoriHashimoto 2:3a28bc9332b6 300 tmp.b1= tmp.b2= false;
AkinoriHashimoto 2:3a28bc9332b6 301
AkinoriHashimoto 2:3a28bc9332b6 302 if( trg.find(cmp1) != string::npos)
AkinoriHashimoto 2:3a28bc9332b6 303 tmp.b1= true;
AkinoriHashimoto 2:3a28bc9332b6 304 if(!cmp2.empty())
AkinoriHashimoto 2:3a28bc9332b6 305 if(trg.find(cmp2) != string::npos)
AkinoriHashimoto 2:3a28bc9332b6 306 tmp.b2= true;
AkinoriHashimoto 2:3a28bc9332b6 307
AkinoriHashimoto 2:3a28bc9332b6 308 return tmp;
AkinoriHashimoto 2:3a28bc9332b6 309 }
AkinoriHashimoto 2:3a28bc9332b6 310
AkinoriHashimoto 2:3a28bc9332b6 311
AkinoriHashimoto 2:3a28bc9332b6 312 /*
AkinoriHashimoto 0:812e6b59aa54 313 int RN41::chkReply(string cmp1, int timeout, string cmp2)
AkinoriHashimoto 0:812e6b59aa54 314 {
AkinoriHashimoto 2:3a28bc9332b6 315 // _read();// rxStrStored += rxStr;
AkinoriHashimoto 0:812e6b59aa54 316
AkinoriHashimoto 2:3a28bc9332b6 317 timer.start(true);
AkinoriHashimoto 2:3a28bc9332b6 318 // int cmp2Size= cmp2.size();
AkinoriHashimoto 2:3a28bc9332b6 319 int idxCR, idx;//1, idx2;//, idxCut;
AkinoriHashimoto 0:812e6b59aa54 320 bool find1, find2;
AkinoriHashimoto 0:812e6b59aa54 321
AkinoriHashimoto 1:5ed110051e39 322 // RxStrのチェック必要じゃね?初期化とか
AkinoriHashimoto 1:5ed110051e39 323 while(true) {
AkinoriHashimoto 2:3a28bc9332b6 324 _read();
AkinoriHashimoto 1:5ed110051e39 325 idxCR= rxStr.find('\r');
AkinoriHashimoto 0:812e6b59aa54 326 if(idxCR != string::npos) { // CR found.
AkinoriHashimoto 2:3a28bc9332b6 327 // 最初のCRまでを確認。
AkinoriHashimoto 0:812e6b59aa54 328 find1= find2= false;
AkinoriHashimoto 1:5ed110051e39 329 // idx1: 0< or npos, idx2: 0< or npos(notfined or size0)
AkinoriHashimoto 1:5ed110051e39 330 //idx1(~CR) -> idx2(~CR)
AkinoriHashimoto 2:3a28bc9332b6 331 idx= rxStr.find(cmp1);
AkinoriHashimoto 2:3a28bc9332b6 332 if((idx != string::npos) && (idx < idxCR))
AkinoriHashimoto 1:5ed110051e39 333 find1= true;
AkinoriHashimoto 1:5ed110051e39 334
AkinoriHashimoto 2:3a28bc9332b6 335 if(!cmp2.empty()) { //(cmp2.size() != 0) {
AkinoriHashimoto 2:3a28bc9332b6 336 idx= rxStr.find(cmp2);
AkinoriHashimoto 2:3a28bc9332b6 337 if((idx != string::npos) && (idx < idxCR))
AkinoriHashimoto 0:812e6b59aa54 338 find2= true;
AkinoriHashimoto 1:5ed110051e39 339 }
AkinoriHashimoto 0:812e6b59aa54 340
AkinoriHashimoto 2:3a28bc9332b6 341 // CRまで切り取り。findならば削除。見つからなければ目的でない、通常通信とみなし、コピー。
AkinoriHashimoto 2:3a28bc9332b6 342 idxCR++;//idxCut= idxCR+ 1;
AkinoriHashimoto 2:3a28bc9332b6 343 if(rxStr[idxCR] == '\n')
AkinoriHashimoto 2:3a28bc9332b6 344 idxCR++;
AkinoriHashimoto 2:3a28bc9332b6 345 // 切り捨てはidxからの切り取りを上書き。コピーは文字数。
AkinoriHashimoto 0:812e6b59aa54 346 if(!(find1 || find2))
AkinoriHashimoto 2:3a28bc9332b6 347 rxStrStored += rxStr.substr(0, idxCR);
AkinoriHashimoto 2:3a28bc9332b6 348 rxStr= rxStr.substr(idxCR); // 切り取りは共通。
AkinoriHashimoto 0:812e6b59aa54 349
AkinoriHashimoto 0:812e6b59aa54 350 if(find1 && find2) // find1有、find2有
AkinoriHashimoto 0:812e6b59aa54 351 return findCmp12;
AkinoriHashimoto 0:812e6b59aa54 352 if(find1)
AkinoriHashimoto 0:812e6b59aa54 353 return findCmp1;
AkinoriHashimoto 0:812e6b59aa54 354 if(find2)
AkinoriHashimoto 0:812e6b59aa54 355 return findCmp2;
AkinoriHashimoto 0:812e6b59aa54 356 // CR内にCMP無い場合は、ループへ
AkinoriHashimoto 1:5ed110051e39 357 } // (idxCR != string::npos)
AkinoriHashimoto 2:3a28bc9332b6 358
AkinoriHashimoto 2:3a28bc9332b6 359 if(timer.read_ms(false, false, false) > timeout)
AkinoriHashimoto 2:3a28bc9332b6 360 return ERR_Timeout;
AkinoriHashimoto 1:5ed110051e39 361 wait_ms(2); // 1ms waiting.
AkinoriHashimoto 0:812e6b59aa54 362 } // end for
AkinoriHashimoto 0:812e6b59aa54 363 }
AkinoriHashimoto 0:812e6b59aa54 364 bool RN41::chkReply_woCR(string cmp, int timeout)
AkinoriHashimoto 0:812e6b59aa54 365 {
AkinoriHashimoto 2:3a28bc9332b6 366 // _read();rxStrStored += rxStr;
AkinoriHashimoto 0:812e6b59aa54 367
AkinoriHashimoto 2:3a28bc9332b6 368 timer.start(true);
AkinoriHashimoto 2:3a28bc9332b6 369 int idx, idxCR;
AkinoriHashimoto 1:5ed110051e39 370 while(true) {
AkinoriHashimoto 2:3a28bc9332b6 371 _read();
AkinoriHashimoto 0:812e6b59aa54 372 idx= rxStr.find(cmp);
AkinoriHashimoto 2:3a28bc9332b6 373
AkinoriHashimoto 0:812e6b59aa54 374 if(idx != string::npos) {
AkinoriHashimoto 2:3a28bc9332b6 375 idxCR= rxStr.find('\r');
AkinoriHashimoto 2:3a28bc9332b6 376
AkinoriHashimoto 2:3a28bc9332b6 377 if(idxCR == string::npos)
AkinoriHashimoto 2:3a28bc9332b6 378 rxStr.erase(); // 目的の単語のみと判断する。使用済みなので消去。
AkinoriHashimoto 2:3a28bc9332b6 379 else { // CR存在するので、目的単語の前後で意図が異なる。
AkinoriHashimoto 2:3a28bc9332b6 380 idxCR++;
AkinoriHashimoto 2:3a28bc9332b6 381 if(rxStr[idxCR] == '\n')
AkinoriHashimoto 2:3a28bc9332b6 382 idxCR++;
AkinoriHashimoto 2:3a28bc9332b6 383 // CR後の最初の文字位置
AkinoriHashimoto 2:3a28bc9332b6 384
AkinoriHashimoto 0:812e6b59aa54 385 rxStr= rxStr.substr(idx);
AkinoriHashimoto 0:812e6b59aa54 386 }
AkinoriHashimoto 0:812e6b59aa54 387 return true;
AkinoriHashimoto 0:812e6b59aa54 388 }
AkinoriHashimoto 2:3a28bc9332b6 389
AkinoriHashimoto 2:3a28bc9332b6 390 if(timer.read_ms(false, false, false) > timeout)
AkinoriHashimoto 2:3a28bc9332b6 391 return false;
AkinoriHashimoto 1:5ed110051e39 392 wait_ms(2);
AkinoriHashimoto 0:812e6b59aa54 393 }
AkinoriHashimoto 0:812e6b59aa54 394 }
AkinoriHashimoto 2:3a28bc9332b6 395 */
AkinoriHashimoto 0:812e6b59aa54 396
AkinoriHashimoto 0:812e6b59aa54 397
AkinoriHashimoto 2:3a28bc9332b6 398 void RN41::_readIrq(void)
AkinoriHashimoto 0:812e6b59aa54 399 {
AkinoriHashimoto 2:3a28bc9332b6 400 if(rn41.readable())
AkinoriHashimoto 2:3a28bc9332b6 401 rxStr += (char)rn41.getc();
AkinoriHashimoto 2:3a28bc9332b6 402 return;
AkinoriHashimoto 2:3a28bc9332b6 403 }
AkinoriHashimoto 2:3a28bc9332b6 404 void RN41::_read()
AkinoriHashimoto 2:3a28bc9332b6 405 {
AkinoriHashimoto 0:812e6b59aa54 406 // Bufferを吸い尽くす
AkinoriHashimoto 2:3a28bc9332b6 407 while(rn41.readable())
AkinoriHashimoto 0:812e6b59aa54 408 rxStr += (char)rn41.getc();
AkinoriHashimoto 2:3a28bc9332b6 409
AkinoriHashimoto 2:3a28bc9332b6 410 return; // Bufferになくなった
AkinoriHashimoto 1:5ed110051e39 411 }
AkinoriHashimoto 2:3a28bc9332b6 412 /*
AkinoriHashimoto 2:3a28bc9332b6 413 void RN41::_read()
AkinoriHashimoto 2:3a28bc9332b6 414 {
AkinoriHashimoto 2:3a28bc9332b6 415 // Bufferを吸い尽くす
AkinoriHashimoto 2:3a28bc9332b6 416 // bps(Baudrate)から一文字分待ち、それでも受信がなければBuf空と判断。
AkinoriHashimoto 2:3a28bc9332b6 417 int iter= 0;
AkinoriHashimoto 2:3a28bc9332b6 418 while(true) {
AkinoriHashimoto 2:3a28bc9332b6 419 if(rn41.readable()) {
AkinoriHashimoto 2:3a28bc9332b6 420 rxStr += (char)rn41.getc();
AkinoriHashimoto 2:3a28bc9332b6 421 iter= 0; //reset
AkinoriHashimoto 2:3a28bc9332b6 422 } else {
AkinoriHashimoto 2:3a28bc9332b6 423 wait_us(usWait4readBuf);
AkinoriHashimoto 2:3a28bc9332b6 424 iter++;
AkinoriHashimoto 2:3a28bc9332b6 425 if(iter > 13) // 安全率30%
AkinoriHashimoto 2:3a28bc9332b6 426 return; // Bufferになくなった
AkinoriHashimoto 2:3a28bc9332b6 427 }
AkinoriHashimoto 2:3a28bc9332b6 428 } // end while
AkinoriHashimoto 2:3a28bc9332b6 429 }
AkinoriHashimoto 2:3a28bc9332b6 430 */
AkinoriHashimoto 1:5ed110051e39 431
AkinoriHashimoto 1:5ed110051e39 432 // EOF