ARM9和Linux下的GPRS模块C代码.doc
ARM9和Linux下的GPRS模块C代码/我前个月使用实验室的周立功ARM7实验箱配套的明基GPRS模块连接到我的QQ2440V3开发板,/在Linux环境下,做模块的初始化、读取模块内的某些硬件信息和拨打电话,实现远程无线控制,/如果想做更复杂的操作,在这些代码的基础上增加少量代码即可实现,/下面的代码是C语言代码,大部分代码是我个人编写的,部分代码参考了一些书籍资料,/我发现很多网友使用明基GPRS模块的过程中出现各种问题,在这里我贴出来给大家参考/如转载本文章,请注明本文出处#include <stdio.h> /标准输入输出定义#include <stdlib.h> /标准函数库定义#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" /自定义函数定义文件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/从模块读取短信而不是从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;M23,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", "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' function 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); 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 setings / /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(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); 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/s3c2410_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_info(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("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_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': newtio.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); cfsetospeed(&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,TCIFLUSH); if(tcsetattr(fd,TCSANOW,&newtio)!=0) perror("com set error"); return -1; tcflush(fd,TCIFLUSH);/The tcflush' function is used to clear the input and/or output /queues associated with the terminal file FILEDES. printf("set done!n"); return 0;