I'm writing a python script to convert .DBC
files (from Datasus) into .CSV
.
I've searched a lot some libs to help me but didn't get success with any of them. Do someone know an option/package which could be useful in this conversion task?
I'm writing a python script to convert .DBC
files (from Datasus) into .CSV
.
I've searched a lot some libs to help me but didn't get success with any of them. Do someone know an option/package which could be useful in this conversion task?
I really don't understand why some members give -1 to the question, but don't really answer it.
Well, I've not found out a good lib to convert .DBC
to .CSV
.
But I could do it using Python + R and I'm sharing here how I achieved it.
The Python script below gets R path and executes the dbc2csv.R
file sending three args: the raw file path, converted file path, and the file name that will be converted.
import subprocess
import commands
def dbc2csv(raw_filename):
dbc2csv_path = "/path/to/script/dbc2csv.R " + raw_files_dir + " " + converted_files_dir + " " + raw_filename
try:
r_script_path = commands.getstatusoutput('which Rscript')[1]
subprocess.call(r_script_path + " --vanilla " + dbc2csv_path, shell=True)
return True
except:
print("(Rscript) Error converting file: " + raw_filename)
return False
The R script dbc2csv.R
bellow is actually who will convert.
#install.packages("read.dbc") You need this package
library("read.dbc")
dbc2dbf <- function(rawDir, convertedDir, file) {
# reads dbc file
x <- read.dbc(paste(rawDir, file, sep=""))
# write it to csv
write.csv(x, file=paste(convertedDir, file, ".csv", sep=""))
}
args = commandArgs(trailingOnly=TRUE)
try(dbc2dbf(args[1], args[2], args[3]), TRUE)
We do know it would be better if we could convert using just Python. But this approach will work fine.
If you're using python on Linux, one of the alternatives is using the project located at this repository: https://github.com/greatjapa/dbc2csv
It works by creating a docker that runs the bash script followed by the python script (you can also run them separately, without using docker, and that will work just as well).
It's also worth noting that the .dbc extension used by Datasus is one that they created themselves (not related to the .dbc used by cantools and other such tools), which just helps it being a pain to work with.
In python there is this awesome library https://pypi.org/project/cantools/
You can go through the source code of it to find the fields you need to extract : https://github.com/eerimoq/cantools/tree/f120d35e1523201b6d87e87306457bf1769330e3
All relevant fields for messages and signals can be found out in cantools/database/can/message.py and /database/can/signal.py in this github link
For eg: if you need names and unit of signals you could use signal.name and signal.unit
Sample code to extract these fields:
#**dbc_file** is the full path of dbc
db = cantools.database.load_file(dbc_file)
for msg in db.messages:
msg_name = msg.name
msg_id = msg.frame_id
msg_length = msg.length
sender = msg.senders
msg_group = db.get_message_by_name(msg_name)
for signal in msg_group.signals:
#name of signal
sig_name = signal.name
#unit of signal
sig_unit = signal.unit
Hope this might help someone!!
I don't know of anything that does an actual conversion, but assuming you mean the DBC BUS format, there's a Python library to read that: https://pypi.org/project/cantools/
And Python has built-in tools for writing CSVs: https://docs.python.org/3/library/csv.html
It should take only a handful of lines to combine those libraries into a converter.
Convert it to XLS (microsoft excel) and from there you can get it to .csv, I am ubuntu guy but I believe there should be a export option. or at least there is in LibreOffice
© 2022 - 2024 — McMap. All rights reserved.