
      Communications Control for Visual Basic by Mark Gamber   March 1992

   This control provides access to the communications ports through the Windows
API. To use the control, copy VBCOMM.VBX to your Visual Basic directory. Select
the file from the listbox displayed by selecting "Add File..." in the "File"
menu. A "port" button should appear in the toolbox. Selecting a port and
placing it on a form will display another small "port" button. This is only
displayed during design mode and should be treated like a timer control.


Port settings:
   These settings define the port setup. They may be set at design time or run
time to any value desired within the ranges described:

   Comport: 0 = COM1:, 1 = COM2:, 2 = COM3:, 3 = COM4:

   Baud:    0 = 300, 1 = 1200, 2 = 2400, 3 = 9600, 4 = 19200

   Parity:  0 = Parity Off, 1 = Parity Even, 2 = Parity Odd

   Data:    0 = 6 bits, 1 = 7 bits, 2 = 8 bits

   Stop:    0 = 1 bit, 1 = 2 bits



Port initialization:
   Before using the port, it must be enabled. Use "Comm1.Enable = 1" to enable
the port and "Comm1.Enable = 0" to disable (close) the port. Attempts to enable
a port already enabled results in error #10001. Bad or unsupported port setup
parameters result in either error #10001 or #10002. If an attempt is made to
disable an unenabled port, error #10004 will occur. All these errors may be
trapped by the Visual Basic program.



Data I/O:
   Sending data involves sending integer values of ASCII codes one at a time
using "DataChr" or by strings using "DataStr". An example of each:

Single character:

   Comm1.DataChr = asc( "A" )            ' Send letter "A" out the port


String:

   Comm1.DataStr = "ATZ" + chr$( 13 )    ' Send "ATZ" command to modem


   Receiving data can be a bit trickier. Internally, a timer is used to poll
the port for any characters and, if one or more are received, it calls the
"InQueue" event. The Visual Basic programmer may use the event to read the data
into a string or character by character. As long as there is data to be read
from the port input buffer, this event is called every time the timer goes off.
An example of "InQueue" code might be:

"InQueue as Integer"

   if InQueue Then                ' If not a false firing...
      a$ = Comm1.DataStr          ' Read data into string
      if len(a$) Then             ' If something was read...
         Picture1,Print a$        ' Do something useful
         while len( a$ )
            a$ = Comm1.DataStr    ' While data is being read...
            if len( a$ ) then
               Picture1.Print a$  ' Be more useful
            end if
         wend
      end if
   end if

End of "InQueue"

Following the code, InQueue is the number of characters waiting to be read. In
some chance the routine is called with nothing waiting, this should be checked.
Next, we read the port buffer and again check to be sure something was actually
read. If so, do something useful and, as long as data is waiting to be read
from the port, repeat the process. If using very high speeds on relatively slow
machines, the "While" section may cause the program to appear to lock up since
there's always data to receive. Omitting that section is less efficient but
doesn't attempt to clear the port before exiting.
   Data may be received in two ways. Incoming data may be read character by
character using "DataChr" and as a string using "DataStr". An example of each:

Single character:

   a% = Comm1.DataChr             ' Read character from port
   if a% <> -1 then               ' -1 means nothing was there
      do something useful         ' Otherwise, valid ASCII code
   end if


String:
   a$ = Comm1.DataStr             ' Read string into a$
   if len( a$ ) then              ' If something was actually read...
      do something fun            ' See code
   endif


Attempting to read or write an unenabled port results in an error 10004.



This software is free for all to use given two conditions:

1. Ownership is retained by Mark Gamber as of March, 1992
   (Give credit where credit is due)

2. I'm not liable.
   (What? Me Worry?)

Bugs, suggestions and whatnot may be directed to PCA MarkG on America Online or
through Internet to pcamarkg@aol.com, E-Mail only. No other messages or mail
may be responded to.

