为了方便查看博客,特意申请了一个公众号,附上二维码,有兴趣的朋友可以关注,和我一起讨论学习,一起享受技术,一起成长。
一、实验目的:
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
);
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;
}
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
);
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
);
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)
{
newttys1
.c_cflag
&= ~CSTOPB
;
}
else if( nStop
== 2)
{
newttys1
.c_cflag
|= CSTOPB
;
}
newttys1
.c_cc
[VTIME
] = 0;
newttys1
.c_cc
[VMIN
] = 0;
tcflush(fd
,TCIFLUSH
);
if((tcsetattr( fd
, TCSANOW
,&newttys1
))!=0)
{
perror("com set error");
return -1;
}
return 0;
}
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))
exit(0);
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';
strcat(call
,number
);
strcat(call
,";\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;
}
int send_message
(int fd
)
{
int count
=0;
char cmgf
[]="at+cmgf=1\r";
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';
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(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(fd
,cmgs
,strlen(cmgs
));
printf("write %s\n",cmgs
);
sleep(5);
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语言打电话发短信
转载请注明原文地址: https://ju.6miu.com/read-1295776.html