基本概述。我正在從Arduino的WPF應用程序發送串行數據。到目前爲止,這一切都是完美的。今天,我在Arduino代碼中實現了一個循環,該代碼在串口中查找「Y」(ascii 89),如果收到則退出循環並返回到我正在呼叫離線模式並停止通過數據發送,通過online =假。從VS WPF到Arduino的串行通信時的奇怪行爲
現在是什麼奇怪這是...
正是這種循環前工作正常,所以它必須是與試圖重新發送新數據一旦離開了「網上循環」。
從Arduino串行監視器,它表明它是一個WPF問題完美工作,雖然代碼沒有改變上傳部分。
這兩個程序的代碼都很大,所以我會盡量保持簡潔,同時提供所有必要的信息。
void loop() {
// Check to see if the testbench is in offline mode and run the respective code.
if (Online == false) {
OfflineMode();
}
// Check to see if the testbench is in online mode and run the respective code.
if (Online == true) {
OnlineMode();
}
}
void OfflineMode() {
while (Serial.available())
processlncomingByte(Serial.read());
}
我那麼有開關的情況下處理傳入的設置 - 我知道這工作得很好,因爲它也將上傳後的Arduino是復位。
void processlncomingByte (const byte c) {
if (isdigit (c)) {
currentValue *= 10;
currentValue += c - '0';
} else {
// end of digit
// The end of the number signals a state change
handlePreviousState();
// set the new state, if we recognize it
switch (c) {
case 'A':
state = GOT_A;
break;
etc...
聯機模式
void OnlineMode() {
CheckForStop();
SendSerialData();
}
void CheckForStop() {
//Serial.println("...");
if (Serial.available() > 0) {
//Serial.println("getting something");
ch = (char)Serial.read();
inputString = ch;
if (ch == 89) {
//Serial.end();
Online = false;
//Serial.begin(9600);
exit;
//return;
}
} else
delay(5);
}
SendSerialData()只包含一個範圍的serial.print
的,輸出爲一個大字符串爲WPF處理。
Here is a screenshot of the serial monitor working
,你從鏈接見上面的監視器吐出數據的加載,停止的時候我送Y和最後我送Q可「問題」 Arduino的是否準備好接收設置S表示是。偉大的東西它的作品!
但是,正如你可以從下面的鏈接看到,這不是在WPF中的情況。對不起,我目前只能上傳2張圖片,所以不得不合並它們。
這是它目前被陷在
private bool checkArduinoisReady() {
Stopwatch Uploadtimer = new Stopwatch();
if (!myPort.IsOpen)
return false;
// Debug.Print("port is ready to be opened");
string tempdata;
Uploadtimer.Start();
myPort.DiscardInBuffer();
Start:
myPort.WriteLine("Q" + Environment.NewLine);
Debug.Print("sent Q");
tempdata = myPort.ReadExisting();
Debug.Print("tempdata_" + tempdata.ToString());
if (Uploadtimer.ElapsedMilliseconds > 5000)
return false;
if (tempdata.Contains("S"))
return true;
else
goto Start;
}
和一個單獨的頁面上的循環,這是怎麼了停止輸入數據。
private void StopTest(object sender, RoutedEventArgs e) {
MessageBoxResult StopConfirm = MessageBox.Show("Are you sure you want to stop the test?", "Stop the test", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (StopConfirm == MessageBoxResult.Yes) {
Timer.Stop();
Debug.Print("Timer Stopped");
myPort.DiscardInBuffer();
Start:
for (int i = 0; i < 100; i++) {
myPort.WriteLine("Y");
}
string tempData = myPort.ReadExisting();
Debug.Print("Checking...");
Debug.Print("tempData_" + tempData);
if (string.IsNullOrWhiteSpace(tempData)) {
Debug.Print("Its null!!");
comments_textbox.Text = comments_textbox.Text + "Test Aborted";
MessageBoxResult SaveCurrentData = MessageBox.Show("Would you like to save the data collected up until this point?", "Save", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (SaveCurrentData == MessageBoxResult.Yes) {
SaveFile();
}
if (SaveCurrentData == MessageBoxResult.No) {
myPort.Close();
NavigationService.Navigate(new Uri("testSettings.xaml", UriKind.RelativeOrAbsolute));
}
} else {
Debug.Print("Still going...");
goto Start;
}
}
}
對我來說,最大的絆腳石就是爲什麼它可以在串口監視器上工作,而不是在應用程序中。而且,只要我重置Arduino,它也可以工作。我也嘗試了Arduino中的resetFunc()
,但這也沒有幫助。
在此先感謝。
首先,我會在發送和重新索引功能之間放一小段時間。然後串口也可以返回一個部分消息,所以寫「tempdata = myPort.ReadExisting();」是不正確的(你應該追加)。最後,我將串口用作異步設備,而不是同步設備。您可以使用1)定時器每秒發送Q字節(例如),然後當您收到正確的字符串時,您可以將其停止。另一個計時器可以檢測到超時(如果您收到正確的數據,請在啓動之前將其停止)。最後通過 – frarugi87
[DataReceived事件](https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v = vs.110).aspx)讀取傳入數據。 SerialPort類。請記住總是附加到緩衝區,然後在完成時從中刪除。試試這個,我認爲它會起作用(這種方法通常會在我的那天在串行端口表現不正常時保存) – frarugi87
HI @ frarugi87,感謝您的回覆!我已經實現了這個計時器,它可以幫助你做很多事情,更好! RE myPort.ReadExisting不正確。我看到你是從哪裏來的,然而讀取和附加一個字節,例如目前不起作用,因爲無論如何,Arduino似乎都沒有發送任何東西,就軟件而言。因此應用程序沒有收到任何東西,並且絕對不是「S」 – charley