Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am using the following Visual Bascic 2010 code to read a numeric sting value from a com port. My question is how can I adapt the code to read a second string from the same com port ? I can add a letter or number at the start of the string if required. Monitoring the code coming off a arduino Due board at the serial port shows the streams in the folowing way.

67.7 51.2
67.6 51.3

The code I am using to read the port is the following (see below)

Thank you for any assistance.
Regards
Nevjc


VB
Delegate Sub myMethodDelegate(ByVal [text] As String)
Dim myDelegate As New myMethodDelegate(AddressOf ProcessReading)

Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
   Dim str As String = sp.ReadLine()
   Invoke(myDelegate, str)
End Sub

Sub ProcessReading(input As String)
   If IsNumeric(input) Then
      temperature = CDbl(input)
      picTherm.Refresh()
   End If
End Sub
Posted
Updated 27-Mar-14 2:36am
v3
Comments
[no name] 26-Mar-14 14:53pm    
What is a numeric string? It's a string. Why can't you sp.ReadLine() again to read a second string?
Member 10701701 27-Mar-14 5:55am    
Hi,
Thanks for your comment. I have to admit I am a complete novice at this and do not know visual basic code. I would appreciate any help. The two streams of code come off an Arduino board to a USB port and are shown by the serial monitor in the following form
67.7 51.2
67.6 51.3
Thank you for your help
Nevjc
[no name] 27-Mar-14 7:41am    
Okay.... and? Did you add another call to sp.ReadLine so you could read another line from the serial port yet? Did it work or didn't it?
Member 10701701 27-Mar-14 7:55am    
Hi,
I added : sp.ReadLine() to the readline but it did not work continuosly it seems to read the second string occasionaly or otherwise blank.
Thanks for the help.
[no name] 27-Mar-14 8:55am    
Well you are only going to get data out of the port if there is actually data in the port to read....

1 solution

Assuming you have two values per String, you will have to parse each string for the two values:

Assuming:
Line 1: 67.7 51.2 
Line 2: 67.6 51.3 
etc..


Then:
a. Trim the white spaces
b. Separate the string into substrings by divding it at every blankspaces
c. Convert each substring into value as in the example.
 
Share this answer
 
v2
Comments
Member 10701701 27-Mar-14 10:26am    
Thanks for that I will give it a go.

Thanks for the help Nevjc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900