0

From the CAN DBC file im taking few Can Messages and assigning values to each signal of Can Messages and then encoding it to send the can message.

for each signal, data is encoded in the different(few examples are listed below)

Data1--> b'\x00\x00d\x01P\x00\x00\x01'
Data2--> b'\x01\x81\x0f\x11\xc8\x00\x00\x00'
Data3--> b'\x00\x00d\x01P\x00\x00\x01'

My doubt is.. what is the meaning of x00d,x01P,x00d these data, what dose it denotes (what is P and d).

please let me know why data byte is in such format (like: x00d,x01P,x00d) and how to get the data as normal/usual format(like:Data2--> b'\x01\x81\x0f\x11\xc8\x00\x00\x00' )

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
anu
  • 21
  • 7

2 Answers2

0

The d and P values you see in your bytestrings are bytes that coincidentally happen to represent ASCII characters. So Python displays them that way. If you want to see the raw hexidecimal values for all of the bytes, you can call the bytes.hex() method on the bytestring, it will give you output like: '0000640150000001' (where the 64 replaces d and 50 replaces P).

But if you're just passing around the bytes object, you probably don't need to do anything. Code that expects a bytes object will find b'd' just as acceptable as b'\x64', since they are two ways of writing the same character.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

This is exactly like C string constants. When the byte is a printable ASCII value, it shows you the ASCII. When it isn't, it shows you the hex. So, your first and third strings represent the hex bytes 00 00 64 01 50 00 00 01. Your second is 01 81 0f 11 c8 00 00 00.

Mostly, you shouldn't worry about this This is simply how Python prints byte strings if you do not format it yourself.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • In other words, whatever tool that's used for displaying the data, it's too dumb to realize that characters can contain raw data and not just strings. For example this is a common defect in bad C debuggers written by PC programmers. – Lundin Nov 12 '21 at 07:38
  • 1
    You are totally wrong. Consider `x = b'abcde\nfghij\n'`. Do you want that displayed in hex? – Tim Roberts Nov 12 '21 at 08:03
  • The point is that data is _either_ a string or raw binary values. It isn't meaningful to treat it as both at once. Decent tools have an option where you can tell it to display data as a string or as raw hex. CAN bus messages are rarely every characters, so a tool displaying them as strings is not a suitable tool, period. – Lundin Nov 12 '21 at 08:27
  • Yes, but it's the responsibility of the tool to know its own context and do the proper formatting. The __repr__ representation, which is what we see here, was NEVER intended to be used for production output. It is used to create a string that can be read back and evaluated in order to recreate the original value. That's it. You're expecting too much. – Tim Roberts Nov 12 '21 at 18:34