我在我的设备上使用.NET串口(CF3.9)。当我试图发送帧时,我会收到以下错误:
2019-02-07 10:26:39,414 [218497034] ERROR Communication.Serial.ControlCommunication - System.IO.IOException: IOException
at System.IO.Ports.SerialStream.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at Communication.Serial.ControlCommunication.WriteFrame(FrameType frameType, Byte[] frame, SamplerDataAddress samplerDataAddress)
at Communication.Serial.ControlCommunication.SerialWriteWithoutDelay(Byte[] frame, Int32 delay, SamplerDataAddress samplerDataAddress)
at Communication.Serial.ControlCommunication.SerialWrite(Byte[] frame, Int32 delay)
at Communication.Serial.ControlCommunication.ExternalThread()
at System.Threading.ThreadHelper.ThreadStartHelper(ThreadHelper t)
at System.Threading.ThreadHelper.ThreadStartHelper()
2019-02-07 10:26:39,467 [165216466] ERROR Communication.Serial.ControlCommunication - System.IO.IOException: IOException
at System.IO.Ports.SerialStream.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at Communication.Serial.ControlCommunication.WriteFrame(FrameType frameType, Byte[] frame, SamplerDataAddress samplerDataAddress)
at Communication.Serial.ControlCommunication.SerialWriteWithoutDelay(Byte[] frame, Int32 delay, SamplerDataAddress samplerDataAddress)
at Communication.Serial.ControlCommunication.SerialWrite(Byte[] frame, Int32 delay)
at Communication.Serial.ControlCommunication.RemoveOutput(ControlCommand command)
at Communication.Devices.ExternalLighting.ExternalLighting.SetLightingState(Boolean enabled)
at Communication.Devices.ExternalLighting.ExternalLighting.CheckState()
at Communication.Devices.ExternalLighting.ExternalLighting.Update()
at Communication.Serial.ControlCommunication.ExternalThread()
at System.Threading.ThreadHelper.ThreadStartHelper(ThreadHelper t)
at System.Threading.ThreadHelper.ThreadStartHelper()有WriteFrame方法:
private void WriteFrame(FrameType frameType, byte[] frame, SamplerDataAddress samplerDataAddress = SamplerDataAddress.None)
{
try
{
lock (serialPort)
{
if (serialPort.IsOpen)
serialPort.Close();
serialPort.Open();
if (serialPort != null && serialPort.IsOpen)
{
switch (frameType)
{
case FrameType.xx:
serialPort.RtsEnable = false;
byte[] versionFrame = ControlMethods.SendVersionReq();
serialPort.Write(versionFrame, 0, versionFrame.Length);
serialPort.BaseStream.Flush();
Thread.Sleep(10);
serialPort.RtsEnable = true;
Thread.Sleep(130);
if (serialPort.BytesToRead > 0)
{
byte[] received = new byte[serialPort.BytesToRead];
serialPort.Read(received, 0, serialPort.BytesToRead);
if (frame != null)
log.Info("CheckFekoIOVersionAndCrcErrors frame: " + BitConverter.ToString(frame));
ControlMethods.EvaluateVersionResponse(received);
}
break;
case FrameType.xy:
serialPort.RtsEnable = false;
var countFrame = ControlMethods.GetFekoResetsCount();
serialPort.Write(countFrame, 0, countFrame.Length);
serialPort.BaseStream.Flush();
Thread.Sleep(10);
serialPort.RtsEnable = true;
Thread.Sleep(200);
if (serialPort.BytesToRead > 0)
{
byte[] response = new byte[serialPort.BytesToRead];
serialPort.Read(response, 0, serialPort.BytesToRead);
if (response != null)
log.Info("CheckFekoIOVersionAndCrcErrors frame: " + BitConverter.ToString(response));
ControlMethods.EvaluateFekoResetsFrame(response);
}
break;
case FrameType.xyz:
serialPort.RtsEnable = false;
serialPort.Write(frame, 0, frame.Length);
serialPort.RtsEnable = true;
if (samplerDataAddress != SamplerDataAddress.None)
Thread.Sleep(200);
if (samplerDataAddress == SamplerDataAddress.GetTemperature)
frameLog.Debug("WriteFrame frame sended" + BitConverter.ToString(frame));
SerialDataReceived(serialPort, null, samplerDataAddress);
break;
}
serialPort.Close();
}
else
{
log.Info("Try to reopen serial port");
int serialPortOpenCounter = 0;
while (serialPortOpenCounter++ < 3 && !OpenSerialPort());
}
}
}
catch(Exception exc)
{
log.Error(exc);
}
}我如何理解和解决这个异常?
在应用程序通信是多线程,WriteFrame是由几个线程访问。在我的测试设备上,所有工作正常,但在客户的设备(设备站在户外),我有以上的错误。
使用Rs-485。
发布于 2019-02-12 14:43:57
在做了更多的研究之后,请看一看:
http://zachsaw.blogspot.com/2010/07/net-serialport-woes.html
引用如下:
你如何在过渡期间修复它?很简单。在调用SerialPort.Open()之前,只需通过将CreateFile和SetCommState的fAbortOnError调用为false来打开串口。现在,您可以安全地打开串口,而不必担心它可能会抛出一个IOException。我已经在C#中找到了一个示例解决方案,您可以将其用作参考。
祝好运!
https://stackoverflow.com/questions/54570961
复制相似问题