7,763
社区成员
发帖
与我相关
我的任务
分享
Private Sub Form Load()
Data =“WD02001234”
outdata =“@00”+ Data
L =Len(outdata)
fcsl = 0
For i = 1 To L
fcsl =fcs1 Xor Asc(Mid(outdata,i,1))
Next i
FCS = Hex(fcs 1) ’计算校验码 FCS
Dim Instring As String ’保存输入子串的缓冲
MSComm1.Output = outdata + FCS + “*”+ Chr$(13)’将命令送到 PLC
Do
DoEvents
Loop Until MSComm1.InBufferCount > = 10 ’等待数据返回到串行端口
Instring = MSComm1.Input ’ 从串行端口读响应。
MSComm1.PortOpen = False ’关闭串行端口
If Mid (Instring,Len (Instring)- 1,1)<>“*” Then ’检查响应数据“*”
MsgBox“通讯出错”
Else
fcs2 = 0
For i = 1 To Len(Instring)- 4 ’校验响应数据FCS
fcs2 = fcs2 Xor Asc(Mid(Instring,i,1))
Next i
fcs3 = Hex(fcs2)
If fcs3 = Mid(Instring,Len(Instring)- 3,2)Then
MsgBox“通讯有效”
Else
MsgBox“通讯出错”
End If
End If
End Sub