开发者

trouble with using the received data from serial port

开发者 https://www.devze.com 2023-04-11 02:03 出处:网络
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. The format of the data coming from the

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. The format of the data coming from the Microcontroller is shown below

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

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

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

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

4,14 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

now I am interested in using only first pair of coordinates and may be saving in two different arrays x1 and 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.T开发者_JAVA技巧extChanged

    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. 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

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:

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.

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

If you are getting other data then the simplest thing to do is firstly to check what the string.Split returns.

You've said that the first line is:

Slave Address: 0xB0 Initialization successful !

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.

Then if that results in an array of length 2 use TryParse to do the string -> double conversion. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消