- 易迪拓培训,专注于微波、射频、天线设计工程师的培养
GPRS模块可以在实时发送经纬度到服务器期间,按键触发,发送另一条数据到这个服务器的另一个端口吗?
试过直接用if(S2==0)放在void sendDataToServer()后面判断按键的方法不行
#include "main.h"
#include "uart.h"
#include "LCD1602.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <REG52.h>
//常量
#define Success 1U
#define Failure 0U
//定义变量
unsigned long Time_Cont = 0; //定时器计数器
_SaveData Save_Data;
sbit s2=P2^2;
code char TCPServer[] = "120.79.207.145"; //TCP服务器地址
code char Port[] = "80"; //端口
code char Port2[] = "8000"; //端口2
char flagConnect = 0;
//****************************************************
//主函数
//****************************************************
void main()
{
unsigned char i = 0;
Uart_Init();
delay_ms(10);
Init_LCD1602();
LCD1602_write_com(0x80);
LCD1602_write_word("Welcome to ");
LCD1602_write_com(0x80+0x40);
LCD1602_write_word(" Rescue system");
clrStruct(); //清空缓存数组
if (sendCommand("AT\r\n", "OK\r\n", 3000, 10) == Success);
else errorLog();
delay_ms(10);
if (sendCommand("AT+CPIN?\r\n", "READY", 1000, 10) == Success);
else errorLog();
delay_ms(10);
if (sendCommand("AT+CREG?\r\n", ",1", 1000, 10) == Success);
else
{
delay_ms(10);
if (sendCommand("AT+CREG?\r\n", ",5", 1000, 10) == Success);
else errorLog();
}
if (sendCommand("AT+QGNSSC?\r\n", "+QGNSSC: 1", 1000, 1) == Success);
else if (sendCommand("AT+QGNSSC=1\r\n", "OK\r\n", 100, 10) == Success);
else errorLog();
if (sendCommand("AT+QICLOSE\r\n", "\r\n", 5000, 10) == Success);
else errorLog();
delay_ms(100);
if (sendCommand("AT+QIDEACT\r\n", "\r\n", 5000, 10) == Success);
else errorLog();
while(1)
{
if (sendCommand("AT+QGNSSRD=\"NMEA/RMC\"\r\n", "OK\r\n", 2000, 10) == Success);
else errorLog();
Save_Data.isGetData = true;
memset(Save_Data.GPS_Buffer, 0, 120); //清空
memcpy(Save_Data.GPS_Buffer, Rec_Buf, point1);
parseGpsBuffer();
printGpsBuffer();
delay_ms(4000);
}
}
void sendDataToServer()
{
xdata char send_buf[100] = {0};
memset(send_buf, 0, 100); //清空
if(s2==0)
{
if(flagConnect == 0)
{
flagConnect = 1;
strcpy(send_buf, "AT+QIOPEN=\"TCP\",\"");
strcat(send_buf, TCPServer);
strcat(send_buf, "\",\"");
strcat(send_buf, Port2);
strcat(send_buf, "\"\r\n");
if (sendCommand(send_buf, "CONNECT OK", 10000, 5) == Success);
else errorLog();
delay_ms(100);
}
//发送数据
if (sendCommand("AT+QISEND\r\n", ">", 3000, 5) == Success);
else errorLog();
delay_ms(1000);
memset(send_buf, 0, 100); //清空
{sprintf(send_buf,"%s N,%s E ID01 SOS\r\n",
Save_Data.latitude,Save_Data.longitude );}
Init_LCD1602();
LCD1602_write_com(0x80);
LCD1602_write_word("Message has ");
LCD1602_write_com(0x80+0x40);
LCD1602_write_word(" been sent ");
if (sendCommand(send_buf, send_buf, 3000, 1) == Success);
else errorLog();
delay_ms(10);
SendData(0x1a);//发送数据
}
else
{
if(flagConnect == 0)
{
flagConnect = 1;
strcpy(send_buf, "AT+QIOPEN=\"TCP\",\"");
strcat(send_buf, TCPServer);
strcat(send_buf, "\",\"");
strcat(send_buf, Port);
strcat(send_buf, "\"\r\n");
if (sendCommand(send_buf, "CONNECT OK", 10000, 5) == Success);
else errorLog();
delay_ms(100);
}
//发送数据
if (sendCommand("AT+QISEND\r\n", ">", 3000, 5) == Success);
else errorLog();
delay_ms(1000);
memset(send_buf, 0, 100); //清空
{sprintf(send_buf,"%s N,%s E ID01\r\n",
Save_Data.latitude,Save_Data.longitude); }
if (sendCommand(send_buf, send_buf, 3000, 1) == Success);
else errorLog();
delay_ms(10);
SendData(0x1a);}//发送数据
// if (sendCommand("AT+QICLOSE\r\n", "OK\r\n", 5000, 10) == Success);
// else errorLog();
// delay_ms(100);
//
// if (sendCommand("AT+QIDEACT\r\n", "OK\r\n", 5000, 10) == Success);
// else errorLog();
}
void errorLog()
{
while (1)
{
if (sendCommand("AT\r\n", "OK\r\n", 100, 10) == Success)
{
soft_reset();
}
delay_ms(200);
}
}
void soft_reset(void) //制造重启命令
{
((void (code *) (void)) 0x0000) ();
}
unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry)
{
unsigned char n;
CLR_Buf();
for (n = 0; n < Retry; n++)
{
SendString(Command); //发送GPRS指令
Time_Cont = 0;
while (Time_Cont < Timeout)
{
delay_ms(100);
Time_Cont += 100;
if (strstr(Rec_Buf, Response) != NULL)
{
return Success;
}
}
Time_Cont = 0;
}
CLR_Buf();
return Failure;
}
void parseGpsBuffer()
{
char *subString;
char *subStringNext;
char i = 0;
if (Save_Data.isGetData)
{
Save_Data.isGetData = false;
// SendString("**************\r\n");
// SendString(Save_Data.GPS_Buffer);
for (i = 0 ; i <= 6 ; i++)
{
if (i == 0)
{
if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
errorLog(); //解析错误
}
else
{
subString++;
if ((subStringNext = strstr(subString, ",")) != NULL)
{
char usefullBuffer[2];
switch(i)
{
// case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break; //获取UTC时间
case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break;
case 3:
memset(Save_Data.latitude, 0, latitude_Length);
memcpy(Save_Data.latitude, subString, subStringNext - subString);break; //获取纬度信息
case 4:
memset(Save_Data.N_S, 0, N_S_Length);
memcpy(Save_Data.N_S, subString, subStringNext - subString);break; //获取N/S
case 5:
memset(Save_Data.longitude, 0, longitude_Length);
memcpy(Save_Data.longitude, subString, subStringNext - subString);break; //获取经度信息
case 6:
memset(Save_Data.E_W, 0, E_W_Length);
memcpy(Save_Data.E_W, subString, subStringNext - subString);break; //获取E/W
default:break;
}
subString = subStringNext;
Save_Data.isParseData = true;
if(usefullBuffer[0] == 'A')
Save_Data.isUsefull = true;
else if(usefullBuffer[0] == 'V')
Save_Data.isUsefull = false;
}
else
{
errorLog(); //解析错误
}
}
}
}
}
void printGpsBuffer()
{
if (Save_Data.isParseData)
{
Save_Data.isParseData = false;
// SendString("Save_Data.UTCTime = ");
// SendString(Save_Data.UTCTime);
// SendString("\r\n");
if(Save_Data.isUsefull)
{
// Save_Data.isUsefull = false;
// SendString("Save_Data.latitude = ");
// SendString(Save_Data.latitude);
// SendString("\r\n");
//
//
// SendString("Save_Data.N_S = ");
// SendString(Save_Data.N_S);
// SendString("\r\n");
//
// SendString("Save_Data.longitude = ");
// SendString(Save_Data.longitude);
// SendString("\r\n");
//
// SendString("Save_Data.E_W = ");
// SendString(Save_Data.E_W);
// SendString("\r\n");
LCD1602_write_com(0x80);
LCD1602_write_word(Save_Data.latitude);
LCD1602_write_data(' ');
LCD1602_write_word(Save_Data.N_S);
LCD1602_write_word(" ");
LCD1602_write_com(0x80+0x40);
LCD1602_write_word(Save_Data.longitude);
LCD1602_write_data(' ');
LCD1602_write_word(Save_Data.E_W);
LCD1602_write_word(" ");
sendDataToServer();
}
else
{
// SendString("GPS DATA is not usefull!\r\n");
LCD1602_write_com(0x80);
LCD1602_write_word("GPS DATA is ");
LCD1602_write_com(0x80+0x40);
LCD1602_write_word(" not usefull ");
}
}
}
void clrStruct()
{
Save_Data.isGetData = false;
Save_Data.isParseData = false;
Save_Data.isUsefull = false;
memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length); //清空
// memset(Save_Data.UTCTime, 0, UTCTime_Length);
memset(Save_Data.latitude, 0, latitude_Length);
memset(Save_Data.N_S, 0, N_S_Length);
memset(Save_Data.longitude, 0, longitude_Length);
memset(Save_Data.E_W, 0, E_W_Length);
}
//****************************************************
//MS延时函数
//****************************************************
void delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
上一篇:基于STM32的无线视频传输系统
下一篇:急求一名蓝牙工程师, 用CSR8675 或者 CSR8670 模块写一个固件!