KVASER CANlib & Python第3部分:U100和Python

  • 2023-01-06
  • Logan Via

在这个简短的指南中,我们将介绍U100接口上的LED的工作原理和使用方式。

作者:Anton Carlsson,软件开发人员

共同作者: L-G Fredriksson,现场应用工程师

版本: 2021-08-09A

本文的目标读者有哪些?

本指南适用于那些希望通过CANlib和canlib Python包设置和使用Kvaser CAN接口的用户。

在使用这个指南前,您必须拥有Kvaser U100接口。

在使用这个指南之前,我们建议您阅读以下内容:

快速入门与关键命令

如果您中断了操作或忘记了使用的命令/流程,请使用以下强大的命令和说明列表。

  • 启动Windows Powershell:
    powershell.exe
  • 切换到您存放Python脚本的目录:cd(如果您已经有一个虚拟环境并且具有运行权限,则跳过接下来的两个步骤)
  • 创建一个虚拟环境:
    py -3 -m venv .venv –prompt .
  • 如果您是使用新计算机或没有权限的新用户:
    Set-ExecutionPolicy Unrestricted -Scope CurrentUser
  • 激活虚拟环境:
    .\.venv\Scripts\activate
  • 如果您尚未在此虚拟环境中安装canlib,请执行以下操作:
    pip install canlib
  • 运行脚本:py check_ch.py(py一直可以运行,以确保所需的接口已连接)
  • 启动编辑器:
    python -m idlelib
    python -m idlelib check_ch.py
  • 停用虚拟环境:
    deactivate

Kvaser U100接口

与前几代Kvaser接口相比,U100进行了一些更新。除了比其他设备更加坚固之外,最明显的变化是接口上的LED。不仅LED在尺寸和形状上发生了变化,它们的工作方式也做出了调整。这些变化使用户更容易看到LED,并从LED获取有关设备的更多信息。

首先,之前迭代的CAN接口有两个LED用于PW,每个可用的CAN通道上各有两个LED。然而,U100只有两个LED条,用于设备上唯一可用的CAN通道。一个LED条用于TX,另一个LED条用于RX。然而,这些LED具有更多的功能,可以显示多种颜色。

U100 LEDs

与以前的设备上拥有的三种颜色(黄色、红色和绿色)不同,U100具有五种颜色。这些颜色包括:黄色、绿色、红色、白色和蓝色。LED条具有两个状态区域,如图所示,每条指示灯有一个状态区域。

当首次连接U100时,两个LED条会先缓慢闪烁一次绿光,然后状态区域会亮起白光。当通道上线时,白色状态区域将变为绿色。当U100接收或发送报文时,其中一个状态区域将闪烁黄色。如果设备接收到报文,RX状态区域将闪烁黄色。如果设备发送报文,TX状态区域将闪烁黄色。如果在传输过程中出现错误,LED将在一定程度上显示我们接收到的错误类型。如果我们尝试在总线上没有其他通道的情况下使用U100发送报文时,通道将进入错误被动状态,两个状态区域都会闪烁红色。此外,由于通道正在传输错误帧,TX LED条将变为红色。如果我们尝试以不匹配的比特率读取报文,状态区域仍将闪烁红色。但是由于我们现在正在接收错误帧,RX LED条将变为红色,而不是TX灯条。

如果我们增加总线负载(增加发送的报文数量),LED指示灯也会随之亮起。根据总线负载的不同,从TX或RX LED的状态区域开始,更多的LED将亮起黄光,这取决于我们是在发送还是接收。要测试LED,我们可以编写以下脚本并将其命名为u100.py。

> .\.venv\Scripts\activate

(pyproj)> py .\u100.py

import time
from canlib import canlib, Frame

# First create two frames with different id:s.
frame_a = Frame(id_=1, data=[72, 69, 76, 76, 79, 33])
frame_b = Frame(id_=2, data=[72, 69, 76, 76, 79, 33])

# Second, open two channels, one for the U100 and one for another interface.
# In this example the U100 is connected to channel 3 and a USBcan is
# connected to channel 2. And set their bitrate.
ch_a = canlib.openChannel(channel=2, bitrate=canlib.Bitrate.BITRATE_100K)
ch_b = canlib.openChannel(channel=3, bitrate=canlib.Bitrate.BITRATE_100K)

#set both channels to normal and go on bus.
ch_a.setBusOutputControl(canlib.Driver.NORMAL)
ch_b.setBusOutputControl(canlib.Driver.NORMAL)
ch_a.busOn()
ch_b.busOn()

# To get a clearer sense of what happens, you will need to press enter between
# each test.
input("Press enter to continue")
# In the first example we will transmit messages with the U100. In the
# second example we will receive messages with the U100. And in the third
# and last example we will both send and receive messages with the U100.
# In all examples we will send 50 messages, you can test to increase this
# number to see its effect.
for i in range(50):
# We will make use of the try function during these tests. The program will
# try to execute the commands within the try-block (the indented region
# underneath). If the program encounters an CanGeneralError (indicated by
# the except command) the program will instead execute the commands within
# the except block. In this example the messages might be sent to quickly
# and result in a CanGeneralError which is why we will wait for 0.1 seconds
# with time.sleep if this happens.
    try:
        # This is the try-block.
        ch_b.write(frame_b)
        ch_a.read(1000)
    except canlib.exceptions.CanGeneralError:
        # This is the except block.
        time.sleep(0.1)

input("Press enter to continue")

for i in range(50):
    try:
        ch_a.write(frame_a)
        ch_b.read(1000)
    except canlib.exceptions.CanGeneralError:
        time.sleep(0.1)

input("Press enter to continue")

for i in range(50):
    try:
        ch_b.write(frame_b)
        ch_a.read(1000)
        ch_a.write(frame_a)
        ch_b.read(1000)
    except canlib.exceptions.CanGeneralError:
        time.sleep(0.1)

ch_a.close()
ch_b.close()

在测试过程中,TX和RX的LED将亮起。如果我们增加报文的数量(在支持范围内的数量),总线负载就会增加,更多的LED灯将变成黄色。

Author Image

Logan Via

联系我们