Hello again!
I am still working on my CAN-Bus project (btw I will of course post the finished code if anyone is interested) from this Link here.
As maybe the whole problem is just too much to ask for here is a little sub-question:
The CAN-Message I receive is in Hex. As long there is no leading “0” in the byte everything is fine.
For instance if I get a “AF” I can convert this to Binary (which would be “1010 1111”).
My Problem is, if I get a single Digit HEX Value (for instance “03” wheres the 0 is not transmitted and I only get a 3).
So a 03 would be “0000 0011” – that’s what I need. What I get is “0011”, as the leading 0 is not in my Buffer.
Any suggestion on how to get this done?
What do you actually means by "is in hex"? An ASCII string "AF", ie the two bytes 0b01000001, 0b01000110 ? Or
is this a binary value already, in which case the description isn't meaningful, internally values are always
binary.
Hi Mark!
Thanks for your reply!
So basically what I get from the mcp_can.h library is a char array of 8 Bytes, representing the CAN-Message I get from the Car.
For instance this could be
"A7 03 B4 9D 00 BB 07 7D"
The message is saved in the rxBuf[0] to rxBuf[8].
I need to convert this message to binary to then again assemble the bits I need for a specific information (example: Startbit 11, Length 9).
So, for the first 2 bytes the binary should look like this:
"0B1010011100000011"
The Problem is, that as soon as there is a leading 0 in my Hex message (the second byte for example) it gets left out.
So my 0x03 converts to 0B0011 instead of 0B00000011
The zeros ahead of the 0B0011 include important information for me (example: Is the blower on or off).
Is there any way to (memory efficiently) "copy" the message into the binary format with access to each separate bit?
Hi aarg,
yes, if you see it in total they are the same number.
Let me try to explain:
0x03 should be 0B00000011
In this message the Bit 1 (so a "0") could represent the status of the blower (on->1 off->0) of the ABS, or so on...
So you see, even though there are only "0000" this could represent the Boolean information on 4 actuators.
If I display this in binary I am missing exactly those leading zeros.
The key word in that phrase is DISPLAY. The 0s are NOT missing. They are just not necessary to convey the required information. 0x3 is the EXACT same value as if you typed 0x00000003.
What's more, if you have an int with the vale 0x3 and you want it to have binary 00000011 then good news. You don't have to do ANYTHING. Everything is stored in binary. You can print it or write it as hex for human consumption, but inside the chip it is ALL binary.
@aarg: What I doing it reading CAN-Bus Data from a Car. I get 8 * 8Bytes of Data, saved in an Array "rx_frame.dada.u8"
So in total, I receive 64 Bits of information.
These Bits could represent the state of 64 actuators in terms of "on/off --> 0/1.
Usually though the 64Bits represent measurement Values of some sort. So, for instance we could receive a message :
Now lets suspect there are 2 known signals in this message.
1 is an On/Off state
1 represents the speed of the car
The CAN Database could say:
Signal 1: Startbit 0, Length 1
Signal 2: Startbit 11, Length 9
For Signal1 I would need to take the first Digit of the Binary - pretty simple.
For Signal2 I would need to take "000011 10110", and convert this to a Decimal Number, multiply with a Faktor and Offset it. If there was no Faktor or Offset the correlating DEC would be 118.
So, for what I see I have to accomplish the following tasks:
Take all the Data of the Array "rx_frame.dada.u8" and put it together in one variable.
I need to somehow sort out the Bits I need (--> Bitshifting?) into a separate variable
You're still talking about converting a binary number to decimal to do some math. That's where you misunderstand. A computer CANNOT do math in decimal. They only understand binary. You can display a number however you want but the numbers themselves are all represented in binary for the processor. You don't need to convert anything to do math with it. Math is the same in any base anyway. The only time comverting bases comes into play is when you want to display a number for a human.
You have 64 bits of data. Use a little math, to determine which byte the data is in, and bitRead() to read the bit from the byte. Do that as many times as needed to get all the bits that make up a value. Then, just use the value. Do not concern yourself with whether the bits represent a binary value, a decimal value, or a hexadecimal value.
@aarg: What I doing it reading CAN-Bus Data from a Car. I get 8 * 8Bytes of Data, saved in an Array "rx_frame.dada.u8"
So in total, I receive 64 Bits of information.
But as others have pointed out, you are not receiving CAN data as a string of binary characters. You are receiving it as a series of bytes, which have all 8 bits, not just the bits that happen to get printed by the Arduino binary print method.
The real answer to my question, which is what are you doing with the binary string, is "nothing except looking at it and being confused". Because, the binary string is just one of many ways of printing it.