近些天一直在研究XC-TC35 模块,此模块使用起来特别方便,只要熟悉一些AT指令及简单的单片机编程即可轻松使用
废话不多说,下面粘出我写的代码
下面粘出代码
//收发 #include<string.h> #define Buf_Max 100 unsigned char Rec_Buf[Buf_Max]; unsigned char i = 0; void setup(){ Serial.begin(9600);//此串口调试时使用 Serial1.begin(9600);//通信使用 Serial1.println("AT");//握手连接 delay(100); while(strstr((const char*)Rec_Buf,"OK") == NULL)SerialRec();; Serial.println("AT"); Serial.println("OK"); CLR_Buf(); Serial1.println("AT+CSCS=\"UCS2\"");// delay(100); while(strstr((const char*)Rec_Buf,"OK") == NULL)SerialRec();; Serial.println("AT"); Serial.println("OK"); CLR_Buf(); Serial1.println("AT+CMGF=0");//PDU delay(100); while(strstr((const char*)Rec_Buf,"OK") == NULL)SerialRec();; Serial.println("AT+CMGF=0"); Serial.println("OK"); CLR_Buf(); Serial1.println("AT+CNMI=2,1");//短信输出方式 delay(100); while(strstr((const char*)Rec_Buf,"OK") == NULL)SerialRec();; Serial.println("AT+CNMI=2,1"); Serial.println("OK"); CLR_Buf(); Serial1.println("AT+CMGD=1");//删除短信 delay(100); while(strstr((const char*)Rec_Buf,"OK") == NULL) SerialRec();; Serial.println("AT+CMGD=1"); Serial.println("OK"); CLR_Buf(); } void loop(){ SerialRec(); while(strstr((const char*)Rec_Buf,"+CMTI")){ CLR_Buf(); Serial1.println("AT+CMGR=1"); delay(100); while(strstr((const char*)Rec_Buf,"OK") == NULL)SerialRec();; if(strstr((const char*)Rec_Buf,"5173")!=NULL){ Serial.println("led turn off"); CLR_Buf(); Serial1.println("AT+CMGS=19");//基数为15,一个汉字对应两个字节 delay(100); while(strstr((const char*)Rec_Buf,">") == NULL) SerialRec();; Serial1.print("0011000D91685165950629F30008AA045173706F"); //0011000D91685165950629F30008AA 04表示发送的字节的个数 5173706F关灯的unicode码 delay(100); Serial1.write(0x1A);//结束符 delay(100); while(strstr((const char*)Rec_Buf,"+CMGS") == NULL) SerialRec();; Serial.println("sendding message is successful"); CLR_Buf(); } else if(strstr((const char*)Rec_Buf,"5F00")!=NULL){ Serial.println("led turn on"); } CLR_Buf(); Serial1.println("AT+CMGD=1");//删除短信 delay(100); while(strstr((const char*)Rec_Buf,"OK") == NULL)SerialRec();; delay(100); CLR_Buf(); } } void SerialRec(){ while(Serial1.available()){ Rec_Buf[i] = Serial1.read(); i++; if(i>Buf_Max){ i = 0; } } } void CLR_Buf(){ for(unsigned k=0;k<Buf_Max;k++){ Rec_Buf[k]=0; } i=0; }
原文:http://my.oschina.net/u/2424848/blog/517781