发表于:2006/4/19 9:58:00
#0楼
本人用vb编写施奈德twido系列plc通讯口,传输的数据是乱码,也不能动作plc,不知问题出现在哪??望各位指教,QQ联系:41233944
程序如下:
Private Sub Command2_Click()
Dim value() As Byte
Dim I As Integer
Me.MSComm1.CommPort = 4
Me.MSComm1.SThreshold = 0
Me.MSComm1.Settings = "9600,e,8,1"
Dim CRC As String
Dim d() As Byte '待传输数据
ReDim d(5) As Byte
Dim data As String
d(0) = 2 'slave address
d(1) = 5 'function
d(2) = 0 'starting address high
d(3) = 1 'starting address low
d(4) = &HFF 'no.of registers high
d(5) = &H0 'no.of registers low
CRC = CRC16(d) '调用CRC16计算函数
'CRC(0)为高位
'CRC(1)为低位
If Me.MSComm1.PortOpen = False Then
Me.MSComm1.PortOpen = True
End If
Me.MSComm1.Output = Chr(2) + Chr(5) + Chr(0) + Chr(1) + Chr(&HFF) + Chr(&H0) + Chr(CRC16Lo) + Chr(CRC16Hi)
Do While I < 9
I = I + 1
Loop
value = Me.MSComm1.Input
MsgBox (value)
Me.MSComm1.PortOpen = False
End Sub
Public Function CRC16(data() As Byte) As String
'CRC寄存器
Dim CL As Byte, CH As Byte '多项式码&HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim I As Integer
Dim Flag As Integer
CRC16Lo = &HFF
CRC16Hi = &HFF
CL = &H1
CH = &HA0
For I = 0 To UBound(data)
CRC16Lo = CRC16Lo Xor data(I) '每一个数据与CRC寄存器进行异或
For Flag = 0 To 7
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2 '高位右移一位
CRC16Lo = CRC16Lo \ 2 '低位右移一位
If ((SaveHi And &H1) = &H1) Then '如果高位字节最后一位为1
CRC16Lo = CRC16Lo Or &H80 '则低位字节右移后前面补1
End If '否则自动补0
If ((SaveLo And &H1) = &H1) Then '如果LSB为1,则与多项式码进行异或
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next Flag
Next I
Dim ReturnData(1) As Byte
ReturnData(0) = CRC16Hi 'CRC高位
ReturnData(1) = CRC16Lo 'CRC低位
CRC16 = ReturnData
End Function
程序如下:
Private Sub Command2_Click()
Dim value() As Byte
Dim I As Integer
Me.MSComm1.CommPort = 4
Me.MSComm1.SThreshold = 0
Me.MSComm1.Settings = "9600,e,8,1"
Dim CRC As String
Dim d() As Byte '待传输数据
ReDim d(5) As Byte
Dim data As String
d(0) = 2 'slave address
d(1) = 5 'function
d(2) = 0 'starting address high
d(3) = 1 'starting address low
d(4) = &HFF 'no.of registers high
d(5) = &H0 'no.of registers low
CRC = CRC16(d) '调用CRC16计算函数
'CRC(0)为高位
'CRC(1)为低位
If Me.MSComm1.PortOpen = False Then
Me.MSComm1.PortOpen = True
End If
Me.MSComm1.Output = Chr(2) + Chr(5) + Chr(0) + Chr(1) + Chr(&HFF) + Chr(&H0) + Chr(CRC16Lo) + Chr(CRC16Hi)
Do While I < 9
I = I + 1
Loop
value = Me.MSComm1.Input
MsgBox (value)
Me.MSComm1.PortOpen = False
End Sub
Public Function CRC16(data() As Byte) As String
'CRC寄存器
Dim CL As Byte, CH As Byte '多项式码&HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim I As Integer
Dim Flag As Integer
CRC16Lo = &HFF
CRC16Hi = &HFF
CL = &H1
CH = &HA0
For I = 0 To UBound(data)
CRC16Lo = CRC16Lo Xor data(I) '每一个数据与CRC寄存器进行异或
For Flag = 0 To 7
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2 '高位右移一位
CRC16Lo = CRC16Lo \ 2 '低位右移一位
If ((SaveHi And &H1) = &H1) Then '如果高位字节最后一位为1
CRC16Lo = CRC16Lo Or &H80 '则低位字节右移后前面补1
End If '否则自动补0
If ((SaveLo And &H1) = &H1) Then '如果LSB为1,则与多项式码进行异或
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next Flag
Next I
Dim ReturnData(1) As Byte
ReturnData(0) = CRC16Hi 'CRC高位
ReturnData(1) = CRC16Lo 'CRC低位
CRC16 = ReturnData
End Function