405 409




Visual Basic 6 Black Book:The Timer And Serial Communications Controls
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Chapter 13The Timer And Serial Communications Controls


If you need an immediate solution to:
Adding A Timer Control To A Program
Initializing A Timer Control
Handling Timer Events
Formatting Times And Dates
Creating A Clock Program
Creating A Stopwatch
Creating An Alarm Clock
Creating Animation Using The Timer Control
Adding A Communications Control To A Program
Setting Up The Receive And Transmit Buffers
Opening The Serial Port
Working With A Modem
Reading Data With The Communications Control
Sending Data With The Communications Control
Setting Up Communications Handshaking
Handling Communications Events
Closing The Serial Port
Adding A MonthView Control To Your Program
Getting Dates From A MonthView Control
Adding A DateTimePicker Control To Your Program
Using A DateTimePicker Control

In Depth
In this chapter, we’re going to cover the timer and communication controls that come with Visual Basic. In particular, we’ll cover the timer control, the serial port communications control, and two controls that exist mostly for convenience: the MonthView control and the DateTimePicker control. Let’s get an overview of these controls first.

The Timer Control
You use a timer control when you want to execute code at specific intervals. To use a timer, you add a timer control to your program (timers are one of the intrinsic controls that appear in the toolbox when you start Visual Basic) and set its Interval property. From then on, while the timer is enabled, it creates Timer events, which are handled in an event handling procedure, like Timer1_Timer() . You place the code you want executed each interval in that procedure.
To add a timer to your program, use the Timer Control tool in the toolbox, which is the seventh tool down on the left in Figure 13.1.

Figure 13.1  The Timer Control tool.
We should note, however, that there are a few issues about using the Interval property. Although measured in milliseconds (1/1000s of a second), Timer events cannot actually occur faster than 18.2 times a second (this is the period of the computer’s timer interrupt). The interval can be set to values between 0 (in which case nothing happens) and 64,767, which means that even the longest interval can’t be much longer than 1 minute (about 64.8 seconds). Of course, you can design your code to wait for several intervals to pass before doing anything.
You shouldn’t count on a timer too closely if your task execution is dependent on exact intervals; if the system is busy executing long loops, intensive calculations, or drive, network, or port access (in which case software routinely disables the timer interrupt), your application may not get Timer events as often as the Interval property specifies. That is to say, Timer events are not guaranteed to happen exactly on time. If you need to be sure, your software should check the system clock when it needs to (using, for example, the Visual Basic Time$ function), rather than try to keep track of time internally.
Another point here has to do with Windows programming philosophy. Using a timer can easily pull programmers back to thinking in terms of sequential programming (as in the DOS days), rather than event-oriented programming. When you use a timer, your code has a lot of control and can get a lot of execution time, because your code is called each time the timer ticks. However, that doesn’t mean you should set a timer interval short and put in all kinds of loops. Remember that Windows is built around user events, not programs that are designed to retain control for long periods of time. Other programs will probably be running at the same time as yours, so it’s considerate not to use timers simply to wrest control from the environment.
With all that said, though, the timer is a uniquely powerful control, and we’ll put it to use in this chapter.
The Communications Control
You use the Microsoft communications control to support serial port—that is, modem—communications. If you want to write your own modem package, this is where you start. You can use the communications control to do everything from dialing phone numbers to creating a full-fledged terminal program.

To add this control to your program, select the Project|Components menu item, click the Controls tab in the Components dialog box that opens, select the Microsoft Comm Control entry, and click on OK to close the Components dialog box. Doing so adds this control to the toolbox, as shown in Figure 13.2; the Communications Control tool is the eleventh tool down on the right.

Figure 13.2  The Communications Control tool.
When you use the communications control, you use a serial port in your computer. The mouse is usually connected to COM1, and the modem is usually connected to COM2. You set baud rate, parity, and so on, and then call another computer by issuing commands to your modem. After the connection is made, you can exchange data with the other computer.

Receiving And Transmitting
When a serial port is opened, your program creates receive and transmit buffers. To work with these buffers, the communications control supports a number of properties that can be set at design time using the control’s property pages.

The InBufferSize and OutBufferSize properties hold the size of the input and output buffers, and the RThreshold and SThreshold properties set or return the number of characters that are received into the receive and transmit buffers before the OnComm event is fired (this event is used to monitor changes in communications states). We’ll see these and other such properties in this chapter.
To establish a connection, you set the communications control’s CommPort property to the serial port’s number (usually 2), the Settings property to the protocol settings you want (for example, “9600,N,8,1”), and set the PortOpen property to True. To start dialing, you send the appropriate commands to your modem.
Sending Data
To actually send data, you use the Output property. You can either send data to your modem or to the other computer. For example, here’s how you dial a phone number, by sending an “ATDT” string to your modem (that string is part of the standard Hayes-compatible command set used with modems; vbCr is a Visual Basic constant standing for the ASCII code for carriage return/line feed):


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


You can also send data this way, as we’ll see in this chapter:



MSComm1.Output = "Here’s some text!"






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.



Wyszukiwarka

Podobne podstrony:
405 a
Smarowanie tylnych łożysk 405
KOSS PRO 405
SAU 398 409
canton gle 409
407 409
05 (405)
22id)405
409 (B2007) Należności krótkoterminowe wycena i prezentacja w bilansie
409 411

więcej podobnych podstron