经过阅读资料--了解了不少用GPRS模块发短信的过程,但很多是理论,并没有实际性的代码;
经过2天的努力,终于把GPRS发送中文短信成功!!
本人通过s3c2410开发板的串口1与wavecom Q2406A接通,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>
int open_port(int fd,int comport);
int set_opt (int fd, int nSpeed, int nBits, char nEvent, int nStop) ;
int change_number(char * pSrc) ;
int main(void) {
int fd;
int nwrite,nread,i ;
char buff[]="AT\r" ; //AT命令--具体看手册
char red[256] ;
char send[200] ;
char send2[100] ;
char centeraddr[] = "8613800100500"; //北京--短信中心号码
char usraddr[] = "861510104175x"; //用户号码
char head[] = "0891";
char phone[] = "1100";
char pho[] = "0D91" ;
char phoafter[] = "0008000" ;
char firemsg[]="0A5bb691cc7740706bff01" ; //家里着火!
char thiefmsg[]="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("after change,the centeraddr is %s\n",centeraddr);
strcpy(send,head); //head=0891
printf("%s\n",send);
strcat(send,centeraddr);
printf("the send is %s\n%d\n",send,strlen(send));
change_number(usraddr);
printf("after change, the usraddr is %s\n",usraddr);
strcat(phone,pho);
printf("%s\n",phone);
strcat(phone,usraddr);
printf("%s\n",phone);
strcat(phone,phoafter);
printf("the phone 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);
return 0;
}
int open_port(int fd,int comport) {
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
//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);
}
}
else if(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);
}
}
else if(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);
return fd;
}
int set_opt