将数据从RS232端口输入C#应用程序时

将数据从RS232端口输入C#应用程序时,c#,serial-port,C#,Serial Port,我有一个电子秤ASCELL I210。 我想通过RS232串行端口从中获取重量信息。 我使用了这段代码,这段代码对我有效,但问题是textbox从缓冲区中一次又一次地得到相同的结果,因此我添加了一个包含_serialPort.Close()的try catch;以及_serialPort.Dispose(); 注意:我尝试只使用_serialPort.Close();但它也不起作用 我得到了我想要的结果,但是一个窗口弹出说 如果有其他方法可以解决这个问题,请告诉我怎么做 public parti

我有一个电子秤ASCELL I210。 我想通过RS232串行端口从中获取重量信息。 我使用了这段代码,这段代码对我有效,但问题是textbox从缓冲区中一次又一次地得到相同的结果,因此我添加了一个包含_serialPort.Close()的try catch;以及_serialPort.Dispose(); 注意:我尝试只使用_serialPort.Close();但它也不起作用 我得到了我想要的结果,但是一个窗口弹出说 如果有其他方法可以解决这个问题,请告诉我怎么做

public partial class Form1 : Form
{
    private SerialPort _serialPort;         //<-- declares a SerialPort Variable to be used throughout the form
    private const int BaudRate = 9600;      //<-- BaudRate Constant. 9600 seems to be the scale-units default value
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] portNames = SerialPort.GetPortNames();     //<-- Reads all available comPorts
        foreach (var portName in portNames)
        {
            comboBox1.Items.Add(portName);                  //<-- Adds Ports to combobox
        }
        comboBox1.SelectedIndex = 0;                        //<-- Selects first entry (convenience purposes)
    }

    private delegate void Closure();
    private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
    {

        if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
            BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     //<-- Function invokes itself
        else
        {          
                while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
                {
                    textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte());

                }

                try 
                { 
                   _serialPort.Close();

                    if (_serialPort != null)
                    {
                        _serialPort.Dispose();
                    }
                }
                catch (InvalidCastException e)
                {
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            //<-- This block ensures that no exceptions happen
            if (_serialPort != null && _serialPort.IsOpen)
                _serialPort.Close();

            if (_serialPort != null)
            {
                _serialPort.Dispose();
            }
            //<-- End of Block

            _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One);       //<-- Creates new SerialPort using the name selected in the combobox
            _serialPort.DataReceived += SerialPortOnDataReceived;       //<-- this event happens everytime when new data is received by the ComPort
            _serialPort.Open();     //<-- make the comport listen
            textBox1.Text = "Listening on " + _serialPort.PortName + "...\r\n";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (_serialPort.IsOpen)
            {
                _serialPort.Close();
                Startbutton.Enabled = true;
                stopbutton.Enabled = false;
                textBox1.ReadOnly = true;
            }
        }
    }
}
公共部分类表单1:表单
{
专用串行端口_SerialPort//