本帖最后由 红日888 于 2021-6-20 21:34 编辑
'此列子为串口转can通信控制伺服电机上使能的列子
$regfile = "m128def.dat" '单片机型号头文件
$crystal = 8000000 '晶振频率
$baud = 19200
$hwstack = 256
$swstack = 256
$framesize = 256
Declare Sub Send(byval Str_code As String)
Declare Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _
Byval Func_code As String , Byval Node_id As String , _
Byval Str_data As String) As String
Declare Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)
Config Portd = Output
Const Std_frame = 1 '标准帧
Const Ext_frame = 0 '扩展帧
Const Position_mode = 0 '位置模式
Const Speed_mode = 1 '速度模式
Dim Can_code As String * 26
Portd = &HFF
Wait 1
'1号伺服位置模式上使能
Set_enable "01" , Position_mode , 1
Portd.3 = 0
Wait 1
'2号伺服速度模式上使能
Set_enable "02" , Speed_mode , 1
Portd.4 = 0
Wait 10
'1号伺服掉使能
Set_enable "01" , Position_mode , 0
Portd.3 = 1
Wait 1
'2号伺服掉使能
Set_enable "02" , Speed_mode , 0
Portd.4 = 1
Wait 1
End
'按值发送
Sub Send(byval Str_code As String )
Local I1 As Integer , Mystr As String * 4 , Mybyte As Byte , Length As Integer
Length = Len(str_code)
For I1 = 1 To Length Step 2
Mystr = Mid(str_code , I1 , 2)
Mybyte = Hexval(mystr)
Printbin Mybyte
Next
End Sub
'Can帧数据Std_mode=1为标准帧=0为扩展帧,
'Data_length为数据区长度最大8字节,
'Func_code为功能码一个字节,
'Node_id为节点ID一个字节,
'Str_data数据区十六进制字符串,最大8个字节
Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _
Byval Func_code As String , Byval Node_id As String , _
Byval Str_data As String) As String
Local Count As Integer , Str_code As String * 26 , I As Integer
Select Case Std_mode
Case Is = Std_frame
Str_code = "0" + Str(data_length) '标准帧的第一个字符为0第二个字符为数据区的字节数(最大8字节)
Case Is = Ext_frame
Str_code = "8" + Str(data_length) '扩展帧的第一个字符为8第二个字符为数据区的字节数(最大8字节)
End Select
Str_code = Str_code + "0000"
If Len(func_code) = 1 Then
Str_code = Str_code + "0"
End If
Str_code = Str_code + Func_code
If Len(node_id) = 1 Then Str_code = Str_code + "0"
Str_code = Str_code + Node_id
Count = Len(str_data)
Count = 16 - Count
Str_code = Str_code + Str_data
For I = 1 To Count
Str_code = Str_code + "0"
Next I
Can_data = Str_code
End Function
'伺服驱动器使能Node_id为驱动器节点ID,
'Ctrl_mode有两种模式,位置模式Position_mode = 0和速度模式 Speed_mode = 1
'Value值设置非0为上使能,设置为0为掉使能
Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)
If Value <> 0 Then
'上使能
'02 00 00 02 0A 00 00 00 00 00 00 00 00 '控制字清0
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0000" ) '获得完整can报文
Send Can_code '发数据
Waitms 5
Select Case Ctrl_mode
Case Is = Position_mode
'01 00 00 03 0A 01 00 00 00 00 00 00 00 '速度模式
Can_code = Can_data(std_frame , 1 , "03" , Node_id , "01" ) '获得完整can报文
Send Can_code '发数据
Waitms 5
Case Is = Speed_mode
'01 00 00 03 0A 03 00 00 00 00 00 00 00 '位置模式
Can_code = Can_data(std_frame , 1 , "03" , Node_id , "03" ) '获得完整can报文
Send Can_code '发数据
Waitms 5
End Select
'02 00 00 02 0A 06 00 00 00 00 00 00 00 '始能第一步
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0600" ) '获得完整can报文
Send Can_code '发数据
Waitms 5
'02 00 00 02 0A 07 00 00 00 00 00 00 00 '始能第二步
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0700" ) '获得完整can报文
Send Can_code '发数据
Waitms 5
'02 00 00 02 0A 0F 00 00 00 00 00 00 00 '始能第三步
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0F00" ) '获得完整can报文
Send Can_code '发数据
Waitms 5
Else
'掉使能02 00 00 02 0A 05 00 00 00 00 00 00 00
Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0500" ) '获得完整can报文
Send Can_code '发数据
End If
End Sub
|