【问题标题】:ReadLine() in Visual Studio - System.IO.IOExceptionVisual Studio 中的 ReadLine() - System.IO.IOException
【发布时间】:2016-04-16 07:20:58
【问题描述】:

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

【问题讨论】:

  • 只添加一个try/catch?

标签: c# visual-studio-2010 visual-studio-2012


【解决方案1】:

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

【讨论】:

  • 好的,现在我有:try { rxString = serialPort1.ReadLine(); } catch (IOException e) { MessageBox.Show("连接丢失");但是当我点击消息框上的“确定”时,主程序停止反应。我什至无法关闭它。
  • 尝试将方法包含的所有代码添加到try块中,包括rt++;this.Invoke(new EventHandler(displayText)); @米歇尔
【解决方案2】:

我有类似的问题。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 2015-11-19
    相关资源
    最近更新 更多