请教各位:上位机采用VB编程,PLC走PPI协议,现想往PLC的VD100中写入十六进制FFFFFFFF数值,上位机VB通过字节格式发送十六进制68 23 23 68 02 00 7C 32 01 00 00 00 00 00 0E 00 00 04 01 12 0A 10 06 00 01 00 01 84 00 03 20 00 04 00 20 FF FF FF FF AF 16 后PLC成功返回E5,接着发送确认指令十六进制10 02 00 5C 5E 16后,PLC返回68 F F 68 00 02 08 32 02 00 00 00 00 00 00 00 00 85 00 C3 16 ,此时查看PLC寄存器VD100的值没被写入,不知何原因,还请高手帮忙分析,先谢谢了。
其次在获得PLC的返回指令后再给PLC的指令似乎需要略延迟些(出于PLC的收发状态转换)
从:西门子PPI协议
可以获知是通过COM口实施串口通信的.
LZ应该放上你自己的具体代码.
Dim WriteDouByte(0 To 40) As Byte
Dim ReceiveByte() As Byte
Dim FCS As Double
Dim ss As Integer
MSComm1.InBufferCount = 0 '清空接受缓冲区
WriteDouByte(0) = &H68
WriteDouByte(1) = &H23
WriteDouByte(2) = &H23
WriteDouByte(3) = &H68
WriteDouByte(4) = &H2
WriteDouByte(5) = &H0
WriteDouByte(6) = &H6C
WriteDouByte(7) = &H32
WriteDouByte(8) = &H1
WriteDouByte(9) = &H0
WriteDouByte(10) = &H0
WriteDouByte(11) = &H0
WriteDouByte(12) = &H0
WriteDouByte(13) = &H0
WriteDouByte(14) = &HE
WriteDouByte(15) = &H0
WriteDouByte(16) = &H0
WriteDouByte(17) = &H4
WriteDouByte(18) = &H1
WriteDouByte(19) = &H12
WriteDouByte(20) = &HA
WriteDouByte(21) = &H10
WriteDouByte(22) = &H6
WriteDouByte(23) = &H0
WriteDouByte(24) = &H1
WriteDouByte(25) = &H0
WriteDouByte(26) = &H1
WriteDouByte(27) = &H84
WriteDouByte(28) = &H0
WriteDouByte(29) = &H3
WriteDouByte(30) = &H20
WriteDouByte(31) = &H0
WriteDouByte(32) = &H4
WriteDouByte(33) = &H0
WriteDouByte(34) = &H20
WriteDouByte(35) = &HFF
WriteDouByte(36) = &HFF
WriteDouByte(37) = &HFF
WriteDouByte(38) = &HFF
For n = 4 To 38
FCS = FCS + WriteDouByte(n)
Next n
WriteDouByte(39) = FCS Mod 256
WriteDouByte(40) = &H16
MSComm1.Output = WriteDouByte
DelayTime 50
ReceiveByte = MSComm1.Input
If UBound(ReceiveByte) = 0 Then '确定PLC返回一个字节
If ReceiveByte(0) = &HE5 Then '判断返回字节是否为E5
Call SendSurePublicByte '发送确认指令
DelayTime 50 '延时
ReceiveByte = MSComm1.Input
Text4 = ""
For ss = LBound(ReceiveByte) To UBound(ReceiveByte)
Text4 = Text4 + Format(hex(ReceiveByte(ss)), "00") + " "
Next ss
End If
End If
End Sub
Sub SendSurePublicByte() '向PLC发送确认指令的公共字节
Dim StrSendSure As String
Dim LenSureByte As Integer
StrSendSure = "10 02 00 5C 5E 16"
StrSendSure = Replace(StrSendSure, " ", "")
LenSureByte = Len(StrSendSure) / 2
ReDim SendSureByte(0 To LenSureByte)
For i = 0 To LenSureByte
SendSureByte(i) = Val("&H" + Mid(StrSendSure, i * 2 + 1, 2))
Next i
MSComm1.Output = SendSureByte
End Sub
Sub SendSurePublicByte() '向PLC发送确认指令的公共字节
Dim StrSendSure As String
Dim LenSureByte As Integer
Dim i As Long
Dim SendSureByte() As Byte
StrSendSure = "10 02 00 5C 5E 16"
StrSendSure = Replace(StrSendSure, " ", "")
Print StrSendSure
ReDim SendSureByte(0 To -1 + Len(StrSendSure) / 2)
For i = 1 To Len(StrSendSure) Step 2
SendSureByte((i - 1) / 2) = Val("&H" + Mid(StrSendSure, i, 2))
Next i
MSComm1.Output = SendSureByte
End Sub
问题即可解决。不过还是感谢zdingyun,Thank you!