Using the RS232.vb component for serial port control

K

Ken Breit

I am using the RS232.vb class to talk to the serial port. The problem I am
having is when I try to read if anything is on the comm port. I call the
read method, with the number of bytes I am expecting to read. As long as I
know the length of the command that I will be getting on the input of the
port it works great.

The problem is when I don't know how long the data I am receiving is. If I
request more bytes than I know I am getting back (ie. I would request to
read the entire buffer, and then just see what is in that buffer) the class
throws a timeout error. I'm assuming that it is waiting for of the bytes
that I specified.

With the VB6 Comm control I was able to read the input and if there was
nothing there it would just return a blank string.

Is there any way to tell how many bytes are in the buffer before I try to
read it, or some way to read the port when there is nothing there and not
get an error?

Thanks,
Ken Breit
 
K

Ken Breit

Thanks Cor. Yes I've looked at some of these controls, but have not tried
them out yet. I liked the idea of the rs232 class and wanted to use it. I
most likely will use one of those components, but I just wanted to see if
there was a way.

Thanks,
Ken
 
D

Dick Grier

Hi,

That class is a little simplistic, IMO. Naturally, since it is provided as
source code, you can modify it as needed. I have another one in my book (see
below) -- I prefer its implementation, and it doesn't have the limitation
that you mention, and there are a number of alternates available online.
You also can download NETComm.ocx from my homepage, for an ActiveX solution.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
J

joe_pool_is

I use that same RS232.vb code. Mine works like this:

Try
j = 0
While SwitchCOM.Read(1) <> -1
DataIn(j) =
Asc(SwitchCOM.InputStreamString.Chars(0))
j += 1
End While
Catch
' // "Switcher buffer empty"
End Try

This fills the array DataIn() with your characters. If you want all
your characters to be in a string, do something like:

dim strDataIn as string = ""
for i = 0 to DataIn.Length - 1
strDataIn &= DataIn(i)
next

Good stuff! :)
 
J

joe_pool_is

I use that same RS232.vb code. Mine works like this:

Try
j = 0
While SwitchCOM.Read(1) <> -1
DataIn(j) =
Asc(SwitchCOM.InputStreamString.Chars(0))
j += 1
End While
Catch
' // "Switcher buffer empty"
End Try

This fills the array DataIn() with your characters. If you want all
your characters to be in a string, do something like:

dim strDataIn as string = ""
for i = 0 to DataIn.Length - 1
strDataIn &= DataIn(i)
next

Good stuff! :)
 

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