求助:用VB编程,建立与PLC的通讯,下面的语句错了吗?
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$附近!
第一次发贴,希望高手指点!
[解决办法]
Input 属性通过一个 Variant 返回文本数据。如果设置 InputMode 为 comInputModeBinary,Input 属性通过一个 Variant .返回一二进制数据的数组。
dim instr() as Variant
a=val(Asc(Left$(Instr(),1)))