SIM71004G模块使用Linux C语言实现打电话发短信

来源:互联网 发布:淘宝买东西往哪里存钱 编辑:程序博客网 时间:2024/06/06 04:47

一、实验目的:

SIM71004G模块使用Linux C语言实现打电话发短信.


二、实验平台:

迅为itop4412开发板(运行Linux最小系统),SIM7100 4G模块.


三、实验流程

(1)串口编程

串口设置其实就相当于串口通信的协议: 主要是初始化设置。
波特率:是为了两者信号流能同步,;
数据位:是指又几位数据封装成一帧 ;
结束位:是指以帧传输数据时,协定好结束位,便于提取有效数据 ;
奇偶校验:检验数据的一种手段 .

(2)实现代码:

#include <stdio.h>#include <errno.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <unistd.h>int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);int send_message(int fd);int call_number(int fd);int main (int argc, char **argv){    int fd;    char select;    fd = open( "/dev/ttyUSB2", O_RDWR|O_NOCTTY|O_NDELAY);/*ttyUSB2为SIM7100的AT指令口*/    if(fd<0){        perror("Can't Open Serial Port");        return -1;    }     /*配置串口,波特率必须和4G模块的相同*/    set_opt( fd,115200,8,'N',1);    printf("==========================================\n");    printf("gprs call number and send message\n");    printf("==========================================\n");    printf("enter your select:  's' is send message,  'c' is call number,   'q' is exit \n");    select=getchar();/*去掉回车符*/     switch(select)    {        case 's':                 send_message(fd);                 break;        case 'c':                 call_number(fd);                 break;        case 'q':                 exit(0);                 break;        default:                 break;    }    close(fd);    return 0;}/************************************************************ * 功能: 串口参数配置 * 输入参数: fd:open打开的文件描述符 nspeed:波特率 nBits:数据位数  * nEvent:奇偶校验 nStop:停止位 ***********************************************************/int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop){    struct termios newttys1,oldttys1;    if(tcgetattr(fd,&oldttys1)!=0)     /*保存原先串口配置*/    {        perror("Setupserial 1");        return -1;    }bzero(&newttys1,sizeof(newttys1));       /*将一段内存区域的内容全清为零*/newttys1.c_cflag|=(CLOCAL|CREAD );       /*CREAD 开启串行数据接收,CLOCAL并打开本地连接模式*/   newttys1.c_cflag &=~CSIZE;              /*设置数据位数*/switch(nBits)     /*选择数据位*/   {        case 7:                newttys1.c_cflag |=CS7;                break;        case 8:                newttys1.c_cflag |=CS8;                break; }switch( nEvent )    /*设置校验位 */  {   case '0':       /*奇校验  */           newttys1.c_cflag |= PARENB;             /*开启奇偶校验 */            newttys1.c_iflag |= (INPCK | ISTRIP);   /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特*/             newttys1.c_cflag |= PARODD;             /*启用奇校验(默认为偶校验)*/             break;  case 'E' :       /*偶校验  */           newttys1.c_cflag |= PARENB;             /*开启奇偶校验 */            newttys1.c_iflag |= ( INPCK | ISTRIP);  /*打开输入奇偶校验并去除字符第八个比特  */           newttys1.c_cflag &= ~PARODD;            /*启用偶校验;  */           break;  case 'N':     /*关闭奇偶校验*/           newttys1.c_cflag &= ~PARENB;           break;   }     switch( nSpeed )        /*设置波特率 */      {        case 2400:                 cfsetispeed(&newttys1, B2400);    /*设置输入速度*/                 cfsetospeed(&newttys1, B2400);    /*设置输出速度*/                 break;        case 4800:                 cfsetispeed(&newttys1, B4800);                 cfsetospeed(&newttys1, B4800);                 break;        case 9600:                 cfsetispeed(&newttys1, B9600);                 cfsetospeed(&newttys1, B9600);                 break;        case 115200:                 cfsetispeed(&newttys1, B115200);                 cfsetospeed(&newttys1, B115200);                 break;        default:                 cfsetispeed(&newttys1, B9600);                 cfsetospeed(&newttys1, B9600);                 break;     }     if( nStop == 1)   /*设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB*/      {        newttys1.c_cflag &= ~CSTOPB;      /*默认为送一位停止位; */      }     else if( nStop == 2)     {        newttys1.c_cflag |= CSTOPB;       /*CSTOPB表示送两位停止位;*/       }     /*设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/     newttys1.c_cc[VTIME] = 0; /*非规范模式读取时的超时时间; */      newttys1.c_cc[VMIN]  = 0;  /*非规范模式读取时的最小字符数;*/       tcflush(fd ,TCIFLUSH); /*tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */     /* 在完成配置后,需要激活配置使其生效*/     if((tcsetattr( fd, TCSANOW,&newttys1))!=0) /*TCSANOW不等数据传输完毕就立即改变属性*/       {         perror("com set error");         return -1;     }    return 0;}/************************************************************************************** *  功能:sim7110模块打电话 *  实现:ATD+号码+; *************************************************************************************/int call_number (int fd){    getchar();   /*将缓冲区回车吃掉,输入c后的回车*/    int count=0;    char call[20]="atd";    /*打电话的AT指令 */    char number[20];    char reply[128];    printf("enter you call number\n");    if(NULL==fgets(number,20,stdin))      /*输入电话号码,其中fgets在读入一个字符串后在字符串尾端默认加入\n字符*/        exit(0);           /*这个语句的功能可以用gets实现,区别在于 fgets 读入的含 "\n"(最后一个字符),gets 不含 "\n"*/    while(strlen(number)!=12)               {        printf("please again number\n");        if(NULL==fgets(number,20,stdin))            exit(0);        if(count==3)            exit(0);        count++;    }    number[strlen(number)-1]='\0'; /*将刚才fgets读入的字符串尾端去除\n字符*/    strcat(call,number);         strcat(call,";\r");            /* \r是换行字符*/    write(fd,call,strlen(call));   /*向串口拨打号码*/    printf("write %s\n",call);    sleep(3);      memset(reply,0,sizeof(reply));    read(fd,reply,sizeof(reply));   /*AT指令*/    printf("%s\n",reply);    printf("=================================================\n");    printf("number is calling,please press 'a' hang up \n");    printf("=================================================\n");   while('a'!=getchar())        printf("please again input 'a' to hung up \n");    memset(call,0,sizeof(call));    strcpy(call,"ATH\r");     write(fd,call,strlen(call));    sleep(1);    memset(reply,0,sizeof(reply));    read(fd,reply,sizeof(reply));   /*数据*/    printf("%s\n",reply);    printf("has hung up\n");    return 0;}/**************************************** *  功能:SIM7100模块发短信函数 *  实现:AT+CMGF=1 (回车)                      AT+CMGS="号码" (回车)         >   (Ctrl + z) ****************************************/int send_message (int fd){    int count=0;    char  cmgf[]="at+cmgf=1\r";  /*设置短信发送模式为text (0-PDU;1-text)*/    char  cmgs[128]="at+cmgs=\"";   /*AT+CMGS="号码"  \:连接字符串*/    char  send_number[16];  /*电话号码*/    char  message[128];     char  reply[128];    getchar();   /*吃掉缓冲区回车,输入s后的回车*/    printf("enter send_message number :\n");    if(NULL==fgets(send_number,16,stdin))        exit(0);    while(12!=strlen(send_number))    {        getchar();        printf("please again input number\n");        if(NULL==fgets(send_number,16,stdin))             exit(0);        if(count==3)            exit(0);        count++;    }    send_number[strlen(send_number)-1]='\0';   /*去除字符串末端读入的换行符\n;*/    strcat(cmgs,send_number);    strcat(cmgs,"\"\r");    /*cmgs:at+cmgs="号码"*/    printf("enter send_message :\n");    if(NULL==fgets(message,128,stdin))        exit(0);    message[strlen(message)-1]='\0';    /*"\x1a对应的控制字符为:SUB:意为后面只跟可打印输出的字符*/    strcat(message,"\x1a"); /*可以打印的*/    /* write 模式 */    write(fd,cmgf,strlen(cmgf));    printf("write %s\n",cmgf);    sleep(2);    memset(reply,0,sizeof(reply));    read(fd,reply,sizeof(reply));    printf("%s\n",reply);    /* write 命令 */    write(fd,cmgs,strlen(cmgs));    printf("write %s\n",cmgs);    sleep(5);    /*write 信息*/    write(fd,message,strlen(message));    printf("write %s\n",message);    sleep(4);    memset(reply,0,sizeof(reply));    read(fd,reply,sizeof(reply));    printf("%s\n",reply);    return 0;} 
(3)编译测试:

将dial.c文件拷贝到ubuntu下,在该目录下使用:
arm-none-linux-gnueabi-gcc -o dial dial.c -static 进行编译,得到编译出来的dial执行文件,使用U盘拷贝到开发板上。
chmod 777 dial :修改权限
./dial :执行
可以得到:
这里写图片描述

发短信执行成功!!!打电话同样可以实现。


四、感谢

参考:
[sim900GPRS使用C语言打电话发短信] (http://blog.csdn.net/hulu_arm/article/details/51043751)

1 0
原创粉丝点击