安信可A7模块实现拨号发短信

来源:互联网 发布:p2p摄像头软件下载 编辑:程序博客网 时间:2024/06/11 05:51

安信可A7模块分为GPS和GPRS部分,这里实现拨号和发短信功能用到GPRS模块。直奔主题:实现拨号和发短信功能的步骤。

1:首先用串口调试助手检验一下模块是否能正常工作,各部分功能是否有用。

串口号自己在电脑上查,波特率设置为115200,如下:


简单的说一下我回的几条AT命令:

AT     返回ok说明连接成功
AT+CPIN? 测试SIM卡是否准备好
AT+CCID  测试SIM卡ID
AT+CSQ   测试信号强度
ATD+号码+; 拨打电话
ATA 应答电话
ATH 挂断电话
AT+SNFS=0 切换到耳机输出
AT+SNFS=1 切换到喇叭输出

手机耳机分为两种标准:OMTP标准通常被叫做国家标准,CTIA被称为国际标准。两者的区别在于插头最后两节
GND和MIC顺序相反。CTIA耳机插入OMTP设备会造成声音小且失真

AT+CMGF=1 //设置TEXT MODE
AT+CSDH=1 //显示文本格式
AT+CNMI=0,2,0,0,0 //显示新收到的短信
 
AT+CMGF=1//设置TEXT MODEAT+CSCS="GSM"
AT+CMGS="17719228082"//其中电话号码要发送给谁短信就写谁TEST MESSAGE//这里写任意短信内容,英文数字标点等然后用HEX格式发送 1A把短信发送出去,然后接收端手机就能接收到模块发送的短信了
AT+CMGF=0//设置PDU格式AT+CSCS="GSM"
AT+CMGS=19//记得是用CMGS不是CMGW0011000D91687117298280F20008AA044F60597D//记得发送这个数据后面别加换行1A//HEX发送数据,把短信发送出去


AT+GPS=1 打开 GPS
AT+GPS=0 关闭 GPS
AT+AGPS=1 打开 AGPS
AT+AGPS=0 关闭 AGPS
AT+GPSRD=0 关闭 NEMA 从 AT 口输出
AT+GPSRD=N NEMA 信息 N 秒从 AT 口输出一次,实际使用将 N 换成数字;
:

2在开发板上调试拨号和发短信功能。

首先在虚拟机上编译生成a.out可执行文件,然后再传送到开发板上运行。(./a.out)

头文件gprs.h

  1 /********************************************************************************  2  *      Copyright:  (C) 2017 luliteng<luliteng@gmail.com>  3  *                  All rights reserved.  4  *  5  *       Filename:  head.h  6  *    Description:  This head file   7  *  8  *        Version:  1.0.0(2017年05月11日)  9  *         Author:  luliteng <luliteng@gmail.com> 10  *      ChangeLog:  1, Release initial version on "2017年05月11日 04时00分22秒" 11  *                  12  ********************************************************************************/ 13 #ifndef __GPRS_H__ 14 #define __GPRS_H__ 15  16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <unistd.h> 19 #include <fcntl.h> 20 #include <string.h> 21 #include <sys/types.h> 22 #include <sys/stat.h> 23  24 extern int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop); 25 extern int send_message(int fd); 26 extern int call_number(int fd); 27  28 #endif 29 ~                               
打电话程序

cellphone.c

 1 /*********************************************************************************  2  *      Copyright:  (C) 2017 luliteng<luliteng@gmail.com>  3  *                  All rights reserved.  4  *  5  *       Filename:  cellphone.c  6  *    Description:  This file   7  *                   8  *        Version:  1.0.0(2017年05月11日)  9  *         Author:  luliteng <luliteng@gmail.com> 10  *      ChangeLog:  1, Release initial version on "2017年05月11日 03时48分15秒" 11  *                  12  ********************************************************************************/ 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <unistd.h> 16 #include <fcntl.h> 17 #include <string.h> 18 #include <sys/types.h> 19 #include <sys/stat.h> 20  21 #include"gprs.h" 22  23 /************************************************************************************** 24  *  Description:    gprs打电话函数 25  *   Input Args:    fd 串口设备文件描述符 26  *  Output Args: 27  * Return Value: 28  *************************************************************************************/ 29 int call_number (int fd) 30 { 31     getchar();                             //将缓冲区回车吃掉 32  33     int count = 0; 34     char call[20] = "atd"; 35     char number[20]; 36     char reply[128]; 37  38     printf("enter you call number\n"); 39     if(NULL == fgets(number,20,stdin))      //输入电话号码,其中fgets在读入一个字符串后在字符串尾端默认加入\n字符 40         exit(0);           //这个语句的功能可以用gets实现,区别在于 fgets 读入的含 "\n"(最后一个字符),gets 不含 "\n" 41  42     while(strlen(number) != 12) 43     { 44         printf("please again number\n"); 45         if(NULL == fgets(number, 20, stdin)) 46             exit(0); 47         if(count == 3) 48             exit(0); 49         count++; 50     } 51  52     number[strlen(number) -1 ]= '\0'; //将刚才fgets读入的字符串尾端去除\n字符 53     strcat(call, number);        //strcat将number字符串加到call尾部 54     strcat(call, ";\r");            // \r是换行字符 55  56     write(fd, call, strlen(call));   //向串口拨打号码 57     printf("write %s\n", call); 58     sleep(3); 59     memset(reply, 0, sizeof(reply)); 60     read(fd, reply, sizeof(reply)); 61     printf("%s\n", reply); 62  63     printf("=================================================\n"); 64     printf("number is calling,please press 'a' hang up \n"); 65     printf("=================================================\n"); 66  67    while('a' != getchar()) 68         printf("please again input 'a' to hung up \n"); 69  70     memset(call, 0, sizeof(call)); 71     strcpy(call, "ATH\r"); 72     write(fd, call, strlen(call)); 73     sleep(1); 74     memset(reply, 0, sizeof(reply)); 75     read(fd, reply, sizeof(reply)); 76     printf("%s\n",reply); 77     printf("has hung up\n"); 78  79  80     return 0; 81 } 82 

发短信代码

setmessage.c

1 /*********************************************************************************  2  *      Copyright:  (C) 2017 luliteng<luliteng@gmail.com>  3  *                  All rights reserved.  4  *  5  *       Filename:  setmessage.c  6  *    Description:  This file   7  *                   8  *        Version:  1.0.0(2017年05月11日)  9  *         Author:  luliteng <luliteng@gmail.com> 10  *      ChangeLog:  1, Release initial version on "2017年05月11日 03时53分24秒" 11  *                  12  ********************************************************************************/ 13 #include <stdio.h> 14 #include <string.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 #include <fcntl.h> 18 #include <sys/types.h> 19 #include <sys/stat.h> 20  21 #include "gprs.h" 22  23  24 /************************************************************************************** 25  *  Description:    gprs发短信函数 26  *   Input Args:    fd 串口设备文件描述符 27  *  Output Args: 28  * Return Value: 29  *************************************************************************************/ 30 int send_message (int fd) 31 { 32     int   count = 0; 33     char  cmgf[] = "at+cmgf=1\r";  //设置短信发送模式为text (0-PDU;1-text) 34     char  cmgs[128] = "at+cmgs=\""; 35     char  send_number[16] = {}; 36     char  message[128] = {}; 37     char  reply[128] = {}; 38  39     getchar();                   //吃掉缓冲区回车 40     printf("enter send_message number :\n"); 41     if(NULL == fgets(send_number, 16, stdin)) 42         exit(0); 43     while(12 != strlen(send_number)) 44     { 45         getchar(); 46         printf("please again input number\n"); 47         if(NULL == fgets(send_number, 16, stdin)) 48              exit(0); 49         if(count == 3) 50             exit(0); 51         count++; 52     } 53  54     send_number[strlen(send_number)-1] = '\0';   //去除字符串末端读入的换行符\n; 55     strcat(cmgs, send_number); 56     strcat(cmgs, "\"\r"); 57  58     printf("enter send_message :\n"); 59     if(NULL == fgets(message, 128, stdin)) 60         exit(0); 61  62     message[strlen(message) -1 ] = '\0'; 63     strcat(message, "\x1a"); 64  65     /* write cmgf */ 66  67     write(fd, cmgf, strlen(cmgf)); 68     printf("write %s\n", cmgf); 69     sleep(2); 70     memset(reply, 0, sizeof(reply)); 71     read(fd, reply, sizeof(reply)); 72     printf("%s\n", reply); 73  74     /* write cmgs */ 75  76     write(fd, cmgs, strlen(cmgs)); 77     printf("writr %s\n", cmgs); 78     sleep(5); 79  80  81     /*write  message*/ 82  83     write(fd, message, strlen(message)); 84     printf("writr %s\n", message); 85     sleep(4); 86     memset(reply, 0, sizeof(reply)); 87     read(fd, reply, sizeof(reply)); 88     printf("%s\n", reply); 89  90  91     return 0; 92 } 93 
vim Makefile

  1 cC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc  2 objs   =  cellphone.o setmessage.o serial.o main.o  3 srcs   =  cellphone.c setmessage.c serial.c main.c  4   5 gprs: $(objs)  6         $(CC) -o gprs $(objs)  7             @make clean  8   9 main.o: $(srcs) gprs.h 10         $(CC) -c $(srcs) 11  12 call.o: call.c gprs.h 13         $(CC) -c call.c 14  15 serial.o: serial.c 16         $(CC) -c serial.c 17  18 masseage.o: masseage.c gprs.h 19         $(CC) -c masseage.c     20  21 clear: 22         @rm  gprs 23  24     .PHONY: clean 25 clean: 26         @rm *.o

main函数

1 /*********************************************************************************  2  *      Copyright:  (C) 2017 luliteng<luliteng@gmail.com>  3  *                  All rights reserved.  4  *  5  *       Filename:  main.c  6  *    Description:  This file   7  *                   8  *        Version:  1.0.0(2017年05月11日)  9  *         Author:  luliteng <luliteng@gmail.com> 10  *      ChangeLog:  1, Release initial version on "2017年05月11日 03时56分13秒" 11  *                  12  ********************************************************************************/ 13 #include"gprs.h" 14  15 int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop); 16 int send_message(int fd); 17 int call_number(int fd); 18 /******************************************************************************** 19  *  Description: 20  *   Input Args: 21  *  Output Args: 22  * Return Value: 23  ********************************************************************************/ 24 int main (int argc, char **argv) 25 { 26     int fd; 27     char select; 28  29     fd = open( "/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY); 30     if(fd < 0){ 31         perror("Can't Open Serial Port"); 32         return -1; 33     } 34      //配置串口 35     set_opt( fd, 115200, 8, 'N', 1); 36  37     printf("==========================================\n"); 38     printf("gprs call number and send message\n"); 39     printf("==========================================\n"); 40     printf("enter your select:  's' is send message,  'c' is call number,   'q' is exit \n"); 41  42     select = getchar(); 43      switch(select) 44     { 45         case 's': 46                  send_message(fd); 47                  break; 48         case 'c': 49                  call_number(fd); 50                  break; 51         case 'q': 52                  exit(0); 53                  break; 54         default: 55                  break; 56     } 57     close(fd); 58  59  60     return 0; 61 } /* ----- End of main() ----- */ 62 

serial.c(串口程序)

1 /*********************************************************************************  2  *      Copyright:  (C) 2017 luliteng<luliteng@gmail.com>  3  *                  All rights reserved.  4  *  5  *       Filename:  serial.c  6  *    Description:  This file   7  *                   8  *        Version:  1.0.0(2017年05月11日)  9  *         Author:  luliteng <luliteng@gmail.com> 10  *      ChangeLog:  1, Release initial version on "2017年05月11日 04时19分12秒" 11  *                  12  ********************************************************************************/ 13 #include <stdio.h> 14 #include <errno.h> 15 #include <sys/stat.h> 16 #include <fcntl.h> 17 #include <termios.h> 18 #include <stdlib.h> 19 #include <string.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22  23  24 /************************************************************************************** 25  *  Description: 串口参数配置 26  *  Input Args: fd:open打开的文件描述符 nspeed:波特率 nBits:数据位数 nEvent:奇偶校验 nStop:停止位 27  *  Output Argtingzhis: 串口参数设置失败返回-1 28  * Return Value: 29  *************************************************************************************/ 30 int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop) 31 { 32     struct termios newttyUSB0, oldttyUSB0; 33  34     if(tcgetattr(fd, &oldttyUSB0) != 0)         //保存原先串口配置 35     { 36         perror("Setupserial 1"); 37         return -1; 38     } 39  40 bzero(&newttyUSB0, sizeof(newttyUSB0));       //将一段内存区域的内容全清为零 41 newttyUSB0.c_cflag |= (CLOCAL | CREAD );       //CREAD 开启串行数据接收,CLOCAL并打开本地连接模式    42 newttyUSB0.c_cflag &= ~CSIZE;              //设置数据位数 43 switch(nBits)     //选择数据位   44  { 45         case 7: 46                 newttyUSB0.c_cflag |=CS7; 47                 break; 48         case 8: 49                 newttyUSB0.c_cflag |=CS8; 50                 break; 51  } 52 switch( nEvent )    //设置校验位   53  { 54    case '0':       //奇校验   55            newttyUSB0.c_cflag |= PARENB;             //开启奇偶校验   56            newttyUSB0.c_iflag |= (INPCK | ISTRIP);   //INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特   57            newttyUSB0.c_cflag |= PARODD;             //启用奇校验(默认为偶校验)   58            break; 59   case 'E' :       //偶校验   60            newttyUSB0.c_cflag |= PARENB;             //开启奇偶校验   61            newttyUSB0.c_iflag |= ( INPCK | ISTRIP);  //打开输入奇偶校验并去除字符第八个比特   62            newttyUSB0.c_cflag &= ~PARODD;            //启用偶校验;   63            break; 64   case 'N':     //关闭奇偶校验 65            newttyUSB0.c_cflag &= ~PARENB; 66            break; 67    } 68  69      switch( nSpeed )        //设置波特率   70      { 71         case 2400: 72                  cfsetispeed(&newttyUSB0, B2400);           //设置输入速度 73                  cfsetospeed(&newttyUSB0, B2400);           //设置输出速度 74                  break; 75         case 4800: 76                  cfsetispeed(&newttyUSB0, B4800); 77                  cfsetospeed(&newttyUSB0, B4800); 78                  break; 79         case 9600: 80                  cfsetispeed(&newttyUSB0, B9600); 81                  cfsetospeed(&newttyUSB0, B9600); 82                  break; 83         case 115200: 84                  cfsetispeed(&newttyUSB0, B115200); 85                  cfsetospeed(&newttyUSB0, B115200); 86                  break; 87         default: 88                  cfsetispeed(&newttyUSB0, B9600); 89                  cfsetospeed(&newttyUSB0, B9600); 90                  break; 91      } 92  93      if( nStop == 1)                      //设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB。   94      { 95         newttyUSB0.c_cflag &= ~CSTOPB;      //默认为送一位停止位;   96      } 97      else if( nStop == 2) 98      { 99         newttyUSB0.c_cflag |= CSTOPB;       //CSTOPB表示送两位停止位;  100      }101 102      //设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时103      newttyUSB0.c_cc[VTIME] = 0;        //非规范模式读取时的超时时间;  104      newttyUSB0.c_cc[VMIN]  = 0;        //非规范模式读取时的最小字符数;  105 106      tcflush(fd ,TCIFLUSH);           //tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 107 108      // 在完成配置后,需要激活配置使其生效109      if((tcsetattr( fd, TCSANOW, &newttyUSB0)) != 0) //TCSANOW不等数据传输完毕就立即改变属性  110      {111          perror("com set error");112          return -1;113      }114     return 0;115 } /* ----- End of if()  ----- */116 

把以上这几个代码放到同一个文件夹中

用交叉编译器执行armgcc *.c生成a.out

mv  a.out gprs.out(因为开发板上.out文件多 所以重命名区分开)

所以应用tftp把gprs.c传到开发板上


这就弄完了  好好学代码内容  哈哈哈