|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Adding A Communications Control To A ProgramThe Testing Department is calling. Wouldnt it be great if users of your program could call in directly to the companys 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:
Now that youve 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 BuffersWhen 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 controls property pages. For example, its 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 controls 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 handshakingsee 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 controls 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 PortBefore 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:
Heres 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 Thats all there is to it. When youre ready to close the port again, set the PortOpen property to False.
Working With A ModemTo 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 controls Output property this way, where were 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-Toneinstead of pulsedialing. 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.
Reading Data With The Communications ControlYou use the Input property to get data from a communication controls 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, youll 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, its 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.
|
|
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. |