C#通过串口发送数据,字节数组中的数据接收不完整

时间:2014-09-07 09:07:32

标签: c# arrays serial-port buffer data-loss

我正在开发一个GUI接口,它接收来自ATmega8A的信息。 GUI中的以下代码必须执行以下操作:

  • 检查标题:在这里,我正在将数据读入字节数组(byte[] test),该数组将针对OxFF进行检查
  • 如果header == 0xFF,请读取第二个字节数组(byte[] data

请参阅以下代码。

现在,我有以下问题。如果我只发送一个数字,例如1,那么这个数字就会显示在textBox1中而没有任何问题。但是,如果我试图发送一个像433这样的号码,我总是收到4,33就是迷路了。我认为这是由于我包含的if语句,但我无法解释为什么数据丢失。

namespace RS232
{
   public partial class fclsRS232Tester : Form
   {
    string InputData = String.Empty;
    string initText = "waiting...";

    delegate void SetTextCallback(string text);

    public fclsRS232Tester()
    {
        InitializeComponent();

        // Nice methods to browse all available ports:
        string[] ports = SerialPort.GetPortNames();

        // Add all port names to the combo box:
        foreach (string port in ports)
        {
            cmbComSelect.Items.Add(port);
        }

        cmbBaud.Items.Add(2400);
        cmbBaud.Items.Add(9600);

        cmbComSelect.SelectedIndex = 0;
        cmbBaud.SelectedIndex = 1;

        button4.Enabled = false;
        textBox1.Text = initText;
        textBox2.Text = initText;

    }

    private void cmbComSelect_SelectionChangeCommitted(object sender, EventArgs e)
    {
        if (port.IsOpen) port.Close();
        port.PortName = cmbComSelect.SelectedItem.ToString();
        stsStatus.Text = port.PortName + ": 9600,8N1";

        // try to open the selected port:
        try
        {
            port.Open();
            button4.Enabled = true;
            textBox1.Clear();
            textBox2.Clear();
        }
        // give a message, if the port is not available:
        catch
        {
            MessageBox.Show("Serial port " + port.PortName + " cannot be opened!", "RS232      tester", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            cmbComSelect.SelectedText = "";
            stsStatus.Text = "Select serial port!";
        }
    }

    private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
    {
        int bytenumber;
        int bufferSize = port.BytesToRead;
        byte[] test = new byte[1];
        byte[] data = new byte[bufferSize];
        byte[] data2 = new byte[bufferSize];

        port.Read(test, 0, 1);

        if (test[0] == 0xFF)  //Receive X-andY- coordinates from MCU and plot the coordinates 
        {
            bytenumber = port.Read(data, 0, bufferSize);
            string info = System.Text.Encoding.ASCII.GetString(data);

            this.Invoke((MethodInvoker)delegate
            {
                this.txtIn.Text += info;                      
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

只要接收缓冲区中的数据可用,就会调用DataReceived处理程序。因此,在接收到下一个数据突发之前,读取缓冲区中的所有数据,然后处理标题的有效性等。一旦收到新数据,旧数据就会被覆盖,从而导致数据丢失。 在您的情况下,通过调用port.Read()只读取1个字节。我会将其更改为 port.Read(test,0,bufferSize);

此外,我只是将数据存储在缓冲区中,并在单独的线程上运行数据处理。花在处理程序上的时间越多,丢失数据的可能性就越大。

答案 1 :(得分:0)

通过将所有数据存储到缓冲区来解决问题:

private void port_DataReceived_1(object sender,SerialDataReceivedEventArgs e)         {             int bytenumber;             byte [] data = new byte [10];

        bytenumber = port.Read(data, 0, 10);

        if (data[0] == 0xFF)
        {
        }
    }

相关问题