Skip to content

Is the high nibble of a CAN packet allowed to overflow the nibble? #63

@Poikilos

Description

@Poikilos

See my new code (commented below) and let me know if it is needed. The only way I could see it is not needed is if the CAN format allows the bits to overlap (counterintuitive, but maybe).

    def receiveChars(self, data):
        self.inboundBuffer += data
        lastByte = 0
        if 0x3B in self.inboundBuffer:
            #  ^ ';' ends message so we have at least one (CR/LF not required)
            # found end, now find start of that same message, earlier in buffer
            for index in range(0, len(self.inboundBuffer)):
                outData = bytearray()
                if 0x3B not in self.inboundBuffer[index:]:
                    break
                if self.inboundBuffer[index] == 0x3A:  # ':' starts message
                    # now start to accumulate data from entire message
                    header = 0
                    for offset in range(2, 9+1):
                        nextChar = (self.inboundBuffer[index+offset])
                        nextByte = (nextChar & 0xF)+9 if nextChar > 0x39 else nextChar & 0xF  # noqa: E501
                        header = (header << 4)+nextByte
                    # offset 10 is N
                    # offset 11 might be data, might be ;
                    lastByte = index+11
                    for dataItem in range(0, 8):
                        if self.inboundBuffer[index+11+2*dataItem] == 0x3B:
                            break
                        # two characters are data
                        byte1 = self.inboundBuffer[index+11+2*dataItem]
                        part1 = (byte1 & 0xF)+9 if byte1 > 0x39 else byte1 & 0xF  # noqa: E501
                        byte2 = self.inboundBuffer[index+11+2*dataItem+1]
                        part2 = (byte2 & 0xF)+9 if byte2 > 0x39 else byte2 & 0xF  # noqa: E501
                        high_nibble = part1 << 4
                        # if part1 > 0xF:  # can't fit > 0b1111 in nibble
                        #     # possible overflow caused by +9 above
                        #     #   (but should only happen on bad packet)?
                        #     #   Commented since not sure if ok
                        #     raise ValueError(
                        #         "Got {} for high nibble (part1 << 4 == {})."
                        #         .format(part1, high_nibble))
                        outData += bytearray([high_nibble | part2])
                        lastByte += 2
. . .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions