平时文档中写的都是通过名字来发送PDO值,有的时候需要根据子索引来发送,但python-canopen库中没有专门的函数,于是我想了一种变通的方法。将字典中的对象根据index取出,然后取出名字,将名字拼接后通过名字来发送。
实例代码如下,其中用到了vcan,不知道怎么用的百度一下。数据字典随便找一个就好,不一定和我的一样。
#!/usr/bin/env python3
# coding: utf-8
import time
import canopen
network = canopen.Network()
network.connect(channel='vcan0', bustype='socketcan')
#remote
node = network.add_node(6, 'CANopenSocket.eds')
node.nmt.state = 'PRE-OPERATIONAL'
node.rpdo.read()
indexParaName = node.object_dictionary[0x2301]
SubindexParaName = node.object_dictionary[0x2301][1]
indexSubindexParaName = indexParaName.name+'.'+SubindexParaName.name
node.rpdo[1][indexSubindexParaName].phys = 0x5
node.rpdo[1].transmit()
# # Change state to operational (NMT start)
node.nmt.state = 'OPERATIONAL'
执行后结果如下,能看到,往RPDO1写了5发送出去。