2017-04-21 46 views
0

請問,我很好奇,軟件系列系列的波特率對我的Arduino代碼的後續執行有什麼影響?我注意到我的代碼中有一個奇怪的行爲。它不斷打印出Test1,它永遠不會進一步。當我的波特率是相同的(9600),我不斷得到不斷。所以我需要解釋一下真正發生的事情。Arduino代碼中的不斷迭代

test.ino

#include "gsm.h" 
#include <SoftwareSerial.h> 
#include <string.h> 
#include <stdio.h> 

SoftwareSerial mySerial(10, 11); // RX, TX 
gsm gm; 
char temp[10]; 

void setup() { 
    Serial.begin(57600); 
    mySerial.begin(9600); 
    gm.getMsg(mySerial, 1); 
    Serial.print("Test1"); 
    if(mySerial.available()){ 
    mySerial.readBytes(temp, 10); 
    Serial.print("Test2"); 
    } 
    Serial.print("Test3"); 
    gm.getText(temp); 
} 

void loop() { 

} 

gsm.cpp

#include "gsm.h" 
#include <string.h> 
#include <arduino.h> 
#include <SoftwareSerial.h> 

void gsm::getText(char str[]) { 
    char *temp1 = strrchr(str, '\"')+3; 
    int l1 = strlen(temp1); 
    char *temp2 = strchr(temp1,'\n'); 
    int l2 = strlen(temp2); 
    int len = l1-l2; 
    char msg[10]; 
    strncpy(msg, temp1, len); 
    msg[len] = '\0'; 
    strncpy(message, msg,len); 
} 

void gsm::getMsg(SoftwareSerial serial, int index) { 
    serial.print("AT+CMGR = "); 
    serial.print(index); 
    serial.print("\r\n"); 
    delay(300); 
} 

gsm.h

#ifndef GSM_H_ 
#define GSM_H_ 
#include <SoftwareSerial.h> 

struct gsm{ 
    char message[20]; 
    void getText(char str[]); 
    void getMsg(SoftwareSerial serial, int index); 
}; 

#endif 
+0

你讀一個字節爲長度爲1的字符數組的期望它是一個字符串? –

+0

其錯字@ gre_gor –

回答

0

您需要設置串行監視器上的波特率相匹配的波特率你設置了Serial.begin

(照片信貸Adafruit

serial monitor baud rate

+0

我做到了,但仍然無法正常工作。@ Keshzv Saharia –