Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Adding A Communications Control To A Program

The Testing Department is calling. Wouldn’t it be great if users of your program could call in directly to the company’s bulletin board? Hmm, you think, how do you do that?

To support serial communications, you use the Microsoft communications control. Adding this control to your program is easy; just follow these steps:

1.  Select the Project|Components menu item.
2.  Click the Controls tab in the Components dialog box that opens.
3.  Select the Microsoft Comm Control entry, and click on OK to close the Components dialog box.
4.  Following the preceding steps adds this control to the toolbox; draw it on your program’s form now. This control is invisible at runtime, so the control’s size and location don’t matter very much.

Now that you’ve added the control, how do you set it up and get it working? Take a look at those topics coming up in this chapter.

Setting Up The Receive And Transmit Buffers

When a port is opened, the program creates receive and transmit buffers. To manage these buffers, the communications control has a number of properties that you set at design time using the control’s property pages. For example, it’s probably not a good idea to have a communications event (an OnComm event) for every byte you read; instead, you can set the RThreshold property to the number of bytes you want to read before triggering that event. The communications control’s buffer management properties are InBufferSize, OutBufferSize, RThreshold, SThreshold, InputLen, and EOFEnable.

InBufferSize And OutBufferSize

The InBufferSize and OutBufferSize properties indicate how much memory is allocated to the receive and transmit buffers. By default, InBufferSize is 1024 and OutBufferSize is 512, although you can set them as you like. If your buffer size is too small, you run the risk of overflowing the buffer (unless you use handshaking—see that topic later in this chapter).

RThreshold And SThreshold

The RThreshold and SThreshold properties set or return the number of bytes that are received into the receive and transmit buffers before the OnComm event is fired. The OnComm event is the important one for the communications control and is used to monitor changes in communications states. For example, when your program receives more than RThreshold bytes, an OnComm event occurs, and the control’s CommEvent property will hold the value comEvReceive. (Setting the value for each of these properties to 0 prevents the OnComm event from occurring.)

InputLen And EOFEnable

You can read data in chunks of specific length by setting the InputLen property; this property sets how many bytes you want to read when you use the Input property. When you set this property to 0 (the default), the communications control will read the entire contents of the receive buffer when you use the Input property. The EOFEnable property is used to indicate when an End Of File (EOF) character is found while reading data. Setting this property to True makes data input stop when an EOF is found and triggers the OnComm event.

Opening The Serial Port

Before you can work with the serial port and call another computer, you have to open that port. There are three properties that you use with the communications control to do that:

  CommPort sets and returns the communications port number.
  Settings sets and returns the baud rate, parity, data bits, and stop bits as a string.
  PortOpen sets and returns the state of a communications port. Also opens and closes a port.

Here’s an example where we open COM2, which is usually the modem port, setting it to 9600 bps, no parity, 8 data bits, and 1 stop bit:

MSComm1.CommPort = 2
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True

That’s all there is to it. When you’re ready to close the port again, set the PortOpen property to False.


TIP:  To close a connection with another computer, you usually do more than just set PortOpen to False. For example, if you’re logged into a shell account on that computer, you should log off by sending the appropriate command (such as “logoff”), either by typing that command or having your program send it. To hang up, send your modem the “ATH” command, followed by carriage return, vbCr.

Working With A Modem

To dial another computer, you send command strings to the modem. How do you do that? You can send standard Hayes-type commands to your modem using the communications control’s Output property this way, where we’re instructing the modem to dial a number:

MSComm1.Output = "ATDT 555-1234" & vbCr

In this case, the command “AT” starts the connection, “D” dials the number, and “T” specifies Touch-Tone—instead of pulse—dialing. Note that a carriage return character (vbCr) must be added when using text strings with Output . (You do not, however, need to add the return character when outputting byte arrays.)

If a command is successful, your modem will usually send back an “OK” result code, and you can look for that result with the Input property.


TIP:  For a list of Hayes-compatible commands, check your modem documentation—the complete list of commands your modem understands should be there.

Reading Data With The Communications Control

You use the Input property to get data from a communication control’s receive buffer. For example, if you wanted to retrieve data from the receive buffer and display it in a text box, you might use the following code:

Text1.Text = MSComm1.Input

To retrieve the entire contents of the receive buffer, you must first set the InputLen property to 0 at design time or runtime. Otherwise, you’ll get the number of bytes specified in the InputLen property.

You can receive incoming data as either text or binary data by setting the InputMode property to either comInputModeText or comInputModeBinary . The data will be either formatted as string or as binary data in a byte array (the default is comInputModeText). Also, it’s worth noting that when every byte of data is received, the InBufferCount property is incremented by 1, which means that you can get the total number of bytes waiting for you by checking this property.


TIP:  You can clear the receive buffer by setting the value of InBufferCount to 0.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.