找回密码
 注册
关于网站域名变更的通知
查看: 379|回复: 2
打印 上一主题 下一主题

串口转can通信控制伺服电机

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2021-9-26 14:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
$regfile = "m128def.dat"                                    '单片机型号头文件
% g$ s! p0 ^! ^! A/ A* g$crystal = 8000000                                          '晶振频率' {6 d: f. g& P
$baud = 19200
  P" q6 f6 w: N6 Z5 [& E/ D$hwstack = 256
9 g: o/ t* S2 v- G' h8 R$swstack = 256
# R" [5 b$ K$ C' w) s3 G5 \$framesize = 256
7 n+ O, w. k* l1 {# M* U5 k
' O: [/ @6 l) v0 H$ Z: W/ c$ YDeclare Sub Send(byval Str_code As String)
- Y* M( Q+ ~: Y- z
% E: g. [/ a2 i  H- YDeclare Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _
, Q8 N! P0 b9 p- L: R6 y                           Byval Func_code As String , Byval Node_id As String , _
" l$ K& j# u! a# \0 [1 E                           Byval Str_data As String) As String- U/ o0 A( y- v' j8 }0 o. |
Declare Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)6 q6 n6 h3 l. P* V1 L2 g

/ D0 y- t; m$ Q  [% r: m% JConfig Portd = Output' a: |% z, Q3 f! H% g5 J
Const Std_frame = 1                                         '标准帧
) J8 Z. ^- ]4 V: R" zConst Ext_frame = 0                                         '扩展帧" U( J) U; O: `& J
Const Position_mode = 0                                     '位置模式& N& M7 ~" U/ A1 L3 \2 G
Const Speed_mode = 1                                        '速度模式4 n9 q4 Q- F' C+ R+ l7 I4 U  R

, W) a7 [! x* g+ Z$ g6 T* I; s2 l) gDim Can_code As String * 26
! p, T/ D3 p2 a3 y; p; b  I  n
; ?& `# H) i# l; [Portd = &HFF
: a0 s( Y( s0 u* HWait 10 S% {/ U+ o3 Z- V6 s9 C
'1号伺服位置模式上使能
2 d0 D$ o) ]; a1 W( M* g" @Set_enable "01" , Position_mode , 1
. R2 m3 c6 L8 h+ ]Portd.3 = 0
! n3 Y- f( ^4 [! t, ~2 P. uWait 19 L# u7 d. [. _, ~! O( F3 a
'2号伺服速度模式上使能9 R3 |+ s3 @4 N* D0 b0 L: C/ b
Set_enable "02" , Speed_mode , 1
4 @+ J) {& m8 f8 n" qPortd.4 = 0
; P4 h( |% b4 Y" V0 Q# ^Wait 10" d/ R2 h9 U& f5 Z0 x
) i4 E* h( w/ u
'1号伺服掉使能* V1 o7 n# n- x
Set_enable "01" , Position_mode , 0
0 q" e9 x* e- k0 l. MPortd.3 = 10 `- o( S) {1 T3 ^6 O
Wait 1
+ P6 b& \- q5 R6 r7 G'2号伺服掉使能
1 @! t2 M4 v9 s  O2 ~0 d; ESet_enable "02" , Speed_mode , 0
- n. b. h5 x# @# G1 t! rPortd.4 = 1* c/ t& d' e0 x
Wait 16 C8 ?: \6 `) Z) {; C

- e5 O/ l/ u1 _. q( EEnd
# V! F! T, K+ r+ V! p) Y
1 L# a2 S: A( Y'按值发送
5 U7 r) `# J3 ^; E5 sSub Send(byval Str_code As String )5 Y2 X3 s( ]3 F" H  y7 Z
    Local I1 As Integer , Mystr As String * 4 , Mybyte As Byte , Length As Integer
( [) C- L* Q* \' ?2 j* ?) Y    Length = Len(str_code)) J9 [6 Z; M7 ^* f' A" ?3 d( [
    For I1 = 1 To Length Step 20 M- \% x8 D4 q4 Z# s5 n* K9 ~
        Mystr = Mid(str_code , I1 , 2)
, y9 G- f9 j) x& V        Mybyte = Hexval(mystr)) R! j; N1 V8 `0 Q, x/ Q+ F
        Printbin Mybyte
. s3 ]2 Y# a: k; X# _    Next
3 K6 G5 X, M. o% P! z4 fEnd Sub
" r2 \* d, E) e# B% U: n" \' L& w8 c+ @3 g, z" D8 u7 h
'Can帧数据Std_mode=1为标准帧=0为扩展帧,8 i% f! f/ |! Q/ N# z" W+ N7 j' t
'Data_length为数据区长度最大8字节,& o4 n* S9 |" o
'Func_code为功能码一个字节,
# A: ^4 E7 v: ]6 Y2 x% M* v'Node_id为节点ID一个字节,
7 k/ p, f0 f! ?, ]4 U9 i$ h# z'Str_data数据区十六进制字符串,最大8个字节
$ T& ^9 S4 u& QFunction Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _1 E3 k! z& w- l) Q; j7 q
                  Byval Func_code As String , Byval Node_id As String , _: a$ y6 }3 j9 @
                  Byval Str_data As String) As String
  z. v" V- O6 `4 ~; q9 ?" P7 ?+ d" O" }3 c9 t* C% ]
   Local Count As Integer , Str_code As String * 26 , I As Integer
3 B, d1 A# a$ x0 f$ C* P7 l% g  G; V$ _" g. `6 Q. L
   Select Case Std_mode# i4 m; J7 O& m; n) N
   Case Is = Std_frame
+ r9 }, U- O1 o  b) Y/ j      Str_code = "0" + Str(data_length)                     '标准帧的第一个字符为0第二个字符为数据区的字节数(最大8字节)* o; g" K: J. d, l4 g
   Case Is = Ext_frame, d% h* K! T) S
      Str_code = "8" + Str(data_length)                     '扩展帧的第一个字符为8第二个字符为数据区的字节数(最大8字节)# d' l, D8 r% W$ l
   End Select# U  T6 M0 y* r! l4 \, l* O
5 y+ d4 u5 O' _& f% t
   Str_code = Str_code + "0000"
# N# S& J* Q5 ~/ w; V3 V" j9 T+ M5 J0 d9 ~4 `' s
   If Len(func_code) = 1 Then4 v  ~' O7 h1 T0 v
      Str_code = Str_code + "0", ?, |8 ?6 ]2 T& V$ o6 s7 V( T# V3 A
   End If9 P) h8 g3 }4 Y6 R/ k- L
   Str_code = Str_code + Func_code! n' P0 p7 [2 g$ p+ _$ d
. S  Q/ r  s& E& D2 G0 T" m; @
   If Len(node_id) = 1 Then Str_code = Str_code + "0". z2 A" P0 J% M
   Str_code = Str_code + Node_id
+ P' i- n4 R! K( k# y& X  G: S) A4 K5 [3 q0 z
   Count = Len(str_data)
- i; Y$ B& P+ q" L. d$ o4 B1 `   Count = 16 - Count
3 z, }. p2 F7 q" V9 G7 D   Str_code = Str_code + Str_data( l6 M- N/ J( s
   For I = 1 To Count: Y/ `# {0 X3 x8 \/ V
      Str_code = Str_code + "0"
+ T, D# C( x* R8 g# p   Next I
! }6 u. J, g2 V1 v9 [   Can_data = Str_code
4 R- V. m- [5 e% b1 s7 ~End Function- E; t5 ~2 j$ A+ d5 v" `

4 B6 e: J; {% S'伺服驱动器使能Node_id为驱动器节点ID,
6 \5 X$ B: c4 j! C'Ctrl_mode有两种模式,位置模式Position_mode = 0和速度模式 Speed_mode = 1/ J) [+ Z9 A" V7 \$ ]4 I
'Value值设置非0为上使能,设置为0为掉使能8 o2 Z6 F# L3 ]! p$ N; \3 }5 Y
Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)
8 Z3 L& s6 i# `6 |# C, Y" S   If Value <> 0 Then0 }# U5 C! \. f# V5 }% l
      '上使能. O9 }8 [! K$ m
      '02 00 00 02 0A 00 00 00 00 00 00 00 00   '控制字清0
! X( P8 K( y7 z3 S" I      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0000" )       '获得完整can报文
8 ]8 d- ?% Z2 g* v/ U$ u      Send Can_code                                         '发数据! _( d4 _$ X& @6 l  h3 ^; X
      Waitms 5* ~7 w/ P' r5 D/ t, ?) {) B
      Select Case Ctrl_mode: t% Z0 l7 u5 e4 g- u/ |9 V5 K
      Case Is = Position_mode  Y: }  J7 i1 M! N* A
        '01 00 00 03 0A 01 00 00 00 00 00 00 00    '速度模式
; o. \9 |; `0 u( C' b        Can_code = Can_data(std_frame , 1 , "03" , Node_id , "01" )       '获得完整can报文, p8 A) f  P1 ]
        Send Can_code                                       '发数据: N# [: N' N; a
        Waitms 5
1 w+ N: l) i4 p$ X' K4 d9 t& \      Case Is = Speed_mode1 G0 G, e3 }; ^: q) J
        '01 00 00 03 0A 03 00 00 00 00 00 00 00    '位置模式
% Y3 B  s# q3 N        Can_code = Can_data(std_frame , 1 , "03" , Node_id , "03" )       '获得完整can报文
$ u8 J* ]0 `5 w/ F) ^6 w        Send Can_code                                       '发数据
, B  L% O; G+ N6 M" Z$ d        Waitms 55 J7 a( [+ \3 ?( I$ R. ?
      End Select
, R; ^' x8 `/ E# G      '02 00 00 02 0A 06 00 00 00 00 00 00 00   '始能第一步2 @6 ~' v( G$ Y4 {: p- _
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0600" )       '获得完整can报文
) Z& u2 _! S( a9 q$ T5 p$ R      Send Can_code                                         '发数据" R' P/ R; m) H9 o+ X* N4 M
      Waitms 5
: G5 `9 u8 k& R0 Z$ z2 P      '02 00 00 02 0A 07 00 00 00 00 00 00 00   '始能第二步4 ]% n* H5 m9 u7 R( n
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0700" )       '获得完整can报文
, \* \$ |5 I6 q      Send Can_code                                         '发数据
2 B9 D# K* U1 B! C, D! P. v1 d      Waitms 5! S1 d/ X. h/ O8 J; {- d9 H) @' o. F
      '02 00 00 02 0A 0F 00 00 00 00 00 00 00   '始能第三步
8 q$ O# V# R6 E/ L6 r      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0F00" )       '获得完整can报文  n" k* y; ^4 b5 X6 K1 r1 E8 O
      Send Can_code                                         '发数据. C/ p9 J4 K2 V, m4 _& i
      Waitms 5
% `, Y$ |* u1 f( Q) d- P% S, a   Else) b, k, _5 Y, y% t/ ~) P
      '掉使能02 00 00 02 0A 05 00 00 00 00 00 00 00
5 C8 V1 Z4 T- A8 B( c$ e2 i' J3 p+ j      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0500" )       '获得完整can报文
4 q' K2 S+ i7 `      Send Can_code                                         '发数据
; h8 W4 k, O1 B   End If5 W# E1 n3 P; T  @* E8 P6 X+ g
End Sub
$ U: X' f0 c: U8 v

该用户从未签到

2#
发表于 2021-9-26 14:55 | 只看该作者
感觉清零有些繁琐
. G7 A; V1 F4 P4 z2 d

该用户从未签到

3#
发表于 2021-9-26 17:31 | 只看该作者
学习了   感谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-6-18 12:44 , Processed in 0.078125 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表