1.5 版本的Python包装函数有很多改进升级: 新增加了两个资料库: LINlib资料库, 它包含 LIN 总线应用API, 和kvaDbLib 资料库,它包括数据库应用API (将会有其他博客来解释怎样使用它们)。
但是的变化是在内部,编程的结构做了调整,使它更易使用和维护。 ‘旧的’ 方法仍然可用,但我们不建议继续使用旧方法,并将在以后取消旧方法。下面是v1.4版本应用中的一个例子:
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硬件的操作。