cannot be loaded because running scripts is disabled on this system.(无法加载,因为在此系统上禁用了运行脚本。)有关更多信息,请参见https:/go.microsoft.com/fwlink/?LinkID=135170 中的about_Execution_Policies。
WARNING: You are using pip version 21.1.1; however, version 21.1.2 is available.(警告:您使用的是版本21.1.1的pip;但是,21.1.2版本是可用的。)
您应该考虑通过“C:\Users\extac\Pyproj\.venv\scripts\python.exe -m pip install –upgrade pip”命令进行升级。
这将打开一个idle窗口,您可以在其中打开代码文件并使用f5或通过运行选项卡运行代码。如果我们通过右键单击.py文件并选择” edit with idle(使用idle编辑)”来打开idle,那么将无法直接使用f5或通过运行选项卡运行代码。
我们需要在虚拟环境内运行的脚本是check_ch,其中包括以下代码:
# check_ch
# Firstly import canlib so that it can be used in the script.
from canlib import canlib
# .getNumberOfChannels() is used to detect the number of channels and
# the number is saved in the variable num_channels.
num_channels = canlib.getNumberOfChannels()
# num_channels is printed out as text so that the user can see how many
# channels were found.
print(f"Found {num_channels} channels")
# Next a for loop is created. This loop will repeat the code within for each
# channel that was detected.
for ch in range(num_channels):
# The data of each specific channel is saved in chd.
chd = canlib.ChannelData(ch)
# Lastly the channel, channel name, product number, serial number, and local
# channel number on the device are printed.
print(f"{ch}. {chd.channel_name} ({chd.card_upc_no.product()}:{chd.card_serial_no}/{chd.chan_no_on_card})")
#send_msg
# The CANlib library is initialized when the canlib module is imported. To be
# able to send a message, Frame also needs to be installed.
from canlib import canlib, Frame
# Firstly, open two CAN channels, one to send the message and one to receive.
# Note that there needs to be a channel to receive, as otherwise the message
# can not be sent. In this example the channels are named ch_a and ch_b. To
# open the channels call on the openChannel method inside of canlib and, as an
# input put in channel=0 and channel=1. Where 0 and 1 represents the two
# CANlib channels 0 and 1.
ch_a = canlib.openChannel(channel=0)
ch_b = canlib.openChannel(channel=1)
# After opening the channel, we need to set the bus parameters. Some
# interfaces keep their params from previous programs. This can cause problems
# if the params are different between the interfaces/channels. For now we will
# use setBusParams() to set the canBitrate to 250K.
ch_a.setBusParams(canlib.canBITRATE_250K)
ch_b.setBusParams(canlib.canBITRATE_250K)
# The next step is to Activate the CAN chip for each channel (ch_a and ch_b in
# this example) use .busOn() to make them ready to receive and send messages.
ch_a.busOn()
ch_b.busOn()
# To transmit a message with (11-bit) CAN id = 123 and contents (decimal) 72,
# 69, 76, 76, 79, 33, first create the CANFrame (CANmessage) and name it. In
# this example, the CANFrame is named frame. Then send the message by calling on
# the channel that will act as the sender and use .write() with the CANFrame
# as input. In this example ch_a will act as sender.
frame = Frame(id_=123, data=[72, 69, 76, 76, 79, 33], flags=canlib.MessageFlag.STD )
ch_a.write(frame)
# To make sure the message was sent we will attempt to read the message. Using
# timeout, only 500 ms will be spent waiting to receive the CANFrame. If it takes
# longer the program will encounter a timeout error. read the CANFrame by calling
# .read() on the channel that receives the message, ch_b in this example. To
# then read the message we will use print() and send msg as the input.
msg = ch_b.read(timeout=500)
print(msg)
# After the message has been sent, received and read it is time to inactivate
# the CAN chip. To do this call .busOff() on both channels that went .busOn()
ch_a.busOff()
ch_b.busOff()
# Lastly, close all channels with close() to finish up.
ch_a.close()
ch_b.close()
# Depending on the situation it is not always necessary or preferable to go of
# the bus with the channels and, instead only use close(). But this will be
# talked more about later.
# send_msg_pyt
from canlib import canlib, Frame
# instead of opening the two channels and closing them one by one, we will use a
# with statement. Using the with statement to open one or more channels with
# canlib.openChannel(i) as ch_x. Within this with statement we will write the
# rest of the code.
with canlib.openChannel(2) as ch_a, canlib.openChannel(3) as ch_b:
# Instead of going on bus with "copy-paste" for each channel, we will use a
# for-loop. Within this loop we will go through a list of all channels opened
# using the with statement. Currently we only have two channels, which makes
# the for-loop somewhat unnecessary. However, when we start using more
# channels the for-loop will be preferred.
for ch in [ch_a, ch_b]:
ch.busOn()
frame = Frame(id_=123, data=[72, 69, 76, 76, 79, 33])
ch_a.write(frame)
msg = ch_b.read(timeout=500)
print(msg)
# After we run out of code within the with statement and exit it, we don't
# need to manually close it or go off bus. The channels that were open using
# the with statement will be automatically closed, and with the channels being
# closed they also went off the bus.
# send_msg_wait
from canlib import canlib, Frame
# We will now open three channels, two from the USBcan and one on
# the leaf pro which we will not connect to the T-cannector. We will use the
# leaf pro channel ch_c to send errorframes.
with canlib.openChannel(2) as ch_a, canlib.openChannel(3) as ch_b, canlib.openChannel(4) as ch_c:
for ch in [ch_a, ch_b, ch_c]:
ch.busOn()
frame = Frame(id_=123, data=[72, 69, 76, 76, 79, 33])
# Instead of using write we will now use writeWait(). We will attempt to send
# a message for 500 milliseconds, if the message is not sent we will receive a
# Timeout occured error.
ch_a.writeWait(frame, timeout=500)
# We will now try to send a message with the channel not connected to the
# T-cannector. This should result in a Timeout occurred error.
ch_c.writeWait(frame, timeout=500)
msg = ch_b.read(timeout=500)
print(msg)
在下一个脚本send_msgs_wait-py中,我们将发送多条报文,并使用waitWrite确保报文已发送:
# send_msgs_wait
from canlib import canlib, Frame
# We will now open three channels, two from the USBcan and one on the
# leaf pro which we will not connect to the T-cannector. We will use the
# leaf pro channel ch_c to send errorframes.
with canlib.openChannel(2) as ch_a, canlib.openChannel(3) as ch_b, canlib.openChannel(4) as ch_c:
for ch in [ch_a, ch_b, ch_c]:
ch.busOn()
frame = Frame(id_=123, data=[72, 69, 76, 76, 79, 33])
# We will now send 199 messages in a for-loop and after the loop use writeWait
# to send a last message, to make sure all previous messages were sent.
for i in range(199):
ch_a.write(frame)
ch_a.writeWait(frame, timeout=500)
msg = ch_b.read(timeout=500)
print(msg)
# We will now do the same with the ch_c channel not connected to the
# T-cannector. This should result in a timeout error.
for i in range(199):
ch_c.write(frame)
ch_c.writeWait(frame, timeout=100)
msg = ch_b.read(timeout=500)
print(msg)
为了找到正确的文档,首先我们需要展开“Using canlib”选项卡。下一步是了解使用方式和用途。目前,我们要发送一条报文,刚好有一个名为“Send and Receive”的选项卡,我们可以展开它。接下来,我们看到“Reading Messages”也是一个标题,那么点击它,直接转到读取报文。在“read”标题下阅读时,我们最终会看到一系列功能列表。其中一项功能如下:“If you want to wait until a message arrives (or a timeout occurs) and then read it, call read with a timeout(如果要等到报文到达(或发生超时)然后读取它,请调用带有超时的read)”。这看起来就是我们所需要的。要了解更多关于该函数的信息,我们可以单击标有“read”的链接直接转到“read”函数。现在我们可以看到“read”有一个名为“timeout”的参数,它是一个整数。该整数决定程序在返回超时错误之前等待报文传递的毫秒数。现在我们已经找到了我们要找的内容,可以将其输入到我们的代码中,例如channel.read(timeout = 500),以使程序等待500毫秒。