Converting HEX values

I'm working on a project that uses a load cell which gives canbus data, But unfortunately I can not find and data sheet reference to it's protocol but have manged to work out which bits to do what.
Byte 0&1 are the load cell values of cell 1
Byte 2&3 are the load cell values of cell 2
Byte 4 is the temperature reading of the load cell
Byte 5&6 is the load cell power supply value.

So with the power supply voltage set to 12.5 the reading are 0x48 & 0x1F.

I'm trying to get the reading converted to 12.5 so that I can display the values of them all but only looking at the power supply one first

// LS 1 = load cell 1
// LS2 = Load cell 2
// Temp = temp reading
//PS power supply value
// PS reading 0x48 & 0x1F should = 12.50 Volts
//                     LS1   LS1    LS2  LS2   TEMP  PS    PS
uint16_t  rx_frame[8] = {0XDD, 0X38, 0XF4, 0XE7, 0X60, 0X48, 0X1F, 0Xff};//incoming data from Load Cell
uint16_t LS1 = 0;
uint16_t LS2 = 0;
uint16_t POWER_SUPPLY = 0;
uint16_t POWER_SUPPLY1 = 0;
uint16_t TEMP = 0;
float PS_Voltage = 0;
float PS_Voltage1 = 0;
float TempFloat = 0;
void setup() {

  // start Serial communication
  Serial.begin(115200);
  LS1 = (rx_frame[0] << 8) | rx_frame[1];
 LS2 = (rx_frame[2] << 8 ) | rx_frame[3];
  TEMP =  (rx_frame[4] *256);
 POWER_SUPPLY = (rx_frame[6] *256) + rx_frame[5];
  POWER_SUPPLY1 = (rx_frame[5] *256) + rx_frame[6];
  PS_Voltage = (POWER_SUPPLY * 5.00) /4096;// see text
  PS_Voltage1 = (POWER_SUPPLY1  * 5.0) / 4096.0; // see text
  TempFloat = (TEMP * 0.02) / 4096.0; // see text
  Serial.print("POWER SUPPLY= ");
  Serial.println(PS_Voltage, 5);
  Serial.print("POWER SUPPLY1 = ");
  Serial.println(PS_Voltage1 , 5);
  Serial.print("temperature = ");
  Serial.println(TempFloat);

}


void loop() {

}

The code above is what I'm trying but trying to adjust the values show that the float voltage macthes but cant get it quite right.
This is the output, I've switched the values around for POWER_SUPPLY1 just to try

POWER SIPPLY= 9.77539
POWER SUPPLY = 22.53784
temperature = 0.12

What would be the correct way to display the values so that they match the real data ?

Please explain the method you used to get 0x48 and 0x1F from 12.5.

Also post a link to the load cell and documentation.

I was wondering that too

uint16_t  rx_frame[8] = {0XDD, 0X38, 0XF4, 0XE7, 0X60, 0X48, 0X1F, 0Xff};//incoming data from Load Cell

rx_frame is an array of 8 values, each of which consists of 2 bytes. How does that match your description of what 6 bytes of the data represents ?

This is the data that I've captured from the device on the canbus, I cannot find the data sheet or any reference for it has it only got the manufactures part no.
I have a PCAN-API which reads the data from it and this is how I got the value.
Highlighted yellow in the picture are the 2 values that change when I alter the power supply voltage


So BOb I've used the ESP32 twai to read the can bus data coming and I just created the array so that I could easily change the values to try and calculate the values using the WOKWI simulator saving uploading to the main board all the time I make a change.

I know it makes it harder without having the data sheet or information on the load cell.
But placing a known load on the cell the first 2 hex values relate to LC1_mV/V the next 2 hex values relate LC2_mV/V and the 4th hex is the temp looking as the picture above

can you give results for some more voltages?

Why is is it an array of 16 bit values when what you will receive in practice is an array of 8 bytes ?

These are the other values but I'll leave the 0x out

volts   reading
10.00   C7,18
12.00   07,1E
12.50   48,1F
13.00   88,20
13.50   08,21
14.00   08,23
15.00   08,25
16.00   08,28
17.00   88,20
18,00   88,2F
19.00   08,32

Not sure this is the way I set up the code to read canbus from another project, but I may have got confused with alerts_to_enable using the TWAI library and the rx_frame is set by this twai_message_t rx_frame;
I can try the byte way and see if it works

You must get proper infromation how to get 12.50 from 0x48, 0x1F.

That's what I'm trying to figure and work out
Here are the 0.1 voltage readings from 12.00 to 13.00V Again without the 0X?? in front

Volts  Readings
12.0   08,1E
12.1   48,1E
12.2   88,1E
12.3   C8,1E
12.4   08,1F
12.5   48,1F
12.6   88,1F
12.7   C8,1F
12.8   08,20
12.9   48,20
13.00  88,20

Try by applying the following Scale factor:
0.00156 obtained from data pattern of #10.

byte  rx_frame[] = {0xDD, 0x38, 0xF4, 0xE7, 0x60, 0x48, 0x1F, 0xff};
int rawVolt =  rx_frame[6] << 8 | rx_frame[5]; //0x1F48 = 8008
float psVolt = rawVolt*0.00156; //12.49
Serial.println(psVolt, 2); //12.49 ~= 12.5


1 Like

The two graphs have a different slope. Some bits went missing. I converted "Readings" to DEC using LSB, MSB

1 Like

What scale factor do you expect from your curve?

1:1

I have claimed it as 0.00156 in #11.

It helps to convert the hex representation in post #10 to decimal, then fit to a line.

According to the data you collected, there is a small offset and the equation to convert the integer value to volts is

float volts = 0.0016*integer_value - 0.0125;

2 Likes

Looking at the data posted by @jremington,

the difference between 12 and 13 volts is 640

7688 = 12 * 640 + 8

8328 = 13 * 640 + 8

Which leads to an equation for 640 per volt, with an offset of 8:

float volts = (integer_value - 8) / 640.0;
2 Likes

1/640 = 0.0015625. Excel rounds that off to four digits past the decimal point to print 0.0016

I just like to say thank you all for your inputs and showing me the way I've ran some quick tests and they all seem to be good and reading the correct values. I've changed the float output to 1 decimal place and it reads the same.
Next I will look at the temperature reading, the hex value to that at the moment is 0XA0 (160 decimal value) and temp reading 34.5 degrees or I may tackle LC2_mV/V thats fluctuating between 0XD0,0X01 and 0XD4,0X01 with a voltage reading of 0.003500 which I will have to convert to display in kilograms

But what I will have to carry out some readings at different weights to work out the average where as the temperature will have to be done over time because I can't control that one unless I pop it in the freezer :wink:

It looks to me like you might have an "endianess" problem, so that the actual values are more like:

10.00   0x18C7
12.00   0x1E07
12.50   0x1F48
13.00   0x2088
13.50   0x2108
14.00   0x2308

(this explains the sudden jump to 0x08 from 0x48)
(implied by the @xfpd graph, BTW, but not explained explicitly.)