繁体   English   中英

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

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

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

我有类似的问题。

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

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

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

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

暂无
暂无

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

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