Python之python-can库基于PCAN-USB使用方法
一、概述
1.介绍
本文章向大家介绍python-can库基于PCAN-USB使用方法,主要包括python-can库基于PCAN-USB使用方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
python-can库为Python提供了控制器局域网的支持,为不同的硬件设备提供了通用的抽象,并提供了一套实用程序,用于在CAN总线上发送和接收消息。
支持硬件接口:
Name | Documentation |
---|---|
"socketcan" | SocketCAN |
"kvaser" | Kvaser’s CANLIB |
"serial" | CAN over Serial |
"slcan" | CAN over Serial / SLCAN |
"ixxat" | IXXAT Virtual CAN Interface |
"pcan" | PCAN Basic API |
"usb2can" | USB2CAN Interface |
"nican" | NI-CAN |
"iscan" | isCAN |
"neovi" | neoVI |
"vector" | Vector |
"virtual" | Virtual |
"canalystii" | CANalyst-II |
"systec" | SYSTEC interface |
2.环境搭建
Python安装:
https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64.exe
PCAN-USB驱动:
https://www.peak-system.com/fileadmin/media/files/pcan-basic.zip
库:
pip install python-can
- 1
3.参考文档
https://python-can.readthedocs.io/en/master/#
二、常用方法
1.接收报文
from can.interfaces.pcan.pcan import PcanBus
def bus_recv():
"""轮询接收消息"""
try:
while True:
msg = bus.recv(timeout=100)
print(msg)
except KeyboardInterrupt:
pass if __name__ == '__main__':
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
bus_recv()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.发送报文
from can.interfaces.pcan.pcan import PcanBus
def bus_send():
"""can消息发送"""
while True:
time.sleep(0.02)
try:
bus.send(msg)
print("消息发送 {}".format(bus.channel_info))
except can.CanError:
print("消息未发送") if __name__ == '__main__':
msg = can.Message(arbitration_id=0x181DFF00, data=[0xEE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFE],
is_extended_id=True) # 报文
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
bus_send()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
3.定期发送报文
def bus_send_periodic():
"""周期发送报文"""
print("开始每200毫秒发送一条消息。持续时间10s")
task = bus.send_periodic(msg, 1.5) # 定期发送
if not isinstance(task, can.ModifiableCyclicTaskABC): # 断言task类型
print("此接口似乎不支持")
task.stop()
return
time.sleep(5) # 持续时间
print("发送完成")
print("更改运行任务的数据以99开头")
msg.data[0] = 0x99
task.modify_data(msg) # 修改data首字节
time.sleep(10) task.stop()
print("停止循环发送")
print("将停止任务的数据更改为单个 ff 字节")
msg.data = bytearray([0xff]) # 重新定向data
msg.dlc = 1 # 定义data长度
task.modify_data(msg) # 修改data
time.sleep(10)
print("重新开始")
task.start() # 重新启动已停止的周期性任务
time.sleep(10)
task.stop()
print("完毕") if __name__ == '__main__':
msg = can.Message(arbitration_id=0x181DFF00, data=[0xEE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFE],
is_extended_id=True) # 报文
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
bus_send_periodic()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
4.循环收发消息
from can.interfaces.pcan.pcan import PcanBus
def send_cyclic(stop_event):
"""循环发送消息"""
print("开始每1秒发送1条消息")
start_time = time.time()
while not stop_event.is_set():
msg.timestamp = time.time() - start_time
bus.send(msg)
print(f"tx: {msg}")
time.sleep(1)
print("停止发送消息")
def receive(stop_event):
"""循环接收消息"""
print("开始接收消息")
while not stop_event.is_set():
rx_msg = bus.recv(1)
if rx_msg is not None:
print(f"rx: {rx_msg}")
print("停止接收消息")
def send_and_recv_msg():
"""发送一个消息并接收一个消息,需要双通道CAN"""
stop_event = threading.Event()
t_send_cyclic = threading.Thread(target=send_cyclic, args=(stop_event,))
t_receive = threading.Thread(target=receive, args=(stop_event,))
t_receive.start()
t_send_cyclic.start()
try:
while True:
time.sleep(0) # yield
except KeyboardInterrupt:
pass # 正常退出
stop_event.set()
time.sleep(0.5)
if __name__ == '__main__':
msg = can.Message(arbitration_id=0x181DFF00, data=[0xEE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFE],
is_extended_id=True) # 报文
bus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)
send_and_recv_msg()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
Spark SQL 读写 Hive Table 部署指南(基于Spark 3.1.2 + Hive 3.1.2)
一、环境要求
- 组件版本:
- Spark: 3.1.2
- Hive: 3.1.2
- Hadoop: 3.2.2(需与Hive和Spark兼容)
- Java: JDK 8 或 11(需与Hadoop版本匹配)
- 节点要求:
- Hive Metastore:部署于独立节点(或共享节点)。
- Spark Workers:所有节点需安装Hive客户端及依赖库。
二、部署步骤详解
1. Hive 环境部署
- 安装Hive 3.1.2:
# 在所有Spark Worker节点安装Hive wget https://downloads.apache.org/hive/hive-3.1.2/apache-hive-3.1.2-bin.tar.gz tar -xzvf apache-hive-3.1.2-bin.tar.gz -C /opt/ ln -s /opt/apache-hive-3.1.2-bin /opt/hive class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
评论记录:
回复评论: