Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Programmatically import, edit and export DBC files

+5
−0

I am looking for a way to programmatically edit and save .dbc files that are meant for J1939 CAN communication. I have a few large files that need to be compared/edited.

Being able to import, edit and export DBC files in a program could greatly speed this up.

There is a neat .NET DBC file parser on git that imports the DBC file and gives you all the data you need, unfortunately this library cannot export the data back to DBC.

So basically I am looking for a library that can create DBC files from data.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Requirements? (5 comments)

1 answer

+5
−0

Credit go to @Lundin‭ for pointing me in the right direction. Kvaser has a CAN SDK free of charge. I added this to my VS22 C# project by adding "...\Kvaser\Canlib\dotnet\x64\netstandard2.0\Kvaser.KvaDbLib.dll" and "...\Kvaser\Canlib\dotnet\x64\netstandard2.0\Kvaser.CanLib.dll" to my project references.

AND (this I was stuck on)

Copying "...\Kvaser\Canlib\bin_x64\kvaDbLib.dll" to your application folder (where your executable is).

I ported a C example application from Kvaser to C# and it seems to work:

EDIT:
(Vector CANdb++) If you get unspecified syntax errors: make sure all message/signal names start with a letter and contain only letters, numbers and underscore!

(Vector CANdb++) If you get the error: "unknown symbol newly defined" make sure your are not defining two messages with e.g. the same ID.

If you want to live debug human readable J1939 messages, with offsets included, use Busmaster, it can convert the DBC file to DBF and then use the DBF to interpret the messages.

using kv = Kvaser.KvadbLib.Kvadblib;

List<string> VFrameFormat_enum_names = new() { "StandardCAN", "ExtendedCAN", "reserved", "J1939PG" };

void add_msg_to_db(kv.Hnd dbHnd, string msgName, int dlc, int msgID, out kv.MessageHnd msgHnd)
{
    kv.AddMsg(dbHnd, out msgHnd);
    kv.SetMsgName(msgHnd, msgName);
    kv.SetMsgDlc(msgHnd, dlc);
    kv.SetMsgIdEx(msgHnd, msgID);
    kv.SetMsgFlags(msgHnd, kv.MESSAGE.J1939);
}

void add_sig_to_msg
(
    kv.MessageHnd msgHnd, 
    string sigName, 
    string comment, 
    string unit, 
    kv.SignalEncoding encoding, 
    kv.SignalType type, 
    byte startBit, 
    byte length,
    double minVal,
    double maxVal,
    out kv.SignalHnd sigHnd
)
{
    kv.AddSignal(msgHnd, out sigHnd);
    kv.SetSignalName(sigHnd, sigName);
    kv.SetSignalComment(sigHnd, comment);
    kv.SetSignalUnit(sigHnd, unit);
    kv.SetSignalEncoding(sigHnd, encoding);
    kv.SetSignalRepresentationType(sigHnd, type);
    kv.SetSignalValueSize(sigHnd, startBit, length);
    kv.SetSignalValueLimits(sigHnd, minVal, maxVal);
}

kv.Open(out kv.Hnd dbHnd);
kv.Create(dbHnd, "local", null);

kv.AddAttributeDefinition(dbHnd, out kv.AttributeDefHnd ProtocolType_attr);
kv.SetAttributeDefinitionType(ProtocolType_attr, kv.AttributeType.AttributeString);
kv.SetAttributeDefinitionName(ProtocolType_attr, "ProtocolType");
kv.SetAttributeDefinitionOwner(ProtocolType_attr, kv.AttributeOwner.AttributeOwnerDb);
kv.SetAttributeDefinitionString(ProtocolType_attr, "J1939");

// This part sometimes gave me errors when opening with Vector's CANdb++ or Kvaser's database editor 3, leaving it out did not make a difference for me.
//kv.AddAttributeDefinition(dbHnd, out kv.AttributeDefHnd //VFrameFormat_attr);
//kv.SetAttributeDefinitionType(VFrameFormat_attr, //kv.AttributeType.AttributeEnumeration);
//kv.SetAttributeDefinitionName(VFrameFormat_attr, "VFrameFormat");
//kv.SetAttributeDefinitionOwner(VFrameFormat_attr, //kv.AttributeOwner.AttributeOwnerMessage);
//for (int i = 0; i < 4; i++)
    //kv.SetAttributeDefinitionString(VFrameFormat_attr, //VFrameFormat_enum_names[i], i);

add_msg_to_db(dbHnd, "TEST_MSG_000", 8, 0xC0FFEE, out kv.MessageHnd msgHand);
add_sig_to_msg(msgHand, "TEST_SIG_000", "I am a comment", "banana", kv.SignalEncoding.Intel, kv.SignalType.Unsigned, 0, 16, 0, 65535, out kv.SignalHnd sigHnd);

kv.WriteFile(dbHnd, "./testDBC.dbc");
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »