广告招募

当前位置:全球装备网 > 技术中心 > 所有分类

Python canlib v1.5版本里改进的API应用

2025年05月17日 18:12:44      来源:广州智维电子科技有限公司 >> 进入该公司展台      阅读量:18

分享:


1.5 版本的Python包装函数有很多改进升级: 新增加了两个资料库: LINlib资料库, 它包含 LIN 总线应用API, 和kvaDbLib 资料库,它包括数据库应用API (将会有其他博客来解释怎样使用它们)。

但是的变化是在内部,编程的结构做了调整,使它更易使用和维护。 ‘旧的’ 方法仍然可用,但我们不建议继续使用旧方法,并将在以后取消旧方法。下面是v1.4版本应用中的一个例子:


import canlib.canlib as canlibdef setUpChannel(channel=0,                 openFlags=canlib.canOPEN_ACCEPT_VIRTUAL,                 bitrate=canlib.canBITRATE_500K,                 bitrateFlags=canlib.canDRIVER_NORMAL):    cl = canlib.canlib()#v1.5版不支持    ch = cl.openChannel(channel, openFlags)    print("Using channel: %s, EAN: %s" % (ch.getChannelData_Name(),  #v1.5版不支持                                          ch.getChannelData_EAN())) # v1.5版不支持    ch.setBusOutputControl(bitrateFlags)    ch.setBusParams(bitrate)    ch.busOn()    return chdef tearDownChannel(ch):    ch.busOff()    ch.close()cl = canlib.canlib() # v1.5版不支持print("canlib version: %s" % cl.getVersion())ch0 = setUpChannel(channel=0)ch1 = setUpChannel(channel=1)msgId = 100msg = [1, 2, 3, 4]flg = canlib.canMSG_EXTch1.write(msgId, msg, flg)# v1.5版不支持while True:    try:        (msgId, msg, dlc, flg, time) = ch0.read()# v1.5版不支持        data = ''.join(format(x, '02x') for x in msg)        print("time:%9d id:%9d flag:0x%02x dlc:%d data:%s" %              (time, msgId, flg, dlc, data))        break    except (canlib.canNoMsg) as ex:        pass    except (canlib.canError) as ex:        print(ex)tearDownChannel(ch0)tearDownChannel(ch1)

列表1: 发送一个帧的编码

运行上面的编码现在意味着我们还在使用不受支持的功能。
canlibdeprecation.py:240: KvDeprecatedUsage: A deprecated function wascalled! Run python with -Wd flag for more information."Run python with -Wd flag for more information."))canlib version: 8.22Using channel: Kvaser USBcan Pro 2xHS v2 (channel 0), EAN: 73-30130-00752-9Using channel: Kvaser USBcan Pro 2xHS v2 (channel 1), EAN: 73-30130-00752-9time: 9 id: 100 flag:0x04 dlc:4 data:

让我们用警示控制标志Wd来运行同一个编码 ,如上面所建议的:

canlibdeprecation.py:238: KvDeprecatedUsage: A deprecated function wascalled! Runpython with -Wd flag for more information."Run python with -Wd flag for more information."))send_single_frame_v1.4.py:23: KvDeprecationWarning: Creating CANLibobjects isdeprecated, all functionality has been moved to the canlib moduleitself.cl = canlib.canlib() # deprecated in v1.5send_single_frame_v1.4.py:24: KvDeprecationWarning: getVersion has beendeprecated,use dllversion instead!print("canlib version: %s" % cl.getVersion())canlib version: 8.21send_single_frame_v1.4.py:8: KvDeprecationWarning: Creating CANLibobjects isdeprecated, all functionality has been moved to the canlib moduleitself.cl = canlib.canlib() # deprecated in v1.5send_single_frame_v1.4.py:10: KvDeprecationWarning: getChannelData_Namehas beendeprecated, use ChannelData(Channel.index).device_name instead!print("Using channel: %s, EAN: %s" % (ch.getChannelData_Name(), #deprecated in v1.5canlibcanlibchannel.py:537: KvDeprecationWarning: getChannelData_Namehas beendeprecated, use ChannelData(channel).device_name instead!return wrapper.getChannelData_Name(self.index)send_single_frame_v1.4.py:11: KvDeprecationWarning: getChannelData_EANhas beendeprecated, use ChannelData(Channel.index).card_upc_no instead!ch.getChannelData_EAN())) # deprecated in v1.5canlibcanlibchannel.py:558: KvDeprecationWarning: getChannelData_EANhas beendeprecated, use ChannelData(channel).card_upc_no instead!return wrapper.getChannelData_EAN(self.index)Using channel: Kvaser USBcan Pro 2xHS v2 (channel 0), EAN: 73-30130-00752-9Using channel: Kvaser USBcan Pro 2xHS v2 (channel 1), EAN: 73-30130-00752-9send_single_frame_v1.4.py:32: KvDeprecationWarning: CallingChannel.write() withindividual arguments is deprecated, please use a Frame object orChannel.write_raw()ch1.write(msgId, msg, flg) # deprecated in v1.5time: 14 id: 100 flag:0x04 dlc:4 data:

这一次我们得到一个详细的指示:第8行和23行的生成 CANlib 对象不受支持,而且所有功能已被转到canlib 模块。第11行的 getChannelData_EAN 也不受支持,应被 ChannelData(通道).card_upc_no 取代。在第32行命令书写时,我们还应该使用帧目标,来替代不受支持的单独命令,而且,虽然我们在第36行并没收到不支持警告,我们也应该在阅读功能中使用帧对象。帧对象然后可直接打印出来。让我们对我们的程序做相应调整:

from canlib import canlib, Framefrom canlib.canlib import ChannelDatadef setUpChannel(channel=0,openFlags=canlib.canOPEN_ACCEPT_VIRTUAL,bitrate=canlib.canBITRATE_500K,bitrateFlags=canlib.canDRIVER_NORMAL):ch = canlib.openChannel(channel, openFlags)print("Using channel: %s, EAN: %s" %(ChannelData(channel).device_name,ChannelData(channel).card_upc_no))ch.setBusOutputControl(bitrateFlags)ch.setBusParams(bitrate)ch.busOn()return chdef tearDownChannel(ch):ch.busOff()ch.close()print("canlib version:", canlib.dllversion())ch0 = setUpChannel(channel=0)ch1 = setUpChannel(channel=1)frame = Frame(id_=100, data=[1, 2, 3, 4], flags=canlib.canMSG_EXT)ch1.write(frame)while True:try:frame = ch0.read()print(frame)breakexcept (canlib.canNoMsg) as ex:passexcept (canlib.canError) as ex:print(ex)tearDownChannel(ch0)tearDownChannel(ch1)

列表 2: 用Python canlib v1.5版程序来发送一个单帧。

当我们填加帧和通道数据到输入部分, 我们也在此把输入说明转换为一个较为可读的格式。运行这个新程序可得到下面的清晰输出:
canlib version: 8.22Using channel: Kvaser USBcan Pro 2xHS v2 (channel 0), EAN: 73-30130-00752-9Using channel: Kvaser USBcan Pro 2xHS v2 (channel 1), EAN: 73-30130-00752-9Frame(id=100, data=bytearray(b'x01x02x03x04'), dlc=4, flags=4,timestamp=10)

请看一下v1.5版发布通知来大致了解有哪些变化,使用Python 警示控制标志 Wd来确保你的编程升级适用新变化 。这个新的操作方式确实能改善资料库的维护,而且也希望它能简 化使用Python来控制Kavser硬件的操作。

版权与免责声明:
1.凡本网注明"来源:全球装备网"的所有作品,版权均属于全球装备网,转载请必须注明全球装备网。违反者本网将追究相关法律责任。
2.企业发布的公司新闻、技术文章、资料下载等内容,如涉及侵权、违规遭投诉的,一律由发布企业自行承担责任,本网有权删除内容并追溯责任。
3.本网转载并注明自其它来源的作品,目的在于传递更多信息,并不代表本网赞同其观点或证实其内容的真实性,不承担此类作品侵权行为的直接责任及连带责任。其他媒体、网站或个人从本网转载时,必须保留本网注明的作品来源,并自负版权等法律责任。 4.如涉及作品内容、版权等问题,请在作品发表之日起一周内与本网联系。