通过gprs发短信的程序,重点在设置串口属性,比如硬件流控,等等。strcat(gsmMessage, "\x1a"); 这一句结束短信内容。串口设置的有关语句如下:
newtio.c_cflag |= CLOCAL | CREAD | CRTSCTS;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0; ////non ICANON
newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 5; /* inter-character timer, timeout VTIME*0.1 */
newtio.c_cc[VMIN] = 0; /* blocking read until VMIN character arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */
int main(void)
{
int fd;
int nread,nwrite,i;
char buff[32];
char reply[128];
char gsmMessage[20] = "Hello!";
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 -1;
}
printf("fd=%d\n",fd);
memset(buff,0,sizeof(buff));
buff[0] = 'A';
buff[1] = 'T';
buff[2] = '\r';
nwrite = write(fd,buff,3);
printf("nwrite=%d,%s\n",nwrite,buff);
memset(reply,0,sizeof(reply));
nread = read(fd,reply,sizeof(reply));
printf("nread=%d,%s\n",nread,reply);
memset(buff,0,sizeof(buff));
strcpy(buff,"AT+CMGF=1");
strcat(buff,"\r");
nwrite = write(fd,buff,10);
printf("nwrite=%d,%s\n",nwrite,buff);
memset(reply,0,sizeof(reply));
nread = read(fd,reply,sizeof(reply));
printf("nread=%d,%s\n",nread,reply);
memset(buff,0,sizeof(buff));
strcpy(buff,"AT+CMGS=");
strcat(buff,"136");
strcat(buff,"\r");
nwrite = write(fd,buff,strlen(buff));
printf("nwrite=%d,%s\n",nwrite,buff);
memset(reply,0,sizeof(reply));
nread = read(fd,reply,sizeof(reply));
printf("nread=%d,%s\n",nread,reply);
strcat(gsmMessage, "\x1a");
nwrite = write(fd,gsmMessage,7);
printf("nwrite=%d,%s\n",nwrite,gsmMessage);
memset(reply,0,sizeof(reply));
nread = read(fd,reply,sizeof(reply));
printf("nread=%d,%s\n",nread,reply);
close(fd);
return 0;
}
阅读(1365) | 评论(0) | 转发(0) |