ARM9和Linux下的GPRS模块C代码_第1页
ARM9和Linux下的GPRS模块C代码_第2页
ARM9和Linux下的GPRS模块C代码_第3页
ARM9和Linux下的GPRS模块C代码_第4页
ARM9和Linux下的GPRS模块C代码_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、ARM9和Linux下的GPRS模块C代码/我前个月使用实验室的周立功ARM7实验箱配套的明基GPRS模块连接到我的QQ2440V3开发板,/在Linux环境下,做模块的初始化、读取模块内的某些硬件信息和拨打电话,实现远程无线控制,/如果想做更复杂的操作,在这些代码的基础上增加少量代码即可实现,/下面的代码是C语言代码,大部分代码是我个人编写的,部分代码参考了一些书籍资料,/我发现很多网友使用明基GPRS模块的过程中出现各种问题,在这里我贴出来给大家参考/如转载本文章,请注明本文出处#include <stdio.h> /标准输入输出定义#include <stdlib.h&

2、gt; /标准函数库定义#include <unistd.h> /Unix 标准函数定义#include <fcntl.h> /文件控制定义#include <sys/types.h>#include <sys/stat.h>#include <termios.h> /PPSIX 终端控制定义#include <time.h>#include <string.h>#include <errno.h> /错误号定义#include "myfunctions.h" /自定义函数定义文

3、件static int fd=0;struct termios newtio,oldtio;/=/明基M23 GPRS模块AT指令,请参考明基的Datasheet/初始化指令AT+HRST=1,关闭模块的电源AT$POWEROF,/长时间不与M23 GPRS模块通讯之后再次通讯时使用指令AT$NOSLEEP=1/在拨打电话时,在对方接通电话之后返回“OK”字符串,使用指令AT+COLP=1/当有电话进入时,显示来电号码,在初始化时需加上指令AT+CLIP=1/读取SIM卡的ID号使用指令AT+CIMI/写入短信中心号码,使用AT+CSCA="短信中心的号码",145/从模块

4、读取短信而不是从SIM卡读取,使用AT+CNMI=2,2,0,0,0/提供本地待机提示音,使用AT+GDT=1,关闭则用AT+GDT=0/确定已经找到SIM卡,使用AT+COPS?,若返回0则表明没有找到/数据通讯时,如果网络断开了,模块返回NO CARRIER/数据通讯时,调整发送数据的延时时间AT$TIMEOUT=XX,XX1005000毫秒/拨号指令“ATDxx;”,XX是号码,号码之后必须带分号/选择差分语音通道:M22M22A,AT$AUPATH=3,1;M23,AT$AUPATH=0,1;差分语音通道,普通手柄通话/选择单端语音通道:M22M22A,AT$AUPATH=2,1;M2

5、3,AT$AUPATH=1,1;单端语音通道,免提童话/语音通话时,调节发送和接收增益:AT$AUGAIN=(0-2),(1-9) 其中的(0-2)代表:0发送的SIDE /TONE,1发送增益,2接收增益;(1-9)代表可选择的增益范围/另一条调节接收增益的AT指令AT$AUVOL=(0-5),0则关闭接收,5为最大/吉林移动的信息中心+8613800200500/初始化GPRS模块的指令阵列/char * cmd20= "AT", "AT", "AT$NOSLEEP=1", "AT+GDT=1", "

6、AT$AUPATH=0,1", "AT$AUGAIN=1,7", "AT$AUGAIN=2,9"/=/=/int tty_writecmd(char *buf,int nbytes) int i; for(i=0; i<nbytes; i+) write(fd,&bufi,1); usleep(100); write(fd,"rn",2); usleep(300000); return tcdrain(fd);/int tcdrain (int FILEDES),The tcdrain' functio

7、n waits until all queued output to the terminal FILEDES has been transmitted./=/=/int tty_read(char *buf,int nbytes) int r=0; r=read(fd,buf,nbytes); return r;/=/int read_info(char *buf) int ir=0,irn=0,nux=0; nux=read(fd,buf,1); if(nux!=0)/如果读到非零0个数据 while(buf0!='0') buf+; ir+; read(fd,buf,1)

8、; usleep(50); else printf("读出0个数据!n"); tcflush(fd,TCIOFLUSH);/清空读写缓存/ return irn=ir+1;/=/int tty_write(char *buf,int nbytes) int i; for(i=0; i<nbytes; i+) write(fd,&bufi,1); usleep(100); return tcdrain(fd);/=/int tty_end() tcsetattr(fd,TCSANOW,&oldtio); / restore old modem setin

9、gs / /int tcsetattr (int FILEDES, int WHEN, const struct termios *TERMIOS-P) /This function sets the attributes of the terminal device with file / descriptor FILEDES. The new attributes are taken from the /structure that TERMIOS-P points to/=/void gprs_init() int ij=0,inf=0,numx=0; char rt3100; for(

10、ij=0; ij<7; ij+) tty_writecmd(cmdij, strlen(cmdij); bzero(rt3,sizeof(rt3100); inf=read_info(rt3); printf("返回信息:n"); printf("%sn",rt3); /=拨叫=/ void gprs_call(char *number, int num) int i=0; tty_write("ATD",3); /发送拨打命令ATD tty_write(number,num); tty_write("",1

11、); tty_write("rn",2); usleep(2000000); printf("正在打电话给:"); for(i=0;i<num;i+) printf("%c",numberi); /=/int main(void) int kf=0,wr=0,simno=0,rt2n=0,irt2=0; char rt100; char rt2100; char sim="AT+COPS?" char no="135960064*"/电话号码 fd = open("/dev/s3

12、c2410_serial0",O_RDWR); if(fd<0) perror("open_port error"); return; if(kf=set_opt(fd,115200,8,'N',1)<0) perror("set_opt error"); return; gprs_init(); wr=tty_writecmd(sim,strlen(sim); bzero(rt,sizeof(rt100); read_info(rt); if(rt0='0') usleep(500); read_i

13、nfo(rt); if(rt0!='0') printf("获取SIM卡n"); else printf("无法获取SIM卡n"); return; tty_writecmd("AT+CIMI",strlen("AT+CIMI"); /获取SIM卡的序列号/ bzero(rt2,sizeof(rt2100); rt2n=read_info(rt2); printf("SIM序号:n"); printf("%sn",rt2); tty_writecmd(&quo

14、t;AT+CGSN",strlen("AT+CGSN"); /读取产品的序列号/ tty_end(); close(fd);/关闭设备文件 return 0; /初始化串口/int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) if ( tcgetattr( fd,&oldtio) != 0) perror("SetupSerial 1"); return -1; bzero( &newtio, sizeof( newtio ) ); newtio.c

15、_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': newti

16、o.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); cfseto

17、speed(&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_ccVTIME = 0; newtio.c_ccVMIN = 0; tcflush(fd,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论