Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix frame decoding #224

Merged
merged 21 commits into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/canmatrix/canmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def pack_bitstring(self, length, is_float, value, signed):
signed=signed)
b = '{:0{}b}'.format(int.from_bytes(b, byteorder='big'),
length)
bitstring = b[:length]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@altendky
seems I found a bug. At least now my tests seem to produce correct data.
This was your code, maybe you should have a look to your code also...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh... I wonder how I haven't had issues. It seems to show up for signed values (I forget big endian or little or...). Anyways, thanks for pointing that out. As a double check I made up #235 to recreate it 'in my world' of .sym files.

bitstring = b[-length:]

return bitstring

Expand Down
8 changes: 4 additions & 4 deletions src/canmatrix/tests/test_frame_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def test_decode_with_dbc_big_endian():

def test_decode_with_dbc_little_endian():
cm = loadDbc()
# 002#0C00057003CD1F83
frameData = bytearray([12, 0, 5, 112, 3, 205, 31, 131])
# 002#0C00057003001F83
frameData = bytearray([12, 0, 5, 112, 3, 0, 31, 131])
frame = cm.frameById(2)
decoded = frame.decode(frameData)
assert decoded["secSig1"].rawValue == 0
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_decode_with_dbc_float():

def test_decode_with_dbc_multiplex():
cm = loadDbc()
frameData1 = bytearray([0x38, 0x63, 0x8A, 0x7E, 0x18, 0x20, 0x00])
frameData1 = bytearray([0x38, 0x63, 0x8A, 0x7E, 0x00, 0x20, 0x00])
frame = cm.frameById(4)
decoded1 = frame.decode(frameData1)
assert decoded1["myMuxer"].rawValue == 0
Expand All @@ -67,7 +67,7 @@ def test_decode_with_dbc_multiplex():
assert decoded1["muxSig4"].rawValue == 0x3F
assert decoded1["muxSig9"].rawValue == 0x20

frameData2 = bytearray([0x38, 0x63, 0x8A, 0x7E, 0x18, 0x20, 0x20])
frameData2 = bytearray([0x38, 0x63, 0x8A, 0x1E, 0x18, 0x20, 0x20])
decoded2 = frame.decode(frameData2)
assert decoded2["myMuxer"].rawValue == 1
assert decoded2["muxSig9"].rawValue == 0x20
Expand Down
8 changes: 3 additions & 5 deletions src/canmatrix/tests/test_frame_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def loadDbc():
return canmatrix.formats.loadp(os.path.join(here ,"test_frame_decoding.dbc"), flatImport = True)


def test_decode_with_dbc_little_endian():
def test_encode_with_dbc_little_endian():
cm = loadDbc()
# 002#0C00057003CD1F83
frame = cm.frameById(1)
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_encode_with_dbc_little_endian():
toEncode["secSig12"] = 12

frameData = frame.encode(toEncode)
assert frameData == bytearray([12, 0, 5, 112, 3, 205, 31, 131])
assert frameData == bytearray([0x0c, 0x00, 0x05, 0x70, 0x03, 0x00, 0x10, 0x83])


def test_encode_with_dbc_float():
Expand All @@ -75,7 +75,6 @@ def test_encode_with_dbc_multiplex():
toEncode1["muxSig2"] = 0x63
toEncode1["muxSig3"] = 0x8A
toEncode1["muxSig4"] = 0x3F
toEncode1["muxSig9"] = 0x20

frameData1 = frame.encode(toEncode1)
assert frameData1 == bytearray([0x38, 0x63, 0x8A, 0x7E, 0x00, 0x20, 0x00])
Expand All @@ -87,6 +86,5 @@ def test_encode_with_dbc_multiplex():
toEncode2["muxSig6"] = 0x18
toEncode2["muxSig7"] = 0x0C
toEncode2["muxSig8"] = -8
toEncode2["muxSig9"] = 0x20
frameData2 = frame.encode(toEncode2)
assert frameData2 == bytearray([0x38, 0x63, 0x8A, 0x7E, 0x18, 0x20, 0x20])
assert frameData2 == bytearray([0x38, 0x60, 0x80, 0x1E, 0x18, 0x20, 0x20])