|
发表于 2007-2-6 18:30:40
|
显示全部楼层
4)编程实现的代码构成
& F1 @' \3 h# k, B5 x% t: H9 y(1) LRC算法校验的实现
1 r% ]: x: f3 |0 X9 `9 e7 I5 sPublic Function LRC(str As String) As String$ N4 ` Z$ } ^& ^! D: Q0 f
c = 0
: R& t% B' R+ w8 z% C$ ]l = Len(str)
" `+ X2 o0 Q8 w5 c+ j2 `3 ~For c = c + 1 To l8 r1 Q" n% a& j, X; S3 o# b O
c_data = Mid$(str, c, 2)
3 A5 |8 P1 m( X( I, Q4 Zd_lrc = d_lrc + Val("&H" + c_data)
5 c( y6 S/ A S5 g6 _6 Gc = c + 1. [. b/ |- S6 f
Next c6 `4 Z/ u b' P9 }) m' ?, z
If d_lrc > &HFF Then
7 y3 z) D7 V' @' F0 Y6 b% [d_lrc = d_lrc Mod &H100' q0 c5 j) d# i' u& X
End If
4 z, F3 Z3 p f/ [0 T' q4 Rh_lrc = Hex(&HFF - d_lrc + 1)
$ `1 H/ G* C- @) MIf Len(h_lrc) > 2 Then* H5 N m8 [' t$ Q
h_lrc = Mid(h_lrc, Len(h_lrc) - 1, 2)# z: H0 v! q$ S7 _4 n) U8 Y9 u
End If' t5 d0 z, J5 J: H
LRC = h_lrc; M% ^. e. i' o+ z6 q# [1 \# Q
End Function/ O5 i" _; S4 |5 e8 N/ j1 i
(2) 运行的开始就判断PLC的状态并设置标志位1 Q5 `. i* j5 k& x5 N2 _
'初次运行打开串口,并显示PLC运行状态$ S1 w( [. l" _& S5 _+ |, M3 o
Private Sub Form_Load()6 S8 I1 r+ b: g8 Z6 r
Dim s1 As String1 Z9 X* J; T( |* k" |
Dim s2 As String$ u0 T8 B4 |& f" H& G
Dim s22 As String
/ g! n# R/ f0 N7 DDim s3 As String
/ F& Z* X3 X/ ~# u( q! A( xDim s4 As String, V2 [! `% _' Z( B0 s
MSComm1.PortOpen = True. x2 O( n- }. t2 A5 \8 ~
s2 = "01010C300001" a* T/ b; m* i$ Z
s22 = LRC(s2). \6 V; s7 S' n% h% {. P; H8 t8 p
s1 = ":" + s2 + s22 + Chr$(13) + Chr$(10)
$ o- Q% q" X' [2 tMSComm1.Output = s1
/ R4 b8 H2 {8 Xs3 = MSComm1.Input$ X- f: r/ a- H8 P) X0 V
s4 = Mid$(s, 6, 8)9 V. }8 {! x+ i' N
If s4 = "0C30FF00" Then
1 l# M* @( n# Z9 C; X! X' C8 w% pplc = 1 'PLC为运行标志
6 ?( a- _' j( X5 ^$ S4 t2 fElse7 l& s9 F: }# V Q
plc = 0 'PLC为停止标志
& K& q# {1 H! Z6 p, R6 n: LEnd If/ x; Q7 _# _4 E3 b
End Sub* F% ^9 J* ^; N6 `/ h* S8 [
(3) 下面一段为用指示灯表示PLC的运行状态
9 m! i/ ^) F" p7 kPrivate Sub Timer5_Timer()
$ d) ]" J; ^2 D3 m# `* nDim s1 As String
3 S g9 S$ Z3 D) C) fDim s2 As String
# b* h' i3 Y+ iDim s220 N- m4 O7 P: S" J9 t5 r
Dim s3 As String1 f0 f- c( c. e7 T
Dim s4 As String
# @) |6 G1 p) ]4 ?8 Os2 = "01010C300001"
5 Y1 `$ x) N9 \( Ys22 = LRC(s2)
& j5 C8 h3 p1 T9 ws1 = ":" + s2 + s22 + Chr$(13) + Chr$(10)% R: `$ Q% G+ k* k" T
MSComm1.Output = s1' ?) o3 p( \; L: ^
s3 = MSComm1.Input
! O/ w$ y& b, q9 o5 q+ as4 = Mid$(s3, 8, 2)$ E; Q1 T% R! O9 S
If s4 = "31" Then( D3 |1 S. x' ], m& e0 P3 {2 b
plc = 1 'PLC为运行标志
+ x. }1 s5 K U8 pElse: If s4 = "30" Then plc = 0 'PLC为停止标志
+ d) J3 b, u, W& t2 a4 j( X) X8 _, yEnd If/ u8 w# g$ `. e; U- D
If plc = 1 Then& ^5 i8 J8 [0 l; p: Q5 d/ _' \) Z6 o
Label2.Caption = "PLC正在运行......"
7 c! s s; I! D2 r& E! |1 B4 tShape1.FillColor = RGB(0, 255, 0) 'green3 b6 n- B# Z1 R5 Y+ k
Else- W5 ?* {! x g7 ^. f
Label2.Caption = "PLC已经停止"% {% S" Q1 A) j+ b& ?! l
Shape1.FillColor = RGB(255, 0, 0) 'red1 {9 y9 d* G0 J* Y; B
End If2 m" O- U0 @( S
End Sub6 t6 B& n& M- N% B, M
(4) PLC的起动与停止
* m; }/ E1 f+ A( {9 ~! p'起动PLC+ u# l! k2 K3 [+ e2 {* U' d
Private Sub start_Click()) e6 E/ P& w9 j. I6 w! p8 T2 L
Dim strout As String
# U* w2 h/ O3 {, ]/ m5 uTimer5.Enabled = False1 k4 v, p* O" }& H' e: D. g9 _- E( H% t
str = "00050C30FF00" 'M1072 为PLC起动停止标志位。查地址表,M1072为OC30.FF00为置% }) A0 H, Y5 s1 O3 z# l3 Y
ON,0000为置OFF。
+ s2 ~ \% C, D0 Y'以上都是固定格式,要牢记。" b) I1 I* K0 F
LRCC = LRC(str) '计算 str的lrc校验码。& l! N4 p( K! ]
strout = ":" + str + LRCC + Chr$(13) + Chr$(10) '欲传送之数据。13为D,10为A
. v* i0 k) \6 ~" _# J. X3 eMSComm1.Output = strout8 n" R6 c( a7 f I: f7 Q0 {
Timer5.Enabled = True5 t- x9 ~% I |2 Y
End Sub; h2 B+ R8 b2 f$ ^9 Z/ V" i' B
'停止PLC
~+ A$ x+ P! G( I! }Private Sub stop_Click()
5 |, o! v- a( [ y6 V1 t: `8 O4 bDim strout As String
: i: P4 N2 M e" L% r! ]4 g- ]Timer5.Enabled = False1 M6 H) r6 P0 H' I: z: a0 f& n
str = "00050C300000"
R8 Z2 |- S3 L+ K$ }8 G3 U: c9 `LRCC = LRC(str)
6 o2 f b' M0 Zstrout = ":" + str + LRCC + Chr$(13) + Chr$(10)
7 y5 P; t k" Z# v9 l6 HMSComm1.Output = strout
/ M3 q+ N* C; p) e) \0 l# Q n+ eTimer5.Enabled = True4 d/ `7 V ^3 o5 g' f2 v
End Sub
; }# D6 K" y5 `3 yY0、Y1的ON/OFF与PLC起动/停止的控制方式相同,指示灯的表示方式也相同。D256,D512数据写入的操作类似,限于篇幅其它代码就不再列出了。5 结束语
# h6 x p$ T. c8 t, H7 U6 bVB语言易于编程使用,为与串行设备的通信提供了很大的方便。台达PLC采用标准的目前广为流行的MODBUS协议,为实现PLC与监控计算机的通信提供了简化的平台。由于台达所有的机电产品都支持MODBUS协议,所以,掌握了VB与PLC通信过程,也就等于掌握了PC与台达所有机电产品的通信。 |
|