linux下GPRS测试程序

说明:基于Motorola  G24模块
/* func:send a message "hello ,bcng!" */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>

int OpenCom1(void);
void DialPhone(int);

int fd;
int Com1;
#define MAXNUM 30
/* open com1  */
int OpenCom1()
{
 
   struct termios oldtio,newtio;
   
   Com1 = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
   if (Com1 <0)
 {
    printf("Could not open Serial port!\n");
         return 0;
 }
 
  tcgetattr(Com1,&oldtio);                                                                             
  newtio.c_iflag=0;//ignore the frame or parity error
  newtio.c_oflag=0;//reflect the Enter key to a new line
  newtio.c_cflag=CLOCAL | CREAD | CS8 ;
  cfmakeraw(&newtio);
  newtio.c_cflag &= ~PARENB ; //no parity
  newtio.c_cflag &=~CSTOPB; //stopbit:1
  newtio.c_cflag|= IXON |IXOFF |IXANY;
 newtio.c_lflag=0;
  cfsetispeed(&newtio,B57600);
  cfsetospeed(&newtio,B57600);
   newtio.c_cc[VTIME]    = 10;
   newtio.c_cc[VMIN]     = 0;//设为1就变为阻塞方式,一直等待字符
  tcflush(Com1, TCIFLUSH);
  tcflush(Com1, TCOFLUSH);
   tcsetattr(Com1,TCSANOW,&newtio);
   fcntl(Com1,F_SETFL,0);//设为非阻塞方式
   return Com1;                                                                              
}

void dial( )
{
  int i;
  unsigned char RevData[MAXNUM];
  unsigned char SendData[MAXNUM]="ATD13787115624;";
  tcflush(fd, TCIFLUSH);
  tcflush(fd, TCOFLUSH); 
  SendData[15]=0x0d;
  if(write(fd,SendData,16)<0)
   printf("write error");
  else
  { 
   i=read(fd,RevData,MAXNUM);
   printf("received %d charater: %s\n",i,RevData);
   printf("calling...\n");
    while(1)
    {}
  }
}

void hangup(void)
{
  int i,j;
  unsigned char SendData[5];
  SendData[0]='A';
  SendData[1]='T';
  SendData[2]='H';
  SendData[3]=0x0d;
  j=write(fd,SendData,4);
  printf("Hangup\n"); 
}

void receivephone(void)
{
  int i,j;
  unsigned char SendData[5];
  SendData[0]='A';
  SendData[1]='T';
  SendData[2]='A';
  SendData[3]=0x0d;
  j=write(fd,SendData,4);
}
void read_rssi()
{
  int i;
  unsigned char RevData[MAXNUM];
  unsigned char SendData[MAXNUM]="at+csq";
  tcflush(fd, TCIFLUSH);
  tcflush(fd, TCOFLUSH);  
  SendData[6]=0xd;
  if(write(fd,SendData,7)<0)
  { 
   printf("write error");
   
  } 
  else
  {
   sleep(0);
   RevData[0]=0;
   RevData[1]=0;
   i=read(fd,RevData,24);
   printf("received %d charater: %s\n",i,RevData);
   
  }
}

int detect_g20()
{
  int i,j;
  unsigned char RevData[MAXNUM];
  unsigned char SendData[MAXNUM]="at";
  tcflush(fd, TCIFLUSH);
  tcflush(fd, TCOFLUSH); 
  SendData[2]=0xd;
  printf("senddata:%s\n",SendData);
  if(write(fd,SendData,3)<0)
  { 
   printf("write error");
   return 0;
  } 
  else
  {
   i=read(fd,RevData,MAXNUM);
 //  printf("\nreceived %d charater:",i);
 //  for(j=0;j<i;j++)
 //  printf(" %x ",RevData[j]);
   //printf("%s",RevData);
   if ((RevData[0]==0xd))
    return 1;
   else
    return 0; 
  }
   
}

main()
{
  int FunctionIndex;
 unsigned char SendData1[5]="ate0";
  fd = OpenCom1();
  
  
  if (fd<0) 
 {
  printf("fd open error");
  exit(1);
  }
   
  printf("\n");
  printf("+----------------------------------------+\n");
  printf("|      ARM  Linux  GPRS                  |\n");
  printf("|      Write on 2007.4.30   xiyong bcng         |\n");
  printf("+----------------------------------------+\n");
  printf("\n");
  printf("\n Please select the function to test ( Main Menu):");
  printf("\n  1: Detect G20 ");
  printf("\n  2: Read RSSI ");
  printf("\n  3: Dial number");
  printf("\n  0: Exit\n");
  /* deliminate the echo using ate0 */ 
  SendData1[5]=0xd;
  printf("senddata:%s\n",SendData1);
  if(write(fd,SendData1,6)<0)
  { 
   printf("write error");
   return 0;
  } 
  for(;;)
  {
    
    scanf("%d",&FunctionIndex);
    switch(FunctionIndex)
    {
     case 1: 
      if (detect_g20())
        printf("detect OK\n");
       else
        printf("can not find g20\n"); 
       break;
     case 2:
      read_rssi();
       break;
      case 3:
       dial();
       break;
     case 0:
        if(!close(fd))
         printf("close Com1\n");
          else
         printf("close error\n");
        exit(1);
     default: 
        if(!close(fd))
         printf("close Com1\n");
          else
         printf("close error\n");
        exit(1); 
    }
   }
}


转载自:http://xiyong8260.blog.163.com/blog/static/66514621200944114440170/


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值