|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
$regfile = "m128def.dat" '单片机型号头文件
8 H( k+ ~( ]) n/ z" S0 Y5 k2 Q$crystal = 8000000 '晶振频率
& H( _" i. T. j2 S5 K+ V p$baud = 192003 E, c- O) w' Q" E8 e( E( N `
$hwstack = 256
/ q% [$ @- p6 y! C$swstack = 256, Q0 @9 b4 z. _
$framesize = 256" H+ |; }; u$ k& h! ]: r* ]
. ]4 m) D$ [* A8 SDeclare Sub Send(byval Str_code As String)
( H7 A9 U' W) C/ b6 G
2 o' G, A# ~1 ?5 @3 ?Declare Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _# B! |7 d9 P9 x; b3 t# U: E( @
Byval Func_code As String , Byval Node_id As String , _
) v/ x2 [8 M; B1 d Byval Str_data As String) As String
3 Z8 m) E" D+ K) Q- xDeclare Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)/ E: f$ p6 V; ]0 @$ C
: N+ l5 c" L: Y& L& z
Config Portd = Output
( K! X( C+ }$ r4 e2 BConst Std_frame = 1 '标准帧; S0 `7 Y9 @/ w0 i& d. k
Const Ext_frame = 0 '扩展帧
! Z% G8 H y! P. k, KConst Position_mode = 0 '位置模式
' V( K' k7 K) v& }6 X) ]2 hConst Speed_mode = 1 '速度模式' d( ?/ B+ W* v0 z4 x2 X( `
9 h0 O( S7 p4 G5 ^9 }3 A$ S: E$ JDim Can_code As String * 26
6 G, N4 Y! G3 o9 s
' X/ Y! z9 ~8 M- b5 L, {' ~Portd = &HFF
0 a$ P1 c) {6 n0 J: XWait 1
$ M y) B* r6 `'1号伺服位置模式上使能
& c3 t* t$ F$ fSet_enable "01" , Position_mode , 1+ c3 n6 o. R7 n& w
Portd.3 = 0
! {; r1 G" h a* D$ o2 r/ S" eWait 11 M; A5 l/ K: L7 D6 [
'2号伺服速度模式上使能/ _' ]# E$ d$ N# `# \2 v( Y5 t/ x7 J4 r
Set_enable "02" , Speed_mode , 1
5 `. A& z" A, t oPortd.4 = 02 }& r, g2 B0 j4 ]. c) d! S
Wait 10) g& A; [4 M: O3 A+ o; h
6 @! o" N# m# ^5 [. j4 ^
'1号伺服掉使能
5 M2 E- ?3 Z1 z g2 mSet_enable "01" , Position_mode , 0
1 z9 ^& `9 w% MPortd.3 = 1% K/ t. D# R% @! G
Wait 1
1 o# e+ H& F% o! R'2号伺服掉使能
2 A `) Z4 s$ {Set_enable "02" , Speed_mode , 0: S4 g) h8 f7 ~! ?# U) h3 ~
Portd.4 = 11 G* m: ^' v# {: G
Wait 1
. D7 {7 A. |7 d" }7 F( _( D E' D+ |
End- w% z; u3 f) H' p, Z* T. ~! H
8 C/ Y3 Q1 J/ u; L: m'按值发送* n8 i- Q( x" s! c! H. G
Sub Send(byval Str_code As String )
$ n$ g( m, Z7 Q6 ~ Local I1 As Integer , Mystr As String * 4 , Mybyte As Byte , Length As Integer
$ o( D, X* s' m! K4 ~& _ Length = Len(str_code)
1 u$ ?8 D( u! d' P, } For I1 = 1 To Length Step 2
& @, d, d" U! F' D Mystr = Mid(str_code , I1 , 2)- ~4 A, O$ B6 V2 p( f6 e
Mybyte = Hexval(mystr)
3 |1 |" d$ Z3 G4 t3 [' Z1 I. N$ d( P Printbin Mybyte# T0 `; r1 a4 q7 }7 e
Next& v1 x Z) k8 q3 P
End Sub
2 S0 |$ k5 s5 r5 K6 H) q
/ P( \: D* T J P1 c- _. l1 C'Can帧数据Std_mode=1为标准帧=0为扩展帧,
" k. N2 y& Y2 Y'Data_length为数据区长度最大8字节,
8 k+ V( G9 U) c _'Func_code为功能码一个字节,
6 L0 T. Q. _8 h0 @'Node_id为节点ID一个字节,
" ~$ O/ u6 g t2 Q M2 l b( e'Str_data数据区十六进制字符串,最大8个字节
" H+ ?/ X1 h" e+ }: V0 m3 K3 dFunction Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _
) _8 z4 j: x5 d" o Byval Func_code As String , Byval Node_id As String , _& q7 {6 P) f! T. K) r
Byval Str_data As String) As String
( C, _1 |# c3 V% r) Q; j; i4 @2 ^9 s3 z% ?) g+ A3 J
Local Count As Integer , Str_code As String * 26 , I As Integer
+ A ]4 K3 J e$ V( b! ?
) q& h% e( c: z' m Select Case Std_mode5 ^8 Y" \7 d, h6 |4 S- x
Case Is = Std_frame
2 C: @7 `% P% F* ~( d$ }( e0 h Str_code = "0" + Str(data_length) '标准帧的第一个字符为0第二个字符为数据区的字节数(最大8字节)' w2 I# h+ p2 l' H9 V
Case Is = Ext_frame
# e& {# Y/ J: g3 F, O9 ~ Str_code = "8" + Str(data_length) '扩展帧的第一个字符为8第二个字符为数据区的字节数(最大8字节)
0 e3 S2 |/ z$ l. G5 ^; Z8 R# m End Select
* s2 ?5 h; U) P1 r( A- }* c, o a B8 K) F7 g7 s% g+ ?
Str_code = Str_code + "0000"
9 u' j7 o# O0 q
: V/ z* A6 Y4 r If Len(func_code) = 1 Then3 [: y0 N( _' ?
Str_code = Str_code + "0"! Q. b- P5 j- ?
End If. d$ N' K2 T$ a8 x* E
Str_code = Str_code + Func_code1 E0 d5 Q6 d# A* D) ~, T! v
% O% e, H- F$ e" j' } If Len(node_id) = 1 Then Str_code = Str_code + "0"
8 h2 e7 C! u. I+ r Str_code = Str_code + Node_id! A2 @7 p, e* Y J8 f7 x& o
4 ~' g) A% {% z `3 j# R
Count = Len(str_data)5 \3 k1 M2 ?2 v
Count = 16 - Count
3 b% t. I$ Y) o+ a9 o Str_code = Str_code + Str_data
l" k9 z$ s6 P( r, T7 h+ T2 M For I = 1 To Count
0 h0 |9 P; P; q Str_code = Str_code + "0"
! I. r0 h8 q: x1 F$ ^ Next I1 C) M* @3 a9 C e
Can_data = Str_code4 v2 _4 z; ]9 B- l) I7 r$ g
End Function: @/ B, m0 @# O. L
1 S, j' e' }$ ]8 j1 Y: o1 g
'伺服驱动器使能Node_id为驱动器节点ID,
. w' P0 J( n1 l; P'Ctrl_mode有两种模式,位置模式Position_mode = 0和速度模式 Speed_mode = 1
5 z5 x5 t/ O4 Y0 P# _'Value值设置非0为上使能,设置为0为掉使能! p a* ~' Q/ T. c% A$ F8 f+ r" i
Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)
; D* e" H |6 A- _0 G F If Value <> 0 Then% f% E! X, Q4 C2 }/ z! k
'上使能
|( X8 ~/ O( Y2 U7 W3 i: B% ]0 u5 Z '02 00 00 02 0A 00 00 00 00 00 00 00 00 '控制字清0% q5 F9 h6 B9 n+ z' L
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0000" ) '获得完整can报文
4 a$ x, T8 G4 n3 `/ l8 k3 p: G Send Can_code '发数据: a- C( T2 j2 P: _4 p% A' l, F' i6 F
Waitms 5
' J) B8 z, b2 @9 M4 m Select Case Ctrl_mode: Z. T) O4 y# c) _
Case Is = Position_mode- ]% W" l; w* b' Q, d
'01 00 00 03 0A 01 00 00 00 00 00 00 00 '速度模式
, Q3 X5 s: l$ o9 {( G) ]+ b( w! L' | Can_code = Can_data(std_frame , 1 , "03" , Node_id , "01" ) '获得完整can报文
! s2 s$ f9 |$ {1 { Send Can_code '发数据
7 O2 a- J U/ s0 k Waitms 55 m5 ^, `( Z5 ]1 w. d
Case Is = Speed_mode! d# k3 A! ?4 R3 O* J7 r G+ s1 M. ?
'01 00 00 03 0A 03 00 00 00 00 00 00 00 '位置模式
) J! ?4 j) p: a' v; } Can_code = Can_data(std_frame , 1 , "03" , Node_id , "03" ) '获得完整can报文
+ h: k# u3 k: T Send Can_code '发数据0 i/ p. K I1 {* ~0 ]+ v$ O" I) x
Waitms 5
$ y9 e3 {! Y1 G: w) I End Select
6 \- j2 G( H9 x3 Y6 S. W '02 00 00 02 0A 06 00 00 00 00 00 00 00 '始能第一步0 T! [ j) C6 D1 ?' e5 q
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0600" ) '获得完整can报文 E0 P$ _4 Q6 {. p
Send Can_code '发数据! N3 P) @& T/ i1 _* ]3 O
Waitms 5
5 \; A) r8 R: y3 x9 k9 Z+ G6 n '02 00 00 02 0A 07 00 00 00 00 00 00 00 '始能第二步- j+ i3 C, i# b v+ Z" l
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0700" ) '获得完整can报文; Y! b' f9 J& o/ y: r' [
Send Can_code '发数据
J+ y" J" u+ L5 m$ Z Waitms 5$ u9 a4 s: @1 z( d
'02 00 00 02 0A 0F 00 00 00 00 00 00 00 '始能第三步
) s7 P8 V9 g! ^ Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0F00" ) '获得完整can报文
$ I) d, V( f7 b. J- O( @ Send Can_code '发数据: b& o9 ]1 l, ?$ M/ |
Waitms 5
, E0 `" w1 k9 z6 O1 C' D3 ~ Else7 S1 C8 U% F e$ Z% H
'掉使能02 00 00 02 0A 05 00 00 00 00 00 00 00
4 Y) K- E8 T2 O# N9 O Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0500" ) '获得完整can报文8 l) u4 d8 h. ^9 J& J/ N
Send Can_code '发数据3 Z% I9 ?8 z. X7 a* s
End If! z- o5 b/ @$ F+ Z y8 o! _; I# @% z
End Sub
6 S% S! ]) F' Q" U |
|