【发布时间】:2016-12-21 12:27:30
【问题描述】:
基本概述。由于 WPF 应用程序,我正在从 Arduino 发送串行数据。到目前为止,这一切都运行良好。今天我在 Arduino 代码中实现了一个循环,该循环在串行端口中查找“Y”(ascii 89),如果收到它会离开循环并返回到我所说的离线模式并停止通过在线发送数据=假的。
现在奇怪的是……
在此循环之前它工作正常,因此它必须与在离开“在线循环”后尝试重新发送新数据有关。
它在 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 字节(例如),然后当您收到正确的字符串时,您可以停止它。另一个计时器可以检测到超时(如果您收到正确的数据,只需在它被触发之前停止它)。最后通过读取传入的数据
-
DataReceived event SerialPort 类。请记住始终附加到缓冲区,然后在完成后从中删除。试试这个,我认为它会起作用(这种方法通常可以节省我在串行端口行为不正确时的一天)
-
嗨@frarugi87,感谢您的回复!我已经实现了计时器,它对一切都有很大帮助,它更好! RE myPort.ReadExisting 不正确。我知道你从哪里来,但是读取和附加一个字节当前不起作用,因为 Arduino 似乎没有发送任何东西,就软件而言无论如何。因此应用程序没有收到任何东西,而且绝对不是“S”
-
事实证明,在我的开关盒中有一个 resetFunc() 阻止了串行监视器继续发送数据!总有一条线可以支撑你!
-
好吧,一切都好,结果很好 :-) 很高兴你能找到解决问题的方法
标签: c# wpf visual-studio arduino arduino-due