好吧 我贴个以前写的代码给你看看!!里面有设置串口协议的 反正自己留着也没用给大家看吧 这个是linux C的
- //短信接收端
//2013-11-6
//yang
//完成于2013-11-7
#include
#include
#include
#include
#include
#include
#include
#include
char change(char *p);
int jiema(char *q);
char * unicode_utf8(char *ch);
int utf_unicode(char *output,char input,int x);
int setS0(void)
{
int fd=0,ret=0;
struct termios old,new;
fd = open("/dev/ttyS0",O_RDWR); //U转串
if(fd == -1)
{
perror("open");
//exit(0);
}
int g = tcgetattr(fd,&old);
if(g == -1)
{
printf("COM get fail\n");
//exit(0);
}
new = old;
cfsetispeed(&new,B9600);
cfsetospeed(&new,B9600);
new.c_cflag |= (CLOCAL | CREAD);
new.c_cflag &= ~PARENB;
new.c_cflag &= ~CSTOPB;
new.c_cflag &= ~CSIZE;
new.c_cflag |= CS8;
tcsetattr(fd,TCSANOW,&new);
return fd;
}
int main()
{
//char chr[350]="0891683108200945F7240D91688186929197F5000831116001516423044F60597D4F60597D";
char chr[350]={'\0'};
int a=0,b=0,ret=0;
char ch[350]="",ch1[25]="",ch2[25]="",ch3[25]="";
char *pn=NULL;
int fd=setS0(); //设置端口
char *p=NULL, *q=NULL;
write(fd,"ate0\r",5); //关闭回显
sleep(1);
write(fd,"at+cmgf=0\r",10); //设置读取格式
sleep(1);
tcflush(fd, TCIOFLUSH);
sleep(1);
ret = 0;
while(1)
{
//*
ret=read(fd,ch3+strlen(ch3),20-strlen(ch3));
if(ret <= 0)
continue;
printf("read===[%s]\n",ch3);
if(strstr(ch3,"+cmti:") == NULL)
{
printf("未发现新短信信息\n");
sleep(2);
continue;
}
else
{
p=strchr(ch3,',');
if(p == NULL)
continue;
q = strchr(p, '\r');
if(q == NULL)
q = strchr(p, '\n');
if(q == NULL)
continue;
memset(ch2,'\0',25);
printf("len = %d\n",q-p-1);
memcpy(ch2,p+1,(q-p-1));
write(fd,"at+cmgr=",strlen("at+cmgr="));
write(fd,ch2,strlen(ch2));
write(fd, "\r", 1);
printf("write:===[at+cmgr=%s]\n",ch2);
sleep(1);
memset(chr,'\0',sizeof(chr));
ret = read(fd,chr,350);
printf("read_ret: %d\n", ret);
printf("rcv mesg is [%s]\n",chr);
//*/
//9168
//08
//p = strstr(chr, "9168"); //查找9168
p = strstr(chr, "9168");
if(p == NULL)
{
printf("can fand 9168\n");
exit(0);
}
pn=p-2;
printf("%s\n",pn);
printf("ch2:%s\n",ch2);
memcpy(ch2,pn,2);
sscanf(ch2,"%02X",&a);
a=(a*2-4);
printf("a===%d\n",a);
memset(ch,'\0',350);
memcpy(ch,pn+6,a);
printf("ch====%s\n",ch);
change(ch);
ch[strlen(ch)-1]='\0';
printf("center num==%s\n",ch);
memset(ch2,'\0',25);
memcpy(ch2,pn+8+a,2);
//printf("0d==%s\n",ch);
sscanf(ch2,"%02X",&b);
printf("来源号码长度=%d\n",b);
memset(ch1,'\0',25);
memcpy(ch1,(pn+(14+a)),b);
change(ch1);
ch1[strlen(ch1)-2]='\0';
printf("message is from :%s\n",ch1);
memset(ch1,'\0',25);
memcpy(ch1,(pn+(17+a+b)),14);
puts(ch1);
change(ch1);
int year=0,month=0,day=0,hour=0,mine=0,secort=0;
sscanf(ch1,"%2d%2d%2d%2d%2d%2d",&year,&month,&day,&hour,&mine,&secort);
printf("send messages time is :%02dyear%02dmonth%02dday\t%2d:%2d:%2d\n",year,month,day,hour,mine,secort);
memset(ch,'\0',350);
memcpy(ch,pn+(a+b+17+14),2); //拷贝信息长度16进制
printf("16len===%s\n",ch);
sscanf(ch,"%02X",&a);
printf("rcv message len is: %d bit\n",a);
memset(ch,'\0',350);
memcpy(ch,pn+(a+b+41),350);
printf("ch: [%s]\n", ch);
char *s=unicode_utf8(ch);
printf("rcv message is: [%s]\n",s);
}
}
}
/*___________________________changed_______________________________*/
char change(char *p)
{
int i=0,j=0;
char tem[2]={'\0'};
for(j=0;j<strlen(p)/2;j++)
{
tem[0]=(p+i);
(p+i)=(p+i+1);
*(p+i+1)=tem[0];
i+=2;
}
printf("p==%s\n",p);
}
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指针指向他
*(utf+x)='\0';
puts(utf);
return utf; //返回指针
}