lngStatus = SetCommErrorEx( _
\ udtPorts(intPortID).lngHandle) GoTo Routine_Exit End If Wend Else
' Some other error occurred. lngWrSize = -1
lngStatus = SetCommErrorEx(\ udtPorts(intPortID).lngHandle) GoTo Routine_Exit
End If End If
For i = 1 To 10 DoEvents Next
Routine_Exit:
CommWrite = lngWrSize Exit Function
Routine_Error:
lngStatus = Err.Number With udtCommError
.lngErrorCode = lngStatus .strFunction = \
.strErrorMessage = Err.Description End With
Resume Routine_Exit End Function
'------------------------------------------------------------------------------- ' CommGetLine - Get the state of selected serial port control lines. '
' Parameters:
' intPortID - Port ID used when port was opened.
' intLine - Serial port line. CTS, DSR, RING, RLSD (CD) ' blnState - Returns state of line (Cleared or Set). '
' Returns:
' Error Code - 0 = No Error.
'------------------------------------------------------------------------------- Public Function CommGetLine(intPortID As Integer, intLine As Integer, _ blnState As Boolean) As Long
Dim lngStatus As Long
Dim lngComStatus As Long, lngModemStatus As Long
On Error GoTo Routine_Error
lngStatus = GetCommModemStatus(udtPorts(intPortID).lngHandle, lngModemStatus)
If lngStatus = 0 Then
lngStatus = SetCommError(\ GoTo Routine_Exit End If
If (lngModemStatus And intLine) Then blnState = True Else
blnState = False End If
lngStatus = 0
Routine_Exit:
CommGetLine = lngStatus Exit Function
Routine_Error:
lngStatus = Err.Number With udtCommError
.lngErrorCode = lngStatus .strFunction = \
.strErrorMessage = Err.Description End With
Resume Routine_Exit End Function
'------------------------------------------------------------------------------- ' CommSetLine - Set the state of selected serial port control lines. '
' Parameters:
' intPortID - Port ID used when port was opened. ' intLine - Serial port line. BREAK, DTR, RTS
' Note: BREAK actually sets or clears a \' the transmit data line.
' blnState - Sets the state of line (Cleared or Set). '
' Returns:
' Error Code - 0 = No Error.
'------------------------------------------------------------------------------- Public Function CommSetLine(intPortID As Integer, intLine As Integer, _ blnState As Boolean) As Long
Dim lngStatus As Long Dim lngNewState As Long
On Error GoTo Routine_Error
If intLine = LINE_BREAK Then If blnState Then
lngNewState = SETBREAK Else
lngNewState = CLRBREAK End If
ElseIf intLine = LINE_DTR Then If blnState Then
lngNewState = SETDTR Else
lngNewState = CLRDTR End If
ElseIf intLine = LINE_RTS Then If blnState Then
lngNewState = SETRTS Else
lngNewState = CLRRTS End If End If
lngStatus = EscapeCommFunction(udtPorts(intPortID).lngHandle, lngNewState)
If lngStatus = 0 Then
lngStatus = SetCommError(\ GoTo Routine_Exit End If
lngStatus = 0
Routine_Exit:
CommSetLine = lngStatus Exit Function
Routine_Error:
lngStatus = Err.Number With udtCommError
.lngErrorCode = lngStatus .strFunction = \
.strErrorMessage = Err.Description End With
Resume Routine_Exit
End Function
'------------------------------------------------------------------------------- ' CommGetError - Get the last serial port error message. '
' Parameters:
' strMessage - Error message from last serial port error. '
' Returns:
' Error Code - Last serial port error code.
'------------------------------------------------------------------------------- Public Function CommGetError(strMessage As String) As Long
With udtCommError
CommGetError = .lngErrorCode
strMessage = \ \ End With
End Function
‘*********************************************************************** Private Sub CommandButton1_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4 Dim lngStatus As Long
intPortID = 1
' Open COM port
lngStatus = CommOpen(intPortID, \ \End Sub
Private Sub CommandButton2_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4 Dim lngStatus As Long Dim strData As String
intPortID = 1
strData = \
'Writa data
lngStatus = CommWrite(intPortID, strData)
End Sub
Private Sub CommandButton3_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4 Dim lngStatus As Long Dim strData As String
intPortID = 1
lngStatus = CommRead(intPortID, strData, 10)
End Sub