neoVI FIRE2 是英特佩斯(intrepidcs)的设备, 支持8路CANFD(最大1M@8M/10M), 2 组各 4 个共 8 个终端电阻可配置,每组同 时最大可开启 2 个终端电阻, 4路LIN, 1路百兆以太网等. 配合上位机Vehicle Spy, 做CCP, XCP, UDS, 数据记录回放等很方便, 功能还是蛮强大的, 当然价格也算感人, 这篇就来盘一下python和fire2的联动.
注意: 这个需要单独供电, 单纯USB带不动. 接口是DB26, 需要自己拿杜邦线插或者买DB26/DB25的单头线. 官方也没有送DB26转DB9的线, 用起来不是很方便.
8路CANFD的命名为:
本篇用到HSCAN和MSCAN, 均配置为500K@2M CANFD, 80%采样点, 开启终端电阻, 使用官方的neoVI Explorer软件配置, 以HSCAN配置为例:
MSCAN可以参考HSCAN来配置.
标准CAN用周立功CAN卡配合做验证, CANFD及后面的用Nvidia的Xavier来做验证.
Python环境搭建在Win10下测试, 使用64位的python3.9.4, 先安装必要的支持:
icsneo40.dll
这个在Win10的path下pip install python-can
, 文档参考 python-can — python-can 3.3.4 documentationpip install python-ics
, 这个是IntrepidCS官方维护的, intrepidcs/python_ics: Library for interfacing with Intrepid devices in Python (github.com), 文档参考 python_ics — ics 904.18 documentation (python-ics.readthedocs.io)pip install filelock
, 这个是can.logger会用到的可以参考官方给的例程 open_device_example.py
, 这里删减一下:
import icsdef dev_name(device):# Return a friendly name of the device (ie. neoVI FIRE2 CY1234)if int("AA0000", 36) <&#61; device.SerialNumber <&#61; int("ZZZZZZ", 36):return device.Name &#43; " " &#43; ics.base36enc(device.SerialNumber)else:return device.Name &#43; " " &#43; str(device.SerialNumber)if __name__ &#61;&#61; "__main__":devices &#61; ics.find_devices()for device in devices:print(device)print(dev_name(device)) #neoVI FIRE2 CY1234ics.open_device(device)# the device will automatically close here# because the device object is going out of# scope.print("Finished.")
其中:
执行结果:
<ics.ics.NeoDevice neoVI FIRE2 CY1234>
neoVI FIRE2 CY1234
Finished.
发送报文
这里用Fire2的HSCAN这个通道发, 周立功USBCAN2收:
import icsdef open_device(index&#61;0):device &#61; Nonedevices &#61; ics.find_devices()for device in devices:print(device)ics.open_device(device)return devicedef transmit_can(device, channel, stdorext, id, data, len):msg &#61; ics.SpyMessage()msg.ArbIDOrHeader &#61; id # CAN Arbitration IDmsg.Data &#61; tuple(data[0:len]) # Data Bytes go heremsg.NetworkID &#61; channel # channel of CAN on the deviceif stdorext:# msg.NumberBytesHeader &#61; 4msg.StatusBitField &#61; ics.SPY_STATUS_XTD_FRAME# msg parameter here can also be a tuple of messagesics.transmit_messages(device, msg)if __name__ &#61;&#61; "__main__":import timedev &#61; open_device(0)txdata &#61; [0,1,2,3,4,5,6,7] #listfor i in range(5):transmit_can(dev, ics.NETID_HSCAN, 0, 0x123, txdata, 7)transmit_can(dev, ics.NETID_HSCAN, 1, 0x123456AB, txdata, 8)txdata[0] &#61; txdata[0] &#43; 1time.sleep(0.1)print("Finished.")
其中:
msg.data接收的数据为tuple类型, 不可改变元素, 所以这里有一个list强制转换成tuple, list的数据是可以改变的
channel在icsnVC40.h
中定义如下, 所以ics.NETID_HSCAN和1是等价的, 发送写成transmit_can(dev, 1, 0x123, txdata)也是可以的:
#define NETID_HSCAN 1
#define NETID_MSCAN 2
#define NETID_HSCAN2 42
#define NETID_HSCAN3 44
#define NETID_HSCAN4 61
#define NETID_HSCAN5 62
#define NETID_HSCAN6 96
#define NETID_HSCAN7 97
运行, 就在周立功上位机界面收到这10帧数据:
接收报文承接上面的发送:
import icsdef open_device(index&#61;0):device &#61; Nonedevices &#61; ics.find_devices()for device in devices:print(device)ics.open_device(device)return devicedef transmit_can(device, channel, stdorext, id, data, len):msg &#61; ics.SpyMessage()msg.ArbIDOrHeader &#61; id # CAN Arbitration IDmsg.Data &#61; tuple(data[0:len]) # Data Bytes go heremsg.NetworkID &#61; channel # channel of CAN on the deviceif stdorext:# msg.NumberBytesHeader &#61; 4msg.StatusBitField &#61; ics.SPY_STATUS_XTD_FRAME# msg parameter here can also be a tuple of messagesics.transmit_messages(device, msg)def receive_can(device):msgs, error_count &#61; ics.get_messages(device)print("Received {} messages with {} errors.".format(len(msgs), error_count))for i, m in enumerate(msgs):if m.NetworkID !&#61; ics.NETID_HSCAN:continueprint(&#39;Message #{}\t&#39;.format(i&#43;1), end&#61;&#39;&#39;)print(&#39;\tArbID: {}\tData: {}&#39;.format(hex(m.ArbIDOrHeader), [hex(x) for x in m.Data]))if __name__ &#61;&#61; "__main__":import timedev &#61; open_device(0)txdata &#61; [0,1,2,3,4,5,6,7] #listfor i in range(5):transmit_can(dev, ics.NETID_HSCAN, 0, 0x123, txdata, 7)txdata[0] &#61; txdata[0] &#43; 1time.sleep(0.1)# ics.get_messages(dev) # clear cachetime.sleep(1)receive_can(dev) # display received messagesprint("Finished.")
用周立功CAN每200ms发送0x12345678这一帧, 运行接收程序, 发现从open_device开始就在接收, 包括自己发送的:
如果不想受到以前报文的干扰, 可以在接收前先接收一次当作清除缓存:
ics.get_messages(dev) # clear cache
time.sleep(1)
receive_can(dev) # display received messages in 1s
这样运行起来就只展示这1ms内的报文:
CANFD发送接收代码如下:
import icsdef open_device(index&#61;0):device &#61; Nonedevices &#61; ics.find_devices()for device in devices:print(device)ics.open_device(device)return devicedef transmit_can(device, channel, stdorext, id, data, len):msg &#61; ics.SpyMessage()msg.ArbIDOrHeader &#61; id # CAN Arbitration IDmsg.Data &#61; tuple(data[0:len]) # Data Bytes go heremsg.NetworkID &#61; channel # channel of CAN on the deviceif stdorext:# msg.NumberBytesHeader &#61; 4msg.StatusBitField &#61; ics.SPY_STATUS_XTD_FRAME# msg parameter here can also be a tuple of messagesics.transmit_messages(device, msg)def transmit_canfd(device, channel, id, data, len):msg &#61; ics.SpyMessage()msg.ArbIDOrHeader &#61; id # CAN Arbitration ID# msg.Data &#61; tuple(data[0:len]) msg.NetworkID &#61; channel # channel of CAN on the devicemsg.Protocol &#61; ics.SPY_PROTOCOL_CANFDmsg.StatusBitField &#61; ics.SPY_STATUS_CANFDmsg.StatusBitField3 &#61; ics.SPY_STATUS3_CANFD_BRSmsg.StatusBitField4 &#61; ics.SPY_STATUS3_CANFD_ESImsg.ExtraDataPtr &#61; tuple(data[0:len]) # Data Bytes go hereics.transmit_messages(device, msg)def receive_can(device):msgs, error_count &#61; ics.get_messages(device)print("Received {} messages with {} errors.".format(len(msgs), error_count))for i, m in enumerate(msgs):if m.NetworkID !&#61; ics.NETID_HSCAN:continueprint(&#39;Message #{}\t&#39;.format(i&#43;1), end&#61;&#39;&#39;)if (m.ExtraDataPtr):print(&#39;\tArbID: {}\tData: {}&#39;.format(hex(m.ArbIDOrHeader), [hex(x) for x in tuple(m.ExtraDataPtr)]))else:print(&#39;\tArbID: {}\tData: {}&#39;.format(hex(m.ArbIDOrHeader), [hex(x) for x in tuple(m.Data)]))if __name__ &#61;&#61; "__main__":import timedev &#61; open_device(0)txdata &#61; [0,1,2,3,4,5,6,7,\0,1,2,3,4,5,6,7,\0,1,2,3,4,5,6,7,\0,1,2,3,4,5,6,7,\0,1,2,3,4,5,6,7,\0,1,2,3,4,5,6,7,\0,1,2,3,4,5,6,7,\0,1,2,3,4,5,6,7] #listfor i in range(2):transmit_can(dev, ics.NETID_HSCAN, 0, 0x123, txdata, 7)transmit_canfd(dev, ics.NETID_HSCAN, 0x456, txdata, 64)txdata[0] &#61; txdata[0] &#43; 1time.sleep(0.1)ics.get_messages(dev) # clear cachetime.sleep(2)receive_can(dev) # display received messages in 1sprint("Finished.")
其中:
can和canfd的发送拆开了, 接收合并成了一个, 用m.ExtraDataPtr是否为空区分
开BRS才是真正的可变波特率 msg.StatusBitField3&#61;SPY_STATUS3_CANFD_BRS
ESI那个不开的话也可以注释掉
这里用Nvidia的Jetson Xavier来测试, Xavier配置好40Pin脚本后, 配置下CAN1, 500K&#64;2M, 采样点80%:
#!/bin/shsudo modprobe can
sudo modprobe can_raw
sudo modprobe mttcansudo ip link set down can1
sudo ip link set can1 type can bitrate 500000 sample-point 0.8 dbitrate 2000000 dsample-point 0.8 fd on restart-ms 100
sudo ip link set up can1 mtu 72
sudo ifconfig can1 txqueuelen 1000
Xavier上写一个发送脚本一直跑着:
#!/bin/shwhile true; docansend can1 18FF0000##0.01.02.03.04.05.06.07.08cansend can1 18FF0001##3.01.02.03.04.05.06.07.08.09.10.11.12.13.14.15.16.01.02.03.04.05.06.07.08.09.10.11.12.13.14.15.16.01.02.03.04.05.06.07.08.09.10.11.12.13.14.15.16.01.02.03.04.05.06.07.08.09.10.11.12.13.14.15.16sleep 1
done
Xavier运行candump -td -x can1
进行接收
然后运行上面Fire2的canfd程序, 收到Xavier发过来的报文:
Xavier收到Fire2发来的程序如下:
可以看到0x456开启了BRS, ESI, 发出了64字节数据
can.viewerpython-can有几个小脚本很好用: Scripts — python-can 3.3.4 documentation
先来看can.viwer, 运行:
python3 -m can.viewer -c 1 -i neovi
其中:
-c 1
表示用的通道HSCAN, 等效于-c HSCAN
, 参见上面发送报文小节icsnVC40.h
中的定义#define NETID_HSCAN 1
. 8个通道(HSCAN, MSCAN, HSCAN2, HSCAN3, HSCAN4, HSCAN5, HSCAN6, HSCAN7)分别对应(1,2,42,44,61,62,96,97), 如 -c HSCAN2
和-c 42
是等效的. 这种方式只能打开一个通道, 多个通道一起看的话暂时不清楚能不能搞-i neovi
指Fire2python3 -m can.viewer -c 1 -i neovi -f 123:1FFFFFFF 18FF0001:1FFFFFFF
这种形式效果如图, 默认分类显示:
按下ESC或者q键可以退出, 按Ctrl&#43;C不行, c清屏, s排序, 空格键暂停.
更多参数通过 python3 -m can.viewer -h
查看
这个用来录包, 运行:
python3 -m can.logger -f 1.log -c 1 -i neovi --filter 123:7FF
其中:
-f 1.log
, 是把log放入1.log文件夹-c 1
表示用的通道HSCAN, 等效于-c HSCAN
, 参见上面发送报文小节icsnVC40.h
中的定义#define NETID_HSCAN 1
. 多个通道一起录的方法暂不清楚-i neovi
指Fire2--filter 123:7FF
, 只接收0x123这一帧, 更多的帧打空格往下写, 如果不要这个滤波器, 默认全接收1.log效果如下:
更多参数通过 python3 -m can.logger -h
查看
有录包就有回放, 运行:
python3 -m can.player -c 1 -i neovi 1.log
可以在已经运行candump -td -x -a can1
的xavier上收到按照时间戳回放的数据:
回放完自动停止, 如果不想按照时间戳发, 想最快发完, 可以加 --ignore-timestamps
参数.
更多参数通过 python3 -m can.player -h
查看
欢迎扫描二维码关注本人微信公众号, 及时获取最新文章: