如何每2秒接收一次传入的serialPort数据?

时间:2015-02-05 11:59:21

标签: c# timer serial-port logic

所以现在我有一个按钮,点击它后会打开一个串口进行通信。但问题是我从串口收到的数据每隔几毫秒才能快速填满我的文本框。

如何控制它,以便我每隔2秒左右使用serialPortN.DataReceived += new SerialDataReceivedEventHandler(datareceived);接收数据?

这就是我的按钮:

private void btnConnect_Click(object sender, EventArgs e)
    {
        if (this.cboComPort.SelectedIndex == -1)    //if no Com Port is selected
        {   //To show error message
            MessageBox.Show("You must select the port first!");

        }
        else
        {
            // If Serial Port is already open, close it first
            if (serialPortN.IsOpen)
                serialPortN.Close();
            try
            {
                // comport parameters
                serialPortN.PortName = cboComPort.Text;
                serialPortN.BaudRate = 9600;
                serialPortN.Parity = Parity.None;
                serialPortN.DataBits = 8;
                serialPortN.StopBits = StopBits.One;
                serialPortN.Encoding = System.Text.Encoding.ASCII;
                serialPortN.ReadTimeout = 500;

                // Open Serial port for communications
                serialPortN.Open();
                txtPortStatus.Text = "OPEN";

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

                // to receive incoming data received by the serial port,
                serialPortN.DataReceived += new SerialDataReceivedEventHandler(datareceived);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

这样的东西?

  1. 创建计时器以及存储数据的地方

    private System.Windows.Forms.Timer portTimer;
    private StringBuilder receievedData = new StringBuilder();
    private object receivedDataLock = new object();
    
  2. 设置并启动计时器(当你打开端口时)

    portTimer = new System.Windows.Forms.Timer();
    portTimer.Interval = 2000;
    portTimer.Tick += portTimer_Tick;
    portTimer.Start();
    
  3. 在端口的数据接收处理程序中,保存数据

    private void serialPortN_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        byte [] buffer = new byte[serialPortN.BytesToRead];
        int numRead = serialPortN.Read(buffer,0,buffer.Length);            
        // Ensure we arent trying to modify string builder from 2 different threads simultaneously
        lock(receivedDataLock){
            // Assuming the data is in ASCII text format if you are displaying to text box            
            receievedData.Append(System.Text.Encoding.ASCII.GetString(buffer));
        }
    }
    
  4. 对计时器处理程序中的数据执行某些操作:

    void portTimer_Tick(object sender, EventArgs e)
    {
        if (receievedData.Length > 0)
        {
            lock (receivedDataLock)
            {
                String data = receievedData.ToString();
                // Do something with your string, then empty the StringBuilder
                receievedData = new StringBuilder();
            }
        }
    }