如何将 can data blf 文件转换为 pcap 文件

问题描述 投票:0回答:1

`如何将 can data blf 文件转换为 pcap 文件?

我需要将此文件中存在的所有数据包转换为 pcap 文件。这是我尝试过的示例,它没有错误,但也没有结果。尝试打开 blf 文件 abd 打印数据,但这也失败了。我知道 blf 文件中有数据。 `

import can
import dpkt
import datetime

blf_file_path = r'c:\Users\sahaanan\Desktop\blftopcap\NewSW_7209_Lines.blf' 
pcap_file_path = r'c:\Users\sahaanan\Desktop\blftopcap\output.pcap'    

# Open the BLF file for reading
with can.BLFReader(blf_file_path) as log:
    # Open the PCAP file for writing
    with open(pcap_file_path, "wb") as pcap_file:
        pcap_writer = dpkt.pcap.Writer(pcap_file)

        for entry in log:
            timestamp = entry.timestamp
            data = bytes(entry.data)

            # Print information about each entry
            print(f"Timestamp: {timestamp}, Data Length: {len(data)}")
    
            # Create a PCAP packet with timestamp
            pcap_packet = (timestamp, data)

            # Write the PCAP packet to the PCAP file
            pcap_writer.writepkt(pcap_packet)
            print("Conversion complete.")

代码运行没有任何错误,但没有输出我也尝试使用“try”和“Exception”进行错误处理,但输出是相同的。

python pcap dpkt python-can
1个回答
0
投票

这是代码的更正版本:

import can
import dpkt
import datetime

blf_file_path = 'path_to_your_blf_file.blf'  
pcap_file_path = 'output.pcap' 

with can.BLFReader(blf_file_path) as log:
    # Open the PCAP file for writing
    with open(pcap_file_path, 'wb') as pcap_file:
        pcap_writer = dpkt.pcap.Writer(pcap_file)

        for entry in log:
            timestamp = entry.timestamp
            data = bytes(entry.data)

            # Calculate the timestamp in seconds and microseconds
            ts_sec = int(timestamp)
            ts_usec = int((timestamp - ts_sec) * 1e6)

           
            pcap_packet = (ts_sec, ts_usec, data)

        
            pcap_writer.writepkt(pcap_packet)
    
        print('Conversion complete.')

确保安装了必要的软件包(can、dpkt)并且输入和输出文件的路径正确。

© www.soinside.com 2019 - 2024. All rights reserved.