CANopen堆栈实现
项目描述
Python对CANopen标准的实现。项目的目标是支持CiA 301标准中最常见的部分,并以简单的Pythonic接口。它主要针对测试和自动化任务,而不是符合标准的主机实现。
该库支持Python 3.6+。
特性
该库主要用于作为主机使用。
NMT主机
SDO客户端
PDO生产者/消费者
SYNC生产者
EMCY消费者
TIME生产者
LSS主机
从EDS中获取对象字典
支持402配置文件
还存在着创建从节点的不完整支持。
SDO服务器
PDO生产者/消费者
NMT从机
EMCY生产者
从EDS中获取对象字典
安装
使用pip从PyPI安装
$ pip install canopen
从GitHub的最新主分支安装
$ pip install https://github.com/christiansandberg/canopen/archive/master.zip
如果您希望在运行时更改代码,请克隆它,然后以开发模式安装它。
$ git clone https://github.com/christiansandberg/canopen.git $ cd canopen $ pip install -e .
单元测试可以使用pytest框架运行
$ pip install pytest $ pytest -v
文档
文档可在Read the Docs上找到
http://canopen.readthedocs.io/en/latest/
也可以通过使用Sphinx从本地克隆生成
$ python setup.py build_sphinx
硬件支持
该库通过python-can包支持多种硬件和驱动程序。请参阅支持设备列表。
还可以将此库与自定义后端集成。
快速入门
以下是一些快速示例,说明您可以做什么
PDO可以通过三种形式访问
1: node.tpdo[n]
或 node.rpdo[n]
2: node.pdo.tx[n]
或 node.pdo.rx[n]
3: node.pdo[0x1A00]
或 node.pdo[0x1600]
n
是PDO索引(通常为1到4)。第二种访问形式是为了向后兼容。
import canopen
# Start with creating a network representing one CAN bus
network = canopen.Network()
# Add some nodes with corresponding Object Dictionaries
node = canopen.RemoteNode(6, '/path/to/object_dictionary.eds')
network.add_node(node)
# Connect to the CAN bus
# Arguments are passed to python-can's can.Bus() constructor
# (see https://python-can.readthedocs.io/en/latest/bus.html).
network.connect()
# network.connect(bustype='socketcan', channel='can0')
# network.connect(bustype='kvaser', channel=0, bitrate=250000)
# network.connect(bustype='pcan', channel='PCAN_USBBUS1', bitrate=250000)
# network.connect(bustype='ixxat', channel=0, bitrate=250000)
# network.connect(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000)
# network.connect(bustype='nican', channel='CAN0', bitrate=250000)
# Read a variable using SDO
device_name = node.sdo['Manufacturer device name'].raw
vendor_id = node.sdo[0x1018][1].raw
# Write a variable using SDO
node.sdo['Producer heartbeat time'].raw = 1000
# Read PDO configuration from node
node.tpdo.read()
node.rpdo.read()
# Re-map TPDO[1]
node.tpdo[1].clear()
node.tpdo[1].add_variable('Statusword')
node.tpdo[1].add_variable('Velocity actual value')
node.tpdo[1].add_variable('Some group', 'Some subindex')
node.tpdo[1].trans_type = 254
node.tpdo[1].event_timer = 10
node.tpdo[1].enabled = True
# Save new PDO configuration to node
node.tpdo[1].save()
# Transmit SYNC every 100 ms
network.sync.start(0.1)
# Change state to operational (NMT start)
node.nmt.state = 'OPERATIONAL'
# Read a value from TPDO[1]
node.tpdo[1].wait_for_reception()
speed = node.tpdo[1]['Velocity actual value'].phys
val = node.tpdo['Some group.Some subindex'].raw
# Disconnect from CAN bus
network.sync.stop()
network.disconnect()
调试
如果您需要更详细地查看正在发生的情况,您可以增加日志级别
import logging
logging.basicConfig(level=logging.DEBUG)
项目详情
下载文件
下载适用于您平台文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。
源代码分发
canopen-2.3.0.tar.gz (101.3 kB 查看哈希值)
构建发行版
canopen-2.3.0-py3-none-any.whl (64.2 kB 查看哈希值)
关闭
canopen-2.3.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 792084a93c138d5b2a406ddd2d4eb5ce208f03bd8fda60f81ad2bb8d56c6b827 |
|
MD5 | 808c04aa0394210be9eeb46fb6e3bf96 |
|
BLAKE2b-256 | 1ab3733e5f98c995d7f3e82853bc5ee2f0677df6203d51d8a4387af188322523 |
关闭
canopen-2.3.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7a62274af6ded7ee85ee96f4ada3afa38bc8607a91f306dcc202c3c8cf2850d7 |
|
MD5 | cd9965b5cc430d02670159865defdfc7 |
|
BLAKE2b-256 | 347fbef6f0e7d237d00705e921a286598e15cc1b9188878ef51b9fec22484c0c |