Three bytes sending

Kilometers are in three bytes.
With this i can get correct value of kilometers from can-bus:

if (canId == 0x540) {
      uint32_t odometer = (uint32_t)rxBuf[7] <<16;
      odometer |=  rxBuf[6]<<8;
      odometer |=  rxBuf[5];

I cannot get it how to send three bytes with realdashcan, this does not work:

  buf[0] = (0xff);
  buf[1] = (0xff);
  buf[2] = (0xff);
  buf[3] = (0xff);
  buf[4] = (0xff);
  buf[5] = ((odometer  & 0xff);
  buf[6] = ((odometer >> 8) & 0xff);
  buf[7] = ((odometer >> 16) & 0xff);

In what form and order it should be written?

Looks ok to me, just use offset=“5” length=“3” in XML. Could you give more details of what is wrong?

In XML i have all like that. Target id is 310 (odometer).
After making this sketch, text gauge shows 1000000 when i turn ignition on, so something goes wrong.

I want to make a test and somehow put buf5-7 together and print value to a serial monitor.
Something like this?
It print value “156”

Serial.println(buf[5] + buf[6] + buf[7])


Serial.println(buf[5]  & 0ff) + (buf[6] << 8) +  (buf[7] << 16);

Also i tried to turn around all like buf7+buf6+buf5 and in can-frame sending code i tried to change all with same princible, what ever i do it print 0, 127 or 156, right value should be about 489000.

I can do some tests, but if you believe the problem is the 3 byte value, you can try to send the data in 4 bytes instead, just set most significant byte to 0.

I got some help in Arduino forum and for now i solved it like this:

else if (rxId == 0x520) {
      uint32_t rawOdo = (uint32_t)rxBuf[7] << 16;
      rawOdo |= (uint32_t) rxBuf[6] <<8;
      rawOdo |= rxBuf[5];
      odometer = rawOdo;

buf[0] = (0xff);
  buf[1] = (0xff);
  buf[2] = (0xff);
  buf[3] = (0xff);
  buf[4] = (0xff);
  buf[5] = ((odometer  & 0xff);
  buf[6] = ((odometer >> 8) & 0xff);
  buf[7] = ((odometer >> 16) & 0xff);
1 Like

New problem with fuel consumption and trip meter values.
I have fuel consumption as a double variable, trip meter is uint16_t.
Tried to send it in one byte (buf[]), it showed ok in RealDash, but without decimal points.
After updating RealDash to newest version, trip meter kilometers began to go back and forth, for example if there were about 2 kilometers, it blinks from 1 to 2 or 2 to 3.
Then i tried in same way as above in last post, but of course it says “invalid operands of types ‘double’ and ‘int’ to binary 'operator& '”
It is correct to send trip meter value in two bytes and fuel consumption in 4 bytes and how i get decimal points to fuel consumption value?

Blinking was because it tried to take kilometers from two different variables.

Trip1 kilometers does not fluctuate but fuel consumption and average speed does.

// build 8th CAN frame, trip1 & avg speed
  buf[0] = (trip1 & 0xff);
  buf[1] = (trip1 >> 8 & 0xff);
  buf[2] = averageTrip1Speed;
  buf[3] = (0xff);
  buf[4] = (0xff);
  buf[5] = (0xff);
  buf[6] = (0xff);
  buf[7] = (0xff);

  // write 8th CAN frame to serial
  SendCANFrameToSerial(07, buf);

// build 9th CAN frame, fuel consumption
  memcpy(buf, &trip1FuelConsumption, 4);
  // write 9th CAN frame to serial
  SendCANFrameToSerial(08, buf);
<frame id="3207">
		<value targetId="311" offset="0" length="2"></value> <!--Trip1-->
		<value name="Trip1 average speed" offset="2" length="1"></value> 
	<frame id="3208">
		<value name="Trip1 fuel consumption" offset="0" length="4" float="true"></value> 
		<value name="Trip2 fuel consumption" offset="4" length="4" float="true"></value>