1

我正在从 C# 中的串行端口读取数据。目前我的微控制器上有两个传感器,我正在从我的应用程序中的两个传感器接收数据。

传入看起来像这样: :*123*188;where, :代表数据的开始

*第一次出现表示传感器 1 数据

*第二次出现表示传感器 2 数据

;代表数据结束

为了摆脱我使用的标点符号:

var DataPacket = SensorData.Split(new[]{":",";"},StringSplitOptions.RemoveEmptyEntries) 

现在我离开了:*123*188。如何分离两个传感器数据并将它们放在不同的文本框中。

我努力了:

foreach(var Data in DataPacket)
{
    string bits[] = Data.Split(new[] {"*"},StringSplitOptions.RemoveEmptyEntries);     
}

但是,textbox1.Text = bits[0]在同一个框中同时读取传感器 1 和传感器 2 的值。并且bits[1]越界了。

请指导我解决这个问题。

编辑:这是读取数据并尝试对其进行相应拆分的扩展代码:

private void OnReceive(object sender, System.IO.Ports.SerialDataReceivedEventArgs s) { //FOR 1 SENSOR string SensorData = AvailablePort.ReadLine(); this.BeginInvoke(new LineReceivedEvent(LineReceived), SensorData);

    }

    private delegate void LineReceivedEvent(string SensorData);

    private void LineReceived(string SensorData)
    {

       /*** FOR SECOND SENSOR****************/ 
        int startReading = SensorData.IndexOf(":");
        int endReading = SensorData.IndexOf(";");
        /********                   ************/

        //int userRange = Convert.ToInt32(Temp_Range.SelectedValue);
        //int data = Int32.Parse(SensorData);


        /*** FOR SECOND SENSOR ************************************************/
        var DataPacket = SensorData.Split(new[] { ":", ";" }, StringSplitOptions.None);

        foreach (var Data in DataPacket)
        {
            //textBox1.Text = Data;
            //TRYING TO READ MORE THAN 1 SENSOR HERE
            var newData = SensorData.Split(new[] { "*" }, StringSplitOptions.None);
            while(true)
            {
                foreach (var data in newData)
                {
                    textBox1.Text = data[0].ToString();
                }
            } 
        }
4

4 回答 4

0

如果要包含 null 或空元素,请不要添加 StringSplitOptions.RemoveEmptyEntries。前任:

Without remove
*121
a[0]=null
a[1]=121
121*
a[0]=121
a[1]=null
111*222
a[0]=111
a[1]=222


with remove
*121
a[0]=121
a[1]=OutOfRangeException
121*
a[0]=121
a[1]=OutOfRangeException
111*222
a[0]=111
a[1]=222
于 2013-03-24T23:46:14.087 回答
0

Although there seems to be some code ambiguity for me here, but I think you need to change the following line of code:

var newData = SensorData.Split(new[] { "*" }, StringSplitOptions.None);
while(true)
{
      foreach (var data in newData)
      {
            textBox1.Text = data[0].ToString(); // Remove the index from data as this is not a collection but a variable
      }
} 

I think you should rewrite this bit of code as follows:

String[] newData = SensorData.Split(new[] { "*" }, StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i<newData.Length; i++)
{
    textBox1.Text = newData[i]; // You will need to manipulate the textboxes better here, since the
}

Now I may be wrong in understanding but I think this should work for you apart from changes in assigning these values to the textboxes as you seem to have hardcoded them. For instance the for loop could be replaced with something like this:

switch(newData.Length)
{
    case 0:
        break;
    case 1:
        textbox1.Text = newData[0];
        break;
    case 2:
        textbox1.Text = newData[0];
        textbox2.Text = newData[1];
        break;
    ...
    case n:
        break;
}

Hope this makes sense.

于 2013-03-24T23:57:19.133 回答
0

我在这里不是 Visual Studio,但你应该尝试这样的事情:

string bits[] = Data.Split("*");

这应该给你这个:

bits[0] = null;
bits[1] = 123;
bits[2] = 188;
于 2013-03-24T23:49:52.990 回答
0

提取数据的方式没有任何问题,但只需确保从串行端口获取的数据真正遵循模式,您的程序是否有可能在读取到数据末尾之前继续?

于 2013-03-25T00:44:54.737 回答