串口读取正常但分配的字符串有垃圾,C#

时间:2012-02-10 11:40:27

标签: c# textbox gps serial-port

我正在尝试从串行端口拆分传入数据,并使用后续数据更新文本框。我首先看看是否可以拆分“$”,如果拆分后的下一个字是“GPGGA”。如果是,我想从这句话中提取数据,其中','用作分隔符。 现在,如您所见,我首先更新串行端口读取的整个数据,这样可以正常工作。显示包含“GPGGA”行的完整句子。但是在我拆分它之后,包含“GPGGA”的句子部分看起来就像这个“GPGGA,1 \ 0 \ 0 \ 0 \ 0 \ 0 ......”真的那句话刚刚更新到文本之前的盒子是“GPGGA,160333 ,,, ......”。我绝对肯定在句子中GPGGA之后有一个值,但是当我尝试在调试模式下查看它时,字符串'ser_data',因此其后续的子字符串都显示相同的垃圾。因此,我想要更新的最终文本框不可避免地最终只显示1。

有谁可以告诉我为什么会这样,以及我如何纠正它。我迫切需要我的论文工作。

谢谢, 布雷特

P.S:我已附上以下代码。

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        byte[] bData = null;
        bData = new byte[40];
        serialPort.Read(bData, 0, 40);
        string ser_data = System.Text.Encoding.GetEncoding("utf-8").GetString(bData);
        txtAck.Invoke(new UpdateTextCallback(this.UpdateTextAck), new object[] { ser_data });
        string[] str = null;
        str = new string[40];
        string[] str_ack = null;

        str = ser_data.Split('$');
        if (str.Length > 1)
        {
            for (int i = 1; i < str.Length; i++)
            {
                string temp1 = null;
                temp1 = str[i];
                if (temp1.StartsWith("GPGGA"))
                {
                    string[] temp2 = null;
                    temp2 = temp1.Split(',');

                    StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);

                    if (temp2.Length > 1)
                    {
                        string Time_GPS = temp2[1];
                        txtEasting.Invoke(new UpdateTextCallback(this.UpdateTextEast), new object[] { Time_GPS });
                        string text = "Time : " + Time_GPS;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    if (temp2.Length > 2)
                    {
                        string Lat = temp2[2];
                        txtLatitude.Invoke(new UpdateTextCallback(this.UpdateTextLat), new object[] { Lat });
                        string text = " Latitude : " + Lat;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    if (temp2.Length > 4)
                    {
                        string Long = temp2[4];
                        txtLongitude.Invoke(new UpdateTextCallback(this.UpdateTextLong), new object[] { Long });
                        string text = " Longitude : " + Long;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    if (temp2.Length > 9)
                    {
                        string Alt = temp2[9];
                        txtNorthing.Invoke(new UpdateTextCallback(this.UpdateTextNorth), new object[] { Alt });
                        string text = " Altitude : " + Alt;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    string Text = "." + '\n';
                    objWriter.WriteLine(Text);
                    objWriter.Close();

                    temp2 = null;
                    flag_status = 0;
                }
                temp1 = null;
            }
        }
        str = null;

2 个答案:

答案 0 :(得分:2)

SerialPort.Read不一定会读取您要求的字符数。

您需要保存返回值,即读取的字符数。

   // nBytesRead will be between 0 and 40, depending on how many bytes were waiting.
   int nBytesRead = serialPort.Read(bData, 0, 40); 
   // Only decode the number of bytes actually retrieved.
   string ser_data = System.Text.Encoding.GetEncoding("utf-8").GetString(bData, 0, nBytesRead); 

答案 1 :(得分:1)

修复并主要清理。这假设有两件事:你的编码真的是UTF-8,你的行以换行符结尾。

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    serialPort.Encoding = ASCIIEncoding.UTF8;
    string ser_data = serialPort.ReadLine();
    txtAck.Invoke(new UpdateTextCallback(this.UpdateTextAck), new object[] { ser_data });

    string[] str = ser_data.Split(new char[] { '$' }, 2);
    if (str.Length > 1)
    {
        for (int i = 1; i < str.Length; i++)
        {
            string temp1 = str[i];
            if (temp1.StartsWith("GPGGA"))
            {
                StreamWriter objWriter = new StreamWriter(@"D:\Server.txt", true);
                try
                {
                    string[] temp2 = temp1.Split(',');
                    if (temp2.Length > 1)
                    {
                        string Time_GPS = temp2[1];
                        txtEasting.Invoke(new UpdateTextCallback(this.UpdateTextEast), new object[] { Time_GPS });
                        string text = "Time : " + Time_GPS;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    if (temp2.Length > 2)
                    {
                        string Lat = temp2[2];
                        txtLatitude.Invoke(new UpdateTextCallback(this.UpdateTextLat), new object[] { Lat });
                        string text = " Latitude : " + Lat;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    if (temp2.Length > 4)
                    {
                        string Long = temp2[4];
                        txtLongitude.Invoke(new UpdateTextCallback(this.UpdateTextLong), new object[] { Long });
                        string text = " Longitude : " + Long;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    if (temp2.Length > 9)
                    {
                        string Alt = temp2[9];
                        txtNorthing.Invoke(new UpdateTextCallback(this.UpdateTextNorth), new object[] { Alt });
                        string text = " Altitude : " + Alt;
                        // StreamWriter objWriter = new System.IO.StreamWriter(@"D:\Server.txt", true);
                        objWriter.WriteLine(text);
                    }
                    objWriter.WriteLine(".\n");
                }
                finally
                {
                    objWriter.Close();
                }

                flag_status = 0;
            }
        }
    }
}