节分端午自谁言,万古传闻为屈原。堪笑楚江空渺渺,不能洗得直臣冤。——文秀《端午》
前面写了GPS模块的C编程,这篇文章主要讲实现GPRS的C语言实现拨打电话以及发短信。
由于GPRS模块的原因,在开发板上不能直接监听串口,必须先用串口调试助手确定模块能用以后,我们才能在开发板上使用,如果模块支持直接在开发板上使用,那么这步不用做。
串口调试助手成功了,然后直接将串口线插入开发板的USB,因为我们使用的仍旧是/dev/ttyUSB0这个串口,然后和前面一样,使用网线连接开发板,这里在上篇博客已经说明如何使用网线连接开发板,不再赘述。
进入开发版以后,和GPS一样,使用microcom命令监听我们的串口,GPRS波特率为115200,因此:
串口监听成功后便可以像在串口调试助手上一样输入命令,执行操作,但是这里有个地方比较无奈,串口调试助手上拨打电话必须要在最后加“;”,在开发板上加不加都即可。
这里成功之后就可以写代码了
1、串口的设置代码和以前的一样,使模板就行,只是波特率要改成115200。
2、打电话程序
/*********************************************************************************
* Copyright: (C) 2017 tangyanjun<519656780@qq.com>
* All rights reserved.
*
* Filename: call.c
* Description: This file
*
* Version: 1.0.0(05/29/2017)
* Author: tangyanjun <519656780@qq.com>
* ChangeLog: 1, Release initial version on "05/29/2017 10:24:55 AM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include"gprs.h"
/**************************************************************************************
* Description: gprs打电话函数
* Input Args: fd 串口设备文件描述符
* Output Args:
* Return Value:
*************************************************************************************/
int call_number (int fd)
{
getchar(); //将缓冲区回车吃掉
int count = 0;
char call[20] = "atd";
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将number字符串加到call尾部
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));
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;
}
3、发短信程序
/*********************************************************************************
* Copyright: (C) 2017 tangyanjun<519656780@qq.com>
* All rights reserved.
*
* Filename: masseage.c
* Description: This file
*
* Version: 1.0.0(05/29/2017)
* Author: tangyanjun <519656780@qq.com>
* ChangeLog: 1, Release initial version on "05/29/2017 10:37:03 AM"
*
********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "gprs.h"
/**************************************************************************************
* Description: gprs发短信函数
* Input Args: fd 串口设备文件描述符
* Output Args:
* Return Value:
*************************************************************************************/
int send_message (int fd)
{
int count = 0;
char cmgf[] = "at+cmgf=1\r"; //设置短信发送模式为text (0-PDU;1-text)
char cmgs[128] = "at+cmgs=\"";
char send_number[16] = {};
char message[128] = {};
char reply[128] = {};
getchar(); //吃掉缓冲区回车
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");
printf("enter send_message :\n");
if(NULL == fgets(message, 128, stdin))
exit(0);
message[strlen(message) -1 ] = '\0';
strcat(message, "\x1a");
/* write cmgf */
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 cmgs */
write(fd, cmgs, strlen(cmgs));
printf("writr %s\n", cmgs);
sleep(5);
/*write message*/
write(fd, message, strlen(message));
printf("writr %s\n", message);
sleep(4);
memset(reply, 0, sizeof(reply));
read(fd, reply, sizeof(reply));
printf("%s\n", reply);
return 0;
}
4、main.c
/*********************************************************************************
* Copyright: (C) 2017 tangyanjun<519656780@qq.com>
* All rights reserved.
*
* Filename: main.c
* Description: This file
*
* Version: 1.0.0(05/29/2017)
* Author: tangyanjun <519656780@qq.com>
* ChangeLog: 1, Release initial version on "05/29/2017 10:42:28 AM"
*
********************************************************************************/
#include"gprs.h"
int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop);
int send_message(int fd);
int call_number(int fd);
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int fd;
char select;
fd = open( "/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd < 0){
perror("Can't Open Serial Port");
return -1;
}
//配置串口
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;
} /* ----- End of main() ----- */
5、头文件
#ifndef __GPRS_H__
#define __GPRS_H__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
extern int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
extern int send_message(int fd);
extern int call_number(int fd);
#endif
6、Makefile
CC =/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc
objs = call.o masseage.o serial.o main.o
srcs = call.c masseage.c serial.c main.c
gprs: $(objs)
$(CC) -o gprs $(objs)
@make clean
main.o: $(srcs) gprs.h
$(CC) -c $(srcs)
call.o: call.c gprs.h
$(CC) -c call.c
serial.o: serial.c
$(CC) -c serial.c
masseage.o: masseage.c gprs.h
$(CC) -c masseage.c
clear:
@rm gprs
.PHONY: clean
clean:
@rm *.o
将上述文件放在同一个文件夹下,交叉编译下载至开发板,给权限,执行即可:
仔细看着这些代码,可以看出来这些都是对字符串的处理,以及我们对文件I/O的处理,下面简单说一下本程序用到的几个字符串函数:
strcat:将后面的字符串加到前面。
strlen:求字符串的长度。
strcpy:将后面的字符串内容复制到前一个去。
对于字符串的处理函数我们最好使用strncat、strnlen等函数,防止字符串的溢出。
下一篇文章我将总结一下常用的字符串处理函数。这里就不多说了。