Skip to content

Commit

Permalink
Merge pull request #5 from hrjkknox/modularise
Browse files Browse the repository at this point in the history
Modularise
  • Loading branch information
hrjkknox authored Dec 16, 2021
2 parents a2e881a + f051b69 commit 1df39de
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 137 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,4 @@ This takes a file name, and returns its rows in a list.

## TODO

- Take code out of `__init__.py` and put it into a separate file

- Update `_extractDataFromPacket` to use the `dataLength` byte
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = canLogParse
version = 1.0.2
version = 1.0.3
author = Hamish Knox
author_email = hrjkknox@gmail.com
description = A tool to import and format data from Kvaser CAN files when you dont have a DBC file.
Expand Down
135 changes: 1 addition & 134 deletions src/canLogParse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,134 +1 @@
import csv

def _fileToList(file):
with open(file) as readFile:
lines = readFile.readlines()
return lines

# Takes a part-formed packet array and returns the data
def _extractDataFromPacket(packet):
dataLength = int(packet[2])
# Remove all non-data bytes
# I know this is janky, but I'm doing this quickly
npacket = packet
npacket.pop(0)
npacket.pop(0)
npacket.pop(0)
npacket.pop(-1)
npacket.pop(-1)

# Convert the array of strings to an array of bytes
dataArray = []
for i in npacket:
dataArray.append(str(i))

return dataArray

# Turns the packet into a nice dictionary
def _formatPacketDict(leadingZero, id, dataLength, data, tr, timeStamp):
outputDict = {}
outputDict["leadingZero"] = leadingZero
outputDict["id"] = id
outputDict["dataLength"] = dataLength
outputDict["data"] = data
outputDict["T/R"] = tr
outputDict["timeStamp"] = timeStamp

return outputDict

# Turns the data into a nice list
def _formatPacketList(leadingZero, id, dataLength, data, tr, timeStamp):
outputArr = []
outputArr.append(leadingZero)
outputArr.append(id)
outputArr.append(dataLength)
outputArr.append(data)
outputArr.append(tr)
outputArr.append(timeStamp)

return outputArr

# Turns the data into a nice tuple
def _formatPacketTuple(leadingZero, id, dataLength, data, tr, timeStamp):
packetArray = _formatPacketList(leadingZero, id, dataLength, data, tr, timeStamp)
packetTuple = tuple(packetArray)

return packetTuple

# Formats the packet into a nice format
def _formatPacket(leadingZero, id, dataLength, data, tr, timeStamp, outputFormat="2dArray"):
if outputFormat == "dict":
# Construct a dictionary with all of the data
output = _formatPacketDict(leadingZero, id, dataLength, data, tr, timeStamp)
elif outputFormat == "array":
# Construct an array with all of the data
output = _formatPacketList(leadingZero, id, dataLength, data, tr, timeStamp)
elif outputFormat == "tuple":
# Construct a tuple with all of the data
output = _formatPacketTuple(leadingZero, id, dataLength, data, tr, timeStamp)

return output

# Take a raw packet line and format it into something more useful
def parsePacket(rawPacket, outputFormat="array"):
packet = rawPacket.split()

# There's always a "logging stopped" line at the end
if packet[0] == "Logging":
return None

# The leading zero at the start of the packet
leadingZero = int(packet[0])
# The ID of the packet
id = packet[1]
# The length of the actual data
dataLength = int(packet[2])
# The transmit/receive byte
tr = packet[-1]
# The timestamp of the packet
timeStamp = float(packet[-2])
# The actual bytes of data
data = _extractDataFromPacket(packet)

formattedPacket = _formatPacket(leadingZero, id, dataLength, data, tr, timeStamp, outputFormat=outputFormat)

return formattedPacket

# Take the contents of a CAN log and format it into something more useful
def parseCanData(rawData, outputFormat="array"):
# The output array
output = []

# Loop through every packet logged
for rawPacket in rawData:
formattedPacket = parsePacket(rawPacket, outputFormat=outputFormat)
if formattedPacket == None:
return output
output.append(formattedPacket)

return output

# Take a CAN log file and format it into something more useful
def importCanLogFile(file, outputFormat="array"):
rawCanData = _fileToList(file)
output = parseCanData(rawCanData, outputFormat=outputFormat)
return output

# Given a 2D array of packets, finds all unique IDs
def findUniqueIDs(packets):
allIDs = []
# Loop through all of the packets
for packet in packets:
# Append the ID to allIDs
allIDs.append(packet[1])

uniqueIDs = set(allIDs)
return list(uniqueIDs)

# Export a log in 2D array format to a csv file
def exportLogToCSV(log, filename):
with open(filename, "w") as csvfile:
logWriter = csv.writer(csvfile)
for i in log:
logWriter.writerow(i)

from parse import *
65 changes: 65 additions & 0 deletions src/canLogParse/parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import utils
import csv

# Take a raw packet line and format it into something more useful
def parsePacket(rawPacket, outputFormat="array"):
packet = rawPacket.split()

# There's always a "logging stopped" line at the end
if packet[0] == "Logging":
return None

# The leading zero at the start of the packet
leadingZero = int(packet[0])
# The ID of the packet
id = packet[1]
# The length of the actual data
dataLength = int(packet[2])
# The transmit/receive byte
tr = packet[-1]
# The timestamp of the packet
timeStamp = float(packet[-2])
# The actual bytes of data
data = utils.extractDataFromPacket(packet)

formattedPacket = utils.formatPacket(leadingZero, id, dataLength, data, tr, timeStamp, outputFormat=outputFormat)

return formattedPacket

# Take the contents of a CAN log and format it into something more useful
def parseCanData(rawData, outputFormat="array"):
# The output array
output = []

# Loop through every packet logged
for rawPacket in rawData:
formattedPacket = parsePacket(rawPacket, outputFormat=outputFormat)
if formattedPacket == None:
return output
output.append(formattedPacket)

return output

# Given a 2D array of packets, finds all unique IDs
def findUniqueIDs(packets):
allIDs = []
# Loop through all of the packets
for packet in packets:
# Append the ID to allIDs
allIDs.append(packet[1])

uniqueIDs = set(allIDs)
return list(uniqueIDs)

# Export a log in 2D array format to a csv file
def exportLogToCSV(log, filename):
with open(filename, "w") as csvfile:
logWriter = csv.writer(csvfile)
for i in log:
logWriter.writerow(i)

# Take a CAN log file and format it into something more useful
def importCanLogFile(file, outputFormat="array"):
rawCanData = utils.fileToList(file)
output = parseCanData(rawCanData, outputFormat=outputFormat)
return output
68 changes: 68 additions & 0 deletions src/canLogParse/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
def fileToList(file):
with open(file) as readFile:
lines = readFile.readlines()
return lines

# Takes a part-formed packet array and returns the data
def extractDataFromPacket(packet):
dataLength = int(packet[2])
# Remove all non-data bytes
# I know this is janky, but I'm doing this quickly
npacket = packet
npacket.pop(0)
npacket.pop(0)
npacket.pop(0)
npacket.pop(-1)
npacket.pop(-1)

# Convert the array of strings to an array of bytes
dataArray = []
for i in npacket:
dataArray.append(str(i))

return dataArray

# Turns the packet into a nice dictionary
def formatPacketDict(leadingZero, id, dataLength, data, tr, timeStamp):
outputDict = {}
outputDict["leadingZero"] = leadingZero
outputDict["id"] = id
outputDict["dataLength"] = dataLength
outputDict["data"] = data
outputDict["T/R"] = tr
outputDict["timeStamp"] = timeStamp

return outputDict

# Turns the data into a nice list
def formatPacketList(leadingZero, id, dataLength, data, tr, timeStamp):
outputArr = []
outputArr.append(leadingZero)
outputArr.append(id)
outputArr.append(dataLength)
outputArr.append(data)
outputArr.append(tr)
outputArr.append(timeStamp)

return outputArr

# Turns the data into a nice tuple
def formatPacketTuple(leadingZero, id, dataLength, data, tr, timeStamp):
packetArray = formatPacketList(leadingZero, id, dataLength, data, tr, timeStamp)
packetTuple = tuple(packetArray)

return packetTuple

# Formats the packet into a nice format
def formatPacket(leadingZero, id, dataLength, data, tr, timeStamp, outputFormat="2dArray"):
if outputFormat == "dict":
# Construct a dictionary with all of the data
output = formatPacketDict(leadingZero, id, dataLength, data, tr, timeStamp)
elif outputFormat == "array":
# Construct an array with all of the data
output = formatPacketList(leadingZero, id, dataLength, data, tr, timeStamp)
elif outputFormat == "tuple":
# Construct a tuple with all of the data
output = formatPacketTuple(leadingZero, id, dataLength, data, tr, timeStamp)

return output

0 comments on commit 1df39de

Please sign in to comment.