转载:GPRS发短信代码

简介:
经过阅读资料--了解了不少用GPRS模块发短信的过程,但很多是理论,并没有实际性的代码;
经过2天的努力,终于把GPRS发送中文短信成功!!

本人通过s3c2410开发板的串口1与wavecom  Q2406A接通,s3c2410串口0用于终端调试;

首先要把串口1设置为不是用于终端模式;之后配置串口属性;最后发送短信。(具体看代码吧)!!!!!!
但看代码前请看看我的前一遍GPRS知识,不然你就不明白代码了!( 如有问题请留言 --)
敬告:严与用于商业用途!抄袭!只用于学习交流之用!!


#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
#include  <sys/types.h>
#include  <unistd.h>
#include  <errno.h>
#include  <sys/stat.h>
#include  <fcntl.h>
#include  <termios.h>

int  open_port(int  fd,int  comport);
int  set_opt  (int  fd,  int  nSpeed,  int  nBits,  char  nEvent,  int  nStop)  ;
int  change_number(char  pSrc)  ;

int  main(void)  {
int  fd;
int  nwrite,nread,i  ;
char  buff[]="AT\r"  //AT命令--具体看手册
char  red[256]  ;
char  send[200]  ;
char  send2[100]  ;
char  centeraddr[]  "8613800100500";          //北京--短信中心号码
char  usraddr[]  "861510104175x";        //用户号码

char  head[]  "0891";

char  phone[]  "1100";
char  pho[]  "0D91"  ;
char  phoafter[]  "0008000"  ;
char  firemsg[]="0A5bb691cc7740706bff01"    //家里着火!
char  thiefmsg[]="0A5c0f50778fdb5c4bff01"    //小偷进屋
        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;
}
printf("fd=%d\n",fd);
//send=head+phone+msg+\x1a
change_number(centeraddr);
printf("after  change,the  centeraddr  is  %s\n",centeraddr);

strcpy(send,head);          //head=0891
printf("%s\n",send);
strcat(send,centeraddr);
printf("the  send  is  %s\n%d\n",send,strlen(send));
change_number(usraddr);
printf("after  change,  the  usraddr  is  %s\n",usraddr);

strcat(phone,pho);
printf("%s\n",phone);
strcat(phone,usraddr);
printf("%s\n",phone);

strcat(phone,phoafter);
printf("the  phone  is  %20s\n",phone);
strcat(send,phone);
printf("%s\n",send);

strcat(send,"\x1a");


printf("%20s\n%d\n",send,strlen(send));

write(fd,"AT\r",3);
sleep(2);
nread=read(fd,red,256);//读串口
if(strstr(red,"OK")  ==  NULL)
    printf("the  com1  is  faule!!\n");
printf("nread=%d,%s\n",nread,red);
write(fd,"AT+CMGF=0\r",10)  ;
sleep(2)  ;
nread=read(fd,red,256);//读串口
//if(strstr(red,"OK")  ==  NULL)
//  printf("the  select  PDU  is  faule!!\n");
printf("nread=%d,%s\n",nread,red);

write(fd,"AT+CMGS=27\r",11)  ;
sleep(1)  ;
nread=read(fd,red,256);//读串口
printf("%s  %d  PDU  CMD\n",red,nread);
       
while(!strstr(red,"\r\n>"))  {
   
    read(fd,red,256)  ;
}
write  (fd,  send,  strlen(send));
read(fd,red,256);
printf("%s\n",red);
sleep(3);
close(fd);
return  0;
}

int  open_port(int  fd,int  comport)  {
char  *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long  vdisable;
//O_NOCTTY标志用于通知Linux系统,这个程序不会成为对应这个端口的控制终端。
//O_NDELAY标志通知Linux系统,这个程序不关心DCD信号线所处的状态(端口的另一端是否激活或者停止)。

if(comport==1)    //串口1
    fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
      if(-1==fd)  {
        perror("Can't  Open  Serial  Port");
        return(-1);
      }
}
else  if(comport==2)      //串口2
    fd=open("/dev/s3c2410_serial1",O_RDWR|O_NOCTTY|O_NDELAY);
    if(-1==fd)  {
      perror("Can't  Open  Serial  Port");
      return(-1);
    }
}
else  if(comport==3)          //串口3
    fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
    if(-1==fd)  {
      perror("Can't  Open  Serial  Port");
      return(-1);
    }
}

if(fcntl(fd,F_SETFL,0)<0)
    printf("fcntl  failed!\n");
else
          printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));

if(isatty(STDIN_FILENO)==0)
    printf("standard  input  is  not  termina  device\n");
else
    printf("isatty  success!\n");
printf("fd=%d\n",fd);
return  fd;
}












int  set_opt  (int  fd,  int  nSpeed,  int  nBits,  char  nEvent,  int  nStop)  {
   
struct  termios  newtio,  oldtio;
if  (tcgetattr  (fd,  &oldtio)  !=  0)                                             
    perror  ("SetupSerial  1");                                                         
    return  -1;
}

bzero  (&newtio,  sizeof  (newtio));//对结构体  清零

newtio.c_cflag  |=  CLOCAL  CREAD;


newtio.c_cflag  &=  ~CSIZE;

switch  (nBits)  {
case  7:
            newtio.c_cflag  |=  CS7;
            break;
case  8:
            newtio.c_cflag  |=  CS8;
            break;
        }


switch  (nEvent)  {
        case  'O':
            newtio.c_cflag  |=  PARENB;
            newtio.c_cflag  |=  PARODD;
            newtio.c_iflag  |=  (INPCK  ISTRIP);
            break;
        case  'E':
            newtio.c_iflag  |=  (INPCK  ISTRIP);
            newtio.c_cflag  |=  PARENB;
            newtio.c_cflag  &=  ~PARODD;
            break;
        case  'N':
            newtio.c_cflag  &=  ~PARENB;
            break;
        }
   

switch  (nSpeed)  {
        case  2400:
            cfsetispeed  (&newtio,  B2400);
            cfsetospeed  (&newtio,  B2400);
            break;
        case  4800:
            cfsetispeed  (&newtio,  B4800);
            cfsetospeed  (&newtio,  B4800);
            break;
        case  9600:
            cfsetispeed  (&newtio,  B9600);
            cfsetospeed  (&newtio,  B9600);
            break;
        case  115200:
            cfsetispeed  (&newtio,  B115200);
            cfsetospeed  (&newtio,  B115200);
            break;
        case  460800:
            cfsetispeed  (&newtio,  B460800);
            cfsetospeed  (&newtio,  B460800);
            break;
        default:
            cfsetispeed  (&newtio,  B9600);
            cfsetospeed  (&newtio,  B9600);
            break;
        }
   
if  (nStop  ==  1)
          newtio.c_cflag  &=  ~CSTOPB;
else  if  (nStop  ==  2)
    newtio.c_cflag  |=  CSTOPB;


newtio.c_cc[VTIME]  0;
newtio.c_cc[VMIN]  0;


tcflush  (fd,  TCIFLUSH);//刷新收到的数据但不读


if  ((tcsetattr  (fd,  TCSANOW,  &newtio))  !=  0)  //TCSANOW:改变的配置立即生效
    perror  ("com  set  error");
    return  -1;
}
printf  ("set  done!\n");
return  0;
}

int  change_number(char  pSrc)  {

int  ;
char  ;
char  F[]  "F"  ;
int  length  strlen(pSrc);
//  1、将短信息中心号码去掉+号,看看长度是否为偶数,如果不是,最后添加F
if(length  1)                       
{
    strcat(pSrc,F)      //  补'F'
    int  length  strlen(pSrc);
    printf("%s\n",pSrc)  ;
}
//2、将奇数位和偶数位交换。
for(i=0;i<length;i  +=  2)  {
    pSrc  ;
    pSrc  pSrc  ;
    pSrc  ;
}
//printf("after  change  number  is:\n%s\n",pSrc);
return  ;
}


/**********************************************************************************
/**********************************************************************************

总结:在本代码中,中文的unicode码,已经中文转换unicode工具转换出来,并没写出其转换代码,所以本代码有点不完整;


本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2009/01/08/1586368.html,如需转载请自行联系原作者


相关文章
|
5天前
|
移动开发 JavaScript
H5唤起手机打电话(拨号)和发短信功能
H5唤起手机打电话(拨号)和发短信功能
65 0
|
6月前
|
小程序 JavaScript
小程序一个页面调用多个号码的手机拨号功能
小程序一个页面调用多个号码的手机拨号功能
75 0
|
8月前
|
移动开发
手机h5页面唤起打电话、发短信功能
手机h5页面唤起打电话、发短信功能
|
12月前
|
测试技术 PHP
VoIP实现即时通话通知
VoIP实现即时通话通知
VoIP实现即时通话通知
|
JSON 小程序 前端开发
微信小程序接入NFC,使用HCE模拟主机卡完成NFC刷卡发送消息
微信小程序接入NFC,使用HCE模拟主机卡完成NFC刷卡发送消息
576 0
微信小程序接入NFC,使用HCE模拟主机卡完成NFC刷卡发送消息
SIP的voip语音通话后30秒左右挂断呼叫怎么解决
SIP的voip语音通话后30秒左右挂断呼叫怎么解决
微信登录电脑,手机接收消息仍有提示音设置方法
微信登录电脑,手机接收消息仍有提示音设置方法
1080 0
微信登录电脑,手机接收消息仍有提示音设置方法
|
数据安全/隐私保护 Android开发 网络架构