B4J Question CAN DBC file parser

Didier99

Member
I am in the process of writing a CAN DBC database parser so of course first approach is to Google it to see if there is one already. On first look, I have found one in Java (CANBabel on GitHub) but not much in Visual Basic or another language that would be easier to convert into B4J.
I know I should learn how to create a B4J library from the GitHub Java code, but as always, I do not have the time to learn to do that now...

Any suggestion appreciated.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm not really familiar with CAN DBC, however maybe this example, based on PyBridge, will help you get started.
It is a port of: https://github.com/cantools/cantools/blob/master/examples/hello_world.py

Installation:
pip install cantools

B4X:
    Dim cantools_db As PyWrapper = Py.ImportModule("cantools").GetField("database")
    Dim db As PyWrapper = cantools_db.Run("load_string").Arg(File.ReadString(File.DirAssets, "motohawk.dbc"))
    Dim message As Map = CreateMap("Temperature": 250.1, _
        "AverageRadius": 3.2, _
        "Enable": "Enabled")
    Dim encoded As PyWrapper = db.Run("encode_message").Arg("ExampleMessage").Arg(message)
    Dim decoded As PyWrapper = db.Run("decode_message").Arg("ExampleMessage").Arg(encoded)
    decoded.Print
    Wait For (encoded.Fetch) Complete (encoded As PyWrapper)
    Dim b() As Byte = encoded.Value
    Dim bc As ByteConverter
    Log(bc.HexFromBytes(b))
 

Attachments

  • Project.zip
    3.9 KB · Views: 19
Upvote 0

Didier99

Member
I'm not really familiar with CAN DBC, however maybe this example, based on PyBridge, will help you get started.
It is a port of: https://github.com/cantools/cantools/blob/master/examples/hello_world.py

Installation:
pip install cantools

B4X:
    Dim cantools_db As PyWrapper = Py.ImportModule("cantools").GetField("database")
    Dim db As PyWrapper = cantools_db.Run("load_string").Arg(File.ReadString(File.DirAssets, "motohawk.dbc"))
    Dim message As Map = CreateMap("Temperature": 250.1, _
        "AverageRadius": 3.2, _
        "Enable": "Enabled")
    Dim encoded As PyWrapper = db.Run("encode_message").Arg("ExampleMessage").Arg(message)
    Dim decoded As PyWrapper = db.Run("decode_message").Arg("ExampleMessage").Arg(encoded)
    decoded.Print
    Wait For (encoded.Fetch) Complete (encoded As PyWrapper)
    Dim b() As Byte = encoded.Value
    Dim bc As ByteConverter
    Log(bc.HexFromBytes(b))
Thank you Erel, that will definitely help!
 
Upvote 0

Didier99

Member
OK, I must admit to be very unfamiliar with python, but I
1) installed Python3
2) installed cantools
\user\AppData\Local\Programs\Python\Python313\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed argparse_addons-0.12.0 bitstruct-8.21.0 cantools-40.2.2 crccheck-1.3.0 diskcache-5.6.3 packaging-25.0 python-can-4.5.0 textparser-0.24.0 typing_extensions-4.14.0 wrapt-1.17.2
3) downloaded the Bleak library that includes a library that I apparently also needed

So the IDE is happy with the project but when I try to run it I get this error:
1750282297350.png

B4J Version: 10.20
Parsing code. (0.03s)
Java Version: 8
Building folders structure. (0.14s)
Compiling code. (0.60s)
Compiling layouts code. (0.01s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
javac 1.8.0_321
src\b4j\example\pyutils.java:1286: error: cannot find symbol
public static void registerMultipleKeys(java.util.List<Object> objects, java.util.List<Integer> keys, java.lang.ref.Cleaner cleaner) {
^
symbol: class Cleaner
location: package java.lang.ref
1 error1750282297350.png

Should I add the PATH recommended in the installation of cantools?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top