请教与PLC通信的时候的问题
请问VB与PLC进行通信的时候是如何实现的?为何我收到的都是乱码,程序如下:
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
End Sub
Private Sub Command1_Click()
Dim Rdstr$
MSComm1.PortOpen = True
MSComm1.InputLen = 0
'(2)读取1#站PLC数据寄存器DO的内容
'指令序列代码格式为:ENQ 01 FF WR 0D000 01 CR LF
SendCom$ = Chr(5) + "01FFWRD6040001" + Chr(13) + Chr(10)
MSComm1.Output = SendCom '向PLC發出讀取D0數據命令字符串
Debug.Print SendCom$
Do While (i < 5000)
i = i + 1
Loop
DoEvents
Rdstr$ = 0
Debug.Print "1:" & Rdstr
Rdstr$ = MSComm1.Input '接收PLC發回數據
Debug.Print "2:" & Rdstr
rvd$ = Mid(Rdstr, 1, 5)
Debug.Print "3:" & rvd$
If rvd = Chr(2) + "01FF" Then '是否接收到
ACKStr$ = Chr(6) + "01FF"
MSComm1.Output = ACKStr
Rdstr = Val("&H" + Trim(Mid(Rdstr, 6, 4))) '取出D0數據
Label1.Caption = Rdstr
MsgBox "读取正确", 16
Else
MsgBox "读取错误!", 16
End If
MSComm1.PortOpen = False
End Sub