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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


To determine how many characters are currently in the output buffer, read the OutBufferCount property. To clear the buffer and erase its current contents without transmitting them, set the OutBufferCount property to zero.

The size of the input buffer is set with the InBufferSize property, whose default value is 1,024. As with the output buffer, too small a value can result in overflow errors, while too large a value simply wastes memory. The InBufferCount property specifies the number of characters currently in the input buffer. Setting this property to zero clears the input buffer.

To transmit data over the serial line—or more precisely, to place the data in the output buffer—put the data in the Comm control’s Output property. To read data received by the serial port, use the Input property. By default, reading the Input property obtains all the characters currently in the input buffer. To read a specified number of characters, set the InputLen property to the desired number of characters before reading the Input property. If the input buffer contains fewer characters than specified by InputLen, Input returns a null string. You can use the InBufferCount property to be sure that the buffer contains enough characters before reading it. Reset InputLen to its default value of zero to return to reading all characters from the input buffer.

Here is an output example. The following code sends each character typed on a form to the Comm control:

Private Sub Form_KeyPress (KeyAscii As Integer)
    MSComm1.Output = Chr$(KeyAscii)
End Sub

As an input example, this code checks to see that input data is available, and if so, it uses the Print method to display it on the form:

If MSComm1.InBufferCount Then
    Form1.Print MSComm1.Input
End If

Determining Port Status

How can you determine the status of the serial port? The CommEvent property returns a code that represents the most recent change in port status. These changes include error conditions, as well as communication events. Values between 1,001 and 1,010 represent error codes; you can refer to the Visual Basic Help system for details on what the codes represent and on the defined constants available for your programs. Values between one and seven represent communication events—that is, normal occurrences during error-free serial communication. It’s important to realize that the value of the CommEvent property reflects the most recent event or change in the Comm control. If the most re-cent occurrence was reception of data, the property value would be two. If the most recent occurrence was an error, the property value would be the code for that error. How do we know when something of importance has happened? The answer to this question is our next topic.

The OnComm Event

Central to most uses of the Comm control is the OnComm event. This event is triggered by a change in the status of the Comm control (with some limitations, as explained later). Expressed another way, this event is triggered any time the CommEvent property changes. We now have the answer to the previous question, “How can we know when the Comm control’s status has changed?” The answer is simple: by using the OnComm event procedure. In this event procedure, we place code that tests the value of the CommEvent property, which branches accordingly. The most important use for this event procedure is to read data from the Input property after it has been received. It can also be used to react to error conditions. Table 15.2 lists the essential communication events.

Table 15.2 Communication events.

Constant Value Of CommEvent Description
comEvSend 1 There are fewer than SThreshold number of characters in the output buffer.
comEvReceive 2 Received RThreshold number of characters. This event is generated continuously until you use the Input property to remove the data from the input buffer.
comEvRing 6 Ring detected. Some UARTs (universal asynchronous receiver-transmitters) may not support this event.
comEvEOF 7 End Of File (ASCII character 26) character received.

The Threshold property specifies the number of characters in the output buffer that trigger the CommEvent procedure. The settings for this property follow:

  0—OnComm is never triggered for data transmission.
  1—OnComm is triggered when the output buffer is empty.
  nOnComm is triggered when the number of unsent characters in the output buffer falls below n.

You can use the OnComm event to pace data transmission from your program to the Output property. When transmitting large quantities of data—for example, a text file—your program may write data to the Comm control’s Output property faster than it can be sent, running the risk of an overflow error. You can eliminate this problem by using use the comEvSend event, which allows the program to send another block of data to the Output property only when the contents of the output buffer fall below a certain level.

The RThreshold property specifies how many characters must be received before triggering an OnComm event. If you want the program to respond each time a single character is received, set this property to one. If you set RThreshold to zero, OnComm will not be triggered, regardless of how many characters are received.

A Demonstration

To demonstrate the use of the Comm control, I have created a simple program that implements a dumb terminal. By definition, a dumb terminal does nothing more than send what you type to the serial port and display on screen any text received by the serial port. Text is displayed in a Text Box control. I could have used the Print method to display the text directly on a form, but the Text Box’s built-in scrolling capabilities are useful when the text exceeds the form length. The form has only two controls: the Text Box and one MSComm control. Set the Text Box’s Multiline property to True, and its ScrollBars property to Both. The form’s objects and properties are given in Listing 15.1, and the operating program is shown in Figure 15.1.

Listing 15.1 Objects and properties in COMMDEMO.FRM.

Begin VB.Form Form1
    Caption         =   “Terminal”
    Begin VB.TextBox Text1
        MultiLine       =   -1  ‘True
        ScrollBars      =   3  ‘Both
    End
    Begin MSCommLib.MSComm MSComm1
    End
End

The code for this program is shown in Listing 15.2. In the Form_Load event procedure, we initialize the Comm control and open the port. I selected the communication parameters of 14000,E,7,1, because they are required by my CompuServe connection to test the program. You will need to set these parameters according to your own requirements. Note that I also set the RThreshold property to one to trigger the OnComm event each time a single character is received.


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. Read EarthWeb's privacy statement.