- 论坛徽章:
- 5
|
- int set_sms_text_mode()
- {
- int ret=0;
- int i;
- char *send_buf,recv_buf[BUFFER_SIZE];
- send_buf="AT+CMGF=1\r\0";
- for (i=0;i<RETRY;i++)
- {
- usleep(5000);
- ret=serial_recv(send_buf,strlen(send_buf),recv_buf,5,0,1,0);
- if (ret<0)
- {
- printf("AT+CMGF:SERIAL SEND AND RECEIVE ERROR!\n");
- return SETSMSTEXTMODE_ERR;
- }
- else
- {
- if (check_OK(recv_buf,ret)==0)
- return 0;
- }
- }
- return SETSMSTEXTMODE_ERR;
- }
- int sms_send (unsigned char *sms_des,unsigned char *sms_content)
- {
- int ret=0;
- int i;
- char send_buf[BUFFER_SIZE],recv_buf[BUFFER_SIZE],content_buf[BUFFER_SIZE];
- char *des_head="AT+CMGS=";
- char *des_tail="\r\0";
- char content_tail[2]={0x1a,0x0};
- memcpy (send_buf, des_head, strlen(des_head));
- memcpy (send_buf+strlen(des_head), sms_des, strlen(sms_des));
- memcpy (send_buf+strlen(des_head)+strlen(sms_des),des_tail,strlen(des_tail)+1);
- memcpy (content_buf,sms_content,strlen(sms_content));
- memcpy (content_buf+strlen(sms_content),content_tail,strlen(content_tail)+1);
- for (i=0;i<RETRY;i++)
- {
- usleep(5000);
- while (serial_busy) usleep(1);
- serial_busy=1;
- ret=serial_send_recv(send_buf,strlen(send_buf),content_buf,strlen(content_buf),recv_buf,20,0,5,0);
- serial_busy=0;
- if (ret<0)
- {
- printf("AT+CMGS:SERIAL SEND AND RECEIVE ERROR!\n");
- return SMSSEND_ERR;
- }
- else
- {
- if (check_OK(recv_buf,ret)==0)
- return 0;
- }
- }
- return SMSSEND_ERR;
- }
- int check_cmgl(unsigned char *recv_buf,unsigned int recv_buf_len)
- {
- int i,j,ret;
- unsigned int head=0;
- unsigned int tail=0;
- ret=0;
- i=0;
- while (i<recv_buf_len)
- {
- if ((recv_buf[i]=='+')&&
- (recv_buf[i+1]=='C')&&
- (recv_buf[i+2]=='M')&&
- (recv_buf[i+3]=='G')&&
- (recv_buf[i+4]=='L')&&
- (recv_buf[i+5]==':')&&
- (recv_buf[i+6]==0x20))
- {
- head=i+7;
- for(j=head;j<recv_buf_len;j++)
- {
- if(recv_buf[j]==',')
- {
- tail=j;
- break;
- }
- }
- memcpy (sms[ret].pos,recv_buf+head,(tail-head));
- sms[ret].pos[tail-head]='\0';
- head=tail+2;
- for(j=head;j<recv_buf_len;j++)
- {
- if(recv_buf[j]=='"')
- {
- tail=j;
- break;
- }
- };
- memcpy (sms[ret].read_flag,recv_buf+head,(tail-head));
- sms[ret].read_flag[tail-head]='\0';
- head=tail+3;
- for(j=head;j<recv_buf_len;j++)
- {
- if(recv_buf[j]=='"')
- {
- tail=j;
- break;
- }
- }
- memcpy (sms[ret].sms_from,recv_buf+head,(tail-head));
- sms[ret].sms_from[tail-head]='\0';
- head=tail+4;
- for (j=head;j<recv_buf_len;j++)
- {
- if(recv_buf[j]=='"')
- {
- tail=j;
- break;
- }
- }
- memcpy (sms[ret].recv_time,recv_buf+head,(tail-head));
- sms[ret].recv_time[tail-head]='\0';
- head=tail+3;
- for (j=head;j<recv_buf_len;j++)
- {
- if(recv_buf[j]=='\r')
- {
- tail=j;
- break;
- }
- }
- memcpy (sms[ret].sms_content,recv_buf+head,(tail-head));
- sms[ret].sms_content[tail-head]='\0';
- sms[ret].flag=1;
- i=tail;
- ret++;
- }
- else i++;
- }
- return ret;
- }
复制代码
- #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>
- #include "serial_receive.h"
- #define DEBUG 0
- void setTermios(struct termios * pNewtio, U16 uBaudRate)
- {
- bzero(pNewtio, sizeof(struct termios)); /* clear struct for new port settings */
- //8N1
- pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL;
- pNewtio->c_iflag = IGNPAR;
-
- pNewtio->c_oflag = 0;
- pNewtio->c_lflag = 0; //non ICANON
- pNewtio->c_cc[VINTR] = 0;
- pNewtio->c_cc[VQUIT] = 0;
- pNewtio->c_cc[VERASE] = 0;
- pNewtio->c_cc[VKILL] = 0;
- pNewtio->c_cc[VEOF] = 4;
- pNewtio->c_cc[VTIME] = 5;
- pNewtio->c_cc[VMIN] = 0;
- pNewtio->c_cc[VSWTC] = 0;
- pNewtio->c_cc[VSTART] = 0;
- pNewtio->c_cc[VSTOP] = 0;
- pNewtio->c_cc[VSUSP] = 0;
- pNewtio->c_cc[VEOL] = 0;
- pNewtio->c_cc[VREPRINT] = 0;
- pNewtio->c_cc[VDISCARD] = 0;
- pNewtio->c_cc[VWERASE] = 0;
- pNewtio->c_cc[VLNEXT] = 0;
- pNewtio->c_cc[VEOL2] = 0;
- }
- int serial_recv(char *send_buf,int send_len,char *recv_buf,int timeout_s,int timeout_us,int wait_s,int wait_us)
- {
- int fd;
- int nread=0;
- int nwrite=0;
- char buff[BUFSIZE];
- struct termios oldtio,newtio;
- struct timeval tv;
- char *dev ="/dev/tts/0";
- fd_set rfds;
- if ( (fd = open(dev,O_RDWR | O_NOCTTY ))<0)
- {
- printf("Can't Open Serial Port!\n");
- return -1;
- }
- tcgetattr(fd,&oldtio);
- setTermios(&newtio,B115200); tcflush(fd, TCIFLUSH);
- tcsetattr(fd,TCSANOW,&newtio);
- tv.tv_sec=timeout_s;
- tv.tv_usec=timeout_us;
- nwrite=write(fd, send_buf, send_len);
- if(nwrite<send_len)
- {
- printf("Serial Write error!\n");
- return -1;
- }
- FD_ZERO(&rfds);
- FD_SET(fd,&rfds);
- if (select(1+fd,&rfds,NULL,NULL,&tv)>0)
- {
- if(FD_ISSET(fd,&rfds))
- {
- sleep(wait_s);
- usleep(wait_us);
- nread=read(fd, buff, BUFSIZE);
- if(nread<0)
- {
- printf("serial read error!\n");
- return -1;
- }
- memcpy(recv_buf,buff,nread);
- }
- }
- #ifdef DEBUG
- buff[nread]='\0';
- printf("RECEIVED::%s\n",buff);
- #endif
- tcsetattr(fd,TCSANOW,&oldtio);
- close(fd);
- return nread;
- }
- int serial_send_recv(char *send_buf,int send_len,char *send_content,int content_len,char *recv_buf,int timeout_s,int timeout_us,int wait_s,int wait_us)
- {
- int fd;
- int nread=0;
- int nwrite=0;
- char buff[BUFSIZE];
- struct termios oldtio,newtio;
- struct timeval tv;
- char *dev ="/dev/tts/0";
- fd_set rfds;
- if ( (fd = open(dev,O_RDWR | O_NOCTTY ))<0)
- {
- printf("Can't Open Serial Port!\n");
- return -1;
- }
- tcgetattr(fd,&oldtio);
- setTermios(&newtio,B115200); tcflush(fd, TCIFLUSH);
- tcsetattr(fd,TCSANOW,&newtio);
- tv.tv_sec=timeout_s;
- tv.tv_usec=timeout_us;
- nwrite=write(fd, send_buf, send_len);
- if(nwrite<send_len)
- {
- printf("Serial Write error!\n");
- return -1;
- }
- FD_ZERO(&rfds);
- FD_SET(fd,&rfds);
- if (select(1+fd,&rfds,NULL,NULL,&tv)>0)
- {
- if(FD_ISSET(fd,&rfds))
- {
- sleep(1);
- usleep(wait_us);
- nread=read(fd, buff, BUFSIZE);
- if(nread<0)
- {
- printf("serial read error!\n");
- return -1;
- }
- memcpy(recv_buf,buff,nread);
- }
- }
- #ifdef DEBUG
- buff[nread]='\0';
- printf("RECEIVED::%s\n",buff);
- #endif
- sleep(1);
- tv.tv_sec=timeout_s;
- tv.tv_usec=timeout_us;
- nwrite=write(fd, send_content, content_len);
- if(nwrite<content_len)
- {
- printf("Serial Write error!\n");
- return -1;
- }
- FD_ZERO(&rfds);
- FD_SET(fd,&rfds);
- if (select(1+fd,&rfds,NULL,NULL,&tv)>0)
- {
- if(FD_ISSET(fd,&rfds))
- {
- sleep(wait_s);
- usleep(wait_us);
- nread=read(fd, buff, BUFSIZE);
- if(nread<0)
- {
- printf("serial read error!\n");
- return -1;
- }
- memcpy(recv_buf,buff,nread);
- }
- }
- #ifdef DEBUG
- buff[nread]='\0';
- printf("RECEIVED::%s\n",buff);
- #endif
- tcsetattr(fd,TCSANOW,&oldtio);
- close(fd);
- return nread;
- }
复制代码
参考一下吧 |
|