ARM9和Linux下的GPRS模块C代码//我前个月使用实验室的周立功ARM7实验箱配套的明基GPRS模块连接到我的QQ2440V3开发板,//在Linux环境下,做模块的初始化、读取模块内的某些硬件信息和拨打电话,实现远程无线控制,//如果想做更复杂的操作,在这些代码的基础上增加少量代码即可实现,//下面的代码是C语言代码,大部分代码是我个人编写的,部分代码参考了一些书籍资料,//我发现很多网友使用明基GPRS模块的过程中出现各种问题,在这里我贴出来给大家参考//如转载本文章,请注明本文出处;structtermiosnewtio,oldtio;//==============================================================////明基M23GPRS模块AT指令,请参考明基的Datasheet//初始化指令AT+HRST=1,关闭模块的电源AT$POWEROF,//长时间不与M23GPRS模块通讯之后再次通讯时使用指令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则表明没有找到//数据通讯时,如果网络断开了,模块返回NOCARRIER//数据通讯时,调整发送数据的延时时间AT$TIMEOUT=XX,XX=100-5000毫秒//拨号指令“ATDxx;”,XX是号码,号码之后必须带分号//选择差分语音通道:M22/M22A,AT$AUPATH=3,1;M23,AT$AUPATH=0,1;差分语音通道,普通手柄通话//选择单端语音通道:M22/M22A,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*cmd[20]={AT,AT,AT$NOSLEEP=1,AT+GDT=1,AT$AUPATH=0,1,AT$AUGAIN=1,7,AT$AUGAIN=2,9};//==============================================================////==============================================================//inttty_writecmd(char*buf,intnbytes){inti;for(i=0;inbytes;i++){write(fd,&buf[i],1);usleep(100);}write(fd,\r\n,2);usleep(300000);returntcdrain(fd);}//inttcdrain(intFILEDES),The`tcdrain'functionwaitsuntilallqueuedoutputtotheterminalFILEDEShasbeentransmitted.////==============================================================////==============================================================//inttty_read(char*buf,intnbytes){intr=0;r=read(fd,buf,nbytes);returnr;}//==============================================================//intread_info(char*buf){intir=0,irn=0,nux=0;nux=read(fd,buf,1);if(nux!=0)//如果读到非零0个数据{while(buf[0]!='\0'){buf++;ir++;read(fd,buf,1);usleep(50);}}else{printf(读出0个数据!\n);}tcflush(fd,TCIOFLUSH);//清空读写缓存//returnirn=ir+1;}//==============================================================//inttty_write(char*buf,intnbytes){inti;for(i=0;inbytes;i++){write(fd,&buf[i],1);usleep(100);}returntcdrain(fd);}//==============================================================//inttty_end(){tcsetattr(fd,TCSANOW,&oldtio);//restoreoldmodemsetings////inttcsetattr(intFILEDES,intWHEN,conststructtermios*TERMIOS-P)//Thisfunctionsetstheattributesoftheterminaldevicewithfile//descriptorFILEDES.Thenewattributesaretakenfromthe//structurethatTERMIOS-Ppointsto//}//==============================================================//voidgprs_init(){intij=0,inf=0,numx=0;charrt3[100];for(ij=0;ij7;ij++){tty_writecmd(cmd[ij],strlen(cmd[ij]));bzero(rt3,sizeof(rt3[100]));inf=read_info(rt3);printf(返回信息:\n);printf(%s\n,rt3);}}//====================拨叫==========================================//voidgprs_call(char*number,intnum){inti=0;tty_write(ATD,3);//发送拨打命令ATDtty_write(number,num);tty_write(;,1);tty_write(\r\n,2);usleep(2000000);printf(正在打电话给:);for(i=0;inum;i++){printf(%c,number[i]);}}//==============================================================//intmain(void){intkf=0,wr=0,simno=0,rt2n=0,irt2=0;charrt[100];charrt2[100];charsim[]=AT+COPS?;charno[]=135960064**;//电话号码fd=open(/dev/s3c2410_serial0,O_RDWR);if(fd0){perror(open_porterror);return;}if((kf=set_opt(fd,115200,8,'N',1))0){perror(set_opterror);return;}gprs_init();wr=tty_writecmd(sim,strlen(sim));bzero(rt,sizeof(rt[100]));read_info(rt);if(rt[0]=='\0'){usleep(500);read_info(rt);}if(rt[0]!='\0'){printf(获取SIM卡\n);}else{printf(无法获取SIM卡\n);return;}tty_writecmd(AT+CIMI,strlen(AT+CIMI));//获取SIM卡的序列号//bzero(rt2,sizeof(rt2[100]));rt2n=read_info(rt2);printf(SIM序号:\n);printf(%s\n,rt2);tty_writecmd(AT+CGSN,strlen(AT+CGSN));//读取产品的序列号//tty_end();close(fd);//关闭设备文件return0;}////////////////初始化串口/////////////////////////intset_opt(intfd,intnSpeed,intnBits,charnEvent,intnStop){if(tcgetattr(fd,&oldtio)!=0){perror(SetupSerial1);return-1;}bzero(&newtio,sizeof(newtio));newtio.c_cflag|=CLOCAL|CREAD;newtio.c_cflag&=~CSIZE;switch(nBits){case7:newtio.c_cflag|=CS7;break;case8: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){case2400:cfsetispeed(&newtio,B2400);cfsetospeed(&newtio,B2400);break;case4800:cfsetispeed(&newtio,B4800);cfsetospeed(&newtio,B4800);break;case9600:cfsetispeed(&newtio,B9600);cfsetospeed(&newtio,B9600);break;case115200:cfsetispeed(&newtio