1.5 版本的Python包装函数有很多改进升级: 新增加了两个资料库: LINlib 资料库, 它包含 LIN 总线应用 API, 和 kvaDbLib 资料库,它包括数据库应用 API (将会有其他博客来解释怎样使用它们)。
但是最大的变化是在内部,编程的结构做了调整,使它更易使用和维护。 ‘旧的’ 方法仍然可用,但我们不建议继续使用旧方法,并将在以后取消旧方法。下面是 Python canlib v1.4 版本应用中的一个例子:
1.5 版本的Python包装函数有很多改进升级: 新增加了两个资料库: LINlib 资料库, 它包含 LIN 总线应用 API, 和 kvaDbLib 资料库,它包括数据库应用 API (将会有其他博客来解释怎样使用它们)。
但是最大的变化是在内部,编程的结构做了调整,使它更易使用和维护。 ‘旧的’ 方法仍然可用,但我们不建议继续使用旧方法,并将在以后取消旧方法。下面是 Python canlib v1.4 版本应用中的一个例子:
import canlib.canlib as canlib
def 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 ch
def 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 = 100
msg = [1, 2, 3, 4]
flg = canlib.canMSG_EXT
ch1.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: 发送一个帧的编码
运行上面的编码现在意味着我们还在使用不受支持的功能。
canlib\deprecation.py:240: KvDeprecatedUsage: A deprecated function was
called! Run python with -Wd flag for more information.
"Run python with -Wd flag for more information."))
canlib version: 8.22
Using channel: Kvaser USBcan Pro 2xHS v2 (channel 0), EAN: 73-30130-
00752-9
Using channel: Kvaser USBcan Pro 2xHS v2 (channel 1), EAN: 73-30130-
00752-9
time: 9 id: 100 flag:0x04 dlc:4 data:01020304
让我们用警示控制标志Wd来运行同一个编码 ,如上面所建议的:
canlib\deprecation.py:238: KvDeprecatedUsage: A deprecated function was
called! Run
python with -Wd flag for more information.
"Run python with -Wd flag for more information."))
send_single_frame_v1.4.py:23: KvDeprecationWarning: Creating CANLib
objects is
deprecated, all functionality has been moved to the canlib module
itself.
cl = canlib.canlib() # deprecated in v1.5
send_single_frame_v1.4.py:24: KvDeprecationWarning: getVersion has been
deprecated,
use dllversion instead!
print("canlib version: %s" % cl.getVersion())
canlib version: 8.21
send_single_frame_v1.4.py:8: KvDeprecationWarning: Creating CANLib
objects is
deprecated, all functionality has been moved to the canlib module
itself.
cl = canlib.canlib() # deprecated in v1.5
send_single_frame_v1.4.py:10: KvDeprecationWarning: getChannelData_Name
has been
deprecated, use ChannelData(Channel.index).device_name instead!
print("Using channel: %s, EAN: %s" % (ch.getChannelData_Name(), #
deprecated in v1
.5
canlib\canlib\channel.py:537: KvDeprecationWarning: getChannelData_Name
has been
deprecated, use ChannelData(channel).device_name instead!
return wrapper.getChannelData_Name(self.index)
send_single_frame_v1.4.py:11: KvDeprecationWarning: getChannelData_EAN
has been
deprecated, use ChannelData(Channel.index).card_upc_no instead!
ch.getChannelData_EAN())) # deprecated in v1.5
canlib\canlib\channel.py:558: KvDeprecationWarning: getChannelData_EAN
has been
deprecated, 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-9
Using channel: Kvaser USBcan Pro 2xHS v2 (channel 1), EAN: 73-30130-
00752-9
send_single_frame_v1.4.py:32: KvDeprecationWarning: Calling
Channel.write() with
individual arguments is deprecated, please use a Frame object or
Channel.
write_raw()
ch1.write(msgId, msg, flg) # deprecated in v1.5
time: 14 id: 100 flag:0x04 dlc:4 data:01020304
这一次我们得到一个详细的指示:第8行和23行的生成 CANlib 对象不受支持,而且所有功能已被转到canlib 模块。第11行的 getChannelData_EAN 也不受支持,应被 ChannelData(通道).card_upc_no 取代。在第32行命令书写时,我们还应该使用帧目标,来替代不受支持的单独命令,而且,虽然我们在第36行并没收到不支持警告,我们也应该在阅读功能中使用帧对象。帧对象然后可直接打印出来。让我们对我们的程序做相应调整:
from canlib import canlib, Frame
from canlib.canlib import ChannelData
def 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 ch
def 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)
break
except (canlib.canNoMsg) as ex:
pass
except (canlib.canError) as ex:
print(ex)
tearDownChannel(ch0)
tearDownChannel(ch1)
列表 2: 用Python canlib v1.5版程序来发送一个单帧。
当我们填加帧和通道数据到输入部分, 我们也在此把输入说明转换为一个较为可读的格式。运行这个新程序可得到下面的清晰输出:
canlib version: 8.22
Using channel: Kvaser USBcan Pro 2xHS v2 (channel 0), EAN: 73-30130-
00752-9
Using channel: Kvaser USBcan Pro 2xHS v2 (channel 1), EAN: 73-30130-
00752-9
Frame(id=100, data=bytearray(b'\x01\x02\x03\x04'), dlc=4, flags=4,
timestamp=10)
请看一下v1.5版发布通知来大致了解有哪些变化,使用Python 警示控制标志Wd来确保你的编程升级适用新变化 。这个新的操作方式确实能改善资料库的维护,而且也希望它能简化使用Python来控制Kavser硬件的操作。