【发布时间】:2021-08-19 11:10:35
【问题描述】:
这是我在 Stackoverflow 中的第一个问题,如果我忘记提及某些内容,请见谅!
我正在使用 WPF C# 应用程序和 Arduino DUE 为工业洗衣机创建界面。 它们通过串行端口进行通信。
串口当前设置:
- 波特率 = 115200
- 通过 USB 电缆(arduino 编程端口)
- 已启用 DTR 和 Rts
- WriteTimeout = 1000;
Arduino 正在毫无延迟地打印到串行端口,所有模拟输入引脚和几个数字输入引脚的状态。
在界面的当前状态下,我尝试简单地打开和关闭某些引脚,但是 c# 应用程序冻结然后崩溃,并显示以下错误消息 = "System.IO.IOException: '信号量超时时间已过期。”。
以下是我的一些经验和以下结果:
-
将 BaudRate 降低到 9600 设法使一些消息通过并且引脚关闭然后打开,如果我将它们更改得太快,则会出现相同的错误消息。
-
波特率回到 115200 并且在 Arduino 代码上我停止将输入引脚状态发送到串行端口,错误消失了,我可以快速更改引脚状态而没有任何问题,但我不能从 arduino 中读取任何内容。
以下代码表示启用发送模拟和数字输入状态的 arduino 代码。 要禁用我评论最后 2 行
void read_input_analog()
{
for(int i = 54; i<= NUMBER_ANALOG_INPUT_PORTS; i++)
{
String pinNumber = "A";
pinNumber += i;
String message = "#A";
message += i-54;
message +=":";
// IMPORTANT NOTE: 54 represents analog pin A0 and 64 Analog pin A11
message += analogRead(i);
message +='!';
Serial.println(message);
}
}
void read_input_digital()
{
for(int i = 22; i<= NUMBER_DIGITAL_INPUTS; i++)
{
String pinNumber = "D";
pinNumber += i;
String message = "#D";
message += i;
message +=":";
// IMPORTANT NOTE: 54 represents analog pin A0 and 64 Analog pin A11
message += digitalRead(i);
message +='!';
Serial.println(message);
}
}
void loop(){
while(Serial.available()>=8)
{
char c = Serial.read();
Serial.println(c);
if(c == '#')
{
recieve_order();
}
}
read_input_analog();
read_input_digital();
}
编辑 1 串行端口设置中未提及的所有属性均保留为默认值。
DataBits 属性默认为 8,Parity 属性默认为 None 枚举值,StopBits 属性默认为 1
编辑 2
我注意到 UART 只能作为半双工工作,我错误地认为是全双工。从那以后,我使用 millis() 函数增加了从 arduino 端发送信息的延迟。这对问题有很大帮助,但问题仍然存在,但不那么频繁了。
编辑 3
我已经做到了,因此 Arduino 上的读取代码通过在每次一个字节进入缓冲区时附加一个字符串来更快地清除缓冲区,而不是让它在做某事之前等待填充 8 个字节。
它有助于发送单个消息不再使程序崩溃,但是现在有了一个滑块来控制模拟信号,当滚动滑块发送大量消息时程序崩溃!
我还测试了一个 Windows 窗体项目(我在网上找到的),它的滑块就像一个魅力!没有崩溃!
System.IO.IOException
HResult=0x80070079
Message=The semaphore timeout period has expired.
Source=System
StackTrace:
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream.EndWrite(IAsyncResult asyncResult)
at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
at System.IO.Ports.SerialPort.Write(String text)
at Washing_Machine_Interface.Communication.Arduino.ArduinoProtocol.AnalogValue(Int32 pinNumber, Int32 power) in D:\André O Viking\Projetos\Washing_Machine_Interface\washing-machine-interface\WM_Interface\Washing_Machine_Interface\Washing_Machine_Interface\Communication\Arduino\ArduinoProtocol.cs:line 67
at Washing_Machine_Interface.MainWindow.Pin2Slider_ValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 e) in D:\André O Viking\Projetos\Washing_Machine_Interface\washing-machine-interface\WM_Interface\Washing_Machine_Interface\Washing_Machine_Interface\MainWindow.xaml.cs:line 115
at System.Windows.RoutedPropertyChangedEventArgs`1.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.RangeBase.OnValueChanged(Double oldValue, Double newValue)
at System.Windows.Controls.Slider.OnValueChanged(Double oldValue, Double newValue)
at System.Windows.Controls.Primitives.RangeBase.OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
at System.Windows.Controls.Slider.UpdateValue(Double value)
at System.Windows.Controls.Slider.OnThumbDragDelta(DragDeltaEventArgs e)
at System.Windows.Controls.Slider.OnThumbDragDelta(Object sender, DragDeltaEventArgs e)
at System.Windows.Controls.Primitives.DragDeltaEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.Thumb.OnMouseMove(MouseEventArgs e)
at System.Windows.UIElement.OnMouseMoveThunk(Object sender, MouseEventArgs e)
at System.Windows.Input.MouseEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at Washing_Machine_Interface.App.Main()
【问题讨论】:
-
“通过 USB 电缆” -- 请说明:使用 CDC ACM 的 UART 接口或 USB 接口。如果稍后,那么为什么以及如何“启用 DTR 和 Rts”?
-
@sawdust 使用 UART 接口,我已启用 dtr 和 Rts 以尝试根据我遇到的一些信息解决问题,但没有任何改变,老实说,我不确定我是否理解目的!我在 C# 端打开串口时启用了它
-
根据arduino.cc/en/Guide/ArduinoDue这个板子有2个USB口,你用哪个USB口和C#通信?您是否按照文档中的说明安装了驱动程序?见create.arduino.cc/projecthub/Arduino_Genuino/…
-
@user9938 如原问题所述,我使用 Arduino DUE 的编程端口,是的,我已经安装了!
-
您的帖子显示的串口属性设置很少。您可以考虑将它们全部添加到您的帖子中。以下内容可能会有所帮助:stackoverflow.com/questions/68777489/…
标签: c# arduino serial-port timeout semaphore