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

Parse unique string from complicated serial data C#

我需要从序列号中解析这个字符串:-

!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 表单。

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

此代码,发送"!00037,00055@"returns两个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;            
    }

最后用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;
    }
  }
}