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


You usually use the Input property with the CommEvent property in the OnComm event handler (see “Handling Communications Events” later in this chapter). For example, here’s how we read input data into a buffer in the OnComm event handler, after checking the CommEvent property to make sure we actually received data:

Private Static Sub MSComm1_OnComm()

    Select Case MSComm1.CommEvent
        Case comEvReceive
            Dim Buffer As Variant
            Buffer = MSComm1.Input
...

Sending Data With The Communications Control

To send data with the communications control, you use the Output property—in fact, you use this property to send both data to another computer and commands to your modem. If you set this property to a string, the data is sent as text; if you set it to binary data (a binary array), that data is sent in binary format.

Here are some examples. In this case, we’re directing the modem to dial a number using a Hayes-compatible modem command:

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

In this case, we’re sending a text string to another computer:

MsComm1.Output = "Here’s the text!" & vbCr

And here we’re reading records from a file and sending them through the modem to another computer:

FileBuffer = Space$(BufferSize)
Get #1, , FileBuffer
MSComm1.Output = FileBuffer


TIP:  You can watch the number of bytes in the transmit buffer by using the OutBufferCount property, and you can clear the transmit buffer by setting this value to 0.

Setting Up Communications Handshaking

Handshaking invokes a data-transmission protocol (which, for example, makes sure that data is not sent too fast or doesn’t overflow the receive buffer). The communications control can handle several different types of handshaking.

In particular, you set the Handshaking property to the handshaking protocol you want to use; the default value is to have no handshaking (Handshaking = comNone). Here are the possible handshaking protocols you can use with the Handshaking protocol:

  comNone—0; no handshaking (the default)
  comXOnXOff—1; XOn/XOff handshaking
  comRTS—2; RTS/CTS (Request To Send/Clear To Send) handshaking
  comRTSXOnXOff—3; both Request To Send and XOn/XOff handshaking

Often the communications protocol itself handles handshaking, which means that setting this property to anything but comNone can result in conflicts.


WARNING!  Here’s an important note: If you set Handshaking to either comRTS or comRTSXOnXOff, also set the RTSEnabled property to True. If you don’t, you will be able to connect and send, but not receive, data.

Handling Communications Events

To handle communications events (and errors), you use the OnComm event and the CommEvent property. The OnComm event is very useful, because you can keep track of just about everything going on with the communications control. In the OnComm event, you can determine what happened by checking the CommEvent property, which will hold one of these values:

  comEvSend—1; there are fewer than SThreshold number of characters in the transmit 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 receive buffer.
  comEvCTS—3; change in Clear To Send line.
  comEvDSR—4; change in Data Set Ready line. This event is only fired when DSR changes from 1 to 0.
  comEvCD—5; change in Carrier Detect line.
  comEvRing—6; ring detected. Some universal asynchronous receiver-transmitters (UARTs) may not support this event.
  comEvEOF—7; End Of File (ASCII character 26) character received.

The OnComm event also occurs for the following errors (these values will be in the CommEvent property):

  comEventBreak—1001; Break Signal. A break signal was received.
  comEventCTSTO—1002; Clear To Send Timeout. The Clear To Send line was low for CTSTimeout number of milliseconds while trying to transmit a character.
  comEventDSRTO—1003; Data Set Ready Timeout. The Data Set Ready line was low for DSRTimeout number of milliseconds while trying to transmit a character.
  comEventFrame—1004; Framing Error. The hardware detected a framing error.
  comEventOverrun—1006; Port Overrun. A character was not read from the hardware before the next character arrived and was lost.
  comEventCDTO—1007; Carrier Detect Timeout. The Carrier Detect line was low for CDTimeout number of milliseconds while trying to transmit a character. Carrier Detect is also known as the Receive Line Signal Detect (RLSD).
  comEventRxOver—1008; Receive Buffer Overflow. There is no room in the receive buffer.
  comEventRxParity—1009; Parity Error. The hardware detected a parity error in transmission.
  comEventTxFull—1010; Transmit Buffer Full. The transmit buffer was full while trying to queue a character.
  comEventDCB—1011; unexpected error retrieving Device Control Block (DCB) for the port.

Here’s an example using OnComm. In this case, we check for a receive event, CommEvent = comEvReceive , and use the Input property to store the received data in a buffer:

Private Static Sub MSComm1_OnComm()

    Select Case MSComm1.CommEvent
        Case comEvReceive
            Dim Buffer As Variant
            Buffer = MSComm1.Input
...

Closing The Serial Port

To close a serial port, you set the PortOpen property to False. Note that although doing so closes the serial port, you usually do more than just close the serial port to close a connection with another computer. For example, if you’re connected to a shell account on another computer, you should log out first, then send the Hayes-compatible “ATH” command to your modem to hang up before setting PortOpen to False.

Here’s an example. When the user clicks Command1, we set up the serial port COM2 and dial a number:

Private Sub Command1_Click()
    MSComm1.CommPort = 2
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.PortOpen = True

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


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.