35
Sample program
'MSCOMM1
Transmission of command and receiving of response
Private Function SendComm(ByVal sSendCommand As String, Optional ByRef sRecvBuffer As
String) As Boolean
Dim sSend As String
'Transmission letters
Dim sRecv As String
'Receiving letters buffer
Dim nTMO As Long
'Time out
On Error GoTo Err_SendComm
'Receiving time out is set to 1sec.
nTMO = GetTic 1000
'Transmission letter is half pitch + CRLF
sSend = StrConv(sSendCommand, vbNarrow)
ShowLog "Send", sSend
sSend = sSend & vbCrLf
With MSComm1
FlashBuffer
. Output = sSend
'transmission of letters
End With
Do
DoEvents
sWait 0.1
'Weight of 100ms
With MSComm1
If . InBufferCount > 0 Then
'Receiving buffer (port) includes letters
sRecv = sRecv & . Input
'Receiving letters stored in buffer
'Debug.Print sRecv
End If
End with
If InStr(sRecv, vbCr) > 0 then
'Receiving letters buffer includes delimiter
sRecv = Left(sRecv, InStr(sRecv, vbCr)
–
1) 'delimiter and after is left
ShowLog "Recv", sRecv
Exit Do
End If
If GetTickCount >= nTMO Then
'time out condition
ShowLog "SendComm", "TMO Error"
GoTo Err_SendComm:
End If
Loop
Exit_SendComm:
'Normal end
sRecvBuffer = sRecv
SendComm = True
Exit Function
Err_SendComm:
'Abnormal end
sRecvBuffer = ""
SendComm = False
MsgBox "An error occurred in SendComm.", vbCritical
Exit Function
End Function