从COM端口显示列表框中的数据

时间:2012-01-05 01:39:54

标签: c# listbox serial-port

我有2个按钮。按钮连接(打开COM端口)&断开连接(关闭端口)。当我点击连接按钮时,计时器将触发&每隔几秒从com端口显示Listbox上的数据。当我单击断开连接并再次单击连接按钮时,数据将不会显示在列表框上,并且不会触发计时器。为什么会这样?

这是我的代码:

    List<byte> buffer = new List<byte>(125);
    private void btnConnect_Click(object sender, EventArgs e)
    {

        if (serialPortN.IsOpen)
            serialPortN.Close();
        try
        {
            {

                serialPortN.PortName = "COM8";
                serialPortN.BaudRate = 9600;
                serialPortN.Parity = Parity.None;
                serialPortN.DataBits = 8;
                serialPortN.StopBits = StopBits.One;
                serialPortN.Encoding = System.Text.Encoding.ASCII;
                serialPortN.ReadTimeout = 500;
            }

            serialPortN.Open();
            label1.Text = "COM8" + " is opened";


            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 5000;
            timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
            timer.Start();

            serialPortN.DataReceived += new SerialDataReceivedEventHandler(datareceived);


            btnDisconnect.Enabled = true;
            btnConnect.Enabled = false;

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
     void datareceived(object sender, SerialDataReceivedEventArgs e)
    {
        myDelegate d = new myDelegate(update);
        listBox1.Invoke(d, new object[] { });

    }

    public void update()
    {

        while (serialPortN.BytesToRead > 0)
            buffer.Add((byte)serialPortN.ReadByte());
        // Call a routine to process the data.          
        ProcessBuffer(buffer);
     }

  private void ProcessBuffer(List<byte> buffer)
    {
       int numberOfBytesToRead = 125;
        if (buffer.Count >= numberOfBytesToRead)
        {
            this.Invoke(new Action(() =>
             Console.WriteLine(string.Format("SPO = {0}, PulseRate = {1}, Time = {2}",
                                                    buffer[43].ToString(),
                                                    buffer[103].ToString(),
                                                    DateTime.Now.ToString()
            ))));

            buffer.RemoveRange(0, numberOfBytesToRead);
        }

    }

   void TimerElapsed(object sender, ElapsedEventArgs e)
    {

        int numberOfBytesToRead = 125;
        if (buffer.Count >= numberOfBytesToRead)
        {
            this.Invoke(new Action(() =>
            listBox1.Items.Add(string.Format("SPO = {0}, PulseRate = {1}, Time = {2}",
                                                    buffer[43].ToString(),
                                                    buffer[103].ToString(),
                                                    DateTime.Now.ToString()
            ))));
            buffer.RemoveRange(0, numberOfBytesToRead);

        }

谢谢大家!

1 个答案:

答案 0 :(得分:0)

  1. 首先关闭 - 更改listbox1.Invoke to this.Invoke in received received。
  2. 仔细检查COM端口 - 如果您使用的是USB串行设备,则可以在更改USB端口和重新启动时重命名。
  3. 设置timer.AutoReset = true