读取串行端口时IO操作中止引发的错误 [英] IO operation aborted error thrown while reading serial port

查看:287
本文介绍了读取串行端口时IO操作中止引发的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试读取使用.Net串行端口类连接到串行端口的外部设备(在这种情况下为秤)写入的数据.

We are trying to read data written by an external device (weighing scale in this case) connected to serial port using .Net serial port class.

首先,我们按如下所示初始化串行端口:

First we initialize the serial port as below:

InitializeSerialPort()
{
   if ((serialPort != null) && (serialPort.IsOpen))
        {
            serialPort.Close();
            serialPort.Dispose();
            serialPort = null;
        }

        serialPort = new SerialPort("COM2", 9600, Parity.None, 8,
                                    StopBits.One) { Handshake = Handshake.None };
        serialPort.DataReceived += serialPort_DataReceived;
        serialPort.NewLine = "\r";
}

我们正在使用后台工作线程通过在串行端口上发送命令(称重秤理解)来连续地轮询设备.一旦我们发送命令,连接到串行端口的设备就会响应一个响应输出.我们调用SerialPort类的ReadLine API来获取设备在DataReceived事件中写入的串行端口上的数据,如下面的代码片段所示:

We are using background worker thread to poll the device on continuous interval by sending a command(understood by the weighing scale) on the serial port. As soon as we send the command the device connected to serial port reacts with a response output. We call ReadLine API of SerialPort class to get the data present on the serial port written by the device in the DataReceived event as shown in the code snippet below :

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    try
        {
            data = serialPort.ReadLine();
        }
        catch(System.IO.IOException ex)
        {
            //since serial port reading threw an error so there is no value to be parsed hence exit the function.
            return;
        }
//if no error then parse the data received
}

我正在使用.net framework 4.0的System.IO.Ports.SerialPort类.我可以看到许多人在其他论坛上发布了此问题,但没有具体解决方案.他们中的一些人将.Net串行端口类称为越野车,到目前为止,Microsoft尚未对其进行修复.提到此错误的论坛之一是

I'm using System.IO.Ports.SerialPort class of .Net framework 4.0. I can see a number of people posting this issue on other forums but with no specific resolution. Some of them terming .Net Serial port class as buggy which has not been fixed by Microsoft till date. One of the forums where this error is mentioned is here

我还尝试了此处发布的解决方案,但没有帮助如果还有其他人遇到此问题或其解决方案,我需要一些投入.

I also tried the solution posted here but of no help. I need some input if any one else has come across this issue or its resolution.

推荐答案

我们能够通过将代码锁定在serialPort_DataReceived方法内部来解决此问题.

We were able to solve this problem by locking the code inside serialPort_DataReceived method.

Object lockObject = new Object();
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    lock(lockObject)
    {
        try
        {
            data = serialPort.ReadLine();
        }
        catch(System.IO.IOException ex)
        {
            //since serial port reading threw an error so there is no value to be parsed hence exit the function.
            return;
        }
    }
    //if no error then parse the data received
}

我们已将轮询间隔设置为10秒,以轮询串行端口上连接的设备. serialPort_DataReceived方法中存在的整个代码有时可能需要10秒钟以上的时间.我们无法确切地确定这一事实,因为它并非每次都可能发生.

We had set the polling interval to poll the device connected on serial port as 10 seconds. Possibly the entire code present inside serialPort_DataReceived method was sometimes taking more than 10 seconds. We were not able to exactly establish this fact as it was not happening every time may be.

因此,我们使用C#中的lock关键字将整个代码段锁定在serialPort_DataReceived方法内,以确保从串行端口接收的新数据的新执行不会开始,除非旧的读取尚未完成.在反复试验的基础上实施此代码后,该问题得到解决.希望这对其他人也有帮助.

So we locked the entire piece of code inside serialPort_DataReceived method using lock keyword in C# to ensure that the new execution for new data received from serial port doesn't start unless the older reading hasn't finished. The issue got resolved after implementing this code on trial and error basis. Hope this helps others as well if they come across such an issue.

这篇关于读取串行端口时IO操作中止引发的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆