Question:
Removing ASCII formatted characters from my CAN messages in Python

Problem:

I would like to remove ASCII characters from my bytearray and see it containing hexadecimal numbers. To explain it better: I receive CAN messages from CANbus using the pyton-can library and catch them to store in a message variable. As I print it I see:

bytearray(b'\x00\x00^]b\tb\xd9')


But I don't want to see ASCII characters, I want it to print this as for example b'\x00\x00\x01\x65\xA5\x12\x1A\xBB


So what should I do?


My code now looks like:

#receiving CAN Bus messages here

bus = can.interface.Bus(interface='kvaser', channel=0, bitrate=1000000)

byte_index_to_check = 1

msg = can.Message(arbitration_id=0x007)

bus.send(msg)

recvMsg = bus.recv(timeout=0.5)

message = bus.recv()

received_message = hex(message.data[byte_index_to_check])


last_message = message

last_received = received_message

print("last message",last_message.data)


Solution1:

You don't so much want to remove characters, as convert them to HEX. The binascii library does just that:

binascii.b2a_hex(message, b' ')


Suggested blogs:

>Creating a form in Django to upload a picture from website

>Sending Audio file from Django to Vue

>How to keep all query parameters intact when changing page in Django?

>Solved: TaskList View in Django

>Implement nested serializers in the Django rest framework

>Define blade component property in server - Laravel

>Can not run phpstan under docker with memory lack error


Ritu Singh

Ritu Singh

Submit
0 Answers