2017-02-15 107 views
2

我試圖用一個Arduino微控制器(試用​​過的Arduino UNO,Arduino的FIO),並使用串行通信接口它Simulink的電位器讀取數據(我試過波特率範圍從57600- 921600)。延遲Arduino的接口和Simulink

這裏是Arduino的源代碼:

/* 
    AnalogReadSerial 
    Reads an analog input on pin 0, prints the result to the serial monitor. 
*/ 
#define ana_pin A0 

void setup() { 
    analogReference(DEFAULT); 
    Serial.begin(57600); 
} 

// the loop routine runs over and over again forever: 
void loop() { 
    // read the input on analog pin 0: 
    int sensorValue = analogRead(ana_pin); 
    // print out the value you read: 
    Serial.print(sensorValue); 
// delay(500);  // delay in between reads for stability 
} 

我與特拉期限軟件接口,並有對應於3 V或0V值的瞬時變化。

然而,當我嘗試使用儀器控制工具箱與Simulink模型接口它:

enter image description here

存在當從3V的ASCII表示值變化至0V 10秒的滯後

enter image description here

該塊採樣時間0.01秒和模型配置參數a重新調整相應的(我試了1秒或更多,延遲仍然存在。此外,我能夠毫不遲延地記錄來自其他傳感器和開發板的數據。

我還與Arduino的支持庫試圖在Simulink中:

enter image description here

而且似乎沒有接收到數據,你可以從Scope1在PNG文件中的狀態信號總是0看到。我還附上從塊的Arduino硬件實現性能Simulink中:

enter image description here

你能幫我明白是怎麼回事,如何解決這個問題呢?

+0

當您更改比特率時,延遲長度是否不變? –

+1

在arduino草圖中,您應該嘗試用'Serial.println(sensorValue);'替換'Serial.print(sensorValue);';它會添加一個終止字符'\ n'(或者窗口中的\ r \ n')。大多數串口函數都會等待一個特殊的字符來知道傳輸已經結束。在你失敗的案例中,我認爲simulink示波器的超時時間爲10秒。 –

+0

感謝您的回覆。建議我使用Serial.println函數,並將儀器控制工具箱中的終止符設置爲<\r\n>。然而,感知價值變化之間的10秒延遲仍然存在。使用Serial.println函數並將ICT中的終止符設置爲,延遲增加約幾秒(總延遲20秒)。直觀地講,它可以理解爲讀取和顯示'\ r \ n'的ASCII字符的延遲。所以我認爲這個延遲是由於arduino和simulink接口造成的。當我改變比特率時,延遲的長度也是恆定的。 –

回答

0

@Patrick Trentin - 我得到的4秒的延遲,當我用的230400,460800和921600. 波特率爲57600波特率,115200我得到10秒的延遲。 謝謝你指出,我以前沒有專注於此。然而,由於我將在每0.01秒精確需要讀數的應用中使用傳感器。我不認爲我可以延遲4秒。