有关于CWSeria串口类的问题l
各位高手帮忙看下这个程序,被闷了好久了,帮忙看看Public Function IMSSendCommand(ByRef SendString As String, Query As Integer) As String 'MDI马达发送命令 所有运行的程序
Dim RecvString As String
Dim CompString As String
On Error GoTo Done
RecvString = ""
frmFC.CWSerial1.Write SendString & Chr(10) '通过USB 收到什么样子的数据 是通过串口CWSerial1传递字符数据 发送数据
GetMore:
RecvString = RecvString & frmFC.CWSerial1.Read(1000) '是写入程序 收到数据字符 一直在这里等待是否我数据字符等待
If InStr(RecvString, ">") < 1 Then GoTo GetMore '判断有多少个字符输入进去,少于一个时候则重新判断 '>代表在IMS始端检测,InStr比较是否有相同的字母
Done:
If Len(RecvString) > 0 Then '判断是否收到指令
'MsgBox RecvString
Else
If InStr(SendString, "YP=") < 1 Then MsgBox "No data available" & vbCrLf & SendString 'Ignore first command - error for some reason
MsgBox "No data available" & vbCrLf & SendString
'TimedDelay2 1#
Exit Function '跳出窗口过程
End If
If InStr(RecvString, ">") < 1 Then
MsgBox ("Command Prompt Failed!!!" & vbCrLf & "Command=" & SendString & vbCrLf & "Response=" & RecvString)
GoTo BadResponse
End If
CompString = SendString
i = InStr(RecvString, CompString) '返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置 判断发送的命令和接收到的命令是否相同
If i < 1 Then
MsgBox ("Command Echo Failed!!!" & vbCrLf & "Command=" & SendString & vbCrLf & "Response=" & RecvString)
GoTo BadResponse
End If
If Query = 0 Then Exit Function '等于0时候就退出
s1 = Right(RecvString, Len(RecvString) - Len(SendString) - 2)
i = InStr(s1, Chr(13))
If i < 1 Then
MsgBox ("Command Terminate Failed!!!" & vbCrLf & "Command=" & SendString & vbCrLf & "Response=" & RecvString)
GoTo BadResponse
End If
s4 = Left(s1, i - 1) '。字符串表达式其中最左边的那些字符将被返回 i-1是字符个数 s1是字符串
s3 = ""
For i = 1 To Len(RecvString)
s1 = Right(RecvString, Len(RecvString) - i + 1)
s2 = Left(s1, 1)
s3 = s3 & i & " " & s2 & " " & Asc(s2) & vbCrLf
Next i
'MsgBox s3
IMSSendCommand = s4
Exit Function
BadResponse:
s3 = ""
For i = 1 To Len(RecvString)
s1 = Right(RecvString, Len(RecvString) - i + 1)
s2 = Left(s1, 1)
s3 = s3 & i & " " & s2 & " " & Asc(s2) & vbCrLf
Next i
MsgBox s3
Open "c:\FC\Temp\IMSErrorMessages.dat" For Append As #2
Print #2, Date, Time, RecvString
Close #2
End Function
以下这这个是怎么样调用
上面的程序的
Public Function IMSCheckStageMotion(ByVal Axis As Integer) As Integer 'Motion移动
Dim Command As String
Dim IStart As Integer
Dim IStop As Integer
If Axis = 0 Then
IStart = 1
IStop = IMSNAxis
Else
IStart = Axis
IStop = Axis
End If
IMSCheckStageMotion = 0
For i = IStart To IStop
If IMSParam(i).Enabled = 1 Then
Command = IMSParam(i).AxisLabel & "PR MV" '?? VM=360000 PR VM The MDrivePlus Motion Control will return a value of 360000
Status = IMSSendCommand(Command, 1) '1. Click in the Terminal Window, and type (followed by ENTER):
If Status = 1 Then IMSCheckStageMotion = 1' 2. The MDrivePlus Motion Control will return a value of 768000
End If
Next i
End Function