近些天一直在研究XC-TC35 模塊,此模塊使用起來特別方便,只要熟悉一些AT指令及簡單的單片機編程便可輕鬆使用編程
廢話很少說,下面粘出我寫的代碼oop
下面粘出代碼調試
//收發 #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; }