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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
$regfile = "m128def.dat"                                    '单片机型号头文件" R3 |( [& y# B* F# b4 a& U
$crystal = 8000000                                          '晶振频率
# e! |( r/ X) s- q( h* I$baud = 19200
: t" s% U: ?! s) o3 \$hwstack = 2566 `- x& w0 X) m- K5 Q4 @% H7 _3 f6 ~+ F5 G
$swstack = 256
6 t, r7 D2 v9 w$ y$framesize = 256* h+ \1 k- j5 J$ A% z
. K( d( w& P) x
Declare Sub Send(byval Str_code As String)+ e& [; n# u- T$ y+ c
2 N0 N* ~; g. f9 w) j2 M& j! f
Declare Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _7 O* ^& T$ H) G. Q+ D
                           Byval Func_code As String , Byval Node_id As String , _& v" i8 `0 g" @; Y4 u  i9 m" m6 F5 y& }
                           Byval Str_data As String) As String7 b6 x4 H3 w  w% v: x' g0 U
Declare Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)
: y2 i% A9 R) ]' v4 l5 o( ]3 E  z6 ^
' C6 T" ^  Y' {2 BConfig Portd = Output
1 s+ @( Z4 E8 Y& [0 t$ e# `5 dConst Std_frame = 1                                         '标准帧
9 Z9 C3 R' l  H( O+ ?Const Ext_frame = 0                                         '扩展帧
. L9 T+ ^# }1 O- l5 WConst Position_mode = 0                                     '位置模式) [4 e' {5 @4 i0 ]7 [, @" ~8 g
Const Speed_mode = 1                                        '速度模式- d  g, h7 ]! ^* i0 N9 M" k
' @$ `5 J4 D3 c% b2 X& S( l
Dim Can_code As String * 26
0 O6 H; j+ o- L; X% C6 S3 n! n( {  I  c4 J
Portd = &HFF
) i! j9 @0 h# ~2 h: G7 M! f% b6 LWait 1
% f0 r7 h4 g! |9 n! T9 F( w9 O) a8 d'1号伺服位置模式上使能
' e; M; I  ], \' Y7 X( n4 }Set_enable "01" , Position_mode , 1( c& O3 G0 A2 t% W# P& V
Portd.3 = 05 H  J+ f8 Y" B+ O1 Q( R) V
Wait 1
: Y  y4 Y1 B% }. g: M5 U'2号伺服速度模式上使能" U! d  p, o  n# K
Set_enable "02" , Speed_mode , 1+ N( S$ |6 Q3 k  |3 ~+ B: }4 _
Portd.4 = 0
7 h: W/ U2 Q4 kWait 10
. o9 }) h" b# Z' @2 m) I6 z8 ?$ V( G6 c  [7 }  A$ C
'1号伺服掉使能
$ ^- A& Z- N- e/ \/ i) z% k. ySet_enable "01" , Position_mode , 0; s! P6 F/ Q9 I  E$ H9 r
Portd.3 = 1
& k- Z7 x5 ~: {! n+ F, U- e9 VWait 1
5 X" q; `( |+ D6 X'2号伺服掉使能) G  \7 M, W) O5 ?4 [
Set_enable "02" , Speed_mode , 0! e5 u) k  Q4 d4 t) v. ]2 d
Portd.4 = 1+ m2 p; @; ]# m& y2 V" U2 G
Wait 1
/ ?/ }% y" @5 ?; c6 P, m& `/ H
End# m, H4 L7 o- _$ q5 V) }" m6 K
. K3 f3 L/ i/ X
'按值发送  Z0 n6 D7 j) R& l6 P3 r
Sub Send(byval Str_code As String )
8 g% }; ?. q7 m$ X    Local I1 As Integer , Mystr As String * 4 , Mybyte As Byte , Length As Integer
& P  V# |8 {" _  i: v+ J9 d    Length = Len(str_code)
2 J' w4 f" x* A! d8 W    For I1 = 1 To Length Step 20 m; D( o1 W  g% d8 j
        Mystr = Mid(str_code , I1 , 2)9 h6 P$ l) ]7 |2 R
        Mybyte = Hexval(mystr)
" {, u- o/ ?8 Q4 G) F        Printbin Mybyte
# e* ^& t# k6 E# G% G    Next% ?( [0 f8 M8 _1 _6 g' g+ ~3 J8 Z6 m
End Sub
7 P" `$ i: G+ f* ?
& g, e3 y# h) @0 `'Can帧数据Std_mode=1为标准帧=0为扩展帧,
& C$ ]4 i( |4 c  J* A' g'Data_length为数据区长度最大8字节,
. D, [0 o" r$ D1 W'Func_code为功能码一个字节,
/ p* F2 {1 o$ I) Y# o4 M'Node_id为节点ID一个字节,
6 L2 K. ^" }6 X! K; A; N' |# y. x'Str_data数据区十六进制字符串,最大8个字节1 s3 k; {# r+ [/ r# a* f
Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _
7 n% K: S% X9 m( L% d2 U( m                  Byval Func_code As String , Byval Node_id As String , _
: k' q' I7 [3 d1 x5 k' B                  Byval Str_data As String) As String  H" Z- Z0 \8 p- Q( v
/ S8 k# [/ p3 G4 S! L
   Local Count As Integer , Str_code As String * 26 , I As Integer$ I4 E$ \' r- `9 u9 V, [0 G
+ V% C( l+ A9 ^% q( k7 `
   Select Case Std_mode3 e5 |/ C& b; U/ J8 o0 W- w
   Case Is = Std_frame7 v! A, Y: I. u3 \2 S$ {
      Str_code = "0" + Str(data_length)                     '标准帧的第一个字符为0第二个字符为数据区的字节数(最大8字节)( @1 w/ I; d6 N9 Z0 S. B  T0 C) C% B  ^
   Case Is = Ext_frame
3 |" \) X" P7 f) Y      Str_code = "8" + Str(data_length)                     '扩展帧的第一个字符为8第二个字符为数据区的字节数(最大8字节), d  ~+ ~7 Q: }; y. R: {
   End Select1 \, E' A' O/ T* ^

% n, T. o+ |6 v0 ?( ]+ N# }  F   Str_code = Str_code + "0000"
) E5 u0 x( P. o  D8 M+ }1 q9 k7 [0 C  s7 x* }6 v7 Y' W; [
   If Len(func_code) = 1 Then
+ S6 |9 d8 d6 T* C! K# k7 {. N$ V      Str_code = Str_code + "0"' f; L! N5 ~# p
   End If* O$ F* ~$ W. G7 x8 n  P* r5 C. l
   Str_code = Str_code + Func_code
6 ~$ q, w) R' j9 r6 e  V4 k
  {% h+ g. ?7 m: I" l6 _4 t$ n   If Len(node_id) = 1 Then Str_code = Str_code + "0"
$ R3 F) J5 y  n6 Y6 X4 @: E   Str_code = Str_code + Node_id
/ q& B* D4 s$ V' v/ Q
6 [: @9 E- A$ [   Count = Len(str_data)
2 o6 b% d" }6 Z' }   Count = 16 - Count
& f, B+ `/ F5 q0 b' C   Str_code = Str_code + Str_data$ s4 r5 J# x% g  y2 B
   For I = 1 To Count
7 }& M' V3 j4 V' e* M) E      Str_code = Str_code + "0"
$ a. Z& N# Y$ N, r6 ]/ ]( ]   Next I
; [- \' `$ S$ Z2 ]  l( u   Can_data = Str_code
5 L" @8 w( E, G& _1 VEnd Function
4 e+ ?* m2 z/ D; H- ?/ w- K8 U& u! b, [8 ~- V
'伺服驱动器使能Node_id为驱动器节点ID,+ ?, U  E) ~4 W/ w% V
'Ctrl_mode有两种模式,位置模式Position_mode = 0和速度模式 Speed_mode = 1
( u$ z/ w- S; Z: u6 W- y'Value值设置非0为上使能,设置为0为掉使能
8 H- t, Z- @, v6 W4 iSub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)
4 b' x+ r! W7 H   If Value <> 0 Then: p, R0 x8 A5 ]+ o1 ?
      '上使能7 Q4 j7 B5 K+ h4 X, n/ y; z
      '02 00 00 02 0A 00 00 00 00 00 00 00 00   '控制字清0
- ~3 h7 i/ j5 P! [. B* ]2 D- R- R      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0000" )       '获得完整can报文
- H2 G& c) W. f! e- z      Send Can_code                                         '发数据. n6 L( \( g( m2 ?5 [  Z2 B5 t) k- V
      Waitms 5
; m% Z! [' o3 @- K" l( F9 T      Select Case Ctrl_mode, L4 ?! h: s$ P$ C
      Case Is = Position_mode
& S% G- t1 Q. t7 j  M4 J: s- ]2 R2 a        '01 00 00 03 0A 01 00 00 00 00 00 00 00    '速度模式. a- E: _1 ]7 z/ d
        Can_code = Can_data(std_frame , 1 , "03" , Node_id , "01" )       '获得完整can报文
2 c$ X. p3 P( g        Send Can_code                                       '发数据
# \# c3 `. T2 p6 G0 Z; G5 L4 O9 d        Waitms 5$ u+ [6 i* k* \* ]) M3 Z
      Case Is = Speed_mode) S8 l+ q( t7 m
        '01 00 00 03 0A 03 00 00 00 00 00 00 00    '位置模式5 K& `' h7 i5 D
        Can_code = Can_data(std_frame , 1 , "03" , Node_id , "03" )       '获得完整can报文7 {  ^" P8 e! ?* I' b
        Send Can_code                                       '发数据
/ S) f( I* N' N: r* c  R- I- n        Waitms 5. a6 C9 l. F/ u
      End Select; y9 t! d4 v: J3 g  i7 k
      '02 00 00 02 0A 06 00 00 00 00 00 00 00   '始能第一步" v: y2 S* Y! A. [- l2 ^
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0600" )       '获得完整can报文+ p: v6 F- O  \) }  G/ O
      Send Can_code                                         '发数据
+ r5 v$ ]3 a6 D: i! M+ o      Waitms 5( ?/ ]( A9 W0 h
      '02 00 00 02 0A 07 00 00 00 00 00 00 00   '始能第二步
/ |1 ~9 D9 H9 ^+ C      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0700" )       '获得完整can报文0 c& {% B2 {$ }' j
      Send Can_code                                         '发数据, z; t2 F, Q6 y- W5 n( p5 y
      Waitms 5" i  g5 S3 R& S% A% v
      '02 00 00 02 0A 0F 00 00 00 00 00 00 00   '始能第三步
- X+ {/ ^' ]) Z) Z  N* _5 K      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0F00" )       '获得完整can报文
0 h) _2 H$ G% N* e2 f4 O      Send Can_code                                         '发数据
, _8 a0 G  u$ M      Waitms 5
' i3 D. p4 ]7 B, C7 I! n   Else7 k! s) ]3 |8 n  {; ^/ Y
      '掉使能02 00 00 02 0A 05 00 00 00 00 00 00 00
6 e# `9 r! e" J2 |) U      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0500" )       '获得完整can报文
) A. B/ ~% w( P% P      Send Can_code                                         '发数据
* P( J4 }3 M7 U5 b/ T% }   End If
6 H% I- w' r5 i+ IEnd Sub
, R5 [% Q0 n1 T6 x5 U& C6 \

该用户从未签到

2#
发表于 2021-9-26 14:55 | 只看该作者
感觉清零有些繁琐" U: I1 x6 U. j/ |# t

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-8-16 06:51 , Processed in 0.109375 second(s), 23 queries , Gzip On.

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

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

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