473,395 Members | 1,629 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,395 software developers and data experts.

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 6643
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
3
by: carmen | last post by:
I'm working in an aplication for a Smart Device that need to "talk" with a printer continuosly through the serial port. I'm trying to use the John Hint's sample code "Use P/Invoke to develop a .NET...
4
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code into the Visual C#.NET. My C++ code is mainly based on the serial communication. So I am using the windows structure such as DCB.. etc and the windows...
6
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
4
by: max_mont | last post by:
Hi all, I'm a newbie in .NET technology. I've already developed Serial communication applications in C++ (WIN32). And I wanted to migrate to .NET technology. There is a serial component in...
4
by: nmsreddi | last post by:
Hi friends , after a long busy work happy to back to forums, i have developed a windows application(C#2,0) for serial communication. the aim of my application is to connect to external...
2
by: Adrian Chen | last post by:
please help me! I come across a problem. Now I develop a finger print management system which is based on B/S.When users click a button in the web pages, a device connected to the COM1 serial port...
0
by: Dhananjay | last post by:
Hi, I am working on an VB.Net application which I want to communicate to external device using comm port (Serial Port) . So for that first I am trying to simulate the communication on serial...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.