|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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, heres 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 ControlTo send data with the communications control, you use the Output propertyin 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, were directing the modem to dial a number using a Hayes-compatible modem command: MSComm1.Output = "ATDT 555-1234" & vbCr In this case, were sending a text string to another computer: MsComm1.Output = "Heres the text!" & vbCr And here were reading records from a file and sending them through the modem to another computer: FileBuffer = Space$(BufferSize) Get #1, , FileBuffer MSComm1.Output = FileBuffer
Setting Up Communications HandshakingHandshaking invokes a data-transmission protocol (which, for example, makes sure that data is not sent too fast or doesnt 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:
Often the communications protocol itself handles handshaking, which means that setting this property to anything but comNone can result in conflicts.
Handling Communications EventsTo 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:
The OnComm event also occurs for the following errors (these values will be in the CommEvent property):
Heres 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 PortTo 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 youre 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. Heres 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
|
|
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. |