-1

I wants to extract the bits from the CAN message (8 bytes).

so, how to i extract bits from 8 bytes.

Akash shah
  • 1
  • 2
  • 9
  • In my opinion you should use some signals definitions from DBC or Arxml, and then act on Message::signal. But in case that you really want extract some bits from bytes use bitwise operations and masks. – YesIO Jan 22 '20 at 07:23

1 Answers1

0

Here is a chunk of code to get the bits from one byte:

on message myMessage
{
 byte bitArray[8]; 
 int iByteToRead = 0;

 f_ByteToBitArray(this.byte(iByteToRead),bitArray);
 write ("f_ByteToBitArray %X :Bits %X %X %X %X %X %X %X %X", iByteToRead , bitArray[0],bitArray[1],bitArray[2],bitArray[3],bitArray[4],bitArray[5],bitArray[6],bitArray[7]);  
}

void f_ByteToBitArray(byte in, byte out[])
{
  //                          7 6 5 4 3 2 1 0
  // f_ByteToBitArray 1 :Bits 0 0 0 0 0 0 0 1
 // f_ByteToBitArray 2 :Bits 0 0 0 0 0 0 1 0

 byte a;
 a = in;
 out[0] = a & 0x01;
 a = a >> 1;
 out[1] = a & 0x01;
 a = a >> 1;
 out[2] = a & 0x01;
 a = a >> 1;
 out[3] = a & 0x01;
 a = a >> 1;
 out[4] = a & 0x01;
 a = a >> 1;
 out[5] = a & 0x01;
 a = a >> 1;
 out[6] = a & 0x01;
 a = a >> 1;
 out[7] = a & 0x01;
}
Bryan M
  • 11
  • 3