Serial Port Help

J

John Wright

I have data coming into a serial port that I need to take action on. I have
created a delegate to read the data from the port and put it into a text
box. Once the data is read, I need to manipulate it. However, when I try
to take any action on the data, I get errors. When I call the function
GetTareandWeight, it gives me an error on the split function because the
data is not done streaming in yet. How can I wait for all the data to get
in from the port before processing it?

John






Code:

Public Delegate Sub myDelegate(ByVal txtBox As TextBox)



Private Sub SerialTare_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialTare.DataReceived



TextBox1.Invoke(New myDelegate(AddressOf UPdateTextBox), New Object()
{TextBox1})





End Sub



Public Sub UPdateTextBox(ByVal UpdateTextBox As TextBox)

Dim bytes As Integer = SerialTare.BytesToRead

Dim buffer(bytes) As Byte

SerialTare.Read(buffer, 0, bytes)

strWeight = System.Text.Encoding.Default.GetString(buffer)

TextBox1.AppendText(strWeight)

GetTareandWeight(strWeight)


End Sub

Private Sub GetTareandWeight(ByVal weight As String)

Dim strweights() As String

Dim newweight As String = Replace(weight, " ", ",")

strweights = Split(weight, ",")

TextBox2.Text &= newweight & vbCrLf

TextBox2.Text &= strweights.Length.ToString & vbCrLf

TextBox2.Text &= "Tare: " & strweights(1).ToString

End Sub
 
H

Harry

John Wright said:
I have data coming into a serial port that I need to take action on. I
have created a delegate to read the data from the port and put it into a
text box. Once the data is read, I need to manipulate it. However, when I
try to take any action on the data, I get errors. When I call the function
GetTareandWeight, it gives me an error on the split function because the
data is not done streaming in yet. How can I wait for all the data to get
in from the port before processing it?

John






Code:

Public Delegate Sub myDelegate(ByVal txtBox As TextBox)



Private Sub SerialTare_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialTare.DataReceived



TextBox1.Invoke(New myDelegate(AddressOf UPdateTextBox), New Object()
{TextBox1})





End Sub



Public Sub UPdateTextBox(ByVal UpdateTextBox As TextBox)

Dim bytes As Integer = SerialTare.BytesToRead

Dim buffer(bytes) As Byte

SerialTare.Read(buffer, 0, bytes)

strWeight = System.Text.Encoding.Default.GetString(buffer)

TextBox1.AppendText(strWeight)

GetTareandWeight(strWeight)


End Sub

Private Sub GetTareandWeight(ByVal weight As String)

Dim strweights() As String

Dim newweight As String = Replace(weight, " ", ",")

strweights = Split(weight, ",")

TextBox2.Text &= newweight & vbCrLf

TextBox2.Text &= strweights.Length.ToString & vbCrLf

TextBox2.Text &= "Tare: " & strweights(1).ToString

End Sub
This is a bit of VB6 code that may help:

Do Until ((commSwipe.InBufferCount = 0) Or _
(Right$(buffer, 1) = vbCr) Or _
(Right$(buffer, 1) = vbLf))
 
J

JDS

This is a bit of VB6 code that may help:

Do Until ((commSwipe.InBufferCount = 0) Or _
          (Right$(buffer, 1) = vbCr) Or _
          (Right$(buffer, 1) = vbLf))- Hide quoted text -

- Show quoted text -

Generally, you will need to either test for a number of characters
received or for a terminating character (or set of characters) - as
the code above does. If this test is not met then append the received
data to a static variable, once the test is met then process.

I have some .Net code for testing for terminating characters if you
need it - let me know.

HTH
 
J

John Wright

I would like to see it. The data comes in the following format:

<CRLF>
G xxx.x lb <CRLF>
T xxx.x lb <CRLF>
N xxx.x lb <CRLF>

OR
<CRLF>
G xxx.x lb<CRLF>
No Tare Weight <CRLF>
No Tare Weight <CRLF>


This is a bit of VB6 code that may help:

Do Until ((commSwipe.InBufferCount = 0) Or _
(Right$(buffer, 1) = vbCr) Or _
(Right$(buffer, 1) = vbLf))- Hide quoted text -

- Show quoted text -

Generally, you will need to either test for a number of characters
received or for a terminating character (or set of characters) - as
the code above does. If this test is not met then append the received
data to a static variable, once the test is met then process.

I have some .Net code for testing for terminating characters if you
need it - let me know.

HTH
 
J

JDS

I would like to see it.  The data comes in the following format:

<CRLF>
G   xxx.x lb <CRLF>
T   xxx.x lb <CRLF>
N  xxx.x lb <CRLF>

OR
<CRLF>
G xxx.x lb<CRLF>
No Tare Weight <CRLF>
No Tare Weight <CRLF>







Generally, you will need to either test for a number of characters
received or for a terminating character (or set of characters) - as
the code above does. If this test is not met then append the received
data to a static variable, once the test is met then process.

I have some .Net code for testing for terminating characters if you
need it - let me know.

HTH- Hide quoted text -

- Show quoted text -

Example code as follows. You'll need to change the check for the
terminating character from vbCr to either vbCrLf or vbLf. I have
stripped out error handling for clarity.

(I notice that you are processing weight data. We do a lot of work
with weighing equipment (our main line of business) so would be
interested in the project and the equipment you are using. Feel free
to email directly.)

'******* Example code *******
Private Shared Sub Display(ByVal Buffer As String)
Static strTempBuffer As String
strTempBuffer &= Buffer
If Not strTempBuffer.EndsWith(vbCr) Then 'partial data
'...flag or do nothing
ElseIf Not IsNumeric(Microsoft.VisualBasic.Left(strTempBuffer,
Len(strTempBuffer) - 1)) Then 'invalid data; this test could be
different for your application data format, e.g. use Mid() and/or test
for number of characters
'... data error handling
strTempBuffer = "" 'vbCR received so clear buffer
Else 'valid data
'... process data
strTempBuffer = "" 'vbCR received so clear buffer
End If
End Sub
'******* End *******

HTH
 
G

Guy Cohen

John

Try this code:

Dim buffer(0 To bytes- 1) As Byte
Instead of:
Dim buffer(bytes) As Byte

Also - why did you choose to invoke the delegate inside the data_received...
I think it might be the problem...
I suggest that you make a class that handles rs232
then dim it withevents and you will have a cleaner code...
Can you try working with low baud-rate?
Write here what you did from above and if it worked...
HTH
Guy
 
D

Dick Grier

Hi,

What you do is to append newly arrived data to a Private (or Static)
variable, then parse that variable to determine if all data have arrived.
How you do this parsing depends on the format of your scale. Some scales
output data in pure ASCII, terminated by a carriage return. Thus, you wait
until the vbCr (or vbCrLf) terminates the string, and the display and use
it. In other cases, the data may be part of a packet that has more
information in it than just weight. In that case, the terminating condition
may be something like a EOT character. The overall process is similar,
however.

BTW, you do not need a delegate, except when it comes time to actually
display the data -- so the error that you are seeing may be caused by a
logical problem. Without seeing your code, and having more to go on than
the description that you've presented, I'd be guessing more than I'd like.

I do have several different scale examples in my book (see below), and one
might be quite close (or even the same) as what you are trying to do.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
D

Dick Grier

Hi,

This is fairly easy.

Simply append new data to a string buffer. Use the buffer Split method of
this string buffer to separate each line into a separate entry in a string
array -- Split on vbCrLf. Then, loop through the array to make sure that
both the first entry (index 1) contains a "G" and the last entry (3)
contains a "N" character -- if each are present, all data have arrived and
you can display the content of the array, or otherwise process it. Don't
forget to reset the original string buffer to an empty string, so that it is
ready to go for the next complete cycle.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
J

John Wright

I got it to work. I just checked the buffer length and started processing
when the data was all there. Since I am processing this data as it comes in
(I have to split it so I can process the data in a paticular way as it
arrives) I used the delegate to update the screen as it comes in (I capture
the raw data). In order to update the textbox I have to use the delegate
because the data_arrival event spawns a separate thread and I get a cross
thread error. I got it to work. Thanks all for the help and suggestions.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top