<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Robert, Colin,<div class=""><br class=""></div><div class="">Thanks for your feedback. Very helpful, and I think I’ve got something workable now.</div><div class=""><br class=""></div><div class="">I found some more reference implementations (including Robert’s openpilot) and they all seem to use 0=big_endian and 1=little_endian. Either the specification is wrong, or everyone is ignoring it in the DBC’s they make.</div><div class=""><br class=""></div><div class="">I coded up something that seems to work (based on a few open source approaches). Haven’t tested it much, but will do more over the coming few days. I’ve committed what I’ve done so far, and it looks pretty good. Some notes on the (long list) of changes made in the past few days:</div><div class=""><br class=""></div><div class=""><ol class="MailOutline"><li class="">Support for both standard and extended message IDs added.<br class=""><br class=""></li><li class="">Re-worked everything to use the MyDBC memory-loaded DBC files. This includes retools, OvmsVehicleDBC, etc.<br class=""><br class=""></li><li class="">Made DBC a core module (no longer optional), as it is now extending tendrils into everything (can buses, metrics, etc).<br class=""><br class=""></li><li class="">Extended our core canbus class to be able to attach/detach a DBC at the can bus level.<br class=""><br class=""></li><li class="">Implemented the dbcSignal::Decode function (including a bunch of extensions to dbcNumber to make it more functional).<br class=""><br class=""></li><li class="">Extended our metrics system to support setting metric value via dbcNumber.<br class=""><br class=""></li><li class="">Extended retools to use canbus DBC’s and multiplexed signals for it’s unique message key determination. Also layed a foundational framework for ‘re dbc’ command tree (starting with ‘re dbc list’ to show RE messages and decodes dbc signals).<br class=""><br class=""></li><li class="">Provided a base implementation for OvmsVehicleDBC. This seems to work, but is largely untested (mainly because base functionality like dbc signal decoding is largely untested).</li></ol></div><div class=""><br class=""></div><div class="">Now at the stage where this is starting to be usable. We should be able to create/load a DBC file, config it on a CAN bus, set vehicle type to DBC, and have metrics decoded from the CAN bus automatically. For the moment, we can use Wifi and Michael’s web interface file editor to maintain these DBC files.</div><div class=""><br class=""></div><div class="">I’m now mainly working on the RE stuff. Commands to directly edit DBC records (messages, signals, etc) on the box itself. Combined with the OvmsVehicleDBC vehicle, this should permit the basic functionality of reverse engineering CAN bus messages to produce metric values, without coding in C/C++.</div><div class=""><br class=""></div><div class="">It remains to be seen just how much can be done in this system. I think that by extending the DBC file format (can be with ‘#’ style comments, or other meta-data), we can even implement actions (such as lock/unlock), notifications (charge starting, etc), and other such high-level functionality.</div><div class=""><br class=""></div><div class="">Regards, Mark.<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 11 Feb 2019, at 10:09 PM, Robert Cotran <<a href="mailto:robert@cotran.ca" class="">robert@cotran.ca</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" class="">
<div text="#000000" bgcolor="#FFFFFF" class=""><p class="">Hi Mark,</p><p class="">I don't know if you know about the openpilot project. It's
basically an open-source driving agent (autopilot). I've done a
lot of work on it including developing the original port to making
it work on Classic Tesla Model S with a friend of mine.</p><p class="">Part of what we needed to do was unpack and repack the Tesla DBC
which has a mix of big and little-endian (Motorola and Intel)
encoding.</p><p class="">Here is the github repo for the parser and packer
(decoding/encoding) that we wrote in c++ to do it:</p><p class=""><a class="moz-txt-link-freetext" href="https://github.com/jeankalud/openpilot">https://github.com/jeankalud/openpilot</a></p><p class="">It's actually a branch from the real openpilot which now
incorporates our packer/parser changes.</p><p class="">The secret sauce is here:</p><p class=""><a class="moz-txt-link-freetext" href="https://github.com/jeankalud/openpilot/tree/tesla/selfdrive/can">https://github.com/jeankalud/openpilot/tree/tesla/selfdrive/can</a></p><p class="">In <a href="http://parser.cc" class="">parser.cc</a> and <a href="http://packer.cc" class="">packer.cc</a>. It should be relatively
self-explanatory.</p><p class="">Let me know if you have any questions!</p><p class="">Rob<br class="">
</p>
<div class="moz-cite-prefix">On 2019-02-10 9:55 PM, Mark
Webb-Johnson wrote:<br class="">
</div>
<blockquote type="cite" cite="mid:4EAE1E15-293A-4565-ADF3-CAC69E5BCF24@webb-johnson.net" class="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" class="">
<div class=""><br class="">
</div>
<div class="">I’ve been going round and round in circles on this,
most of the weekend, but not getting anywhere. I’ve rewritten
the Decoder code four times so far, but just can’t get it to
work. So calling for help here.</div>
<div class=""><br class="">
</div>
<div class="">I attach the DBC specification. You can also find an
extensive python library here:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class=""><a href="https://github.com/eerimoq/cantools.git" class="" moz-do-not-send="true">https://github.com/eerimoq/cantools.git</a></div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">(for anyone playing with DBC, that seems an
excellent library and set of command line tools)</div>
<div class=""><br class="">
</div>
<div class="">The seemingly simple problem that I am trying to
solve is:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">Given a CAN frame, we need to decode a particular
signal. We want the integer value (signed or unsigned) out of
it.</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">The signal species the start bit, size (number of
bits), endian-ness (big or little), and value type (signed or
unsigned). The DBC specification says:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class=""><i class="">The start_bit value specifies the
position of the signal within the data field of the
frame. For signals with byte order Intel (little endian) the
position of the least-significant bit is given. For signals
with byte order Motorola (big endian) the position of the
most significant bit is given. The bits are counted in a
saw-tooth manner.</i></div>
<div class=""><i class=""><br class="">
</i></div>
<div class=""><i class="">byte_order = '0' | '1' (* 0=little
endian, 1=big endian *)<br class="">
<br class="">
The byte_format is 0 if the signal's byte order is Intel
(little endian) or 1 if the byte order is Motorola (big
endian).</i></div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">There are also factor and offset values for signals,
but that is irrelevant for the moment (trivial to apply once we
have the actual integer raw signal value extracted) and can be
ignored for the moment.</div>
<div class=""><br class="">
</div>
<div class="">Wikipedia explains endian-ness:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">In big-endian format, whenever addressing memory
or sending/storing words bytewise, the most significant
byte—the byte containing the most significant bit—is stored
first (has the lowest address) or sent first, then the
following bytes are stored or sent in decreasing significance
order, with the least significant byte—the one containing
the least significant bit—stored last (having the highest
address) or sent last.<br class="">
<br class="">
Little-endian format reverses this order: the sequence
addresses/sends/stores the least significant byte first
(lowest address) and the most significant byte last (highest
address).</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">So, we have two strange things in the DBC
specification: (1) the start bit is not always the first bit in
the stream, and (2) this ’saw-tooth manner’ comment. So, I
create a very simple DBC, and run it through the cantools dump
to visualise it:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">$ cat mark.dbc</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">BU_: WS200
MCN_Powertrain</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">BO_ 1 LittleEnd: 8
WS200</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ OneByte : 7|8@0+
(1,0) [0|0] "" Vector__XXX</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ TwoByte :
15|16@0+ (1,0) [0|0] "" Vector__XXX</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ TwelveBit :
39|12@0+ (1,0) [0|0] "" Vector__XXX</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">$ cantools dump
mark.dbc</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">=================================
Messages =================================</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
------------------------------------------------------------------------</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> Name:
LittleEnd</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> Id: 0x1</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> Length: 8 bytes</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> Cycle time: - ms</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> Senders: WS200</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> Layout:</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
Bit</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 6 5
4 3 2 1 0</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 0
|<-----------------------------x|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+-- OneByte</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 1
|<------------------------------|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 2
|------------------------------x|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> B
+-- TwoByte</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> y
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> t 3 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> e
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 4
|<------------------------------|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 5
|--------------x| | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+-- TwelveBit</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 6 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> Signal tree:</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> -- {root}</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> +-- OneByte</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> +-- TwoByte</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> +-- TwelveBit</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
------------------------------------------------------------------------</span></font></div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">I also used 'cantools generate_c_source mark.dbc’ to
generate a C decoder for this, and this is what it looks like:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">int
mark_little_end_unpack(</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> struct
mark_little_end_t *dst_p,</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> const uint8_t
*src_p,</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> size_t size)</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">{</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> if (size < 8u)
{</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> return
(-EINVAL);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> }</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> memset(dst_p, 0,
sizeof(*dst_p));</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> dst_p->one_byte
|= unpack_right_shift_u8(src_p[0], 0u, 0xffu);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> dst_p->two_byte
|= unpack_left_shift_u16(src_p[1], 8u, 0xffu);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> dst_p->two_byte
|= unpack_right_shift_u16(src_p[2], 0u, 0xffu);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
dst_p->twelve_bit |= unpack_left_shift_u16(src_p[4],
4u, 0xffu);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
dst_p->twelve_bit |= unpack_right_shift_u16(src_p[5],
4u, 0xf0u);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> return (0);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">}</span></font></div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">Let’s look at that TwoByte value. This is little
endian, so the least significant byte should come first in p[1]
followed by the most significant in p[2]. So, why does the
generator produce:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> dst_p->two_byte
|= unpack_left_shift_u16(src_p[1], 8u, 0xffu);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> dst_p->two_byte
|= unpack_right_shift_u16(src_p[2], 0u, 0xffu);</span></font></div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">From what I can see, that means take p[1] and shift
it left 8, then add on p[2]. That seems big endian, not little
endian?</div>
<div class=""><br class="">
</div>
<div class="">Let’s try a big endian example:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">BO_ 3 BigEnd2: 8 WS200</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ EightBit2 :
5|8@1+ (1,0) [0|0] "" Vector__XXX</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ OneByte2 :
16|8@1+ (1,0) [0|0] "" Vector__XXX</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ TwelveBit2 :
48|11@1+ (1,0) [0|0] "” Vector__XXX</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
Bit</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 6 5
4 3 2 1 0</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 0
|----------x| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 1 | | |
|<------------------|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+-- EightBit2</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 2
|<-----------------------------x|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> B +--
OneByte2</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> y
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> t 3 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> e
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 4 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 5 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 6
|------------------------------x|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 | | |
| | |<----------|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+-- TwelveBit2</span></font></div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">Notice the weird alignment of EightBit2 and
TwelveBit2? I presume that is the ’saw-tooth’ pattern talked
about in the DBC specification.</div>
<div class=""><br class="">
</div>
<div class="">A Tesla Model S encodes values like the battery
temperature in CAN ID 0x102, and we decode this in C like this:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">StandardMetrics.ms_v_bat_temp->SetValue((float)((((int)d[7]&0x07)<<8)+d[6])/10);</span></font></div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">So that is little endian (d[7] is shifted right,
followed by d[6] normally - so d[7] is the MSB and d[6] the
LSB). But how to represent that in DBC?</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">BO_ 1 LittleEnd2: 8
WS200</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ TwoByte :
47|11@0+ (1,0) [0|0] "” Vector__XXX</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
Bit</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 6 5
4 3 2 1 0</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 0 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 1 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 2 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> B 3 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> y
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> t 4 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> e
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 5
|<------------------------------|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 6
|----------x| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+-- TwoByte</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+—+</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
<div class=""> dst_p->two_byte |=
unpack_left_shift_u16(src_p[5], 3u, 0xffu);</div>
<div class=""> dst_p->two_byte |=
unpack_right_shift_u16(src_p[6], 5u, 0xe0u);</div>
</span></font></div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">That doesn’t work. How about big endian?</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">BO_ 1 BigEnd2: 8 WS200</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> SG_ TwoByte :
48|11@1+ (1,0) [0|0] "” Vector__XXX</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
Bit</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 6 5
4 3 2 1 0</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 0 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 1 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 2 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> B 3 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> y
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> t 4 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> e
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 5 | | |
| | | | | |</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 6
|------------------------------x|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> 7 | | |
| | |<----------|</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+---+---+---+---+---+---+---+---+</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">
+— TwoByte</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""><br class="">
</span></font></div>
<div class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> dst_p->two_byte
|= unpack_right_shift_u16(src_p[6], 0u, 0xffu);</span></font></div>
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class=""> dst_p->two_byte
|= unpack_left_shift_u16(src_p[7], 8u, 0x07u);</span></font></div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">That seems correct.</div>
<div class=""><br class="">
</div>
<div class="">Looking in the source for cantools dbc
(cantools/cantools/database/can/formats/dbc.py), I see:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class=""><font class="" face="Andale Mono"><span style="font-size: 14px;" class="">byte_order=(0 if
signal.byte_order == 'big_endian' else 1),</span></font></div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">This code also says the same thing (0 = big endian):</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class=""><a href="https://github.com/julietkilo/CANBabel/blob/master/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java" class="" moz-do-not-send="true">https://github.com/julietkilo/CANBabel/blob/master/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java</a></div>
<div class=""><br class="">
</div>
<div class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre;">boolean</span><span style="caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);" class=""> isBigEndian </span><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre;">=</span><span style="caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);" class=""> </span><span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre;"><span class="pl-pds" style="box-sizing: border-box;">"</span>0<span class="pl-pds" style="box-sizing: border-box;">"</span></span><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre;">.</span><span style="caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);" class="">equals(splitted[</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre;">2</span><span style="caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12px; white-space: pre; background-color: rgb(255, 255, 255);" class="">]);</span></div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">WTF?</div>
<div class=""><br class="">
</div>
<div class="">There is this (from googling), which makes things
clear as mud:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class=""><a href="https://github.com/ebroecker/canmatrix/wiki/signal-Byteorder" class="" moz-do-not-send="true">https://github.com/ebroecker/canmatrix/wiki/signal-Byteorder</a></div>
<div class=""><a href="https://se.mathworks.com/help/vnt/ug/canpack.html" class="" moz-do-not-send="true">https://se.mathworks.com/help/vnt/ug/canpack.html</a></div>
<div class=""><a href="https://github.com/ebroecker/SocketCandecodeSignals/blob/master/datenbasis.c#L112-L122" class="" moz-do-not-send="true">https://github.com/ebroecker/SocketCandecodeSignals/blob/master/datenbasis.c#L112-L122</a></div>
<div class=""><a href="https://github.com/julietkilo/CANBabel/blob/master/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java" class="" moz-do-not-send="true">https://github.com/julietkilo/CANBabel/blob/master/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java</a></div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">My conclusions are that:</div>
<div class=""><br class="">
</div>
<div class="">
<ol class="MailOutline">
<li class="">The DBC specification is incorrect, in that 0 is
big endian, and 1 is little endian in the public code we
see.<br class="">
<br class="">
and/or<br class="">
<br class="">
</li>
<li class="">The DBC specification is incorrect, in that all
CAN bus byte ordering is little endian, and the byte_order
setting only affects the interpretation of the bit_ordering,
and saw-tooth decode/encode.</li>
</ol>
</div>
<div class=""><br class="">
</div>
<div class="">But that really makes no sense to me. This is
supposed to be the defacto standard for automotive CAN bus
signal definition - how can it be such a mess?</div>
<div class=""><br class="">
</div>
<div class="">Help?</div>
<div class=""><br class="">
</div>
<div class="">Mark</div>
<div class=""><br class="">
</div>
<br class="">
<fieldset class="mimeAttachmentHeader"></fieldset>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" class="">
<br class="">
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
OvmsDev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:OvmsDev@lists.openvehicles.com">OvmsDev@lists.openvehicles.com</a>
<a class="moz-txt-link-freetext" href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a>
</pre>
</blockquote>
</div>
_______________________________________________<br class="">OvmsDev mailing list<br class=""><a href="mailto:OvmsDev@lists.openvehicles.com" class="">OvmsDev@lists.openvehicles.com</a><br class="">http://lists.openvehicles.com/mailman/listinfo/ovmsdev<br class=""></div></blockquote></div><br class=""></div></body></html>