乐于分享
好东西不私藏

用AI写一个解析CANoe的ASC日志软件-从文档开始

用AI写一个解析CANoe的ASC日志软件-从文档开始

一直都想有一个工具软件能脱离CANoe能比较方便的打开xx.asc的LIN日志文件.现在AI比较厉害,对于没有啥软件经验也可以使用AI写一个软件解析LIN日志文件.
LIN日志文件格式说明可以在CANoe的安装目录找到,上传了一份到百度云:LIN_ASC_Format.pdf;    https://pan.baidu.com/s/1Izg6YGwJRvp4PzJqMepdQw 提取码: eekh
文档解读
这个官方文档的内容是用英文写的,第一步可以先让AI总结一下这篇文档关于报文格式的详细说明:

定义了三大类 LIN报文:文档详细规定了如何用文本记录各种 LIN 总线活动的信息记录.

  • 报文事件:正常的 LIN 帧收发。

  • 错误事件:传输错误、接收错误、同步错误、校验和错误等。

  • 信息/状态事件:波特率变化、休眠/唤醒、调度表切换等。

  • 正常报文事件

0.073973 Li 2d Tx 8 00 f0 f0 ff ff ff ff ff checksum = 70 header time = 40, full time = 130 SOF = 0.067195 BR = 19230 break = 937125 114062 EOH = 0.069266 EOB = 0.069789 0.070312 0.070835 0.071358 0.071881 0.072404 0.072927 0.073450 sim = 1 EOF = 0.073973 RBR = 19231 HBR = 19230.769231 HSO = 26000 RSO = 26000 CSM = enhanced
正常报文字段说明
  • 错误事件
    1.传输错误(TransmErr)
    触发条件:主节点发送帧头后,无任何从节点返回响应。
        报文格式如下:
    <Time> <Channel> <ID> TransmErr (slave = <slaveid>, state = <state>) header time = <headertime>, full time = <fulltime> SOF = <startofframe> BR = <baudrate> break = <SyncBreak> <SyncDel> (subId = <NAD> <MessageId> <SupplierId>) EOH = <endofheader> HBR = <headerbaudrate> HSO = <stopbitoffsetinheader> CSM = <checksummodel>
        实际报文:
    0.424674 Li 33 TransmErr header time = 40, full time = 166 SOF = 0.416054 BR = 19230 break = 937187 113250 EOH = 0.418122 HBR = 19230.769231 HSO = 26000 CSM = enhanced
    2.接收错误(RcvError)  
        触发条件:包含多种场景:同步间隔过短、同步字段错误、ID 奇偶错误、从节点非法字符、帧错误、数据不匹配、帧不完整等.
             报文格式如下:
<Time> <Channel> (<ID> <DLC>) RcvError: <description> (char = <offendingbyte>) (slave = <slaveid>, state = <state>) StateReason = <StateReason> ShortError = <IsShortError> DlcTimeout = <IsDLCTimeout> HasDatabytes = <HasDatabytes> (<D0>...<D7>) SOF = <startofframe> BR = <baudrate> break = <SyncBreak> <SyncDel> (subId = <NAD> <MessageId> <SupplierId>) (EOH = <endofheader>) (EOB = <T0> … <T7>) RBR = <responsebaudrate> HBR = <headerbaudrate> HSO = <stopbitoffsetinheader> RSO = <stopbitoffsetinresponse> CSM = <checksummodel>
          实际报文:
0.554673 Li 33 8 RcvError: timeout while waiting for checksum field StateReason = 0c ShortError = 0 DlcTimeout = 0 HasDatabytes = 1 05 00 00 00 00 ff ff ff SOF = 0.546052 BR = 19230 break = 937187 112437 EOH = 0.548121 EOB = 0.548644 0.549167 0.549690 0.550213 0.550736 0.551259 0.551782 0.552305 RBR = 19231 RSO = 26000 HBR = 19230.769231 HSO = 26000 CSM = enhanced
    3.同步错误(SyncError)
        触发条件:仅外部主节点模式下,硬件无法同步(波特率偏差 > 15% 或同步字段0x55损坏)

       报文格式:

<Time> <Channel> SyncError <TimeInterval0>..<TimeInterval3> SOF = <startofframe> BR = <baudrate> break = <SyncBreak> <SyncDel>

        实际报文:

2.022336 L2 SyncError 208 0 0 0 SOF = 2.021077 BR = 19230 break = 937125 113312

    4.校验和错误(CSErr)

        触发条件:从节点发送的响应帧结构完整,但校验和计算错误.

报文格式:

<Time> <Channel> <ID> CSErr <Dir> <DLC> <D0>...<D7> (slave = <slaveid>, state = <state>) checksum = <checksum> header time = <headertime>, full time = <fulltime> SOF = <startofframe> BR = <baudrate> break = <SyncBreak> <SyncDel> (subId = <NAD> <MessageId> <SupplierId>) EOH = <endofheader> EOB = <T0> … <T7> sim = <simulated> EOF = <endofframe> RBR = <responsebaudrate> HBR = <headerbaudrate> HSO = <stopbitoffsetinheader> RSO = <stopbitoffsetinresponse> CSM = <unknown|classic|enhanced|error>

        实际报文:

0.462829 Li 33 CSErr Tx 8 05 00 00 00 00 ff ff ff checksum = 86 header time = 40, full time = 130 SOF = 0.456050 BR = 19230 break = 937187 114875 EOH = 0.458122 EOB = 0.458645 0.459168 0.459691 0.460214 0.460737 0.461260 0.461783 0.462306 sim = 1 EOF = 0.462829 RBR = 19231 HBR = 19230.769231 HSO = 26000 RSO = 26000 CSM = enhanced

    5.尖峰事件(Spike)

        触发条件:检测到总线上持续时间小于 1 位时间的显性干扰信号.

 报文格式:

<Time> <Channel> Spike <Dir> <SpikeLength> microseconds SOF = <startofframe> BR = <baudrate>

      实际报文:

5.990958 L2 Spike Rx 56 microseconds SOF = 5.990902 BR = 9615
  • LIN 睡眠 / 唤醒事件(LIN Sleep/Wakeup Events)

        触发条件:LIN 硬件在唤醒 / 睡眠模式之间切换时生成

 报文格式:

<Time> <Channel> SleepModeEvent <simulated> <descriptiontext><Time> <Channel> Unexpected wakeup: approx. <Width> us SOF = <startofframe> BR = <baudrate>

         实际报文:

0.7772 Li SleepModeEvent 0 entering sleep mode due to sleep mode frame
0.892363 Li Unexpected wakeup: approx. 260 us SOF = 0.891843 BR = 19230

 提取文档所有示例报文

向豆包/DeepSeek输入提取所有的示例报文到一个文件即可:

将示例报文丢给AI

之所以用示例报文,是因为包含的测试用例比较全面。接下来只需要告诉AI:C++解析测试报文,思考过程如下:

思考过程考虑了到每一种类型的帧的解析,还是挺不错的.

核心代码如下:

bool parseMessageLine(const std::string& line, std::vector<LINMessage>& messages) {        std::regex msgRegex(R"(^([\d.]+)\s+(Li|L\d)\s+([0-9a-fA-F]{2})\s+(Tx|Rx)\s+(\d+)\s+([0-9a-fA-F\s]+)\s+checksum\s*=\s*([0-9a-fA-F]+))");        std::smatch match;        if (std::regex_search(line, match, msgRegex)) {            LINMessage msg;            msg.timestamp = std::stod(match[1]);            msg.channel = match[2];            std::string idStr = match[3];            msg.id = std::stoi(idStr, nullptr, 16);            msg.direction = match[4];            msg.dlc = std::stoi(match[5]);            std::string dataStr = match[6];            std::istringstream dataStream(dataStr);            unsigned int byte;            while (dataStream >> std::hex >> byte) {                msg.data.push_back(static_cast<unsigned char>(byte));            }            msg.attributes["checksum"] = match[7];            extractAttributes(line, msg.attributes);            messages.push_back(msg);            return true;        }        return false;    }    bool parseErrorLine(const std::string& line, std::vector<LINError>& errors) {        std::regex patterns[] = {            std::regex(R"(^([\d.]+)\s+(Li|L\d)\s+\w+\s+TransmErr)"),            std::regex(R"(^([\d.]+)\s+(Li|L\d)\s+\w+\s+\d+\s+RcvError)"),            std::regex(R"(^([\d.]+)\s+(L\d)\s+SyncError)"),            std::regex(R"(^([\d.]+)\s+(Li|L\d)\s+\w+\s+CSErr)"),            std::regex(R"(^([\d.]+)\s+(L\d)\s+Spike)"),            std::regex(R"(^([\d.]+)\s+(Li)\s+Dominant\s+signal)")        };

当然也可以指定AI用其他的编程语言,如使用前端编程,界面漂亮还免安装.

总结
本文针对 CANoe的LIN ASC官方文档,给出一套 AI 辅助的轻量化解析LIN的 ASC日志的方案。核心工作流程分为三步:一是解读官方 LIN-ASC 格式文档,明确各类报文的格式规则与字段定义;二是提取文档内全场景示例报文,作为完整测试用例;三是依托 AI 快速生成解析代码,实现脱离 CANoe 的日志解析能力。后续可结合 LDF 文件拓展信号解析、数据回放功能,提升工具的工程实用性。