bool GPRS::ok(Stream* stream) { stream->println("AT"); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("No OK"); #endif return false; } return true; }
// --- Stream front-end data in/out the module for read/write commands void stream(void) { uint32_t rateFactor = 0; if (myTrig) { myTrig = 0; if (streamMode == 1) // This module is reading/writing itself { #ifndef TX sprintf(sertemp,"%d\n\r",samplePD()); send(rwComPort, sertemp); if (rwComLED) LED_toggle(); #else writeToFE(rwComPort); if (rwComLED) LED_toggle(); #endif } else if (streamMode == 3) // This module is being read/written by another module { #ifndef TX readOK(readComSrc, rwComLED); #else writeToFE(rwComPort); if (rwComLED) LED_toggle(); #endif } else if (streamMode == 4) // This module is reading all other receiver { // Display previous data readAllDisp(); // Inquire new data rateFactor = floor((48*1000000)/rwComRate); message[0] = _MesID_broadcast; // broadcast command message[1] = _MesID_read; // read command message[2] = (uint8_t)rateFactor; // rwComRate: Low Byte message[3] = (uint8_t)(rateFactor>>8); message[4] = (uint8_t)(rateFactor>>16); message[5] = (uint8_t)(rateFactor>>24); message[6] = rwComLED; // rwComLED broadcast(myID); // Activate the modules stack. Expect response from all receiver modules modStack = numOfRX+1; #ifndef TX modBuf[myID-1] = samplePD(); #endif } else if (streamMode == 5) // This module is writing all other transmitters
bool GPRS::configureSMS(Stream* stream) { #ifdef GPRS_DEBUG Serial.println("\nConfigure message"); #endif stream->println("AT+CMGF=1"); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("No OK"); #endif return false; } return true; }
bool GPRS::reset(Stream* stream) { #ifdef GPRS_DEBUG Serial.println("\nReset"); #endif stream->println("ATZ"); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("No OK"); #endif return false; } return true; }
Connector::Connector() { //readOp = {0x09, 0xAC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x02}; readOp[0] = 0x09; // 数据总数 readOp[1] = 0xAC; // 读数据 readOp[2] = 0xFF; // 密码 readOp[3] = 0xFF; readOp[4] = 0xFF; readOp[5] = 0xFF; readOp[6] = 0xFF; readOp[7] = 0xFF; readOp[8] = 0x01; // 起始扇区 readOp[9] = 0x02; // 扇区数(最多5) //waitOp = {0x01, 0xAD}; waitOp[0] = 0x01; // 数据总数 waitOp[1] = 0xAD; // 关射频 //writeOp = {0x09, 0xAB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x02}; writeOp[0] = 0x09; // 数据总数 writeOp[1] = 0xAB; // 写数据 writeOp[2] = 0xFF; // 密码 writeOp[3] = 0xFF; writeOp[4] = 0xFF; writeOp[5] = 0xFF; writeOp[6] = 0xFF; writeOp[7] = 0xFF; writeOp[8] = 0x01; // 起始扇区 writeOp[9] = 0x02; // 扇区数 //后面跟数据,最长240byte //读取数据的返回头 returnHead[0] = 0x5A; returnHead[1] = 0x59; returnHead[2] = 0x48; returnHead[3] = 0x02; setPortName("/dev/ttyO5"); setBaudRate(QSerialPort::Baud9600); setDataBits(QSerialPort::Data8); setStopBits(QSerialPort::OneStop); setParity(QSerialPort::NoParity); open(QIODevice::ReadWrite); timer.setInterval(2000); connect(&timer, SIGNAL(timeout()), this, SLOT(onTimeOut())); connect(this, SIGNAL(readOK(QByteArray)), this, SLOT(onReadOK(QByteArray))); startRead(1, 4); }
bool GPRS::quality(Stream* stream) { int buflen = 100; char buffer[buflen]; stream->println("AT+CSQ"); readUntil(stream, buffer, buflen, '+'); readUntil(stream, buffer, buflen, '\n'); #ifdef GPRS_DEBUG Serial.print("B: "); Serial.println(buffer); #endif if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("No OK"); #endif return false; } return true; }
void Connector::handleReadData() { qDebug()<<"Connector::handleReadData"; int dataStart = ba.indexOf(QByteArray(returnHead, 4), 0); if(dataStart != -1) { // 找到返回头 // 下一个是数据长度 if(dataStart + 4 >= ba.size()) { return; // 数据长度不够 } int dataLen = ba[dataStart + 4]; qDebug()<<dataStart + 5 + dataLen<<" "<<ba.size(); if(dataStart + 5 + dataLen > ba.size()) { return; // 数据长度不够 } else { emit readOK(ba.mid(dataStart + 6, dataLen)); //结束 stopRead(); } } else { callForData(start, num); } }
// Taken from http://www.sparkfun.com/datasheets/CellularShield/SM5100B%20TCPIP%20App%20Note.pdf bool GPRS::sendTCP(Stream* stream, char* host, int port, char* data) { int buflen = 100; char buffer[buflen]; #ifdef GPRS_DEBUG Serial.println("AT+CGATT?"); #endif stream->println("AT+CGATT?"); if (!matchChars(stream, "+CGATT: 1")) { #ifdef GPRS_DEBUG Serial.println("Could not find CGATT : 1"); #endif return false; } if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } /*#ifdef GPRS_DEBUG Serial.println("AT+CGDCONT=?"); #endif // It appears that this should only happen once, however there is no real way to test it easily stream->println("AT+CGDCONT?"); if (readOK(stream)) { #ifdef GPRS_DEBUG Serial.print("AT+CGDCONT=1,\"IP\",\""); Serial.print(pdp); Serial.println("\""); #endif stream->print("AT+CGDCONT=1,\"IP\",\""); stream->print(pdp); stream->println("\""); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } } else { #ifdef GPRS_DEBUG Serial.println("Have context already"); #endif consume(stream); } */ #ifdef GPRS_DEBUG Serial.print("AT+CGDCONT=1,\"IP\",\""); Serial.print(pdp); Serial.println("\""); #endif stream->print("AT+CGDCONT=1,\"IP\",\""); stream->print(pdp); stream->println("\""); if (!matchChars(stream, "OK", -1, "+CME ERROR: 4")) { #ifdef GPRS_DEBUG Serial.println("Not ok or error 4"); #endif return false; } if (strlen(username) > 0) { #ifdef GPRS_DEBUG Serial.println("Username/Password"); #endif stream->print("AT+CGPCO=0,\""); stream->print(username); stream->print("\",\""); stream->print(password); stream->println("\", 1 "); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } } #ifdef GPRS_DEBUG Serial.println("AT+CGACT=1,1"); #endif stream->println("AT+CGACT=1,1"); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } #ifdef GPRS_DEBUG Serial.print("AT+SDATACONF=1,\"TCP\",\""); Serial.print(host); Serial.print("\","); Serial.println(port); #endif stream->print("AT+SDATACONF=1,\"TCP\",\""); stream->print(host); stream->print("\","); stream->println(port); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } #ifdef GPRS_DEBUG Serial.println("AT+SDATASTART=1,1"); #endif stream->println("AT+SDATASTART=1,1"); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } bool connected = false; #ifdef GPRS_DEBUG Serial.println("AT+SDATASTATUS=1"); #endif while (!connected) { stream->println("AT+SDATASTATUS=1"); readUntil(stream, buffer, buflen, '+'); readUntil(stream, buffer, buflen, '\n'); // Expecting //+SOCKSTATUS: 1,0,0104,0,0,0 #ifdef GPRS_DEBUG Serial.print("B: "); Serial.println(buffer); #endif if (strcmp(buffer, "SOCKSTATUS: 1,1,0102,0,0,0\r") == 0) { connected = true; } } #ifdef GPRS_DEBUG Serial.print("AT+SDATATSEND=1,"); Serial.println(strlen(data)); #endif stream->print("AT+SDATATSEND=1,"); stream->println(strlen(data)); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } if(!readTcpPrompt(stream)) { #ifdef GPRS_DEBUG Serial.println("No prompt"); #endif return false; } #ifdef GPRS_DEBUG Serial.print(data); #endif stream->print(data); stream->print(26, BYTE); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } #ifdef GPRS_DEBUG Serial.println("AT+SDATASTART=1,0"); #endif stream->println("AT+SDATASTART=1,0"); if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("Not ok"); #endif return false; } return true; }
bool GPRS::sendSMS(Stream* stream, char* number, char* msg) { /* AT+CMGF=1 AT+CMGS="15921272576" > HELLO9527<ctrl-z > +CMGS: 36 OK */ if (!configureSMS(stream)) { #ifdef GPRS_DEBUG Serial.println("\nCould not configure"); #endif return false; } #ifdef GPRS_DEBUG Serial.println("\nSend phone number"); #endif stream->print("AT+CMGS=\""); stream->print(number); stream->println("\""); #ifdef GPRS_DEBUG Serial.println("\nWait for prompt"); #endif if (!readPrompt(stream)) { #ifdef GPRS_DEBUG Serial.println("No Prompt"); #endif return false; } stream->print(msg); stream->print(26, BYTE); int buflen = 100; char buffer[buflen]; #ifdef GPRS_DEBUG Serial.println("\nWait for response"); #endif if (!matchChars(stream, "+CMGS:")) { #ifdef GPRS_DEBUG Serial.println("No +CMGS:"); #endif return false; } if (!readUntil(stream, buffer, buflen, '\n')) { #ifdef GPRS_DEBUG Serial.println("No new line"); #endif return false; } #ifdef GPRS_DEBUG Serial.println("\nWait for OK"); #endif if (!readOK(stream)) { #ifdef GPRS_DEBUG Serial.println("No OK 2"); #endif return false; } return true; }