Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading canbus data as hex
#1
Hello ,
I have start to work on a small canbus project
which I need to read the data
I have this :
while True:
    message = bus.recv()
    print (message)
    PID = str(message.arbitration_id)
    dlc= message.dlc
    print ('PID is - ' + str(PID))
    Data = message.data
    print(Data)
and this is result I'm getting
Timestamp: 1608032877.333082    ID: 0eddcc08    X                DLC:  8    0a bc 0d 04 05 06 cc 1f     Channel: can0
PID is - 249416712
I need\want it to save the HEX value (0eddcc08) an not convert it to int


I did this
message_str = str(bus.recv())
    print (message_str)
    n = message_str.find("ID: ")
    end = message_str.find("X")
    pid = message_str[n + 4:int(end)]
    print (pid)
and I get the wanted answer
but its very crooked , and if I will get another pid size

****
and another question :
I can't seem to get the data : 0a bc 0d 04 05 06 cc 1f
I have try this , but I get error
 File "Documents/Read_Canbus.py", line 30, in <module>
    data - message.data
NameError: name 'data' is not defined
but when I look at the API
https://python-can.readthedocs.io/en/stable/message.html
I undersatnd there is such object called message.data , no ?

Thanks ,
Reply
#2
The value of an integer is always the same if it is displayed in binary, octal, decimal, hexadecimal or any other base. If you want to print the hex representation of PID you can do this:
while True:
    message = bus.recv()
    PID = message.arbitration_id
    print('PID is - ' + hex(PID))
    print(f'PID is = {PID:x}')
Save PID as a number, not a string. Convert the number to whatever representation you want when displaying the value.
Reply
#3
OK

can you help about the second question?

I mange to get the data
but this is what I get
prin(message)
MyData = message.data
print(str(MyData)
and this is what I get:
Timestamp: 1608032877.333082    ID: 0eddcc08    X                DLC:  8    0a bc 0d 04 05 06 cc 1f     Channel: can0

bytearray(b'\n\xbc\r\x04\x05\x06\xcc\x1f')
why I don't get the hx number
and why some values are missing?




Thanks
Reply
#4
Your code should be:
if PID == 0x11ddcc08:
    print ('I have found you!!!)
else:
    print('no such pid - problem!')
As I mentioned in my first post, there is no such thing as a hex value, only a hex representation. PID is an integer, not a decimal or hex number. PID is printed in decimal because that is how the default representation for an integer.
Reply
#5
ohh
now I understand
because now I did this
if PID == hex(0x11ddcc08):
    print ('I have found you!!!)
elif PID == hex(0x01ddcc08):
    print('I have found another one')
else:
    print('no such pid - problem!')
and now it's working (I send 2 pid and I see the printing messageis change according to what I'm sending)

so it's all good now ,right?
this is the right way to do it?
Thanks ,
Reply
#6
If I understand correctly, message.arbitration_id is an integer. 0x11ddcc08 is an integer. You can directly compare message.arbitration_id to 0x11ddcc08. You don't need to use hex() anywhere.

You can also do this:
known_pid = [0x11ddcc08, 0x01ddcc08] # A list of all known PID
if message.arbitration_id in known_pid:
    print (f'I know you 0x{message.arbitration_id:x}')
Reply
#7
You do not need to do this:
print(str(MyData))
The print function automatically calls str() for MyData to get a string REPRESENTATION of MyData that it can print.

This is a bytearray:
Output:
bytearray(b'\n\xbc\r\x04\x05\x06\xcc\x1f')
To be consistent I should say that this is a REPRESENTATION of a bytearray printed by the print() function. If you want to print this as a list of hex numbers you could do this:
x = bytearray(b'\n\xbc\r\x04\x05\x06\xcc\x1f')
print(x) # Prints x as a bytearray
print(' '.join([hex(a) for a in x]))
# prints x as numbers in hex format.

You probably do not want to convert your data to a hex string because it is really difficult to do anything with a hex string. You might want to convert you data to a number or multiple numbers. I don't know because I don't know what the data represents. Is it 8 bytes? 64 bits? Two 32 bit integers? A long long integer? Some kind of encoded string?
Reply
#8
the data I need to check is in HEX -this is how I get it
and every bytes mean something else
so I cna't comare it to int
I need to exame evey byte by it self

but now it;s seem to be working
so thank you !
Reply
#9
In your initial post your program did this:
PID = str(message.arbitration_id)
print ('PID is - ' + str(PID))
And the output was this:
Output:
PID is - 249416712
You wanted PID to be hex so you could compare it to hexadecimal value 0eddcc08.

I have been trying to tell you that no conversion is required. Both 249416712 and 0x0eddcc08 are integers, and they are the both the same value. If you need proof try typing this in your Python console:
Output:
>>> print(249416712 == 0x0eddcc08) True
Your data is not hex. When you print your data Python represents the data as a string. The default representation for an integer is to print a decimal string. This does not make the number a decimal, it is still an integer. As has been demonstrated you can print an integer as a hex string if you want, but that doesn't make it a hex number either. This is from a readthedocs example for the canbus libaray:
Quote: msg = can.Message(arbitration_id=0xc0ffee,
data=[0, 25, 0, 1, 3, 1, 4, 1],
is_extended_id=True)
Here they decided to use hex notation to specify the arbitration id. The could also use 851950 decimal and it would work exactly the same. For data they used decimal representation, but it could also be data=[0x00, 0x19, 0x00, 0x01, 0x03, 0x04, 0x01]. Once again it makes no difference what representation you use for the integers.

You need to stop thinking that str(thing) and thing are the same. str(thing) is the output of thing.__str__() or thing.__repr__(). The author of these functions chose to display the attributes of thing in the way they thought was most useful for the user.
Reply
#10
Thank you
I understand now the different between the two
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,341 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Reading Data from JSON tpolim008 2 1,077 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Help reading data from serial RS485 korenron 8 13,924 Nov-14-2021, 06:49 AM
Last Post: korenron
  how to startup canbus interface on pi korenron 2 2,217 Oct-24-2021, 09:51 PM
Last Post: DeaD_EyE
  Help with WebSocket reading data from anoter function korenron 0 1,328 Sep-19-2021, 11:08 AM
Last Post: korenron
  Fastest Way of Writing/Reading Data JamesA 1 2,187 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Reading data to python: turn into list or dataframe hhchenfx 2 5,367 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Reading data from mysql. stsxbel 2 2,202 May-23-2021, 06:56 PM
Last Post: stsxbel
  Reading Serial data Moris526 6 5,375 Dec-26-2020, 04:04 PM
Last Post: Moris526
  wrong data reading on uart fahri 6 3,340 Sep-29-2020, 03:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020