简体   繁体   English

Visual Studio 中的 ReadLine() - System.IO.IOException

[英]ReadLine() in Visual Studio - System.IO.IOException

I have an application in Visual studio, reading value from serial port and drawing it on a chart.我在 Visual Studio 中有一个应用程序,从串行端口读取值并将其绘制在图表上。 Everything goes perfecly fine, but when I click a close button on the application (or serial port disconnect button), an error occurs:"IOException() was unhandled", and the program highlights the serial1.Readline() command.一切都非常顺利,但是当我单击应用程序上的关闭按钮(或串行端口断开连接按钮)时,出现错误:“IOException() 未处理”,程序突出显示 serial1.Readline() 命令。 How can I handle the exception?我该如何处理异常?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Windows.Forms.DataVisualization.Charting;

namespace usart3
{
    public partial class OknoGlowne : Form
    {
        public OknoGlowne()
        {
            string[] mojePorty = SerialPort.GetPortNames();

            InitializeComponent();

            foreach (string port in mojePorty)
            {
                cmbPorty.Items.Add(port);
            }

            cmbBaud.Items.Add(2400);
            cmbBaud.Items.Add(9600);
            cmbBaud.Items.Add(19200);

            btnRozlacz.Enabled = false;
        }
        private volatile string rxString;

        //private byte[] rxByte;
        private Object thisLock = new Object();
        Boolean i = false;
        private void btnPolacz_Click(object sender, EventArgs e)
        {
            i = true;
            serialPort1.PortName = cmbPorty.Text;      
            serialPort1.BaudRate = Convert.ToInt32(cmbBaud.Text);        

            serialPort1.Parity = Parity.None;  
            serialPort1.StopBits = StopBits.One;
            serialPort1.DataBits = 8;        
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
            serialPort1.Open();

            btnPolacz.Enabled = false;          
            btnRozlacz.Enabled = true;

    }

        int rt = 0;
        private void btnRozlacz_Click(object sender, EventArgs e)
        {

                serialPort1.Close();
                btnPolacz.Enabled = true;
                btnRozlacz.Enabled = false;


        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            rt++;


             rxString = serialPort1.ReadLine();
            this.Invoke(new EventHandler(displayText));

        }


        private void displayText(object o, EventArgs e)
        {
            richTextBox1.AppendText(rxString);
            this.chart1.Series["Temperatura"].Points.AddXY(rt, rxString);
        }


        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 nowy = new Form2();
            nowy.Show();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void Start2_Click(object sender, EventArgs e)
        {
            Form2 nowy = new Form2();
            nowy.Show();
        }



        private void button1_Click_1(object sender, EventArgs e)
        {
            Form2 nowy = new Form2();
            nowy.Show();
        }
    }
}

I had similar issue.我有类似的问题。

The IOExcpection is kind of confusing because accordingly to the documentation ReadLine() is not supposed to throw this type of exception. IOExcpection 有点令人困惑,因为根据文档 ReadLine() 不应抛出此类异常。 The problem is that ReadLine is in a state of waiting for an EOL character when we close the port.问题是当我们关闭端口时,ReadLine 处于等待 EOL 字符的状态。 Closing the port stops the thread holding ReadLine in an unsupported state.关闭端口会停止线程将 ReadLine 保持在不受支持的状态。

For me the solution was to follow this port closure sequence:对我来说,解决方案是遵循这个端口关闭顺序:

  1. Unsubscribe data_reciver method from the port.从端口取消订阅 data_reciver 方法。
  2. Send a port.WriteLine("get serial number") command.发送 port.WriteLine("get serial number") 命令。 The goal is to trig an EOL character in the input_buffer.目标是触发 input_buffer 中的 EOL 字符。 This will release ReadLine.这将释放 ReadLine。
  3. Wait until EOL is recieved.等到收到 EOL。
  4. Close the port.关闭端口。

Surround your call with a try/catch block.try/catch块包围你的调用。 In the catch you should code in way that you can handle the error by doing something else.在捕获中,您应该以可以通过执行其他操作来处理错误的方式进行编码。

try {    
    rxString = serialPort1.ReadLine(); 
} catch (IOException e) {  
    // or do something else  
}

EDIT:编辑:

try {   
    rt++; 
    rxString = serialPort1.ReadLine(); 
    this.Invoke(new EventHandler(displayText));
} catch (IOException e) {  
    MessageBox.Show("Connection is lost");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM