473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Serial Port Help

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
Jan 15 '08 #1
7 1624

"John Wright" <ri***********@hotmail.comwrote in message
news:O9**************@TK2MSFTNGP06.phx.gbl...
>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))
Jan 15 '08 #2
JDS
On Jan 15, 8:10*pm, "Harry" <harryNoS...@ffapaysmart.com.auwrote:
"John Wright" <riley_wrig...@hotmail.comwrote in message

news:O9**************@TK2MSFTNGP06.phx.gbl...
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))- 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
Jan 15 '08 #3
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>
"JDS" <ad***@jeremysage.comwrote in message
news:6b**********************************@i12g2000 prf.googlegroups.com...
On Jan 15, 8:10 pm, "Harry" <harryNoS...@ffapaysmart.com.auwrote:
"John Wright" <riley_wrig...@hotmail.comwrote in message

news:O9**************@TK2MSFTNGP06.phx.gbl...
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))- 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
Jan 15 '08 #4
JDS
On Jan 15, 9:53*pm, "John Wright" <riley_wrig...@hotmail.comwrote:
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>

"JDS" <ad...@jeremysage.comwrote in message

news:6b**********************************@i12g2000 prf.googlegroups.com...
On Jan 15, 8:10 pm, "Harry" <harryNoS...@ffapaysmart.com.auwrote:


"John Wright" <riley_wrig...@hotmail.comwrote in message
news:O9**************@TK2MSFTNGP06.phx.gbl...
>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, whenI
>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))- 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- 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
Jan 16 '08 #5
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
"John Wright" <ri***********@hotmail.comwrote in message
news:O9**************@TK2MSFTNGP06.phx.gbl...
>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


Jan 16 '08 #6
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.
Jan 16 '08 #7
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.
Jan 16 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
1
by: Andreas Horneff | last post by:
Hi @ all, I've got a problem with serial communication in Borland C++ Builder. I've already found a lot of stuff about serial communication in the internet, but it dosen't work. What I want...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
4
by: Frank | last post by:
Hello, how to get information about all serial ports in the PC? I use the following code, but i got only the data of the FIRST serial port. All other serial port information are not available...
38
by: shussai2 | last post by:
Hi, I am trying to access Serial Port in XP. I am using Dev-C++ IDE that uses Mingw as a compiler. I just want to know how I can open up serial port on COM1 and write some data. I have searched...
7
by: davetelling | last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things work they way I want. Using Visual C# Express, I have a form in which I added a user control to display a graph, based...
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
3
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
6
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.