经过阅读资料--了解了不少用GPRS模块发短信的过程,但很多是理论,并没有实际性的代码;
经过2天的努力,终于把GPRS发送中文短信成功!!
本人通过s3c2410开发板的串口1与wavecomQ2406A接通,s3c2410串口0用于终端调试;
首先要把串口1设置为不是用于终端模式;之后配置串口属性;最后发送短信。(具体看代码吧)!!!!!!
但看代码前请看看我的前一遍GPRS知识,不然你就不明白代码了!(如有问题请留言--)
敬告:严与用于商业用途!抄袭!只用于学习交流之用!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<termios.h>
intopen_port(int fd,int comport);
intset_opt (int fd, int nSpeed, int nBits, char nEvent, int nStop) ;
intchange_number(char * pSrc) ;
intmain(void) {
intfd;
intnwrite,nread,i ;
charbuff[]="AT\r" ; //AT命令--具体看手册
charred[256] ;
charsend[200] ;
charsend2[100] ;
charcenteraddr[] = "8613800100500"; //北京--短信中心号码
charusraddr[] = "861510104175x"; //用户号码
charhead[] = "0891";
charphone[] = "1100";
charpho[] = "0D91" ;
charphoafter[] = "0008000" ;
charfiremsg[]="0A5bb691cc7740706bff01" ; //家里着火!
charthiefmsg[]="0A5c0f50778fdb5c4bff01" ; //小偷进屋
if((fd=open_port(fd,2))<0) {
perror("open_port error");
return -1;
}
if((i=set_opt(fd,115200,8,'N',1))<0){
perror("set_opt error");
return;
}
printf("fd=%d\n",fd);
//send=head+phone+msg+\x1a
change_number(centeraddr);
printf("afterchange,the centeraddr is %s\n",centeraddr);
strcpy(send,head); //head=0891
printf("%s\n",send);
strcat(send,centeraddr);
printf("thesend is %s\n%d\n",send,strlen(send));
change_number(usraddr);
printf("afterchange, the usraddr is %s\n",usraddr);
strcat(phone,pho);
printf("%s\n",phone);
strcat(phone,usraddr);
printf("%s\n",phone);
strcat(phone,phoafter);
printf("thephone is %20s\n",phone);
strcat(send,phone);
printf("%s\n",send);
strcat(send,"\x1a");
printf("%20s\n%d\n",send,strlen(send));
write(fd,"AT\r",3);
sleep(2);
nread=read(fd,red,256);//读串口
if(strstr(red,"OK")== NULL)
printf("the com1 is faule!!\n");
printf("nread=%d,%s\n",nread,red);
write(fd,"AT+CMGF=0\r",10);
sleep(2);
nread=read(fd,red,256);//读串口
//if(strstr(red,"OK")== NULL)
//printf("the select PDU is faule!!\n");
printf("nread=%d,%s\n",nread,red);
write(fd,"AT+CMGS=27\r",11);
sleep(1);
nread=read(fd,red,256);//读串口
printf("%s%d PDU CMD\n",red,nread);
while(!strstr(red,"\r\n>")){
read(fd,red,256) ;
}
write(fd, send, strlen(send));
read(fd,red,256);
printf("%s\n",red);
sleep(3);
close(fd);
return0;
}
intopen_port(int fd,int comport) {
char*dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
longvdisable;
//O_NOCTTY标志用于通知Linux系统,这个程序不会成为对应这个端口的控制终端。
//O_NDELAY标志通知Linux系统,这个程序不关心DCD信号线所处的状态(端口的另一端是否激活或者停止)。
if(comport==1){ //串口1
fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd) {
perror("Can't Open Serial Port");
return(-1);
}
}
elseif(comport==2) { //串口2
fd=open("/dev/s3c2410_serial1",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd) {
perror("Can't Open Serial Port");
return(-1);
}
}
elseif(comport==3) { //串口3
fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd) {
perror("Can't Open Serial Port");
return(-1);
}
}
if(fcntl(fd,F_SETFL,0)<0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a termina device\n");
else
printf("isatty success!\n");
printf("fd=%d\n",fd);
returnfd;
经过2天的努力,终于把GPRS发送中文短信成功!!
本人通过s3c2410开发板的串口1与wavecom
首先要把串口1设置为不是用于终端模式;之后配置串口属性;最后发送短信。(具体看代码吧)!!!!!!
但看代码前请看看我的前一遍GPRS知识,不然你就不明白代码了!(如有问题请留言
敬告:严与用于商业用途!抄袭!只用于学习交流之用!!
#include
#include
#include
#include
#include
#include
#include
#include
#include
int
int
int
int
int
int
char
char
char
char
char
char
char
char
char
char
char
char
}
if((i=set_opt(fd,115200,8,'N',1))<0)
}
printf("fd=%d\n",fd);
//send=head+phone+msg+\x1a
change_number(centeraddr);
printf("after
strcpy(send,head);
printf("%s\n",send);
strcat(send,centeraddr);
printf("the
change_number(usraddr);
printf("after
strcat(phone,pho);
printf("%s\n",phone);
strcat(phone,usraddr);
printf("%s\n",phone);
strcat(phone,phoafter);
printf("the
strcat(send,phone);
printf("%s\n",send);
strcat(send,"\x1a");
printf("%20s\n%d\n",send,strlen(send));
write(fd,"AT\r",3);
sleep(2);
nread=read(fd,red,256);//读串口
if(strstr(red,"OK")
printf("nread=%d,%s\n",nread,red);
write(fd,"AT+CMGF=0\r",10)
sleep(2)
nread=read(fd,red,256);//读串口
//if(strstr(red,"OK")
//
printf("nread=%d,%s\n",nread,red);
write(fd,"AT+CMGS=27\r",11)
sleep(1)
nread=read(fd,red,256);//读串口
printf("%s
while(!strstr(red,"\r\n>"))
}
write
read(fd,red,256);
printf("%s\n",red);
sleep(3);
close(fd);
return
}
int
char
long
//O_NOCTTY标志用于通知Linux系统,这个程序不会成为对应这个端口的控制终端。
//O_NDELAY标志通知Linux系统,这个程序不关心DCD信号线所处的状态(端口的另一端是否激活或者停止)。
if(comport==1)
}
else
}
else
}
if(fcntl(fd,F_SETFL,0)<0)
else
if(isatty(STDIN_FILENO)==0)
else
printf("fd=%d\n",fd);
return