发表于:2023/11/3 9:27:20
#0楼
本文将使用运动控制器实现与松下A编码器进行数据读取操作。
'************************绝对值编码器部分******************************
SETCOM(38400,8,1,0,1,0) '设置 485 口 PORT1 为自定义协议
GLOBAL DIM tempchar '接收的一个字节
GLOBAL DIM neqbuff(2) '发送识别码 485 为 81H,05H
neqbuff(0) = $81
neqbuff(1) = $05
GLOBAL DIM eotbuff(2) '接收识别码 485 为 80H,04H
eotbuff(0) = $80
eotbuff(1) = $04
GLOBAL DIM ackbuff '接收应答 06H
ackbuff = $06
GLOBAL DIM cmdbuff(20) '发送命令数组
GLOBAL DIM getbuff(20) '接收的字符串
GLOBAL DIM getnum '接收的字节数
getnum = 0
GLOBAL DIM highdata '编码器多圈数据
GLOBAL DIM lowdata '单圈数据
runtask 4,get_char '启动接收字符串任务
MODBUS_REG(0)=0
WHILE 1
IF MODBUS_REG(0)=1 THEN '判断是否接收到了数据
MODBUS_REG(0)=0
getmpos(1,45) '读取站号 1 的多圈与单圈值。
ENDIF
WEND
END
'读坐标
GLOBAL SUB getmpos(sifunum,rcr) '读伺服编号为 1 的电机的绝对值位置
cmdbuff(0) = $00
cmdbuff(1) = sifunum
cmdbuff(2) = $d2
cmdbuff(3) = rcr
neqbuff(0) = $80 + sifunum
neqbuff(1) = $05
eotbuff(0) = $80
eotbuff(1) = $04
getnum = 0
putchar #1,neqbuff
TICKS = 2000 '延时
WAIT UNTIL (getnum = 2) OR TICKS < 0
IF getnum = 2 THEN '如果接到了 2 个字符
IF(getbuff(0)=$80+sifunum) AND (getbuff(1)=$04) THEN'收到应答发送命令
getnum = 0
PUTCHAR #1,cmdbuff '发送读取编码器命令
TICKS = 2000
WAIT UNTIL (getnum = 3) OR TICKS < 0
IF(getbuff(0) = $06) AND (getbuff(1) = $80) AND (getbuff(2) = $05) THEN'收到发送请求,给应答
getnum = 0
PUTCHAR #1,eotbuff '发送应答,等待接收数据
TICKS = 2000
WAIT UNTIL (getnum = 15) OR TICKS < 0
IF getnum = 15 THEN '读到数据 11-10 为多圈数据 9-7 为单圈数据
PUTCHAR #1,ackbuff
highdata = getbuff(11) * $100 + getbuff(10)
lowdata = getbuff(9) * $10000 + getbuff(8) * $100 +getbuff(7)
PRINT getbuff(11),getbuff(10),getbuff(9),getbuff(8),getbuff(7),getnum
ELSE
PRINT getnum,getbuff(0) ,getbuff(1),"超时重新读取 1"
ENDIF
ELSE
PRINT getbuff(0) ,getbuff(1),"超时重新读取 2"
ENDIF
ELSE
PRINT getbuff(0) ,getbuff(1),"驱动器无应答"
ENDIF
ELSE
PRINT "驱动器无应答"
ENDIF
END SUB
'串口接收
GLOBAL SUB get_char()
WHILE 1
GET #1,tempchar
getbuff(getnum) = tempchar
getnum = getnum + 1
WEND
END SUB
'************************绝对值编码器部分******************************
SETCOM(38400,8,1,0,1,0) '设置 485 口 PORT1 为自定义协议
GLOBAL DIM tempchar '接收的一个字节
GLOBAL DIM neqbuff(2) '发送识别码 485 为 81H,05H
neqbuff(0) = $81
neqbuff(1) = $05
GLOBAL DIM eotbuff(2) '接收识别码 485 为 80H,04H
eotbuff(0) = $80
eotbuff(1) = $04
GLOBAL DIM ackbuff '接收应答 06H
ackbuff = $06
GLOBAL DIM cmdbuff(20) '发送命令数组
GLOBAL DIM getbuff(20) '接收的字符串
GLOBAL DIM getnum '接收的字节数
getnum = 0
GLOBAL DIM highdata '编码器多圈数据
GLOBAL DIM lowdata '单圈数据
runtask 4,get_char '启动接收字符串任务
MODBUS_REG(0)=0
WHILE 1
IF MODBUS_REG(0)=1 THEN '判断是否接收到了数据
MODBUS_REG(0)=0
getmpos(1,45) '读取站号 1 的多圈与单圈值。
ENDIF
WEND
END
'读坐标
GLOBAL SUB getmpos(sifunum,rcr) '读伺服编号为 1 的电机的绝对值位置
cmdbuff(0) = $00
cmdbuff(1) = sifunum
cmdbuff(2) = $d2
cmdbuff(3) = rcr
neqbuff(0) = $80 + sifunum
neqbuff(1) = $05
eotbuff(0) = $80
eotbuff(1) = $04
getnum = 0
putchar #1,neqbuff
TICKS = 2000 '延时
WAIT UNTIL (getnum = 2) OR TICKS < 0
IF getnum = 2 THEN '如果接到了 2 个字符
IF(getbuff(0)=$80+sifunum) AND (getbuff(1)=$04) THEN'收到应答发送命令
getnum = 0
PUTCHAR #1,cmdbuff '发送读取编码器命令
TICKS = 2000
WAIT UNTIL (getnum = 3) OR TICKS < 0
IF(getbuff(0) = $06) AND (getbuff(1) = $80) AND (getbuff(2) = $05) THEN'收到发送请求,给应答
getnum = 0
PUTCHAR #1,eotbuff '发送应答,等待接收数据
TICKS = 2000
WAIT UNTIL (getnum = 15) OR TICKS < 0
IF getnum = 15 THEN '读到数据 11-10 为多圈数据 9-7 为单圈数据
PUTCHAR #1,ackbuff
highdata = getbuff(11) * $100 + getbuff(10)
lowdata = getbuff(9) * $10000 + getbuff(8) * $100 +getbuff(7)
PRINT getbuff(11),getbuff(10),getbuff(9),getbuff(8),getbuff(7),getnum
ELSE
PRINT getnum,getbuff(0) ,getbuff(1),"超时重新读取 1"
ENDIF
ELSE
PRINT getbuff(0) ,getbuff(1),"超时重新读取 2"
ENDIF
ELSE
PRINT getbuff(0) ,getbuff(1),"驱动器无应答"
ENDIF
ELSE
PRINT "驱动器无应答"
ENDIF
END SUB
'串口接收
GLOBAL SUB get_char()
WHILE 1
GET #1,tempchar
getbuff(getnum) = tempchar
getnum = getnum + 1
WEND
END SUB
[此贴子已经被jint于2023/11/3 10:03:23编辑过]