Slow Serial Communication with VB.net?

G

Guest

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
 
D

Dick Grier

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.
 
G

Guest

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.
 
D

Dick Grier

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.
 
D

Dick Grier

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.
 

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