Convert Serial Port Data String to Integer

cartman1417

New member
Joined
May 30, 2014
Messages
1
Programming Experience
Beginner
Hi, I am new to VB .NET and am having trouble converting a continuous stream of string data coming from serial port to integer. Can someone please help?

My data coming from the serial port comes as 6 lines of string as shown below:

-2,0,316,411,184,1638,6005,1626,2399,1659,268,3502
-2,1,161,411,331,1633,19,1661,2205,1665,278,3768
-2,2,91,403,119,1640,400,1668,2432,1660,287,3607
-2,3,365,129,390,1673,316,1631,2355,1662,329,3721
-2,4,298,123,177,1627,100,1628,2592,1665,306,3678
-2,5,155,120,323,1678,0,1623,2817,1661,315,4102

This is data for a motor where "+" or "-" in the beginning shows the direction of rotation. I am using the code below to read the data coming from the serial port and saving that data to a text file.

What I want to do is process the data in real time as it comes in to calculate several parameters like averages, display the direction of rotation on my form, etc. Basically process these 72 values into say 15 parameters that are stored in a buffer or another array and the results will be displayed on my Form.

Any suggestions on how I can do that? Please let me know if I am not clear about what I want. Thanks in advance!

Code:


PrivateSub SerialPort1_DataReceived(ByVal sender AsObject, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceivedTry
SW = New StreamWriter("C:\Read.txt")
ReceivedText(SerialPort1.ReadExisting())
'Automatically called every time a data is received at the serialPort
For Each Line As String In SplitEx(rtbReceived.Text, True, ",", vbCrLf) 'This is a function that splits the string
Line = Line.Replace(
" ", "")
Line = Line.Replace(
"+", "1") 'Assign 1 to clocwise direction to convert string to integer
Line = Line.Replace("-", "2") ' Assign 2 to counter-clockwise direction to convert string to interger

SW.WriteLine(Line)
Next

SW.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
EndTry
EndSub


PrivateSub ReceivedText(ByVal [text] AsString)
'compares the ID of the creating Thread to the ID of the calling Thread
IfMe.rtbReceived.InvokeRequired Then
Dim x AsNew SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, NewObject() {(text)})
Else
Me.rtbReceived.Text &= [text]
EndIf
EndSub
 
Back
Top