当前位置: 首页 > article >正文

C#合并CAN ASC文件:实现与优化

C#合并CAN ASC文件:实现与优化

在汽车电子和工业控制领域,CAN(Controller Area Network)总线是一种广泛使用的通信协议。CAN ASC(American Standard Code)文件则是记录CAN总线通信数据的标准格式,常用于数据分析和故障排查。当需要处理多个时间段的CAN数据时,合并多个ASC文件就成为了必要操作。本文将介绍如何使用C#实现CAN ASC文件的合并功能。

一、ASC文件格式简介

CAN ASC文件通常包含以下部分:

  • 文件头部:包含日期、时间戳类型等信息
  • 数据记录:每条记录包含时间戳、CAN ID、数据长度和数据内容

一个典型的ASC文件示例:

date Wed May 29 10:30:00 am 2025
base hex  timestamps absolute0.000000 123             Tx   d 8 01 02 03 04 05 06 07 08  Channel: 11.500000 456             Rx   d 8 11 12 13 14 15 16 17 18  Channel: 1

二、合并CAN ASC文件的挑战

合并多个ASC文件看似简单,但实际上需要考虑以下几个关键问题:

    1. 时间戳处理:如何将不同文件中的相对时间戳合并为统一的时间线
    1. 文件顺序:按什么顺序合并文件才能保证时间连续性
    1. 数据一致性:合并后的数据行数是否正确
    1. 重复合并检查:避免重复合并相同的文件

三、C#实现CAN ASC文件合并器

下面是一个完整的C#实现,它能够处理多个ASC文件的合并,并解决上述挑战:

/// <summary>
/// 报文合并
/// </summary>
public class AscMerger
{/// <summary>/// 源文件夹路径,存储需要合并的 ASC 文件/// </summary>private readonly string sourcePath;/// <summary>/// 目标文件夹路径,用于存储合并后的 ASC 文件/// </summary>private readonly string destinationPath;public AscMerger(string sourcePath, string destinationPath){// 设置源文件夹路径this.sourcePath = sourcePath;// 设置目标文件夹路径this.destinationPath = destinationPath;}public (string filePath, ErrorCode errorCode) Merge(){try{if (!Directory.Exists(this.sourcePath)){return ("", ErrorCode.SourceFolderNotFound);}if (!Directory.Exists(this.destinationPath)){return ("", ErrorCode.DestinationFolderNotFound);}var files = this.GetFiles();if (!files.Any()){return ("", ErrorCode.AscFilesNotFound);}var messages = files.Select(f => this.ReadFile(f)).ToList();var headerTime = this.CalculateHeaderTime(messages);var headerTimestamp = ToTimestamp(headerTime);var combinedMessages = this.CombineMessages(messages, headerTimestamp);// 验证文件已存在var targetName = $"{this.ToFileString(headerTime)}.asc";if (files.Any(f => f.Name == targetName)){return ("", ErrorCode.AlreadyMerged);}// 验证行数var lineCount = messages.Select(m => m.Skip(2).Count()).Sum();if (lineCount != combinedMessages.Count){return ("", ErrorCode.Inconsistent);}// 保存var target = Path.Combine(this.destinationPath, targetName);if (!this.SaveDestinationFile(headerTime, combinedMessages, target)){return ("", ErrorCode.FileWriteError);}return (target, ErrorCode.None);}catch (Exception e){Console.WriteLine($"{e.Message}\r\n{e.StackTrace}");return ("", ErrorCode.FileReadError);}}private List<FileInfo> GetFiles(){return new DirectoryInfo(this.sourcePath).GetFiles("*.asc").ToList();}private List<string> ReadFile(FileInfo f){using (var sr = new StreamReader(f.FullName)){var list = new List<string>();var content = "";while (!string.IsNullOrWhiteSpace((content = sr.ReadLine()))){list.Add(content);}return list;}}private DateTime CalculateHeaderTime(List<List<string>> messages){return messages.Where(m => m.Any()).Select(m => m.Take(1).First()).Select(m => this.FromCanHeaderString(m)).Min();}private DateTime FromCanHeaderString(string s){s = s.Replace("date ", "").Replace("am", "AM").Replace("pm", "PM");var format = "ddd MMM dd hh:mm:ss tt yyyy";var culture = CultureInfo.CreateSpecificCulture("en-US");return DateTime.ParseExact(s, format, culture);}private double ToTimestamp(DateTime d){if (d == DateTime.MinValue){return 0;}return new DateTimeOffset(d).ToUnixTimeMilliseconds() / 1000.0;}private List<string> CombineMessages(List<List<string>> messages, double headerTimestamp){return (from dict infrom m in messagesselect this.CalculateTimestamp(m)from kp in dictorderby kp.Item1select this.ReplaceTimestamp(kp.Item2, headerTimestamp, kp.Item1)).ToList();}private List<(double, string)> CalculateTimestamp(List<string> messages){if (messages == null || messages.Count < 2){return new List<(double, string)>();}var header = messages.Take(2).ToList();var time = this.FromCanHeaderString(header[0]);var timestamp = ToTimestamp(time);return messages.Skip(2).Select(m => (this.ExtractTimestamp(m), m)).Select(m => (timestamp + m.Item1, m.Item2)).ToList();}private double ExtractTimestamp(string s){var reg = new Regex("^\\d{1,}.\\d{6}");var match = reg.Match(s);return double.Parse(match.Value);}private string ReplaceTimestamp(string s, double baseTimestamp, double timestamp){return Regex.Replace(s, "^\\d{1,}.\\d{6}", (timestamp - baseTimestamp).ToString("0.000000"));}private bool SaveDestinationFile(DateTime headerTime, List<string> messages, string path){using (var sw = new StreamWriter(path)){sw.WriteLine($"date {this.ToCanHeaderString(headerTime)}");sw.WriteLine("base hex timestamps absolute");sw.Flush();foreach (var m in messages){sw.WriteLine(m);}return true;}}private string ToFileString(DateTime time){return $"{time:yyyyMMdd_HHmmss}";}public string ToCanHeaderString(DateTime d){var format = "ddd MMM dd hh:mm:ss tt yyyy";var culture = CultureInfo.CreateSpecificCulture("en-US");return d.ToString(format, culture).Replace("AM", "am").Replace("PM", "pm");}
}public enum ErrorCode
{None = 0,SourceFolderNotFound = 1,DestinationFolderNotFound = 2,AscFilesNotFound = 3,FileReadError = 4,FileWriteError = 5,Inconsistent = 6,AlreadyMerged = 7
}

四、代码解析

这个ASC文件合并器主要包含以下几个核心功能:

    1. 初始化与路径验证:通过构造函数接收源文件夹和目标文件夹路径,并在合并前验证这些路径是否存在。
    1. 文件读取ReadFile方法负责读取单个ASC文件的内容,将其存储为字符串列表。
    1. 时间戳处理
    • CalculateHeaderTime方法确定所有文件中最早的时间戳
    • ExtractTimestamp方法从每条记录中提取相对时间戳
    • ReplaceTimestamp方法将所有时间戳转换为相对于合并后文件开始时间的相对时间
    1. 文件合并CombineMessages方法将所有文件的内容按时间顺序合并,并处理时间戳转换。
    1. 数据验证:在合并前后进行数据验证,确保合并过程中没有数据丢失。
    1. 错误处理:使用枚举类型ErrorCode处理各种可能的错误情况,确保程序的健壮性。

五、使用示例

下面是如何使用这个合并器的简单示例:

static void Main(string[] args)
{string sourcePath = @"C:\CAN\Source";string destinationPath = @"C:\CAN\Destination";var merger = new AscMerger(sourcePath, destinationPath);var result = merger.Merge();if (result.errorCode == ErrorCode.None){Console.WriteLine($"合并成功!文件保存至: {result.filePath}");}else{Console.WriteLine($"合并失败!错误码: {result.errorCode}");}
}

六、性能优化建议

对于处理大量或大型ASC文件的情况,可以考虑以下优化:

  1. 使用并行处理来加速文件读取
  2. 实现流式处理,避免将整个文件加载到内存中
  3. 添加进度报告功能,让用户了解合并进度
  4. 增加文件过滤功能,只合并特定时间段的文件

通过这种方式实现的CAN ASC文件合并器,不仅能够正确处理时间戳问题,还提供了完善的错误处理机制,确保合并过程的可靠性和数据的完整性。无论是用于汽车诊断、工业自动化还是其他CAN总线应用场景,这个工具都能帮助工程师更高效地处理和分析CAN数据。

http://www.lryc.cn/news/2404782.html

相关文章:

  • [TIP] Ubuntu 22.04 配置多个版本的 GCC 环境
  • 如何思考?分析篇
  • Redis:Hash数据类型
  • 抗辐照MCU在卫星载荷电机控制器中的实践探索
  • 快捷键的记录
  • Python读取阿里法拍网的html+解决登录cookie
  • electron-vite串口通信
  • 中山大学美团港科大提出首个音频驱动多人对话视频生成MultiTalk,输入一个音频和提示,即可生成对应唇部、音频交互视频。
  • Maven的配置与运行
  • MySQL 迁移至 Docker ,删除本地 mysql
  • redis分片集群架构
  • 关于物联网的基础知识(一)
  • 浏览器后台服务 vs 在线教育:QPS、并发模型与架构剖析
  • 电脑商城--用户注册登录
  • Riverpod与GetX的优缺点对比
  • Three.js怎么工作的?
  • LangChain面试内容整理-知识点1:LangChain架构与核心理念
  • 双面沉金线路板制作流程解析:高可靠性PCB的核心工艺
  • 什么是梯度磁场
  • 从零开始的python学习(七)P102+P103+P104+P105+P106+P107
  • Linux--进程的调度
  • Hadolint:Dockerfile 语法检查与最佳实践验证的终极工具
  • Python爬虫实战:研究Hyper 相关技术
  • 基于langchain的简单RAG的实现
  • VmWare Ubuntu22.04 搭建DPDK 20.11.1
  • selenium-自动更新谷歌浏览器驱动
  • 34、协程
  • Apache POI操作Excel详解
  • Docker容器部署elasticsearch8.*与Kibana8.*版本使用filebeat采集日志
  • OpenCV CUDA模块图像处理------双边滤波的GPU版本函数bilateralFilter()