0
我有3種不同的功能,使用我的Arduino和SIM900發送短信。Arduino附近具有完全不同輸出的相同功能
第一功能有兩個參數,併發送給它的目標電話號碼
void sendText(char msg[], char num[]) {
Serial.print("[Text] Sending text to ");
Serial.println(num);
simController.print("AT+CMGF=1\r");
delay(100);
simController.print("AT + CMGS = \"");
simController.print(num);
simController.println("\"");
delay(100);
Serial.print("[Text] ");
Serial.println(msg);
simController.println(msg);
delay(100);
simController.println((char)26);
delay(100);
simController.println();
delay(5000);
}
最後兩個函數是幾乎相同的,除了它們具有不同數目的參數,並且具有完全不同的結果
字符數組void sendText(char msg[], char num[], int startChar) {
char *newMsg = (char *)malloc(strlen(msg)+1);
memset(&newMsg[0], 0, sizeof(newMsg));
Serial.print("[Text] Sending text to ");
Serial.println(num);
simController.print("AT+CMGF=1\r");
delay(100);
simController.print("AT + CMGS = \"");
simController.print(num);
simController.println("\"");
delay(100);
for (int x=0;x<strlen(msg);x++) newMsg[x] = msg[x+startChar];
Serial.print("[Text] ");
Serial.println(newMsg);
simController.println(newMsg);
delay(100);
simController.println((char)26);
delay(100);
simController.println();
delay(5000);
}
void sendText(char msg[], char num[], int startChar, int endChar) {
char *newMsg = (char *)malloc(strlen(msg)+1);
memset(&newMsg[0], 0, sizeof(newMsg));
Serial.print("[Text] Sending text to ");
Serial.println(num);
simController.print("AT+CMGF=1\r");
delay(100);
simController.print("AT + CMGS = \"");
simController.print(num);
simController.println("\"");
delay(100);
for (int x=0;x<strlen(msg);x++) newMsg[x] = msg[x+startChar];
Serial.print("[Text] ");
Serial.println(newMsg);
simController.println(newMsg);
delay(100);
simController.println((char)26);
delay(100);
simController.println();
delay(5000);
}
調用這些功能看起來像這樣
sendText("Hello","+447597865XXX");
sendText("Hello","+447597865XXX", 1);
sendText("Hello","+447597865XXX", 1, 3);
當我使用的代碼
輸出看起來像這樣
[Text] Sending text to +447597865XXX
[Text] Hello
[Text] Sending text to +447597865XXX
[Text] ello
[Text] Sending text to +447597865XXX
[Text] ell��U�����=ڨl��fr}Ѧ�o�e�6�������f��+��V���_{���
這究竟是爲什麼?記得有是void sendText(char msg[], char num[], int startChar, int endChar)
身體和void sendText(char msg[], char num[], int startChar)