You are on page 1of 5

VB AB [ [[ [PLC [ [ [ [

\b bC [
b _C [|!j|{j)C 1,|[;
S\ !{j])j|jS\ |)|j[jj]
|j\b ,|)C 1,|[|{|j],|
o'o_x.SC00 _C|j|;.
Otou lx't
tu: ouuutu_
vut Su| CouuC')
t|0)
:t
uTu|'0 t|)) Tu
o :t 0 To ;
TxtS'Txt Stt|:t)) C)
xt :t
TxtS'Txt C) C0)
lu
lu Su|
Private Sub Command2_Click()
ReDim tm%(5)
tm%(0) = Rnd * 32768
tm%(1) = Rnd * 32768
tm%(2) = Rnd * 32768
tm%(3) = Rnd * 32768
tm%(4) = Rnd * 32768
If Not WriteTable(4, tm%()) Then Text1.SelText = "/|,''"
End Sub
Private Sub Exit_Click()
Unload Me
End
End Sub
Private Sub Form_Load()
Comm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
Comm1.PortOpen = False
End Sub
www.PLCworld.cn
Private Sub CalcCRC(mes$)
Dim byt%, res&
',|crc |,;);])],;
byt% = 3
Do
res& = res& Xor Asc(Mid(mes$, byt%, 1))
rotate res&
If Asc(Mid(mes$, byt%, 1)) = 16 Then
mes$ = Left$(mes$, byt%) + Chr(16) + Right$(mes$, Len(mes$) -
byt%)
byt% = byt% + 1
End If
byt% = byt% + 1
Loop While (byt% <= Len(mes$) - 2)
res& = res& Xor 3
rotate res&
mes$ = mes$ + Chr(res& Mod 256) + Chr(Int(res& / 256))
End Sub
Function ReadTable(start, n%())
Dim st, com$
')PLC CIF |,,{|, Micrologix=N7 SLC500=N9
If comunicating Then Exit Function
comunicating = True
Form1.Comm1.InputLen = 0 ',[
com$ = Form1.Comm1.Input
'|,
com$ = Chr(16) + Chr(2) + Chr(0) + Chr(0)
com$ = com$ + Chr(1) + Chr(0) + Chr(tns%) + Chr(0)
com$ = com$ + Chr(start) + Chr(0) + Chr(UBound(n%) * 2)
com$ = com$ + Chr(16) + Chr(3)
'|crc ,}[)];
CalcCRC com$
tns% = tns% + 1
If tns% = 256 Then tns% = 0
'j
Form1.Comm1.Output = com$
'|]
st = Timer
Do
DoEvents
Loop While st + 3 > Timer And Form1.Comm1.InBufferCount < 2
'),|]
www.PLCworld.cn
Form1.Comm1.InputLen = 2
com$ = Form1.Comm1.Input
If com$ <> Chr(16) + Chr(6) Then
comunicating = False
Exit Function
End If
st = Timer '|
Do
DoEvents
Loop While st + 3 > Timer And Form1.Comm1.InBufferCount < 12 +
(UBound(n%)
* 2)
']]j
If Form1.Comm1.InBufferCount < 12 + (UBound(n%) * 2) Then
comunicating = False
Exit Function
End If
'j]
Form1.Comm1.Output = Chr(16) + Chr(6)
']
Form1.Comm1.InputLen = 0
com$ = Form1.Comm1.Input
st = 3
Do
If Mid(com$, st, 1) = Chr(16) Then
com$ = Left(com$, st) + Right(com$, Len(com$) - 1 - st)
End If
st = st + 1
Loop While st < Len(com$) - 4
'];
For st = 0 To UBound(n%) - 1
n%(st) = 256 * Asc(Mid(com$, 2 * st + 10, 1)) + Asc(Mid(com$, 2
* st + 9,
1))
Next st
ReadTable = True
comunicating = False
End Function
Private Sub rotate(res&)
Dim bitout%, shift%
For shift% = 1 To 8
bitout% = res& Mod 2
res& = Int(res& / 2)
www.PLCworld.cn
If bitout% Then
res& = res& Xor &H1000A001
res& = res& - &H10000000
End If
Next shift%
End Sub
Function WriteTable(start, n%())
Dim st, com$
'] PLC CIF |,, Micrologix=N7 SLC500=N9
If comunicating Then Exit Function
comunicating = True
Form1.Comm1.InputLen = 0
com$ = Form1.Comm1.Input
com$ = Chr(16) + Chr(2) + Chr(0) + Chr(0)
com$ = com$ + Chr(8) + Chr(0) + Chr(tns%) + Chr(0)
com$ = com$ + Chr(start) + Chr(0)
For st = 0 To UBound(n%)
com$ = com$ + Chr(n%(st) Mod 256) + Chr(Int(n%(st) / 256))
Next st
com$ = com$ + Chr(16) + Chr(3)
tns% = tns% + 1
If tns% = 256 Then tns% = 0
CalcCRC com$
Form1.Comm1.Output = com$
st = Timer
Do
DoEvents
Loop While st + 3 > Timer And Form1.Comm1.InBufferCount < 2
Form1.Comm1.InputLen = 2
com$ = Form1.Comm1.Input
If com$ <> Chr(16) + Chr(6) Then
comunicating = False
Exit Function
End If
st = Timer
Do
DoEvents
Loop While st + 3 > Timer And Form1.Comm1.InBufferCount < 12
Form1.Comm1.Output = Chr(16) + Chr(6)
If Form1.Comm1.InBufferCount < 12 Then
comunicating = False
Exit Function
End If
www.PLCworld.cn
Form1.Comm1.Output = Chr(16) + Chr(6)
WriteTable = True
comunicating = False
End Function
www.PLCworld.cn

You might also like