源码1:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<termios.h>
#include<signal.h>
#include<fcntl.h>
#include<semaphore.h>
#include<sys/sem.h>
#include<pthread.h>
#include<locale.h>
#define SIZE 500
pthread_t pth1,pth2;
int fd_gprs = 0;
int fd_gps = 0;
int fd_read = 0;
char * gga_start;
char * gga_end;
char * rmc_start;
char * rmc_end;
char * buf;
char * GGA;
char * RMC;
char msg_gps[100] = {'\0'};
//pthread_mutex_t mp=PTHREAD_MUTEX_INITIALIZER;
//signal function
void handler(int sig);
//pthread 1 (send message)
void * pthread_send(void * arg);
//pthread 2 (recive message)
void * pthread_recive(void * arg);
//exchange parity
char * change(char a[]);
//change chinese to PDU
int utf_unicode(char * output,char * input,int x);
//change PDU to chinese
char * unicode_utf8(char ch[]);
char * get_gps_info(char * gga,char * rmc);
int main()
{
struct termios old_gprs;
struct termios new_gprs;
struct termios old_gps;
struct termios new_gps;
int flag_getattr_gprs = 0;
int flag_setattr_gprs = 0;
int flag_getattr_gps = 0;
int flag_setattr_gps = 0;
// pthread_mutex_init(&mp,NULL);
signal(SIGALRM,handler);
//open GPRS
fd_gprs = open("/dev/ttyUSB0",O_NONBLOCK|O_RDWR);
if(fd_gprs < 0)
{
perror("open GPRS error");
return 1;
}
//open GPS
fd_gps = open("/dev/ttyUSB1",O_RDONLY);
if(fd_gps < 0)
{
perror("open GPS error");
return 1;
}
//set attribute of serial GPRS
flag_getattr_gprs = tcgetattr(fd_gprs,&old_gprs);
if(flag_getattr_gprs != 0)
{
perror("get attribute of serial GPRS error");
return 1;
}
new_gprs = old_gprs;
cfsetispeed(&new_gprs,B9600);
cfsetospeed(&new_gprs,B9600);
new_gprs.c_cflag&=~CSIZE;
new_gprs.c_cflag|=CS8;
new_gprs.c_cflag&=~CSTOPB;
new_gprs.c_cflag&=~PARENB;
flag_setattr_gprs=tcsetattr(fd_gprs,TCSANOW,&new_gprs);
if(flag_setattr_gprs != 0)
{
perror("set attribute of serial GPRS error");
return 1;
}
//set attribute of serial GPS
flag_getattr_gps = tcgetattr(fd_gps,&old_gps);
if(flag_getattr_gps != 0)
{
perror("get attribute of serial GPS error");
return 1;
}
new_gps = old_gps;
cfsetispeed(&new_gps,B4800);
cfsetospeed(&new_gps,B4800);
new_gps.c_cflag&=~CSIZE;
new_gps.c_cflag|=CS8;
new_gps.c_cflag&=~CSTOPB;
new_gps.c_cflag&=~PARENB;
flag_setattr_gps=tcsetattr(fd_gps,TCSANOW,&new_gps);
if(flag_setattr_gps != 0)
{
perror("set attribute of serial GPRS error");
return 1;
}
//create pthread(recive)
pthread_create(&pth2,NULL,pthread_recive,NULL);
raise(SIGALRM);
//wait pthread
pthread_join(pth2,NULL);
//pthread_mutex_destroy(&mp);
//close file
close(fd_gprs);
close(fd_gps);
return 0;
}
//create pth1
void handler(int sig)
{
if(sig == SIGALRM)
{
pthread_create(&pth1,NULL,pthread_send,NULL);
pthread_join(pth1,NULL);
}
alarm(90);
}
//send message
void * pthread_send(void * arg)
{
// pthread_mutex_lock(&mp);
buf = (char *)malloc(SIZE * (sizeof(char)));
if(NULL == buf)
{
printf("apply buf_memory error\n");
return ;
}
GGA = (char *)malloc(100 * (sizeof(char)));
if(NULL == GGA)
{
printf("apply GGA_memory error\n");
return ;
}
RMC = (char *)malloc(100 * (sizeof(char)));
if(NULL == RMC)
{
printf("apply RMC_memory error\n");
return ;
}
char PDU[400] = {'\0'};
char core_pre[10] = "089168"; //中心号码前缀
char core_tle_num[20] = "13800907500";//"13800290500"; //中心号码
char usr_pre[12] = "11000D9168"; //用户前缀
char usr_tle_num[20] = "18629535392"; //用户号码
char form[5] = "0008"; //格式
char msg_len[10];//信息长度
char msg[70]; //信息内容
char *output = (char *)malloc(300);
char * gps_msg = (char *)malloc(300 * sizeof(char));
//读取GPS信息
while(1)
{
//read information from serial
memset(buf,'\0',SIZE);
tcflush(fd_gps,TCIOFLUSH);
fd_read = 0;
while(fd_read < SIZE)
{
fd_read += read(fd_gps,buf + fd_read,SIZE - fd_read);
}
gga_start = strstr(buf,"GPGGA");
if(NULL == gga_start)
{
continue;
}
gga_end = strchr(gga_start + 1,'*');
if(NULL == gga_end)
{
continue;
}
rmc_start = strstr(buf,"GPRMC");
if(NULL == rmc_start)
{
continue;
}
rmc_end = strchr(rmc_start + 1,'*');
if(NULL == rmc_end)
{
continue;
}
memset(GGA,'\0',100);
memset(RMC,'\0',100);
memcpy(GGA,gga_start,gga_end - gga_start);
memcpy(RMC,rmc_start,rmc_end - rmc_start);
gps_msg = get_gps_info(GGA,RMC);
if(gps_msg != NULL)
{
break;
}
}
strcat(PDU,core_pre);
// printf("请输入中心号码:\n");
// printf("%s\n",core_tle_num);
change(core_tle_num);
strcat(PDU,core_tle_num);
strcat(PDU,usr_pre);
// printf("请输入用户号码:\n");
//gets(usr_tle_num);
// printf("%s\n",usr_tle_num);
change(usr_tle_num);
strcat(PDU,usr_tle_num);
strcat(PDU,form);
//printf("请输入新信息内容:\n");
// printf("%s\n",gps_msg);
int len = 0;
// char *input =(char *)malloc(70);
//gets(input);
len = utf_unicode(output,gps_msg,300);
printf("%s\n",output);
// printf("%d\n",sizeof(gps_msg));
sprintf(msg_len,"%04X",len/2);
strcat(PDU,msg_len);
strcat(PDU,output);
char com1[20] = "at+cmgf=0";
com1[strlen(com1)] = '\r';
com1[strlen(com1)] = '\0';
write(fd_gprs,com1,strlen(com1));
sleep(1);
char com2[20] = "at+cmgs=";
printf("%d\n",strlen(PDU));
sprintf(msg_len,"%d",(strlen(PDU)-18)/2);
strcat(com2,msg_len);
com2[strlen(com2)] = '\r';
com2[strlen(com2)] = '\0';
write(fd_gprs,com2,strlen(com2));
sleep(1);
printf("%s\n",PDU);
write(fd_gprs,PDU,strlen(PDU));
sleep(1);
char com3[4] = {'\0'};
com3[0] = 0x1a;
com3[1] = '\r';
write(fd_gprs,com3,strlen(com3));
sleep(1);
if(NULL != buf)
{
free(buf);
buf=NULL;
}
if(NULL != GGA)
{
free(GGA);
GGA=NULL;
}
if(NULL != RMC)
{
free(RMC);
RMC=NULL;
}
// pthread_mutex_unlock(&mp);
pthread_exit(&pth1);
}
//recive message
void * pthread_recive(void * arg)
{
//pthread_mutex_lock(&mp);
while(1)
{
char * propt;
char * point;
struct TIME
{
char year[3];
char mon[3];
char day[3];
char hour[3];
char min[3];
char sec[3];
char minsec[3];
}tm={0,0,0,0,0,0,0};
char position[4] = {'\0'};
int i;
char com[20] = "at+cmgr=";
char a[10];
char core_tle_num[20] = {'\0'}; //中心号码
char usr_tle_num[20] = {'\0'}; //用户号码
char time[15] = {'\0'};
char msg[70]; //信息内容
char * utf;
char ch[256] = {'\0'};
printf("Wait...\n");
//sleep(1);
memset(ch,'\0',256);
read(fd_gprs,ch,255);
printf("%s\n",ch);
sleep(1);
propt = strstr(ch,"+CMTI");
if(NULL == propt)
{
continue;
}
point = strchr(ch,',');
if(NULL == point)
{
continue;
}
memcpy(position,point+1,ch+strlen(ch)-point);
strcat(com,position);
com[strlen(com)] = '\r';
com[strlen(com)] = '\0';
write(fd_gprs,com,strlen(com));
sleep(1);
memset(ch,'\0',256);
read(fd_gprs,ch,sizeof(ch));
printf("%s\n",ch);
sleep(1);
propt = strstr(ch,"+CMGR");
if(NULL == propt)
{
continue;
}
point = strstr(propt,"0891");
if(NULL == point)
{
continue;
}
sscanf(point,"%6s%12s%8s%12s%4s%14s%2s%s",a,core_tle_num,a,usr_tle_num,a,time,a,msg);
// sscanf(ch+26,"%12s%4s%14s%2s%s",usr_tle_num,a,time,a,msg);
change(usr_tle_num);
printf("用户号码:\n");
for(i = 0;i < 11;i++)
{
printf("%c",usr_tle_num[i]);
}
printf("\n");
change(time);
sscanf(time,"%2s%2s%2s%2s%2s%2s%2s",tm.year,tm.mon,tm.day,tm.hour,tm.min,tm.sec,tm.minsec);
printf("时间:\n");
printf("%s年%s月%s日%s时%s分%s秒%s毫秒\n",tm.year,tm.mon,tm.day,tm.hour,tm.min,tm.sec,tm.minsec);
utf = unicode_utf8(msg);
printf("信息内容:\n");
printf("%s\n",utf);
sleep(2);
if(strcmp(usr_tle_num,"18629535392") == 0)
{
raise(SIGALRM);
sleep(1);
}
}
//pthread_mutex_unlock(&mp);
}
//exchange parity
char * change(char a[])
{
int len;
int temp;
int i = 0;
len = strlen(a);
if(len%2 == 0)
{
for(i = 0;i < len;i = i+2)
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
else
{
a[len] = 'F';
for(i = 0;i <= len;i = i+2)
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
return a;
}
//change chinese to PDU
int utf_unicode(char * output,char * input,int x)
{
int i;
int n;
wchar_t wt[300] = L"\0";
setlocale(LC_ALL,"zh_CN.UTF-8");
n = mbstowcs(wt,input,x);
for(i = 0;i < n;i++)
{
sprintf(output+4*i,"%04X",wt[i]);
}
return strlen(output);
}
//change PDU to chinese
char * unicode_utf8(char ch[])
{
int x=0;
int n;
int unicode_len=strlen(ch);
wchar_t wch[70]=L"\0";
char *utf=(char *)malloc(70);
setlocale(LC_ALL,"zh_CN.UTF-8");
for(x=0;x<unicode_len-1;x++)
{
sscanf(ch+4*x,"%04X",wch+x);
}
*(wch+x)='\0';
n=wcstombs(utf,wch,unicode_len);
*(utf+x)='\0';
return utf;
}
char * get_gps_info(char * gga,char * rmc)
{
struct GPS
{
int hour;
int minite;
int second;
float longitude; //经度
char tag_longitude;
float latitude; //纬度
char tag_latitude;
float high;
float speed;
int year;
int month;
int day;
char flag;
}gps={0,0,0,0,'\0',0,'\0',0,0,0,0,0,'\0'};
char tag[20] = {'\0'};
float a = 0;
sscanf(gga,"GPGGA,%2d%2d%2d%4s,%f,%c,%f,%c,%1s,%2s,%3s,%f,%1s,%f,M,,0000*4F",&gps.hour,&gps.minite,&gps.second,tag,&gps.longitude,&gps.tag_longitude,&gps.latitude,&gps.tag_latitude,tag,tag,tag,&gps.high,tag,&gps.speed);
//速度小于0的默认为0
if(gps.speed < 0)
{
gps.speed = 0;
}
sscanf(rmc,"GPRMC,%f,%c,%f,%1s,%f,%1s,%f,%f,%2d%2d%2d,,,A*70",&a,&gps.flag,&a,tag,&a,tag,&a,&a,&gps.day,&gps.month,&gps.year);
#if 1//时间转换
gps.year += 2000;
gps.hour += 8;
if(gps.hour > 24)
{
gps.hour -= 24;
gps.day++;
}
switch(gps.month)
{
case 1:;
case 3:;
case 5:;
case 7:;
case 8:;
case 10:if(gps.day>31)
{
gps.day-=31;
gps.month++;
}
break;
case 2:if(((gps.year%4==0)&&(gps.year%100!=0))||(gps.year%400==0))
{
if(gps.day>29)
{
gps.day-=29;
gps.month++;
}
}
else
{
if(gps.day>28)
{
gps.day-=28;
gps.month++;
}
}
break;
case 4:;
case 6:;
case 9:;
case 11:if(gps.day>30)
{
gps.day-=30;
gps.month++;
}
break;
case 12:if(gps.day>31)
{
gps.day-=31;
gps.month++;
gps.month-=12;
gps.year++;
}
break;
}
#endif
//判断数据是否有效
if(gps.flag == 'A')
{
// printf("数据有效!\n");
// printf("时间:%d:%d:%d\n经度:%.2f 半球:%c\n纬度:%.2f 半球:%c\n高度:%.2f\n速度:%.2f\n日期:%d年%d月%d日\n",gps.hour,gps.minite,gps.second,gps.longitude,gps.tag_longitude,gps.latitude,gps.tag_latitude,gps.high,gps.speed,gps.year,gps.month,gps.day);
sprintf(msg_gps,"%d:%d:%d %d-%d-%d 经度:%.2f 半球:%c 纬度:%.2f 半球:%c速度:%.2f",gps.hour,gps.minite,gps.second,gps.year,gps.month,gps.day,gps.longitude,gps.tag_longitude,gps.latitude,gps.tag_latitude,gps.speed);
return msg_gps;
}
if(gps.flag == 'V');
{
// printf("数据无效!\n");
//printf("时间:%d:%d:%d\n日期:%d年%d月%d日\n",gps.hour,gps.minite,gps.second,gps.year,gps.month,gps.day);
return NULL;
}
}
源码2:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<signal.h>
#include<pthread.h>
#include<termios.h>
#include<locale.h>
#include<string.h>
#include<sys/types.h>
void getgps(int a);//alarm函数
char * gpsprintf(char * rmc,char *gga);//gps读取
int utf_unicode(char *output,char* input,int x);//汉字转换内容
void zhuanhuan(char*a);//奇偶位交换
void * thread1(void * arg);
char * zuhe(char*pp);
void send(char * pdu);
pthread_mutex_t mp=PTHREAD_MUTEX_INITIALIZER;
int gprsid,gpsid;
int j=0;
int main()
{
pthread_mutex_init(&mp,NULL);
signal(SIGALRM,getgps);
//signal(SIGUSR1,getmsg);
pthread_t thd;
//打开gprs
gprsid=open("/dev/ttyUSB2",O_RDWR|O_NONBLOCK);
if(gprsid<0)
{
perror("open gprs error");
return 1;
}
//设置属性
struct termios tgprs;
tcgetattr(gprsid,&tgprs);
cfsetspeed(&tgprs,B9600);
tgprs.c_cflag &= ~CSIZE;
tgprs.c_cflag |= CS8;
tgprs.c_cflag &= ~CSTOPB;
tgprs.c_cflag &= ~PARENB;
tcsetattr(gprsid,TCSANOW,&tgprs);
//打开gps
gpsid=open("/dev/ttyUSB1",O_RDONLY);
if(gpsid<0)
{
perror("open gps error");
return 1;
}
//设置属性
struct termios tgps;
tcgetattr(gpsid,&tgps);
cfsetspeed(&tgps,B4800);
tgps.c_cflag &= ~CSIZE;
tgps.c_cflag |= CS8;
tgps.c_cflag &= ~CSTOPB;
tgps.c_cflag &= ~PARENB;
tcsetattr(gpsid,TCSANOW,&tgps);
write(gprsid,"at+cmgf=0\r",10);
pthread_create(&thd,NULL,thread1,NULL); //创建线程
raise(SIGALRM);
//alarm(60);//300秒后触发alarm信号
pthread_join(thd,NULL);//等待
pthread_mutex_destroy(&mp);
close(gprsid);//关闭
close(gpsid);
}
void getgps(int a)
{
pthread_mutex_lock(&mp);
char buf[300];
int len=0,k=0;
char *pcs,*pce,*pas,*pae;
char *wish;
char *pdu;
char *rmc;
char *gga;
while(1)
{
memset(buf,'\0',300);
setbuf(stdin,NULL);
len=0;
while(len<300)
{
len+=read(gpsid,buf+len,300-len);
}
pcs=strstr(buf,"$GPRMC");
if(pcs==NULL)
{
continue;
}
pce=strchr(pcs+1,'*');
if(pce==NULL)
{
continue;
}
pas=strstr(buf,"$GPGGA");
if(pas==NULL)
{
continue;
}
pae=strchr(pas+1,'*');
if(pae==NULL)
{
continue;
}
rmc=(char*)malloc(pce-buf);
gga=(char*)malloc(pae-buf);
memset(rmc,'\0',pce-buf);
memset(gga,'\0',pae-buf);
strncpy(rmc,pcs,pce-pcs);
strncpy(gga,pas,pae-pas);
if(rmc[18]=='A')
{
wish=gpsprintf(rmc,gga);
if(k!=0)strcat(wish,"刚才GPS信号中断,此短信为检测到信号后第一时间发送。");
}
if(rmc[18]=='V')
{
k=1;
continue;
}
//发送 内容是wish.
printf("wish=%s\n",wish);
pdu=zuhe(wish);
printf("%s\n",pdu);
//pdu出来了
send(pdu); //调用send函数
if(rmc!=NULL)
{
free(rmc);
rmc=NULL;
}
if(gga!=NULL)
{
free(gga);
gga=NULL;
}
if(wish!=NULL)
{
free(wish);
wish=NULL;
}
if(pdu!=NULL)
{
free(pdu);
pdu=NULL;
}
break;
}
alarm(300);
pthread_mutex_unlock(&mp);
}
char* gpsprintf(char * rmc,char *gga)
{
char * xx=(char*)malloc(300);
memset(xx,'\0',300);
char x,we,ns;
float jd,wd,l;
int sudu,gao,i;
char laji[30];
char day[10]="\0";
char jing[20]="\0";
char wei[20]="\0";
char gaoo[20]="\0";
char suduu[20]="\0";
char *p,*q;
sscanf(rmc+18,"%c,%f,%c,%f,%c,%f,%f,%6s",&x,&jd,&ns,&wd,&we,&l,&l,day);
sscanf(gga+18,"%f,%1s,%f,%1s,%1s,%f,%f,%d.%f,M,%d",&l,laji,&l,laji,laji,&l,&l,&gao,&l,&sudu);
if(sudu<0)sudu=0;
sprintf(jing,"%f",jd);//float 转字符串
sprintf(wei,"%f",wd);
sprintf(gaoo,"%d",gao);//int 转字符串
sprintf(suduu,"%d",sudu);
jing[strlen(jing)-2]='\0';
wei[strlen(wei)-2]='\0';
gaoo[strlen(gaoo)]='M';
strcat(suduu,"M/S");
strcpy(xx,"经度:");
strcat(xx,jing);
xx[strlen(xx)]=ns;
strcat(xx,"\n纬度:");
strcat(xx,wei);
xx[strlen(xx)]=we;
strcat(xx," \n高度:");
strcat(xx,gaoo);
strcat(xx," \n速度:");
strcat(xx,suduu);
strcat(xx,"\n一切正常请放心!");
return xx;
}
int utf_unicode(char *output,char* input,int x)//汉字转换函数
{
int i;
int n;
wchar_t wt[300]=L"\0";//定义宽字符型数组
setlocale(LC_ALL,"zh_CN.UTF-8");//本地字符编码
n=mbstowcs(wt,input,x);//将汉字转换成宽字符类型
for(i=0;i<n;i++)
sprintf(output+4*i,"%04X",wt[i]);//将宽字符型转换成十六进制
return strlen(output);
}
void zhuanhuan(char*a)//奇偶位交换
{
int ret=0;
int i=0;
char x;
ret=strlen(a);
if(ret%2!=0)
{
a[ret]='F';
}
ret=strlen(a);
for(i=0;i<ret;i=i+2)
{
x=a[i];
a[i]=a[i+1];
a[i+1]=x;
}
}
char* zuhe(char*pp)
{
char c[10]="\0";
char cc[30]="0891683108808905F011000D9168";
char s[15]="18191255873";
char n[300]="\0";
char *output=(char*)malloc(300);
memset(output,'\0',300);
char q[4]="\0";
int ret=0,x;
char *z=(char*)malloc(300);
memset(z,'\0',300);
zhuanhuan(s);
strcat(s,"0008");
strncpy(n,pp,290);
x=strlen(n);
ret=utf_unicode(output,n,x);
printf("%d\n",ret);
sprintf(q,"%04X",ret/2);
strcat(z,cc);
strcat(z,s);
strcat(z,q);
strcat(z,output);
free(output);
return z;
}
void * thread1(void * arg)
{
char du[30]="\0";
char dd[300]="\0";
int i=0,ret=0;
char cmid[5]="\0";
char dakai[20]="\0";
char * bf;
char * cmti;
char * cmtie;
char haoma[20]="\0";
while(1)
{
pthread_mutex_lock(&mp);
pthread_mutex_unlock(&mp);
memset(du,'\0',30);
setbuf(stdin,NULL);
read(gprsid,du,29);
printf("du=%s\n",du);
sleep(1);
cmti=strstr(du,"+CMTI");
if(cmti==NULL)
{
continue;
}
cmtie=strstr(cmti+1,"SM");
if(cmtie==NULL)
{
continue;
}
//读到+CMTI: "SM",x
ret=strlen(du);//把后面的数字,放进at+cmgr中
for(i=0;i<ret-14;i++)
{
cmid[i]=du[14+i];
}
strcpy(dakai,"at+cmgr=");
strcat(dakai,cmid);
printf("dakai=%s\n",dakai);
ret=strlen(dakai);
write(gprsid,dakai,ret);//打开新短信
sleep(1);
read(gprsid,dd,299);
sleep(1);
cmti=strstr(dd,"+CMGR");
if(cmti==NULL)
{
continue;
}
cmtie=strstr(cmti+1,"0891");
if(cmtie==NULL)
{
continue;
}
memcpy(haoma,cmtie+26,12);
haoma[12]='\0';
zhuanhuan(haoma);
haoma[11]='\0';
printf("haoma=%s\n",haoma);
if(strcmp(haoma,"18191255873")==0)
{
raise(SIGALRM);
sleep(3);
}
}
}
void send(char * pdu)
{
int len=0;
write(gprsid,"at+cmgf=0\r",10);
sleep(1);
len=(strlen(pdu)-18)/2;
char length[15]="at+cmgs=";
sprintf(length+8,"%d",len);
strcat(length,"\r");
write(gprsid,length,strlen(length));
sleep(1);
strcat(pdu,"\x1A\r");
write(gprsid,pdu,strlen(pdu));
sleep(1);
}