Skip to content

TypeError in slcan.py when formatting standard (non-extended) CAN frames #75

@felixavy

Description

@felixavy

A TypeError occurs in the _send_frame function of the TxWorker class inside the slcan.py driver when attempting to format standard (non-extended) CAN frames (frame.extended == False). The issue arises because the format string %03X incorrectly attempts to process both the marker (string) and frame.id (integer), but %X expects only an integer.

Code Snippet

def _send_frame(self, frame):
        marker = 'D' if frame.canfd else 'T'
        dlc_len = CANFrame.datalength_to_dlc(len(frame.data))
        line = '%s%X%s\r' % (('%c%08X' if frame.extended else 't%03X') % (marker, frame.id),
                             dlc_len,
                             binascii.b2a_hex(frame.data).decode('ascii'))

        self._conn.write(line.encode('ascii'))
        self._conn.flush()

Error Traceback
TypeError: %X format: an integer is required, not str

Proposed Fix
Adjust the formatting logic to separate the marker and frame.id when frame.extended == False, ensuring that only frame.id is passed to %03X.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions