VBA 中使用 API 串口通信 Serial Port(英文)(4)

2019-03-03 17:11

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


VBA 中使用 API 串口通信 Serial Port(英文)(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:我国企业员工绩效考核中存在的问题及对策分析硕士论文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: