<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Hi Mark,</p>
    <p>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>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>Here is the github repo for the parser and packer
      (decoding/encoding) that we wrote in c++ to do it:</p>
    <p><a class="moz-txt-link-freetext" href="https://github.com/jeankalud/openpilot">https://github.com/jeankalud/openpilot</a></p>
    <p>It's actually a branch from the real openpilot which now
      incorporates our packer/parser changes.</p>
    <p>The secret sauce is here:</p>
    <p><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>In parser.cc and packer.cc.  It should be relatively
      self-explanatory.</p>
    <p>Let me know if you have any questions!</p>
    <p>Rob<br>
    </p>
    <div class="moz-cite-prefix">On 2019-02-10 9:55 PM, Mark
      Webb-Johnson wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:4EAE1E15-293A-4565-ADF3-CAC69E5BCF24@webb-johnson.net">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <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>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <br>
      <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>
  </body>
</html>