|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
The Text Box controls KeyPress event procedure links the users input to the Comm control. Rather than sending each individual character as it is typed, this procedure saves the input in a buffer and sends it all at once when the user presses the Enter key. Received data is handled in the OnComm event procedure. After checking the CommEvent property to see that the event was indeed triggered by receipt of data, the code adds the received character to the end of the Text Boxs Text property, then moves the insertion point to the end of the text.
Listing 15.2 Code in COMMDEMO.FRM. Option Explicit Dim Buf1 As String Private Sub Text1_KeyPress(KeyAscii As Integer) Add the key to the buffer. Buf1 = Buf1 & Chr$(KeyAscii) If it was a carriage return, send the buffer contents to the Comm control and empty the buffer. If KeyAscii = 13 Then MSComm1.Output = Buf1 Buf1 = End If End Sub Private Sub Form_Load() Initialize the Comm control. MSComm1.CommPort = 1 MSComm1.Settings = 14000,E,7,1 Debug.Print ---------------- Comm event will be triggered when a single character is received. MSComm1.RThreshold = 1 Open the port. MSComm1.PortOpen = True End Sub Private Sub Form_Resize() Set Text Box to fill the form. Text1.Width = Form1.ScaleWidth Text1.Height = Form1.ScaleHeight Text1.Left = 0 Text1.Top = 0 End Sub Private Sub Form_Unload(Cancel As Integer) Close the port. MSComm1.PortOpen = False End Sub Private Sub MSComm1_OnComm() Dim s As String When a comm event occurs. Was it a receive event? If so, add the received character to the Text Box and set the insertion point at the end of the text. Other events are ignored. Select Case MSComm1.CommEvent Case comEvReceive s = MSComm1.Input If Asc(s) = 13 Then s = vbCrLf Text1.Text = Text1.Text & s Text1.SelStart = Len(Text1.Text) End Select End Sub If you use this program with a modem, youll have to type the modem commands yourself (the commands are usually sent automatically by a communication program). Most modems adhere to the AT command set, originally developed by Hayes for its modems. The first command you should send is ATZ, which resets and initializes the modem. The model will respond OK. A few other commands are listed in Table 15.3; refer to your modem documentation for more information. This very basic demonstration program is intended to show you only the fundamentals of using the Comm control. As it stands, the program includes no error handling and is vulnerable to many problems in use. Even with the Comm control, writing a full-featured and reliable serial communication program is a major task. Whenever I need anything but the most basic communication capabilities in a Visual Basic program, I always investigate the third-party market for a custom control rather than trying to do the job myself using the Comm control.
|
![]() |
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. |