简体   繁体   English

使用从串行端口接收的数据的麻烦

[英]trouble with using the received data from serial port

the problem I am having is probably mainly because of I am very new at VB.net, anyway, the thing is, I am able to receive data from microcontroller successfully. 我遇到的问题可能主要是因为我是VB.net的新手,无论如何,问题是,我能够成功地从微控制器接收数据。 The format of the data coming from the Microcontroller is shown below 来自单片机的数据格式如下所示

0,2 1023,1023 1023,1023 1023,1023 0,2 1023,1023 1023,1023 1023,1023

1,5 1023,1023 1023,1023 1023,1023 1,5 1023,1023 1023,1023 1023,1023

2,8 1023,1023 1023,1023 1023,1023 2,8 1023,1023 1023,1023 1023,1023

3,11 1023,1023 1023,1023 1023,1023 3,11 1023,1023 1023,1023 1023,1023

4,14 1023,1023 1023,1023 1023,1023 4,14 1023,1023 1023,1023 1023,1023

5,17 1023,1023 1023,1023 1023,1023 5,17 1023,1023 1023,1023 1023,1023

here basically Microcontroller is sending me coordinates of four different points in [x1,y1 x2,y2 x3,y3 x4,y4] format 这里基本上是微控制器以[x1,y1 x2,y2 x3,y3 x4,y4]格式向我发送四个不同点的坐标

now I am interested in using only first pair of coordinates and may be saving in two different arrays x1 and y1. 现在我只想使用第一对坐标,并且可能保存在两个不同的数组x1和y1中。 Then I want to use these two coordinates as a screen coordinates. 然后,我想将这两个坐标用作屏幕坐标。

Public Class Form1
    Public Delegate Sub myDelegate()
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        sp1.Open()
    End Sub
    Public Sub updateTextBox()
        Dim strarr(8) As String
        Dim str1 As String
        str1 = sp1.ReadLine
        txtreceive.AppendText(str1)
        strarr = str1.Split(",")

        Dim x1 As Double      

        x1 = Val(strarr(0))

        MsgBox(x1)

        txtreceive.ScrollToCaret()

    End Sub

    Private Sub sp1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp1.DataReceived

        txtreceive.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})



    End Sub

    Private Sub txtreceive_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtreceive.TextChanged

    End Sub
End Class

here I am getting the x coordinate of the first pair but whenever I am trying to get y1 from first pair I am getting error. 在这里,我得到第一对的x坐标,但是每当我试图从第一对获取y1时,我都会出错。 it says "Index was outside the bounds of the array". 它说:“索引超出了数组的范围”。 Now if there is another way of getting the data please suggest me. 现在,如果还有另一种获取数据的方法,请提出建议。 Thank you 谢谢

There are two delimiters here: 这里有两个定界符:

  • the space character, which separates the four pairs from each other 空格字符,将四对字符分隔开
  • the comma character, which separates the X and Y coordinates 逗号字符,用于分隔X和Y坐标

If you split on JUST commas, your elements would be the same as if your typed: 如果按逗号分隔,则元素将与您键入的元素相同:

strarr(0) = "0"
strarr(1) = "2 1023"
strarr(2) = "1023 1023"
strarr(3) = "1023 1023"
strarr(4) = "1023"

If you want all four pairs, split on spaces first and then split each of the four resulting strings on commas. 如果要全部四对,请先在空格处分割,然后在逗号处分割四个结果字符串中的每一个。

As you only want the first pair, truncate the string at the first space and then split that on the comma, as shown below. 因为只需要第一对,所以在第一个空格处截断字符串,然后在逗号处将其分割,如下所示。

Public Sub updateTextBox()
    Dim strarr() As String
    Dim str1 As String
    str1 = sp1.ReadLine

    ' NEW: truncate str1 at the first space
    str1 = str1.SubString(0, str1.IndexOf(" ") -1)

    txtreceive.AppendText(str1)
    strarr = str1.Split(",")

    Dim x1 As Double
    Dim y1 As Double      

    x1 = Val(strarr(0))
    y1 = Val(strarr(1))

    MsgBox(x1 & ", " & y1)

    txtreceive.ScrollToCaret()

End Sub

string.split() will return an array of the correct size, so your declaration of it: string.split()将返回正确大小的数组,因此您对其进行了声明:

Dim strarr(8) As String

is superfluous. 是多余的。

The following code should split the string on commas. 以下代码应在逗号上分割字符串。

Dim strarr() As String
strarr = str1.Split(",")

Double check that the string is being split as you expect. 仔细检查该字符串是否已按预期拆分。 The fact that index 1 is being reported as out of bounds implies that the string isn't being split correctly because it isn't in the format you're expecting. 索引1被报告为越界的事实意味着该字符串未正确拆分,因为它的格式不符合您的期望。

Use the debugger or a message box to verify that the line of data you've just read is of the format: 使用调试器或消息框来验证您刚刚读取的数据行的格式:

0,2 1023,1023 1023,1023 1023,1023 0,2 1023,1023 1023,1023 1023,1023

If you are getting other data then the simplest thing to do is firstly to check what the string.Split returns. 如果要获取其他数据,那么最简单的方法是首先检查string.Split返回的内容。

You've said that the first line is: 您已经说过第一行是:

Slave Address: 0xB0 Initialization successful ! 从站地址:0xB0初始化成功!

In this case the array will only be one long as there are no commas in this string. 在这种情况下,只要该字符串中没有逗号,该数组将只有一个。

However, you've got another problem in that you need to split on spaces first to get an array of coordinates and then split that on the comma. 不过,你得在你需要分割的空间首先得坐标的数组,然后拆分上逗号另一个问题。 So first you'll need: 因此,首先您需要:

strarr = str1.Split(" ")

to give you: 为你带来:

strarr(0) = "0,2"
strarr(1) = "1023,1023"
strarr(2) = "1023,1023"
strarr(3) = "1023,1023"

Next, check that this the right length - 4 then split the first element on the comma. 接下来,检查长度是否正确-4然后在逗号上分割第一个元素。

Then if that results in an array of length 2 use TryParse to do the string -> double conversion. 然后,如果结果是长度为2的数组,请使用TryParse进行字符串->双重转换。 This will fail safe if the string isn't a numeric value. 如果字符串不是数字值,这将失败并安全。 Then you can use your double values. 然后,您可以使用双精度值。

If the current line fails these tests then just read the next line and repeat. 如果当前行未通过这些测试,则只需读取下一行并重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM