作者:​半岛鱼

sim300 GPRS数据收发程序


//int openport(char *Dev) //打开串口

//int setport(int fd, int baud,int databits,int stopbits,int parity)//设置串口,波特率,数据位,停止位,校验

//int readport(int fd,char *buf,int len,int maxwaittime)//读数据,参数为串口,BUF,长度,超时时间

//int writeport(int fd,char *buf,int len)   //发送数据

//int modem_init(int fd)                    // modem设备的初始化

//int modem_send()                          //数据发送

#include   <stdio.h>

#include   <string.h>

#include   <unistd.h>

#include   <fcntl.h>

#include   <errno.h>

#include   <termios.h>

#include   <sys/time.h>

#include   <stdlib.h>

unsigned char rbuf[256],rbuf1[256];

int openport(char *Dev)

{

int fd = open( Dev, O_RDWR|O_NOCTTY|O_NDELAY );

if (-1 == fd)

{   

perror("Can''t Open Serial Port");

return -1;

}

else

return fd;

}

  

int setport(int fd, int baud,int databits,int stopbits,int parity)

{

int baudrate;

struct   termios   newtio;

switch(baud)

{

case 300:

baudrate=B300;

break;

case 600:

baudrate=B600;

break;

case 1200:

baudrate=B1200;

break;

case 2400:

baudrate=B2400;

break;

case 4800:

baudrate=B4800;

break;

case 9600:

baudrate=B9600;

break;

case 19200:

baudrate=B19200;

break;

case 38400:

baudrate=B38400;

break;

default :

baudrate=B9600;

break;

}

tcgetattr(fd,&newtio);   

bzero(&newtio,sizeof(newtio));

   //setting   c_cflag

newtio.c_cflag   &=~CSIZE;   

switch (databits)

{

case 7:

newtio.c_cflag |= CS7; //7位数据位

break;

case 8:   

newtio.c_cflag |= CS8; //8位数据位

break;

default:  

newtio.c_cflag |= CS8;

break;   

}

switch (parity) //设置校验

{

case 'n':

case 'N':  

newtio.c_cflag &= ~PARENB;  

newtio.c_iflag &= ~INPCK;    

break;

case 'o':

case 'O':   

newtio.c_cflag |= (PARODD | PARENB);

newtio.c_iflag |= INPCK;            

break;

case 'e':

case 'E':

newtio.c_cflag |= PARENB;       

newtio.c_cflag &= ~PARODD;      

newtio.c_iflag |= INPCK;      

break;

case 'S':

case 's':

     newtio.c_cflag &= ~PARENB;

newtio.c_cflag &= ~CSTOPB;break;

default:

newtio.c_cflag &= ~PARENB;  

newtio.c_iflag &= ~INPCK;    

break;  

}

switch (stopbits)//设置停止位

{

case 1:  

newtio.c_cflag &= ~CSTOPB; //1

break;

case 2:  

newtio.c_cflag |= CSTOPB; //2

    break;

default:

newtio.c_cflag &= ~CSTOPB;

break;

}

newtio.c_cc[VTIME] = 10;  

newtio.c_cc[VMIN] = 0;

newtio.c_cflag   |=   (CLOCAL|CREAD);

newtio.c_oflag|=OPOST;

newtio.c_iflag &=~(IXON|IXOFF|IXANY);

             

    cfsetispeed(&newtio,baudrate);

    cfsetospeed(&newtio,baudrate);

   //newtio.c_cflag &= ~CNEW_RTSCTS;

   newtio.c_lflag     &= ~(ICANON | ECHO | ECHOE | ISIG);

   newtio.c_oflag     &= ~OPOST;

    tcflush(fd,   TCIOFLUSH);

if (tcsetattr(fd,TCSANOW,&newtio) != 0)

{

perror("SetupSerial 3");

return -1;

}

return 0;

}

int readport(int fd,unsigned char *buf,int len,int maxwaittime)//读数据,参数为串口,BUF,长度,超时时间

{

int no=0;int rc;int rcnum=len;

int gotend=0;

struct timeval tv;

fd_set readfd;

tv.tv_sec=maxwaittime/1000;    //SECOND

tv.tv_usec=maxwaittime00*1000; //USECOND

FD_ZERO(&readfd);

FD_SET(fd,&readfd);

rc=select(fd+1,&readfd,NULL,NULL,&tv);

if(rc>0)

{

//while(len)

{

   while(!gotend)

   {

   rc=read(fd,&buf[no],1);

  

   if(no>=2)

   {

    if(((buf[no]==0x0A)&&(buf[no-1]==0x0D))||(buf[no]=='#')||((buf[no-1]=='>')&&(buf[no]==0x20))) gotend=1; //(buf[no]==' ')||

   }

   if(rc>0)

       no=no+1;

     len=len-1;

}


}

// printf("t buf=%cn",buf[2]);

if(no!=rcnum)

   return no;        //如果收到的长度与期望长度不一样,返回-1

return rcnum;      //收到长度与期望长度一样,返回长度

}

return -1;

}

//csshixia

#if 0

int readport_1(int fd,unsigned char *buf,int len,int maxwaittime)//读数据,参数为串口,BUF,长度,超时时间

{

int no=0;int rc;int rcnum=len;

int gotend=0;

struct timeval tv;

fd_set readfd;

tv.tv_sec=maxwaittime/1000;    //SECOND

tv.tv_usec=maxwaittime00*1000; //USECOND

FD_ZERO(&readfd);

FD_SET(fd,&readfd);

rc=select(fd+1,&readfd,NULL,NULL,&tv);

if(rc>0)

{

while(len)

{

   //while(!gotend)

   {

   rc=read(fd,&buf[no],1);

   //printf("%dn",rc);

// printf("t buf=%cn",buf[no]);

// {

if((no>=2)&&(buf[no]=='#')) break; //(buf[no]==' ')||

// }

   if(rc>0)

      no=no+1;

     len=len-1;

}


}

// printf("t buf=%cn",buf[2]);

if(no!=rcnum)

   return no;        //如果收到的长度与期望长度不一样,返回

return rcnum;      //收到长度与期望长度一样,返回长度

}

return -1;

}

#endif

//ceshi

int writeport(int fd,unsigned char *buf,int len) //发送数据

{

write(fd,buf,len);

}

void clearport(int fd)      //如果出现数据与规约不符合,可以调用这个函数来刷新串口读写数据

{

tcflush(fd,TCIOFLUSH);

}

int check_recv(unsigned char *buf,int sno)

{

switch(sno)

   {

   case 0:

   case 1:

   case 3:

  

       if(strcmp(&buf[2],"OK")==0)

        {

         printf("%dn",strcmp(&buf[2],"OK"));

         return 0;

        }

       else

         return -1;

         //if(strcmp(buf,"ERROR")==0

        break;

   case 2:

        if(strcmp(&buf[2],"SHUT OK")==0)

        {

         printf("%dn",strcmp(&buf[2],"SHUT OK"));

         return 0;

        }

       else

        return -1;

       break;

   case 4:

        if(strcmp(&buf[2],"CONNECT OK")==0)

        {

         printf("%dn",strcmp(&buf[2],"CONNECT OK"));

         return 0;

        }

       else

        return -1;

       break;

   case 5:

        if(strcmp(&buf[2],"> ")==0)

        {

         printf("%dn",strcmp(&buf[2],"> "));

         return 0;

        }

       else

        return -1;

       break;

   case 6:

        if(strcmp(&buf[2],"SEND OK")==0)

        {

         printf("%dn",strcmp(&buf[2],"SEND OK"));