从复杂的串行数据C#中解析唯一字符串

时间:2015-05-18 16:16:42

标签: c# .net string winforms serial-port

我需要从串口解析这个字符串: -

!00037,00055@

00037作为一个字符串,00055作为另一个字符串

然而,当机器人的轮胎旋转时,这个字符串出现,而其他一些字符串也可能在我需要解析的字符串之前和之后显示。例如,这是收到的一些传输: -

11,00085@R-STOPR-STOP!00011,00095@!00001,00015@R-STOP!00001,00085@!00003,00075@!00006,00015@R-STOP!00009,00025@!00011,00035@!00011,00085@R-STOPR-STOP!00011,00095@!00001,00015@R-STOP!00001,00085@!00003,00075@!00006,00015@R-STOP!00009,00025@!00011,00035@R-STOP!00001,00085@!00003,00075@!00006,00015@R-STOP!00009,00025@!00011,00035@R-STOP!00037,00055@!00023,00075@R-STOPR-STOP!00022,00065@!00011,00085@R-STOPR-STOP!00011,00095@!00001,00015@R-STOP!00001,00085@!00003,00075@!00006,00015@R-STOP!00009,00025@!00011,00035@R-STOP!00037,00055@!00023,00075@R-STOPR-STOP!00022,00065@!00011,00085@R-STOPR-STOP!00011,00095@!00001,00015@

到目前为止,我仍然坚持在SerialPort.ReadExisting()

之后接下来要做什么

以下是一些检索串行数据的代码: -

private void serialCom_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        InputData = serialCom.ReadExisting();
        if (InputData != String.Empty)
        {
            this.BeginInvoke(new SetTextCallback(IncomingData), new object[] { InputData });
        }
    }
    catch
    {
        MessageBox.Show("Error");
    }
}

并在文本框中显示传入的串行数据

private void IncomingData(string data)
{           
    tb_incomingData.AppendText(data);
    tb_incomingData.ScrollToCaret();            
}

此代码使用的是.NET Framework 4.0和Windows窗体。

2 个答案:

答案 0 :(得分:1)

最后使用indexof和substring解决它。

private void IncomingData(string data)
{
  //Show received data in textbox
  tb_incomingData.AppendText(data);
  tb_incomingData.ScrollToCaret();

  //Append data inside longdata (string)
  longData = longData + data;
  if (longData.Contains('@') && longData.Contains(',') && longData.Contains('!')) 
  {
    try
    {
      indexSeru = longData.IndexOf('!'); //retrieve index number of the symbol !
      indexComma = longData.IndexOf(','); //retrieve index number of the symbol ,
      indexAlias = longData.IndexOf('@'); //retrieve index number of the symbol ,
      rotation = longData.Substring(indexSeru + 1, 5); //first string is taken after symbol ! and 5 next char
      subRotation = longData.Substring(indexComma + 1, 5); //second string is taken after symbol ! and 5 next char
      //tss_distance.Text = rotation + "," + subRotation;
      longData = null; //clear longdata string      
    }
    catch
    {
      indexSeru = 0;
      indexComma = 0;
      indexAlias = 0;
    }
  }
}

答案 1 :(得分:0)

您可以使用SPLIT函数确定将此字符串转换为数组的模式。

此代码,发送"!00037,00055 @"返回两个itens:00037和00055。

    static void Main(string[] args)
    {
        string k = "!00037,00055@";
        var array = k.ToString().Split(',');
        Console.WriteLine("Dirty Itens");
        for (var x = 0; x <= array.Length - 1; x++)
        {
            var linha = "Item " + x.ToString() + " = " + array[x];
            Console.WriteLine(linha);
        }
        Console.WriteLine("Cleaned Itens");
        for (var x = 0; x <= array.Length - 1; x++)
        {
            var linha = "Item " + x.ToString() + " = " + CleanString(array[x]);
            Console.WriteLine(linha);
        }
        Console.ReadLine();

    }

    public static string CleanString(string inputString)
    {
        string resultString = "";
        Regex regexObj = new Regex(@"[^\d]");
        resultString = regexObj.Replace(inputString, "");
        return resultString;            
    }