char __gsmSendTcpDataLowLevel(const char *p, int len) { int i; char buf[16]; char *reply; sprintf(buf, "AT+QISEND=%d\r", len); //len多大1460 ATCommand(buf, NULL, configTICK_RATE_HZ / 2); for (i = 0; i < len; i++) { ATCmdSendChar(*p++); } while (1) { reply = ATCommand(NULL, "SEND", configTICK_RATE_HZ * 3); if (reply == NULL) { return false; } if (0 == strncmp(reply, "SEND OK", 7)) { AtCommandDropReplyLine(reply); return true; } else if (0 == strncmp(reply, "SEND FAIL", 9)) { AtCommandDropReplyLine(reply); return false; } else if (0 == strncmp(reply, "ERROR", 5)) { AtCommandDropReplyLine(reply); return false; } else { AtCommandDropReplyLine(reply); } } }
void __handleSendSMS(GsmTaskMessage *msg) { static const char *hexTable = "0123456789ABCDEF"; char buf[16]; int i; char *p = __gsmGetMessageData(msg); sprintf(buf, "AT+CMGS=%d\r", msg->length - 1); ATCommand(buf, NULL, configTICK_RATE_HZ / 5); for (i = 0; i < msg->length; ++i) { ATCmdSendChar(hexTable[*p >> 4]); ATCmdSendChar(hexTable[*p & 0x0F]); ++p; } ATCmdSendChar(0x1A); p = ATCommand(NULL, "OK", configTICK_RATE_HZ * 15); if (p != NULL) { AtCommandDropReplyLine(p); printf("Send SMS OK.\n"); } else { printf("Send SMS error.\n"); } }
int __gsmGetImeiFromModem() { char *reply; reply = ATCommand("AT+GSN\r", ATCMD_ANY_REPLY_PREFIX, configTICK_RATE_HZ / 2); if (reply == NULL) { return 0; } if (!__gsmIsValidImei(reply)) { return 0; } strcpy(__imei, reply); AtCommandDropReplyLine(reply); return 1; }
bool __gsmCheckTcpAndConnect(const char *ip, unsigned short port) { char buf[44]; static uint32_t connectT = 0; static uint32_t connectInterval = 10 * configTICK_RATE_HZ; char *reply; if (__gsmIsTcpConnected()) { gprsconnected =1 ; return true; } if (xTaskGetTickCount() - connectT < connectInterval) { gprsconnected =0 ; return false; } connectT = xTaskGetTickCount(); printf("Connect to %s:%d\n", ip, port); // ATCommand("AT+QIDEACT\r", NULL, configTICK_RATE_HZ * 2); ATCommandAndCheckReply("AT+QIDEACT\r", "DEACT OK", configTICK_RATE_HZ * 20); sprintf(buf, "AT+QIOPEN=\"TCP\",\"%s\",\"%d\"\r", ip, port); reply = ATCommand(buf, "CONNECT", configTICK_RATE_HZ * 40); if (reply == NULL) { goto __error; } if (strncmp("CONNECT OK", reply, 10) == 0) { int size; const char *data; connectInterval = 10 * configTICK_RATE_HZ; AtCommandDropReplyLine(reply); data = ProtoclCreatLogin(__imei, &size); __gsmSendTcpDataLowLevel(data, size); ProtocolDestroyMessage(data); AtCommandDropReplyLine(reply); // printf("chargepile send login command\n" ); gprsconnected =1 ; return true; } AtCommandDropReplyLine(reply); __error: connectInterval += configTICK_RATE_HZ * 15; if (connectInterval > 60 * configTICK_RATE_HZ * 10) { connectInterval = 60 * configTICK_RATE_HZ * 10; } gprsconnected =0 ; return false; }
/// Check if has the GSM modem connect to a TCP server. /// \return true When the GSM modem has connect to a TCP server. /// \return false When the GSM modem dose not connect to a TCP server. //static char Count = 0; //static char Dflag = 0; bool __gsmIsTcpConnected() { char *reply; unsigned char k = 0; while (1) { reply = ATCommand("AT+QISTAT\r", "STATE:", configTICK_RATE_HZ * 2); if (reply == NULL) { return false; } if (strncmp(&reply[7], "CONNECT OK", 10) == 0) { AtCommandDropReplyLine(reply); return true; } if (strncmp(&reply[7], "TCP CONNECTING", 12) == 0) { AtCommandDropReplyLine(reply); vTaskDelay(configTICK_RATE_HZ * 5); if (++k >= 5) { // Dflag = 1; return false; } continue; } if (strncmp(&reply[7], "IP CLOSE", 8) == 0) { AtCommandDropReplyLine(reply); // Count++; // if (Count > 6) { // Dflag = 1; // Count = 0; // } return false; } AtCommandDropReplyLine(reply); break; } return false; }
void __handleSendAtCommand(GsmTaskMessage *msg) { ATCommand(__gsmGetMessageData(msg), NULL, configTICK_RATE_HZ / 10); }
int main(int argc, char *argv[]) { key_t key; int shmid; char *data; int mode; key = ftok("/home/Zeno/serial/Maxstream.c", 'R'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); data = shmat(shmid, (void *)0, 0); int dataFlag; char sCmd[254]; char sResult[254]; if (argc < 2 || argc > 2) { printf("Serial needs one parameter for the serial port\n"); printf(" For Linux use 'adrserial 0' to connect to /dev/ttyS0\n"); printf(" For Cygwin, use 'adrserial 0' to connect to COM1\n"); return 0; } // end if printf("Type q to quit.\n\n"); if (OpenPort(argv[1]) < 0) return 0; pid_t pid = fork(); if (pid == 0) { // Child process: // int j = 0; //while (1){ //ReadPort(sResult,254) > 0; //if (sResult[0]!='N' && j<254 && sResult[0]!=0x0d){ //data[j] = sResult[0]; //j++; //} //if (sResult[0]==0x0d){ // j=0; // printf("%s\n",data); //} // } } else if (pid > 0) { // Parent process: while (1) { printf("?:"); gets(sCmd); printf("");//Do not remove this line or the system crashes ATCommand(1,sCmd);//sCmd); //printf("Say goodbye to her for me \n\r"); //printf("your mom\r\n"); } } else { // Error: fprintf(stderr, "can't fork, error %d\n", errno); exit(1); } }