gprs.c
资源名称:gprs.rar [点击查看]
上传用户:rjlighting
上传日期:2022-05-27
资源大小:2k
文件大小:4k
源码类别:
手机短信编程
开发平台:
Visual C++
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <string.h>
- #define GPRSTTY "/dev/ttyS1"
- #define BUFSIZE 500
- #define ATREADTIMES 5
- #define ATTRYTIMES 5
- void setTermios(struct termios * pNewtio, short uBaudRate)
- {
- bzero(pNewtio, sizeof(struct termios)); /* clear struct for new portsettings */
- pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL;
- pNewtio->c_iflag = IGNPAR;
- pNewtio->c_oflag = 0;
- pNewtio->c_lflag = 0; //non ICANON
- /*
- initialize all control characters
- we don't need them here
- */
- pNewtio->c_cc[VINTR] = 0; /* Ctrl-c */
- pNewtio->c_cc[VQUIT] = 0; /* Ctrl- */
- pNewtio->c_cc[VERASE] = 0; /* del */
- pNewtio->c_cc[VKILL] = 0; /* @ */
- pNewtio->c_cc[VEOF] = 4; /* Ctrl-d */
- pNewtio->c_cc[VTIME] = 5; /* inter-character timer, timeout
- VTIME*0.1 */
- pNewtio->c_cc[VMIN] = 0; /* blocking read until VMIN character
- arrives */
- pNewtio->c_cc[VSWTC] = 0; /* ' ' */
- pNewtio->c_cc[VSTART] = 0; /* Ctrl-q */
- pNewtio->c_cc[VSTOP] = 0; /* Ctrl-s */
- pNewtio->c_cc[VSUSP] = 0; /* Ctrl-z */
- pNewtio->c_cc[VEOL] = 0; /* ' ' */
- pNewtio->c_cc[VREPRINT] = 0; /* Ctrl-r */
- pNewtio->c_cc[VDISCARD] = 0; /* Ctrl-u */
- pNewtio->c_cc[VWERASE] = 0; /* Ctrl-w */
- pNewtio->c_cc[VLNEXT] = 0; /* Ctrl-v */
- pNewtio->c_cc[VEOL2] = 0; /* ' ' */
- }
- int sendSMS(const char * strAlarmMobile, const char * strAlarmMessage)
- {
- int fd,nTotal,i;
- struct termios oldtio,newtio;
- char strATResult[BUFSIZE];
- char strAT[BUFSIZE];
- struct timeval tv;
- //1、打开 GPRS Modem 连接的串口,进行相关设置,如波特率等,保存原有串口设置;
- if ( (fd = open(GPRSTTY,O_RDWR | O_NOCTTY ))<0){
- printf("Can't Open Serial Port!GPRSTTYn");
- return -1;
- }
- else{
- tcgetattr(fd,&oldtio); /* save current serial port settings */
- setTermios(&newtio,B9600);
- tcflush(fd, TCIFLUSH);
- tcsetattr(fd,TCSANOW,&newtio);
- sprintf(strAT,"at+cmgf=1%c%c",0x0d,0x0a);
- nTotal=0;
- while (ATComUntilStr(fd,strAT,"OK",strATResult,
- ATREADTIMES)!= 0) {
- if ((nTotal++)<ATTRYTIMES)
- continue;
- else
- goto sendSMSEND;
- }
- /* sprintf(strAT,"at+csca="%s"%c%c",strSMSServer,0x0d,0x0a);
- nTotal=0;
- while (ATComUntilStr(fd,strAT,"OK",
- strATResult,ATREADTIMES)!= 0) {
- if ((nTotal++)<ATTRYTIMES)
- continue;
- else
- goto sendSMSEND;
- }*/
- //send message
- i=0;
- while (1){
- sprintf(strAT,"at+cmgs="%s"%c%c",strAlarmMobile,0x0d,0x0a);
- nTotal=0;
- while (ATComUntilStr(fd,strAT,">",strATResult,ATREADTIMES)!= 0){
- if ((nTotal++)<ATTRYTIMES)
- continue;
- else
- goto sendSMSEND;
- }
- sprintf(strAT,"%s%c",strAlarmMessage,0x1a);
- if (ATComUntilStr(fd,strAT,"OK" ,strATResult,ATREADTIMES*3)!= 0){
- if ((i++)<ATTRYTIMES)
- continue;
- else
- goto sendSMSEND;
- }
- else
- break;
- }
- sendSMSEND:
- //3、恢复原有的串口设置。
- tcsetattr(fd,TCSANOW,&oldtio);
- close(fd);
- return 0;
- }
- }
- int ATComUntilStr(int fd, const char * strATCommand, const char * strPrompt,
- char * strResult, int nATReadTimes)
- {
- int nRes,ch,nTotal;
- char strAT[BUFSIZE];
- write(fd,strATCommand,strlen(strATCommand));
- nTotal=0;
- nRes=0;
- ch=0;
- while (1)
- {
- nRes = read(fd,strAT,BUFSIZE-nTotal);
- if (nRes>0){
- strAT[nRes]=0;
- memcpy(strResult+nTotal,strAT,nRes);
- nTotal+=nRes;
- ch=0;
- }
- else {
- if (ch++==nATReadTimes)
- break;
- }
- }
- strResult[nTotal]=0;
- //check if OK
- if (strstr(strResult,strPrompt)!=NULL)
- return 0;
- else
- return -1;
- }
- int main(int argc, char* argv[])
- {
- char* telno="136********"; //自己填号码
- char* sm="alarm!!";
- int ret;
- ret=sendSMS(telno,sm);
- return ret;
- }