|
发表于 2007-2-6 18:30:40
|
显示全部楼层
4)编程实现的代码构成+ f/ R% C% S- _+ a9 [. {
(1) LRC算法校验的实现
[, r: t9 F* l4 ^' N9 X9 c& K3 f s5 iPublic Function LRC(str As String) As String
! f/ O. ]' c' W' I/ y+ Pc = 0. C. y$ [, ]8 \; z; g# @
l = Len(str) 9 y% I& i& U, a$ [3 B8 ~, V
For c = c + 1 To l* z9 q! p4 N/ _, @4 S0 u1 x+ K
c_data = Mid$(str, c, 2)
4 J& L0 u+ Y# M# P& M/ Ud_lrc = d_lrc + Val("&H" + c_data)
9 i ^1 {0 ^6 Vc = c + 1
% S! C" p: z* \. r; lNext c! q0 _' \3 D% j6 M# P, z# l1 b
If d_lrc > &HFF Then$ q' x) |9 q/ L0 R! v
d_lrc = d_lrc Mod &H1004 [: q! Z+ d) W6 P! b. V6 A
End If
d8 m4 X! u' d% C- H1 @h_lrc = Hex(&HFF - d_lrc + 1)/ C- S) T$ U( A9 @" {0 ]! O2 O7 G
If Len(h_lrc) > 2 Then+ x8 Z1 g- K2 w% N6 f
h_lrc = Mid(h_lrc, Len(h_lrc) - 1, 2)
, v k5 s+ j) X! m% REnd If! ? i- M% R1 E& A
LRC = h_lrc
+ [* H% a8 ^2 H3 ?" jEnd Function
8 u% u8 ^, I: m9 Z7 U& t- {(2) 运行的开始就判断PLC的状态并设置标志位
% ~0 I/ A' }0 S. p/ F'初次运行打开串口,并显示PLC运行状态
' F! B- [ m$ x, sPrivate Sub Form_Load() s o# i# b" Q8 m }( s
Dim s1 As String
+ Q' {7 `) Z* P% G, o8 D. U" cDim s2 As String$ C7 N6 E# L% \) F: \" N, o7 K- J
Dim s22 As String
4 E: B2 i! w+ ~- gDim s3 As String
Q* s6 q# s( A8 n6 W' MDim s4 As String- Y' |: m" T( {
MSComm1.PortOpen = True) ?6 T# t: ?0 v n% Q5 b4 ?
s2 = "01010C300001"
1 c8 X; g5 ?0 S9 m1 a/ qs22 = LRC(s2)
: } c4 k9 b$ R0 v3 [) gs1 = ":" + s2 + s22 + Chr$(13) + Chr$(10)
. r, o$ l) C6 O6 |MSComm1.Output = s1
* [3 x t6 D7 {) [s3 = MSComm1.Input
9 S8 v( `: H% e) A. G2 l9 {" Es4 = Mid$(s, 6, 8)! z! B" e" r- ^2 f _" C3 G i6 @
If s4 = "0C30FF00" Then0 T$ Q/ V% G' M1 {
plc = 1 'PLC为运行标志
& g6 i/ Y$ r: C3 Z7 ?Else7 e* k4 ]9 J6 z. l- ^' t
plc = 0 'PLC为停止标志3 o& U' l9 U3 N, M
End If7 M& T: E% N$ @' }4 Q, t* f
End Sub' d/ P( H% J8 t" w7 y" U# o& W
(3) 下面一段为用指示灯表示PLC的运行状态5 g0 v) b3 Z+ F
Private Sub Timer5_Timer()
+ {) k6 p# @# z1 iDim s1 As String8 I! j* P/ g5 b
Dim s2 As String
% j1 y P1 z( ^. _Dim s22
3 \- x% W; @' W' {0 SDim s3 As String4 `; i2 L0 o6 j4 P
Dim s4 As String2 b5 Z/ w1 M' _6 H" p6 a% h
s2 = "01010C300001"! ?8 L5 z+ N6 w% m
s22 = LRC(s2)
6 [1 e$ M. Y2 Z! T) H: t8 A1 hs1 = ":" + s2 + s22 + Chr$(13) + Chr$(10)* V/ q3 L: Z6 E/ [
MSComm1.Output = s17 \0 D& K# e! c' ]$ C
s3 = MSComm1.Input
$ ]% o( {& c; }2 as4 = Mid$(s3, 8, 2)+ C9 y8 D9 u8 o1 X% b
If s4 = "31" Then
* w" B2 D1 V& @# `$ g$ N. D: c" oplc = 1 'PLC为运行标志. d0 [, S1 O2 S4 z( \* I
Else: If s4 = "30" Then plc = 0 'PLC为停止标志
- R. N2 F0 r, z5 rEnd If) r6 [6 ^4 _7 l
If plc = 1 Then% e$ i$ x" }2 }: h
Label2.Caption = "PLC正在运行......"& U7 ]/ |" T. \
Shape1.FillColor = RGB(0, 255, 0) 'green
: `* T0 E$ D# v H( M% w2 d* @$ VElse
$ R; z, N- l G; k2 sLabel2.Caption = "PLC已经停止"( U; J# M5 k: c. S# A' A
Shape1.FillColor = RGB(255, 0, 0) 'red% z# n4 t! @6 }
End If
4 S: Q, o% j9 O9 m) GEnd Sub
5 c7 s2 `) R% t; J6 |% M(4) PLC的起动与停止$ ]- [* U2 r9 R1 U2 R8 ?! T
'起动PLC0 a5 Y: r. w) F0 k1 G, S- Z
Private Sub start_Click()7 |/ ^# S1 T9 k5 ~
Dim strout As String1 L- I# L, H4 H( e% m
Timer5.Enabled = False
' Z6 s3 b& ^( R- y* ?: ostr = "00050C30FF00" 'M1072 为PLC起动停止标志位。查地址表,M1072为OC30.FF00为置: ?; K* l' L- y! m! H7 J: B0 J
ON,0000为置OFF。
1 i$ f0 I7 n: J5 t& L- k/ V'以上都是固定格式,要牢记。- l4 b6 x- ]; ]* |( [' R i4 I
LRCC = LRC(str) '计算 str的lrc校验码。
' ]* ^4 E( k/ `* M; G5 u. Vstrout = ":" + str + LRCC + Chr$(13) + Chr$(10) '欲传送之数据。13为D,10为A
- c) J- x8 y. h/ c. P0 P2 ]MSComm1.Output = strout
* F4 p9 R$ z- P3 y% _8 ATimer5.Enabled = True8 @. ?% D- @# K5 l) G" ` m8 e
End Sub" N9 r, X+ ?+ n3 H: e: H; n3 A
'停止PLC
4 [6 e. t$ j2 y/ b3 t. T/ \Private Sub stop_Click()
6 |+ F; e+ |6 V0 ]1 A n& l2 `Dim strout As String% ?5 [+ X6 T) {5 L- z+ {
Timer5.Enabled = False" n) u7 w# H: Y4 J
str = "00050C300000"! N! f6 |- W: M( m9 {! G7 M
LRCC = LRC(str) t0 I3 k( Z# d2 d* j
strout = ":" + str + LRCC + Chr$(13) + Chr$(10)7 ~7 k0 L( ?: [ C2 N, S
MSComm1.Output = strout. X3 `5 j n0 }
Timer5.Enabled = True
6 h+ V5 P, r* s3 j( r: o* ?+ {End Sub
1 @9 ~ y( x0 i% T) I8 \" qY0、Y1的ON/OFF与PLC起动/停止的控制方式相同,指示灯的表示方式也相同。D256,D512数据写入的操作类似,限于篇幅其它代码就不再列出了。5 结束语
# H' v# q6 B: U [( l( p0 ]VB语言易于编程使用,为与串行设备的通信提供了很大的方便。台达PLC采用标准的目前广为流行的MODBUS协议,为实现PLC与监控计算机的通信提供了简化的平台。由于台达所有的机电产品都支持MODBUS协议,所以,掌握了VB与PLC通信过程,也就等于掌握了PC与台达所有机电产品的通信。 |
|