
此 python 脚本使用由 tshark 导出的值组成的文本文件。此导出列严格由每个 canbus 有效负载组成,它是 5 字节十六进制值。 (10 个字符)此程序将 canbus 十六进制值转换为 kph 或 mph。
这是我用来从 canid589.pcap 中提取此信息的命令,该命令本身是从 wireshark 导出的对 canbus id 589(指速度)的剖析。还有 32 种其他不同类型的 canbus id,但我们目前不需要关心这些值。
┌──(kali㉿z3r0)-[/media/sf_shared_kali/ncl doc/scanningrecon] └─$ tshark -r canid589.pcap -t fields -e data.data > data_speed.txt
(-r) 读取现有的 pcap 文件,而 (-t fields) 指示 tshark 输出特定字段(而不是完整的数据包详细信息、摘要或原始数据)。这是一种自定义输出的方法,仅提取所需的信息,而不是转储所有数据包数据。 -e 选项用于指定要从数据包中提取哪些字段。在这种情况下,data.data表示每个数据包的数据字节。 “data.data”是指十六进制形式的 canbus 帧的实际内容(有效负载)。我必须尝试不同的值,直到将正确的数据导出到文本文件。
这里是与 can 协议相关的不同字段的列表。
这也可以针对每个数据包单独完成,但我有 352 个不同的 can.id =“589”(速度)数据包来迭代
def format_hex_value(hex_value):
# Tshark exported specific packets to column data.data unformatted.
return ' '.join(hex_value[i:i+2] for i in range(0, len(hex_value), 2))
def calculate_speed_from_hex_value(hex_value):
# 5 byte check
if len(hex_value) < 10:
raise ValueError("Hex value must have at least 10 characters for 5 bytes")
# Extract the relevant bytes from payload (the last two bytes)
high_byte = int(hex_value[-4:-2], 16)
low_byte = int(hex_value[-2:], 16)
speed = (high_byte << 8) + low_byte
# Example: 00 00 00 04 e1 - (04 << 8) + e1 = 1024 + 225 = 1249
# Convert speed from centi-KPH to KPH then to MPH
speed_kph = speed / 100.0 # Assuming the value is in centi-KPH
speed_mph = speed_kph * 0.621371 # Convert KPH to MPH
return speed_mph
def main():
speeds = []
with open('data_speed.txt', 'r') as file:
for line in file:
hex_value = line.strip()
if hex_value:
formatted_hex_value = format_hex_value(hex_value)
print(f"Formatted Hex Value: {formatted_hex_value}")
try:
# Calculate speed and store it in the speeds list
speed_mph = calculate_speed_from_hex_value(hex_value)
speeds.append(speed_mph)
print(f"Calculated Speed: {speed_mph:.2f} MPH")
except ValueError as e:
print(f"Error processing value '{hex_value}': {e}")
speeds.sort()
#Sort lowest to highest
print("\nFinal Sorted Speeds (MPH):")
for speed in speeds:
print(f"{speed:.2f} MPH")
if __name__ == "__main__":
main()
如果有人有任何问题、意见、补充或建设性批评,请随时与我联系。谢谢你
以上就是CAN总线速度转换器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号