Private Sub Command2_Click()
Dim Outstr, Instr As String
Dim a, b, c As String
Outstr = Chr$(2) & "000A202" & Chr$(3)
Outstr = Outstr + CheckSum(Mid$(Outstr, 2, Len(Outstr)))
'组成命令帧
MSComm1.Output = Outstr '发送指令
Do '接受PLC的应答
DoEvents
Loop Until MSComm1.InBufferCount > 9
Instr = MSComm1.Input '读取PLC的应答帧
MSComm1.PortOpen = False '关闭串行口
---------------------------------------------
a=val(Asc(Left$(Instr,1)))'应答帧的起始字节的数值
b=checksum(mid$(instr,2,len$(instr)-3))'计算应答帧的和数检查码
c=mid(instr,len(instr)-1,2)'求接收到应答帧的和数检查码
----------------------------------------------------
If a = 2 And b = c Then '检查起始字节是否为STX及和数检查是否正确
MsgBox ("通信成功!")
Else
MsgBox ("通信失败!")
End If
End Sub
中间用-------------隔开的三行语句,系统总是提醒"缺少(",且箭头指向LEFT$附近!
第一次发贴,希望高手指点!
因为Instr跟一个函数InStr同名了,把Instr都改成Instr2就可以了
这个帖子到VB中去问就可以了