Writing a script for just so basic things every day... 
That's exactly where scripts are for, doing basic things all day that you don't want to do manually...
I spend many bucks to have a tool to give me some basic ouput like "ID, databytes, ackbit". I guess i have to spend even more bucks? Why are so often simple basic things not implemented/working? I want to write on my firmware, and not scripts. And thats not all: i wrote a script now, but still i have to do stuff by hand...
This is my script, which converts a rather usless (in my case) list (why do the columns of each row even have no relation at all?):
import os
import glob
import time
import re
# Define the pattern and the timeframe (in seconds)
pattern = 'decoder--*.csv' # Change to your desired pattern
timeframe = 1 # 1 hour in seconds
output_file_path = 'currentFrames.csv'
# Get the absolute path of the current script
current_script_path = os.path.abspath(__file__)
# Get the base path (directory) of the current script
basepath = os.path.dirname(current_script_path)
print("Script running")
global newestFile
while 1:
# Get the current time
current_time = time.time()
# Find files matching the pattern
files = glob.glob(pattern)
newestFile = None
# Filter for recently created file
for file in files:
try:
# Get the creation time (on Windows) or last modification time (on Unix-like systems)
creation_time = os.path.getctime(f"{basepath}\\\\{file}") # Use os.path.getmtime(file) for Unix-like systems
except:
print(f"EXEPT: {file} ")
if current_time - creation_time <= timeframe:
newestFile = open(file, 'r')
if not newestFile:
continue
#wait until the file has been written
time.sleep(3)
# Open the output file for writing
output_file = open(output_file_path, 'w')
global lines
startPattern = r'[0-9]+,(.*),Start of frame'
idPattern = r'^.+Identifier: [0-9]+ \((0x[0-9a-f]+)\)'
lengthPattern = r'.+Data length code: ([0-9]+)'
dataBytePattern = r'^.+Data byte [0-9]+: 0x([0-9a-f]+)'
ackPattern = r'.*ACK slot: (.+)$'
# Open the recent file
lines = newestFile.readlines()
global startTime
global dataBytes
for line in lines:
timeMatch = re.search(startPattern, line)
if timeMatch:
startTime = float(timeMatch.group(1))/1000/1000
sTimeString = (f"{startTime:.3f};").replace('.',',')
output_file.write(sTimeString)
dataBytes = None
idMatch = re.search(idPattern, line)
if idMatch:
id = int(idMatch.group(1),16)
output_file.write(f"{id};")
lengthMatch = re.search(lengthPattern, line)
if lengthMatch:
length = lengthMatch.group(1)
#print("")
dataMatch = re.search(dataBytePattern, line)
if dataMatch:
dataByte = int(dataMatch.group(1),16)
output_file.write(f"{dataByte};")
ackMatch = re.search(ackPattern, line)
if ackMatch:
output_file.write(f"{ackMatch.group(1)}\n")
output_file.close()
The source file looked like this:
Id,Time[ns],CAN-FD: Fields
1,3756800.00,Start of frame
2,3758800.00,Identifier: 36 (0x24)
3,3782800.00,Remote transmission request: data frame
4,3784800.00,Identifier extension bit: standard frame
5,3786800.00,Flexible data format: 0
6,3790800.00,Data length code: 8
7,3798800.00,Data byte 0: 0x11
8,3816800.00,Data byte 1: 0xfd
9,3834800.00,Data byte 2: 0xbe
10,3852800.00,Data byte 3: 0xb4
11,3868800.00,Data byte 4: 0x6a
12,3884800.00,Data byte 5: 0x33
13,3900800.00,Data byte 6: 0x95
14,3916800.00,Data byte 7: 0x5d
15,3932800.00,CRC-15 sequence: 0x0c70
16,3962800.00,CRC delimiter: 1
17,3965400.00,ACK slot: ACK
18,3967400.00,ACK delimiter: 1
19,3969400.00,End of frame
This is what i wanted:
