469,076 Members | 1,377 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,076 developers. It's quick & easy.

Slow Serial Communication with VB.net?

Hey all-
I am pretty new to serial communication, but from what I am experiencing it
is a little slow in VB.net (2.0). Using other tools I can communicate with
my hardware extreamly fast without any problems. With VB, I have to insert a
Sleep(200) to receive all of the data back from my orignal request. Any way
to speed this up a bit? The return message is very small.

Dim myport As SerialPort = New SerialPort
myport.PortName = "COM1"
myport.BaudRate = "115200"
myport.StopBits = IO.Ports.StopBits.One
myport.DataBits = 8
myport.Parity = IO.Ports.Parity.None
myport.Open()
myport.Write(buffer, 0, buffer.Length)
Dim Outputbuffer(15) As Byte
Dim mystring As String
Dim i As Integer
Sleep(200) ' Sleep for 1 second
myport.Read(Outputbuffer, 0, Outputbuffer.Length)
For i = 0 To Outputbuffer.Length - 1
mystring = mystring & " " & Hex(Outputbuffer(i))
Next
myport.Close()
txtOutput.Text = mystring

Jul 25 '07 #1
4 6344
Hi,

You don't need Sleep (though it can be used to provide a fixed delay).
Simply use a loop. The receive process in the SerialPort object uses a
background thread, so a loop will not cause any "issues." Perhaps:

Dim Outputbuffer(15) As Byte
Dim mystring As String
Dim i As Integer
Do Until myport.BytesToRead >= 16
Sleep(1) 'this is "OK" to do, though not absolutely needed
Loop
myport.Read(Outputbuffer, 0, Outputbuffer.Length)
For i = 0 To Outputbuffer.Length - 1
mystring = mystring & " " & Hex(Outputbuffer(i)) 'realize that
this is a fairly slow process, though I often do something like it (for
debugging, only)
Next

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.
Jul 25 '07 #2
Dick,
Thanks for the help. Is there a better way to get the data back from a
serial port?

When I tried the...
Do until myport.bytestoread >= 16

my system would hang. I must be doing something wrong. I also tried <=16,
but it acted very similar to operating too fast and only return the first few
bytes.

Thanks again,
Doug DeVore

BTW...I have a request in to the boss to purchase your book.

"Dick Grier" wrote:
Hi,

You don't need Sleep (though it can be used to provide a fixed delay).
Simply use a loop. The receive process in the SerialPort object uses a
background thread, so a loop will not cause any "issues." Perhaps:

Dim Outputbuffer(15) As Byte
Dim mystring As String
Dim i As Integer
Do Until myport.BytesToRead >= 16
Sleep(1) 'this is "OK" to do, though not absolutely needed
Loop
myport.Read(Outputbuffer, 0, Outputbuffer.Length)
For i = 0 To Outputbuffer.Length - 1
mystring = mystring & " " & Hex(Outputbuffer(i)) 'realize that
this is a fairly slow process, though I often do something like it (for
debugging, only)
Next

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.
Jul 25 '07 #3
Hi,

Put a Debug.Print statement in the loop to see how much data actually is
received. Perhaps it "hangs" because you are expecting more bytes than
actually appears?

You can use the DataReceived event to process receive data. It is generated
whenever ReceiveThreshold (or more) bytes are received. It executes in the
thread context of the SerialPort receive (background) thread, so if you
interact with the UI after you receive data you must Invoke/Begin Invoke a
delegate or delegate event. If you code does not interact with the UI, then
DataReceived may be the way to go.

The reason that you might want to use a loop is that is is a simple matter
to add a Timeout to a loop (Do Until sometimeout). If there is a timeout --
without having received the data that you expect, you can examine what you
actually have received to see "what's up." Timeouts, while still possible,
using DataReceived are tricky and may cause more trouble than they are
worth.

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.
Jul 26 '07 #4
Example.

Dim Timeout As Integer = Now.TimeOfDay.TotalMilliseconds + 200
Dim DataProcessed As Boolean
With Serialport
Do Until Now.TimeOfDay.TotalMilliseconds Timeout
If .BytesToRead >= 16 Then
'DoSomethingUseful
DataProcessed = True
Exit Do
Else
Debug.Print(.BytesToRead.ToString)
End If
Loop
If DataProcessed = False Then 'Darn - now we have to do some work!

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.
Jul 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andreas Horneff | last post: by
3 posts views Thread by carmen | last post: by
6 posts views Thread by Leandro Berti via DotNetMonster.com | last post: by
4 posts views Thread by max_mont | last post: by
reply views Thread by Dhananjay | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.