ReadLine() in Visual Studio - System.IO.IOException

本文关键字:System IO IOException Studio Visual in ReadLine | 更新日期: 2023-09-27 18:30:02

我在Visual studio中有一个应用程序,从串行端口读取值并将其绘制在图表上。一切都很顺利,但当我单击应用程序上的关闭按钮(或串行端口断开连接按钮)时,会出现一个错误:"IOException()未处理",程序会突出显示serial1.Readline()命令。如何处理异常?

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();
        }
    }
}

ReadLine() in Visual Studio - System.IO.IOException

我也遇到过类似的问题。

IOExcpection有点令人困惑,因为根据文档ReadLine()不应该抛出这种类型的异常。问题是,当我们关闭端口时,ReadLine处于等待EOL字符的状态。关闭端口会使持有ReadLine的线程处于不受支持的状态。

对我来说,解决方案是遵循这个港口关闭顺序:

  1. 从端口取消订阅data_reciver方法
  2. 发送port.WriteLine("获取序列号")命令。目标是在input_buffer中触发一个EOL字符。这将释放ReadLine
  3. 等待,直到收到EOL
  4. 关闭端口

try/catch块包围您的呼叫。在catch中,您应该以可以通过执行其他操作来处理错误的方式进行编码。

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

编辑:

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