matplotlib 阅读CAN .asc文件并从中绘制特定数据

4nkexdtk  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(210)

我对这门学科和python的东西还很陌生,我有一段代码,乍一看很好,运行起来也没有问题,但它并没有给我给予我期望的结果。

import pandas as pd
import matplotlib.pyplot as plt

# Step 1: Read the .asc file, ignoring the header and using varying spaces as separators
file_path = 'StartupElektroMode.asc'
data = []
try:
    with open(file_path, 'r') as file:
        lines = file.readlines()
except FileNotFoundError:
    print(f"Error: The file'{file_path}' was not found")
timestamps = []
voltage_data = []
current_data = []

for line in lines[6:]:
    parts = line.split()
    if len(parts) >= 3:
        timestamp = parts[0]
        can_id = parts[2]
        voltage_hex = None
        current_hex = None

    if can_id == '1E3' and len(parts) > 12:
        voltage_hex = parts[13]
     elif can_id == '4E3' and len(parts) > 13:
        current_hex = parts[12]

# Step 2: Convert the hex values to decimal
for item in data:
    if voltage_hex is not None:
        first_digit = int(voltage_hex[0], 16)
        second_digit = int(voltage_hex[1], 16)
        voltage = (first_digit * 256 + second_digit) * 0.25
        voltage_data.append(voltage)

    if current_hex is not None:
        first_digit = int(current_hex[0], 16)
        second_digit = int(current_hex[1], 16)
        current = (first_digit * 256 + second_digit) * 0.25
        current_data.append(current)

data = {'Time': timestamps, 'Voltage': voltage_data, 'Current': current_data}
# Step 3: Create Pandas DataFrame
df = pd.DataFrame(data)

# Step 4: Plot Voltage and Current over time
plt.figure(figsize=(12, 6))
plt.plot(timestamps, voltage_data, label='Voltage (1E3)')
plt.title('Voltage Over Time')
plt.xlabel('Time')
plt.ylabel('Voltage (V)')
plt.legend()
plt.grid()

plt.plot(timestamps, current_data, label='Current (4E3)')
plt.title('Current Over Time')
plt.xlabel('Time')
plt.ylabel('Current (A)')
plt.legend()
plt.grid()

plt.tight_layout()
plt.show()

file.close()

字符串
这里的文件的一部分,我试图读取

0.028121 1  CFF4912x        Rx   d 8 00 00 03 00 11 00 00 00  Length = 280152 BitCount = 144 ID = 218056978x
   0.029376 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.029492 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.031802 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.031918 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.034060 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239683 BitCount = 124 ID = 1302
   0.034176 1  206             Rx   d 1 04  Length = 107929 BitCount = 58 ID = 518
   0.036204 1  1E3             Rx   d 8 01 59 00 00 00 00 1A 05  Length = 235910 BitCount = 122 ID = 483
   0.036456 1  100             Rx   d 8 00 00 00 00 00 00 00 08  Length = 243683 BitCount = 126 ID = 256
   0.036703 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.036819 1  206             Rx   d 1 04  Length = 107680 BitCount = 58 ID = 518
   0.037249 1  2E3             Rx   d 8 C4 C3 00 64 19 15 01 01  Length = 221668 BitCount = 115 ID = 739
   0.038204 1  3E3             Rx   d 8 68 01 38 04 03 00 00 00  Length = 235910 BitCount = 122 ID = 995
   0.038885 1  144             Rx   d 8 00 00 00 00 00 00 01 00  Length = 241668 BitCount = 125 ID = 324
   0.039133 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.039249 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.039487 1  4E3             Rx   d 8 1E 00 8C 00 00 F8 FF 00  Length = 229910 BitCount = 119 ID = 1251
   0.041397 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.041513 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.043613 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.043729 1  206             Rx   d 1 04  Length = 107929 BitCount = 58 ID = 518
   0.045825 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.045941 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.046193 1  100             Rx   d 8 00 00 00 00 00 00 00 08  Length = 243668 BitCount = 126 ID = 256
   0.047126 1  CF00400x        Rx   d 8 F8 7D 7D 00 00 00 F0 7D  Length = 280167 BitCount = 144 ID = 217056256x


有什么建议我可以做什么或我的问题可能在哪里

wmomyfyw

wmomyfyw1#

您的电压和电流数据是在不同的帧中接收的,因此总是具有不同的时间戳。由于最终会得到很多NaN值,因此使用一个帧不太适合绘制数据。
构建与电压和电流并行的时间戳列表,并将它们绘制在一起(在一个或不同的图上)。另外,似乎你的十六进制转换是错误的(或者电流/电压应该分布在两个字节上?)我稍微修改了你的示例,使其具有多个数据点:

import pandas as pd
import matplotlib.pyplot as plt
from io import StringIO

file = StringIO(""""   0.028121 1  CFF4912x        Rx   d 8 00 00 03 00 11 00 00 00  Length = 280152 BitCount = 144 ID = 218056978x
   0.029376 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.029492 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.031802 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.031918 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.034060 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239683 BitCount = 124 ID = 1302
   0.034176 1  206             Rx   d 1 04  Length = 107929 BitCount = 58 ID = 518
   0.036204 1  1E3             Rx   d 8 01 59 00 00 00 00 1A 05  Length = 235910 BitCount = 122 ID = 483
   0.036456 1  100             Rx   d 8 00 00 00 00 00 00 00 08  Length = 243683 BitCount = 126 ID = 256
   0.036703 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.036819 1  206             Rx   d 1 04  Length = 107680 BitCount = 58 ID = 518
   0.037249 1  2E3             Rx   d 8 C4 C3 00 64 19 15 01 01  Length = 221668 BitCount = 115 ID = 739
   0.038204 1  3E3             Rx   d 8 68 01 38 04 03 00 00 00  Length = 235910 BitCount = 122 ID = 995
   0.038885 1  144             Rx   d 8 00 00 00 00 00 00 01 00  Length = 241668 BitCount = 125 ID = 324
   0.039133 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.039249 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.039487 1  4E3             Rx   d 8 1E 00 8C 00 00 F8 FF 00  Length = 229910 BitCount = 119 ID = 1251
   0.041397 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.041513 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.043613 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.043729 1  206             Rx   d 1 04  Length = 107929 BitCount = 58 ID = 518
   0.045825 1  516             Rx   d 8 00 00 00 00 00 00 00 00  Length = 239668 BitCount = 124 ID = 1302
   0.045941 1  206             Rx   d 1 04  Length = 107914 BitCount = 58 ID = 518
   0.046193 1  100             Rx   d 8 00 00 00 00 00 00 00 08  Length = 243668 BitCount = 126 ID = 256
   0.046194 1  1E3             Rx   d 8 01 59 00 00 00 00 1A 0F  Length = 235910 BitCount = 122 ID = 483
   0.046195 1  4E3             Rx   d 8 1E 00 8C 00 00 F8 2F 00  Length = 229910 BitCount = 119 ID = 1251
   0.047126 1  CF00400x        Rx   d 8 F8 7D 7D 00 00 00 F0 7D  Length = 280167 BitCount = 144 ID = 217056256x""")

lines = file.readlines()

t_volt = []
voltage_data = []
t_curr = []
current_data = []

for line in lines:
    parts = line.split()
    if len(parts) >= 3:
        timestamp = parts[0]
        can_id = parts[2]
        voltage_hex = None
        current_hex = None

    if can_id == '1E3' and len(parts) > 12:
        voltage_hex = parts[13]
    elif can_id == '4E3' and len(parts) > 13:
        current_hex = parts[12]

    # Convert the hex values to decimal
    if voltage_hex is not None:
        voltage = int(voltage_hex, 16) * 0.25
        voltage_data.append(voltage)
        t_volt.append(float(parts[0]))

    if current_hex is not None:
        current = int(current_hex, 16) * 0.25
        current_data.append(current)
        t_curr.append(float(parts[0]))

# Plot Voltage and Current over time
fig, ax = plt.subplots(figsize=(12, 6))
ax.plot(t_volt, voltage_data, label='Voltage (1E3)')
ax.set_title('Voltage and Current Over Time')
ax.set_xlabel('Time')
ax.set_ylabel('Voltage (V)')

ax2 = ax.twinx()
ax2.plot(t_curr, current_data, color='orange', label='Current (4E3)')
ax2.set_ylabel('Current (A)')

fig.legend()
ax.grid()

fig.tight_layout()
plt.show()

字符串
输出量:
x1c 0d1x的数据

相关问题