![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
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 lineor more precisely, to place the data in the output bufferput the data in the Comm controls 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 eventsthat is, normal occurrences during error-free serial communication. Its 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 EventCentral 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 controls 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.
The Threshold property specifies the number of characters in the output buffer that trigger the CommEvent procedure. The settings for this property follow:
You can use the OnComm event to pace data transmission from your program to the Output property. When transmitting large quantities of datafor example, a text fileyour program may write data to the Comm controls 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 DemonstrationTo 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 Boxs 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 Boxs Multiline property to True, and its ScrollBars property to Both. The forms 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.
|
![]() |
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. |