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

When spn_start is a range cut_data is incorrectly retuned as None #25

Closed
s5y3XZpGvQPApqR opened this issue May 23, 2021 · 1 comment
Closed

Comments

@s5y3XZpGvQPApqR
Copy link

s5y3XZpGvQPApqR commented May 23, 2021

def get_spn_cut_bytes(spn_start, spn_length, message_data_bitstring, is_complete_message):
    spn_end = spn_start[0] + spn_length - 1
    if not is_complete_message and spn_end > message_data_bitstring.length:
        return bitstring.Bits(bytes=b'')

    cut_data = message_data_bitstring[spn_start[0]:spn_end + 1]
    if len(spn_start) > 1:
        lsplit = int(spn_start[1] / 8) * 8 - spn_start[0]
        rsplit = spn_length - lsplit
        cut_data = bitstring.BitArray(message_data_bitstring[spn_start[0]:spn_start[0] + lsplit]).append(
            message_data_bitstring[spn_start[1]:spn_start[1] + rsplit])
    return cut_data

if spn_start is a list e.g. [0, 8] requiring the first two bytes then the cut_data is incorrectly returned at None

Here's what I expect:
in log.txt
(1621774359.366587) can0 14FEBF80#1010010203040506

{
    "PGN": "EBC2(65215)",
    "DA": "All(255)",
    "SA": "Unknown(128)",
    "Front Axle Speed": "16.0625 [kph]",
    "Relative Speed; Front Axle, Left Wheel": "-7.75 [kph]",
    "Relative Speed; Front Axle, Right Wheel": "-7.6875 [kph]",
    "Relative Speed; Rear Axle #1, Left Wheel": "-7.625 [kph]",
    "Relative Speed; Rear Axle #1, Right Wheel": "-7.5625 [kph]",
    "Relative Speed; Rear Axle #2, Left Wheel": "-7.5 [kph]",
    "Relative Speed; Rear Axle #2, Right Wheel": "-7.4375 [kph]"
}

The first entry Front Axle Speed 16 kph is not returned in the current version. rsplit and lsplit are not correct and the cut_data assignment doesn't work even if they are 0 and 16.
This works for me but I'm not sure if this breaks other functionality

def get_spn_cut_bytes(spn_start, spn_length, message_data_bitstring, is_complete_message):
    print("cut bytes")
    spn_end = spn_start[0] + spn_length - 1
    if not is_complete_message and spn_end > message_data_bitstring.length:
        return bitstring.Bits(bytes=b'')

    cut_data = message_data_bitstring[spn_start[0]:spn_end + 1]
    if len(spn_start) > 1:
        lsplit = int(spn_start[0])
        rsplit = int(spn_start[1]) + 8
        cut_data = message_data_bitstring[lsplit:rsplit]
    return cut_data
@BenGardiner
Copy link
Member

Thanks for bringing this up, @s5y3XZpGvQPApqR . Before the changes you propose the Front Axle Speed is missing from the description as you say

# python3 pretty_j1939.py --candata --real-time --format --da-json tmp/J1939DA_201910.json tmp/issue25.log
(1621774359.366587) can0 14FEBF80#1010010203040506 ; {
                                                   ;     "PGN": "EBC2(65215)",
                                                   ;     "DA": "All(255)",
                                                   ;     "SA": "Unknown(128)",
                                                   ;     "Relative Speed; Front Axle, Left Wheel": "-7.75 [km/h]",
                                                   ;     "Relative Speed; Front Axle, Right Wheel": "-7.688000000000001 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #1, Left Wheel": "-7.626 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #1, Right Wheel": "-7.564 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #2, Left Wheel": "-7.502000000000001 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #2, Right Wheel": "-7.44 [km/h]"
                                                   ; }

Then with your changes we get some front axle speed as described. Thank you for the testcase here!

# python3 pretty_j1939.py --candata --real-time --format --da-json tmp/J1939DA_201910.json tmp/issue25.log
cut bytes
cut bytes
cut bytes
cut bytes
cut bytes
cut bytes
cut bytes
(1621774359.366587) can0 14FEBF80#1010010203040506 ; {
                                                   ;     "PGN": "EBC2(65215)",
                                                   ;     "DA": "All(255)",
                                                   ;     "SA": "Unknown(128)",
                                                   ;     "Front Axle Speed": "16.0625 [km/h]",
                                                   ;     "Relative Speed; Front Axle, Left Wheel": "-7.75 [km/h]",
                                                   ;     "Relative Speed; Front Axle, Right Wheel": "-7.6875 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #1, Left Wheel": "-7.625 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #1, Right Wheel": "-7.5625 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #2, Left Wheel": "-7.5 [km/h]",
                                                   ;     "Relative Speed; Rear Axle #2, Right Wheel": "-7.4375 [km/h]"
                                                   ; }

The problem was introduced by me when I ported @j4l-Shvn's code in PR #6. Their code was fine. I didn't use bitstring's .append() the correct way. つ﹏⊂

I really appreciate you pointing out this problem, @s5y3XZpGvQPApqR . It also led me to create issues #28 and #27 during testing.

BenGardiner added a commit that referenced this issue Jan 26, 2022
fix issue #25 by using append the right way...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants