dcsimg
Results 1 to 7 of 7

Thread: read updated serial port data and split the string

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    4

    read updated serial port data and split the string

    Hi, i manage to read the incoming data with following code from https://www.electroniclinic.com/how-...ensors-values/
    Code:
    Imports System.IO
    Imports System.IO.Ports
    
    Public Class Form1
    
        Dim value1 As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SerialPort1.Close()
            SerialPort1.PortName = "com15"
            SerialPort1.BaudRate = "9600"
            SerialPort1.DataBits = 8
            SerialPort1.Parity = Parity.None
            SerialPort1.StopBits = StopBits.One
            SerialPort1.Handshake = Handshake.None
            SerialPort1.Encoding = System.Text.Encoding.Default
            SerialPort1.Open()
        End Sub
    
    
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim s As String
    
            s = TextBox1.Text + "," + "," + "," + ","
    
            Dim somestring() As String
            ' Split string based on comma
            somestring = s.Split(New Char() {","c})
    
            TextBox2.Text = somestring(0)
            ' value1 = Convert.ToDecimal(TextBox2.Text)
            TextBox3.Text = somestring(1)
            TextBox4.Text = somestring(2)
            TextBox1.Text = ""
    
    
        End Sub
    
        Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Try
                Dim mydata As String = ""
                mydata = SerialPort1.ReadExisting()
    
                If TextBox1.InvokeRequired Then
                    TextBox1.Invoke(DirectCast(Sub() TextBox1.Text &= mydata, MethodInvoker))
                Else
                    TextBox1.Text &= mydata
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    End Class
    how do i just display the latest data (while keep on updating. i don't need history data.)?
    secondly, how can i split the data. i have tried split method like in following link but no luck (nothing is displayed: https://www.youtube.com/watch?v=Da9L...ature=youtu.be
    please attachment Name:  ahmi issue 8.jpg
Views: 152
Size:  16.4 KB

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: read updated serial port data and split the string

    sorry i'm new here, i don't know if i can't post big image. you may see my attachment is too small.
    actually the data i received is continuously updated in new line. i just need one line which is keep on updating. here is the received data:

    
    YES 00:32:41 13/09/19 0.0 0.0
    0.0 0.0 -90.0 -90.0 -20.0












    
    YES 00:32:42 13/09/19 0.0 0.0
    0.0 0.0 -90.0 -90.0 -20.0












    
    YES 00:32:43 13/09/19 0.0 0.0
    0.0 0.0 -90.0 -90.0 -20.0












    
    YES 00:32:44 13/09/19 0.0 0.0
    0.0 0.0 -0.0 -90.0 -20.0












    
    YES 00:32:4 13/09/19 0.0 0.0
    0.0 0.0 -90.0 -90.0 -20.0

    i don't know what is the symbol on the first line.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,058

    Re: read updated serial port data and split the string

    1- WELCOME to the Forum!!

    2- This part of the Forum is for Visual Basic 6.0 and Earlier...you have posted some code from a .NET product...I'll notify the Moderators to move it for you.

    3-I believe (but hard to see) if you clear your textbox before each receipt, you will only see the 'latest' information.

    4-As far as splitting the line(s), what do you want do see? (In VB6 you might use Instr() to 'find' the beginning of the string you want to save)

    BUT, this IS in the wrong part of the Forum at this time...wait and see what the experts tell you when this is moved to the proper forum.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,991

    Re: read updated serial port data and split the string

    Every version of Visual Studio since VS6 has been VB.NET. Therefore, 2002, 2003, 2005, 2008, 2010, 2012, 2013, 2015, 2017, and 2019 are all VB.NET.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: read updated serial port data and split the string

    sorry for that. I believe this thread has been moved to correct place?
    hope someone can help with some idea to solve the issue.

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: read updated serial port data and split the string

    sorry for that. I believe this thread has been moved to correct place?
    hope someone can help with some idea to solve the issue.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,250

    Re: read updated serial port data and split the string

    Hi joko.

    Yeah, the forum resizes images so that they are no wider than 600 pixels. If you need to show images, then it's best to crop them to show just the relevant information, such as just the contents of TextBox1. Anything that is text such as data and error messages is best copy/pasted into the forum post, as you did in your second post.


    The code in your DataReceived event handler is converting incoming data to text and adding it to the end of what is already in TextBox1. Assuming the contents of TextBox1 is what you have shown on the right of your image in the first post (in the blue box), then something looks broken to me.

    You can just about make out that the first record is incomplete, and the first three records all appear to be shown on the same line; subsequent records appear on their own lines. Then, in your second post, the output has lots of white space between records. Where has that come from? You also make mention of an unexpected character at the start of the lines. It doesn't show in my browser, but it looks like it might be a representation of the non printable ASCII character 0x15.

    Given the above, I'm wondering if the device is actually sending what you think it is sending. Maybe it includes protocol header data, or checksums. You need to understand the data's structure before trying to split it up.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width