Download The Windows Serial Port Programming Handbook PDF

TitleThe Windows Serial Port Programming Handbook
File Size13.8 MB
Total Pages824
Table of Contents
                            Front cover
Table of Contents
About the Author
Acknowledgments
Chapter 1. The Fundamentals of Serial Port Communications
Chapter 2. Serial Port Programming for MS-DOS in ANSI C and Assembly Languages
Chapter 3. Serial Port Interfaces Developed in VC++ 6.0
Chapter 4. Serial Port Programming in Visual BASIC
Chapter 5. Serial Port Programming in LabVIEW
Chapter 6. Serial Port Programming in MATLAB
Chapter 7. Serial Port Programming in Smalltalk
Chapter 8. Serial Port Programming in Java
Appendix A: ASCII Code Table
INDEX
Back cover
                        
Document Text Contents
Page 2

The
Windows
Serial Port
Programming
Handbook

Page 412

Serial Port Programming in Visual BASIC 393

23: After the data is sent out, we need to wait for the feedback from the slave program to
confirm that the slave computer did receive that data. To make this waiting start, we
enable the VBTimer to monitor the possible timeout error.

24: Another Do-Until loop is used to wait for one of two conditions exists: either the
feedback from the slave program or a timeout error. When that loop is terminated by
either event, the VBTimer should be disabled to prevent any additional timeout errors
from being reported.

25: If a timeout error occurs, the function returns to the calling event procedure with a
TIME_OUT constant to indicate this situation. The TimeOut flag is reset to 0 (False)
to indicate that the timeout error has been responded.

26: Otherwise it means that a character has arrived in the input buffer on the master computer.
The Input property of the MSComm control is used to pick up that character. The
received character is then stored in the local variable revdData.

27: If the received character is “6” (ACK), which means that the slave program has success-
fully received the data sent by the master program, the function returns to the calling
event procedure with an ACK_RECEIVED constant to indicate this situation to the event
procedure.

FIGURE 4.65 Code for the CommSend() function.

(General) CommSend

Private Function CommSend(sData As String) As Integer
Dim revdData As String
VBTimer.Enabled = True
Do
DoEvents
Loop Until (MSComm1.CTSHolding = True) Or (TimeOut = True)

VBTimer.Enabled = False
If TimeOut = True Then 'time_out occurs
CommSend = TIME_OUT 'return the time_out to calling function
TimeOut = False
Else 'RTS active received
MSComm1.Output = sData 'send out the data
End If
VBTimer.Enabled = True 'enable the timer
Do 'wait for the feedback from the slave
DoEvents
Loop Until (MSComm1.InBufferCount > 0) Or (TimeOut = True)
VBTimer.Enabled = False 'disable the timer

If TimeOut = True Then 'time_out occurs
CommSend = TIME_OUT 'return the time_out to calling function
TimeOut = False 'reset the time_out flag
Else 'received the feedback from the slave
revdData = MSComm1.Input 'pick up the feedback character
If revdData = "6" Then 'if the feedback = ACK
CommSend = ACK_RECEIVED 'return ACK
Else
CommSend = NAK_RECEIVED 'return NAK
End If
End If
End Function

18

19

20
21

22

23
24

25

26

27

28

Page 413

394 The Windows Serial Port Programming Handbook

28: Otherwise it means that a NAK character is received. The function returns to the calling
event procedure with a NAK_RECEIVED constant. The program needs to re-send that
data if this situation is really occurred.

The last function we need to coding is the getFileName() function.

4.3.3.1.4.5 The Coding of the getFileName() Function
The purpose of this function is to allow the user to select the desired file to be transmitted from

the files stored in the local derives on the master computer. Microsoft Common Dialog Control 6.0
is used to accomplish this. In Section 4.3.3.1.1, we illustrated how to add this component to our
current project. This component provides a sequence of properties and allows us to modify this
dialog control as we desired. Four properties are very important to us, which are

• DialogTitle
• Filter
• FileName
• ShowOpen

and we want to use them in the current project.
The DialogTitle is used to display the title of the dialog control when it is opened. The

Filter is used to select the desired type of the default files, and the ShowOpen property is used
to display the dialog control when it is opened. The FileName is used to store the file name
selected by the user at run time. Now we add a new function into this Visual Basic project by
clicking the Tools|Add Procedure menu item. Assign the new function the parameters that
are listed in Table 4.33.

Type As String at the end of this new added function because this function needs to return
the selected file name that is a string variable. Enter the code that is shown in Figure 4.66 into this
function. Your finished function body should match the one shown in Figure 4.66.

Each section of this code is explained in the following list.

29: The With statement is used to assign the values to the properties of the Microsoft
Common Dialog Control 6.0, dlgFile, which is the name of the dialog control we
assigned when we add that control to our project. The usage of With statement can save
the time and space when assigning the value to each property of the control. For example,
we do not need to precede the control name dlgFile for each property to assign its
value. The property Filter is assigned by “All Files (*.*)|*.*” value to display all files
in the current folder. The property ShowOpen is to indicate to the dialog control that
this dialog needs to be displayed when it is opened. The End With statement is needed
to complete the using of the With statement.

30: A Do-Until loop is used to pause processing until the FileName property of the
dlgFile control is filled by a certain file name selected by the user at run time.

TABLE 4.33
Properties for the getFileName() Function

Property Value
Name getFileName
Type Function
Scope Private

Similer Documents