Shmewnix Shmewnix - 1 year ago 110 Question

SerialPort Datareceive in - how can I parse the data received

I have a PC that sends Serial data to a sign. What I'd like to do is remove the sign, and display it on my own using a .Net program, and another PC. I have a .Net program that reads the data from a com port (serial cable from PC to PC)- The reading is working just fine, however i'm not sure how to properly parse the data I want, and display it in a Label.

Here is how it currently works.

  • The computer sends transaction information (price) to the com port. 30 seconds later, it sends the termination of the string - to indicate we no longer want to display the information anymore.

  • I'm able to See this, including the price, with the start of the message and end of the message characters. Image of the data i'm receiving in my current .net is below. You'll notice it sends the 01 Then the price, in this case 2000 ($20.00). Then after 30 seconds, it displays the remaining characters.

enter image description here

Here is my Current Code:

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports

Public Class Form1
Dim myport As Array
Delegate Sub settextcallback(ByVal [text] As String)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'This is the connect button
SerialPort1.PortName = "COM3" 'Connect on COM3
SerialPort1.BaudRate = "1200" 'Set BaudRate to 1200
Button1.Enabled = False

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'this is the disconnect button

End Sub

Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

End Sub
Private Sub ReceivedText(ByVal [text] As String)
'now we display the information
If Me.RichTextBox1.InvokeRequired Then

Dim x As New settextcallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})

Me.RichTextBox1.Text &= [text]

End If
End Sub
End Class

How can I parse out the leading 01, and just display, in this case, $20.00 in a label - let's call it "label1", until I receive the trailing '01'. Once I receive the trailing '01', I want to Clear the label.

I've attempted to clear, then transfer the text box in the thread however, since the data comes in one at a time - that doesn't give me the result i'm looking for.

Answer Source

I think this is very robust but you can figure it out on your own.

Put this in a timer and it works fine.

Private Sub TimerPrice_Tick(sender As Object, e As EventArgs) Handles TimerPrice.Tick

Dim output As Array = TextBox1.Text.Split("#")

    If output.Length = 2 Then
        Label1.Text = output(1)
        Label1.Text = vbNullString
    End If
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download