news 2026/10/20 16:09:25

树莓派CAN(FD) 测试

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
树莓派CAN(FD) 测试

双向压力测试

#!/usr/bin/env python3# -*- coding: utf-8 -*-importosimporttimeimportsocketimportstructimportselectimportthreadingimportsubprocessimportargparse# ===== CAN 常量 =====SOL_CAN_RAW=getattr(socket,"SOL_CAN_RAW",101)CAN_RAW_FILTER=getattr(socket,"CAN_RAW_FILTER",1)CAN_RAW_LOOPBACK=getattr(socket,"CAN_RAW_LOOPBACK",3)CAN_RAW_RECV_OWN_MSGS=getattr(socket,"CAN_RAW_RECV_OWN_MSGS",4)CAN_ECHO_FLAG=0x20000000STD_MASK=0x7FFCAN_ID=0x123PAYLOAD=bytes([0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88])CAN_FRAME_FMT="=IB3x8s"# can_id, dlc, pad, data# ===== 工具函数 =====defrun(cmd):subprocess.run(cmd,shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)defsetup_can():run("sudo ifconfig can0 down")run("sudo ifconfig can1 down")run("sudo ip link set can0 up type can bitrate 1000000")run("sudo ip link set can1 up type can bitrate 1000000 dbitrate 1000000 restart-ms 1000 berr-reporting on fd on")run("sudo ifconfig can0 txqueuelen 65536")run("sudo ifconfig can1 txqueuelen 65536")defopen_can(iface,is_tx):s=socket.socket(socket.AF_CAN,socket.SOCK_RAW,socket.CAN_RAW)s.bind((iface,))flt=struct.pack("=II",CAN_ID,STD_MASK)s.setsockopt(SOL_CAN_RAW,CAN_RAW_FILTER,flt)ifis_tx:s.setsockopt(SOL_CAN_RAW,CAN_RAW_LOOPBACK,0)else:s.setsockopt(SOL_CAN_RAW,CAN_RAW_RECV_OWN_MSGS,0)returnsdefpack_frame():returnstruct.pack(CAN_FRAME_FMT,CAN_ID,8,PAYLOAD)deffmt_frame():return"ID=0x123 DATA=11 22 33 44 55 66 77 88"# ===== 发送线程 =====defsend_loop(sock,rate,end_t,stat,key,tag):interval=1.0/rate next_t=time.perf_counter()frame=pack_frame()printed=Falsewhiletime.perf_counter()<end_t:now=time.perf_counter()ifnow<next_t:time.sleep(min(0.0005,next_t-now))continuetry:sock.send(frame)stat[key]+=1ifnotprinted:print(f"[{tag}TX OK]{fmt_frame()}")printed=TrueexceptOSError:passnext_t+=interval# ===== 接收线程 =====defrecv_loop(sock,end_t,stat,key,tag):printed=Falsewhiletime.perf_counter()<end_t:r,_,_=select.select([sock],[],[],0.05)ifnotr:continueframe=sock.recv(16)can_id,dlc,data=struct.unpack(CAN_FRAME_FMT,frame)ifcan_id&CAN_ECHO_FLAG:continueif(can_id&STD_MASK)==CAN_IDanddata[:8]==PAYLOAD:stat[key]+=1ifnotprinted:print(f"[{tag}RX OK]{fmt_frame()}")printed=True# ===== 主程序 =====defmain():parser=argparse.ArgumentParser()parser.add_argument("--no-setup",action="store_true",help="跳过 can 配置")parser.add_argument("--duration",type=float,default=3.0,help="每档测试秒数")parser.add_argument("--rates",default="10,50,100,200,500,1000,2000,3000,4000,5000")args=parser.parse_args()ifnotargs.no_setup:setup_can()tx0=open_can("can0",True)tx1=open_can("can1",True)rx0=open_can("can0",False)rx1=open_can("can1",False)rates=[int(x)forxinargs.rates.split(",")]print("\n=== CAN0 <-> CAN1 双向收发测试 ===")print("ID=0x123 DATA=11 22 33 44 55 66 77 88\n")print("rate(Hz) | can0->can1 sent/recv drop% | can1->can0 sent/recv drop%")forrateinrates:stat={"s0":0,"r1":0,"s1":0,"r0":0}start=time.perf_counter()send_end=start+args.duration recv_end=send_end+0.5ts0=threading.Thread(target=send_loop,args=(tx0,rate,send_end,stat,"s0","can0"))ts1=threading.Thread(target=send_loop,args=(tx1,rate,send_end,stat,"s1","can1"))tr0=threading.Thread(target=recv_loop,args=(rx0,recv_end,stat,"r0","can0"))tr1=threading.Thread(target=recv_loop,args=(rx1,recv_end,stat,"r1","can1"))tr0.start();tr1.start()ts0.start();ts1.start()ts0.join();ts1.join()tr0.join();tr1.join()d01=0ifstat["s0"]==0else(stat["s0"]-stat["r1"])/stat["s0"]*100d10=0ifstat["s1"]==0else(stat["s1"]-stat["r0"])/stat["s1"]*100print(f"{rate:7d}|{stat['s0']:5d}/{stat['r1']:5d}{d01:6.2f}%"f" |{stat['s1']:5d}/{stat['r0']:5d}{d10:6.2f}%")ifd01>1ord10>1:print(">> 已明显开始丢包,再提速意义不大了")breakprint("\n=== 测试结束 ===")if__name__=="__main__":ifos.geteuid()!=0:print("请 sudo 运行")exit(1)main()

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/20 9:19:59

32、深入了解Samba与Linux安全策略

深入了解Samba与Linux安全策略 在当今复杂的网络环境中,无论是实现不同操作系统间的资源共享,还是保障系统的安全性,都是至关重要的任务。Samba作为实现Linux与Windows系统资源共享的关键工具,以及Linux系统安全策略的制定与实施,对于系统的稳定运行和数据安全起着举足轻…

作者头像 李华
网站建设 2026/10/20 1:52:27

26、调试 Shell 程序的实用方法

调试 Shell 程序的实用方法 在编程过程中,调试是不可或缺的环节。对于使用 bash 进行 UNIX 编程的开发者来说,虽然 bash 具备丰富的特性和控制结构,但缺乏像 C 和 C++ 那样强大且集成的编程支持工具。不过,bash 自身也提供了一些实用的调试功能,下面将详细介绍。 基本调…

作者头像 李华
网站建设 2026/10/19 22:08:28

Symbolic 英文单词学习

1️、基本信息单词&#xff1a;symbolic词性&#xff1a;形容词发音&#xff1a; &#x1f1fa;&#x1f1f8; /sɪmˈbɑː.lɪk/&#x1f1ec;&#x1f1e7; /sɪmˈbɒl.ɪk/词源&#xff1a; 来自希腊语 symbolikos&#xff08;象征的、符号的&#xff09;&#xff0c;由 s…

作者头像 李华
网站建设 2026/10/19 11:40:35

AI开发全流程工具链:从编码辅助到模型部署的实战指南

在AI开发的浪潮中&#xff0c;工具链已成为效率与质量的决定性因素。本文将系统拆解现代AI开发全流程&#xff0c;涵盖智能编码、数据处理、模型训练、评估部署五大环节&#xff0c;通过5个核心工具、12段实战代码、8个mermaid流程图、15个Prompt示例和6组对比图表&#xff0c;…

作者头像 李华
网站建设 2026/10/19 9:55:01

英语综合练习题

一、综合练习题I 单项选择 (每小题 1 分&#xff0c;共 20 分)( )21. Bruce likes playing ____ football, so his father will buy him ____ football.A. a; an B. the; a C. /; the D. /; a答案D&#xff08;play 球类运动不加冠词&#xff1b;buy a football 表示 “买一个…

作者头像 李华
网站建设 2026/10/20 11:34:29

电力物联网系统能够发挥什么作用

电力物联网系统作为能源领域数字化转型的核心支撑&#xff0c;通过物联网、大数据、人工智能等技术的深度融合&#xff0c;实现了电力生产、传输、分配、消费全环节的智能化升级。其核心作用可归纳为以下六个方面&#xff0c;覆盖技术、经济、社会及环境多维度价值&#xff1a;…

作者头像 李华