Interpreting Hexadecimal Data From Sensor? Honeywell CSNV700

Hello
I am using the Honeywell CSNV700 sensor and am struggling to understand the data. The sensor gives out outputs, but I am unsure if I am using start bit, length, and hexadecimal correctly. The link to the sensor is (CSNV700 Series Flux Gate Current Sensors | Honeywell) or can be found on google with a data sheet.

My current code tries to take a value x that the sensor outputs and tries to read the ipvalue which starts at bit 24 and has a length of 32 bits. This hexadecimal value should ideally read
80000000h = 0 mA
7FFFFFFFh = -1 mA
80000001h = 1 mA
which corresponds to the table on the data sheet, provided in the link above. However, I am not getting any hexadecimal values with readable data. Am I missing something here?


long long num = 1272552552450002;
String numbinary;
String numhex;
int binaryarray[32];

void setup() {
  Serial.begin(9600);
  //convert num to binary
  numbinary = String(num, BIN);
}



void loop() {
  Serial.print("binary: ");
  Serial.println(numbinary);
  /*Serial.print("24th bit: ");
  int bit24 = numbinary.charAt(24) == '1' ? 1 : 0; 
  Serial.println(bit24);*/
  
  //get 32 bits
  Serial.print("32 bits extracted: "); 
  int j = 24;
  for (int i = 0; i < 32; i++) {
    binaryarray[i] = numbinary[j] == '1' ? 1 : 0; // Check if the bit is '1' or '0' and store it
    //binaryarray[i] = (numbinary, j);
    Serial.print(binaryarray[i]); 
    //Serial.print(i);
    j++;
  }

  //convert to decimal
  long decimalValue = 0;
  long bitWeight = 1;
  for (int i = 31; i >= 0; i--) {
    decimalValue += binaryarray[i] * bitWeight;
    bitWeight *= 2;
  }

  numhex = String(decimalValue, HEX); //convert to hexadecimal

  Serial.println();
  Serial.print("Decimal value: ");
  Serial.println(decimalValue);
  Serial.print("HexadecimalValue: ");
  Serial.println(numhex);
  Serial.println();
  delay(1000);
}






replace

with

  Serial.print("\r\nDecimal value: ");
  Serial.println(decimalValue, DEC);

  Serial.print("HexadecimalValue: ");
  Serial.println(decimalValue, HEX);
  Serial.println();

To clarify, are you saying I should work with the original 8 bytes and converting to hexadecimal and try to go from there?

int32_t num = 0x80000001;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  Serial.print("\r\nbinary: ");
  Serial.print(num,BIN);
  
  Serial.print("\tDecimal value: ");
  Serial.print(num, DEC);

  Serial.print("\tHexadecimalValue: ");
  Serial.println(num, HEX);

  Serial.print(int(num+0x80000000));
  Serial.println("mA");
  delay(1000);
  num--;
}

Please post the code that reads the sensor. It is easier to extract the value from that.

Keep in mind that all numbers in a computer are binary. Hexadecimal notation and decimal notation are simply two different human-readable representations of binary numbers.

Hello,
I have been working on a project using a sensor Honeywell CSNV700 current sensor and CAN bus to try and get data. (CSNV700 Series Flux Gate Current Sensors | Honeywell)

I have gotten the data packets but now am stuck on how to interpret the data.
One of the sensor values is the current which is at start bit 24 and extends length 32.
The sensor sends data in packets that are 8 bytes. I am attempting to take these 8 bytes which are in big endian order as the sensor states, and get that current value. Currently, I am not sure if I am missing a step or there is something wrong with my code. I am converting the sensor value into a binary string so that I can get these byte values and then find the current value by reassembling these byte values back together. Any help would be greatly appreciated.

#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include <FlexCAN_T4.h>
#include <ST7735_t3.h>
#include <ST7789_t3.h>
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2; //look intor transmission data
#define TFT_RST    9 //hardware teensy
#define TFT_DC     8
#define TFT_MOSI   11
#define TFT_SCLK   13
#define TFT_CS     10
#define SD_CS     7
ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); //
int len1 = 0;
int len2 = 0; 
int lenFD = 0;
String id1;
String id2;
String idFD;
String idhit1;
String idhit2;
String data1;
String data2;
String dataFD;
bool newData1 = true;
bool newData2 = true;
bool newDataFD = true;
unsigned long long number = 127255255249000137ULL;  // Your number
unsigned long long mask = 0xFFFFFFFFULL;  // A 32-bit mask with all bits set to 1
String hexstring;
unsigned long long signalValue = number & mask;
String rawhex = 0;

String bit2;
String bit3;
String bit4;
String bit5;

long long num = 127255255251000299;
String numbinary;

String out;
unsigned long long outbin;
unsigned long long amp;

void setup() {
  Serial.begin (9600);
  can2.begin();
  can2.setBaudRate(500000);
  can2.enableFIFO();
  can2.enableFIFOInterrupt();
  can2.onReceive(canSniff2);
  Serial.print("LEN2\t");
  Serial.print("ID2\t");
  Serial.print("IDHIT2\t");
  Serial.print("DATA2\t\t\t\t");
}



String SensVal(int startBit, int length, String numbinary) {
  String result = "";
  for (int i = 0; i < length; i++) {
    result += numbinary.charAt(numbinary.length() - startBit - i - 1) - '0';
    //Serial.println(result);
  }
  return result;
}

unsigned int binaryToDecimal(String binaryString) {
  unsigned int decimalValue = 0;
  for (int i = 0; i < binaryString.length(); i++) {
    char c = binaryString.charAt(i);
    if (c == '0' || c == '1') {
      decimalValue = decimalValue * 2 + (c - '0');
    } else {
      return 0;
    }
  }
  return decimalValue;
}



void loop() {
  delay(100);
  if (!id2.equals("") && newData2) {
    Serial.print(len2);
    Serial.print("\t");
    Serial.print(id2);
    Serial.print("\t");
    Serial.print(idhit2);
    Serial.print("\t");
    Serial.print(data2);
    Serial.print("\t");
    if(data2.length()<13){
      Serial.print("\t");
    }
    Serial.print("\t");

    num = atoll(data2.c_str());
    numbinary = String(num, BIN);
    out = SensVal(24, 32, numbinary);
    outbin = binaryToDecimal(out);

    amp = outbin - 2147483648;
    //amp = outbin/2147483648;
    Serial.print("Theoretical Amp: ");
    Serial.print(amp);
    Serial.print("\t");
    Serial.print("\t");

    newData2 = false;
  } else {
    Serial.print("\t");
    Serial.print("\t");
    Serial.print("\t");
    Serial.print("\t");
  }
  can2.events();
  Serial.println();
}
void canSniff2(const CAN_message_t &msg) {
  len2 = msg.len;
  id2 = String(msg.id, HEX);
    idhit2 = (String)msg.idhit;
    data2 = "";
    Serial.print("buffer: ");
    for (uint8_t i = 0; i < msg.len; i++) {
      data2 = data2 + (String)msg.buf[i];
      Serial.print(msg.buf[i]);
      Serial.print("\t");
    }
    /*bit2 = msg.buf[i];
    bit2 = String(num, BIN);

    bit3 = msg.buf[i];
    bit4 = msg.buf[i];
    bit5 = msg.buf[i];*/
    newData2 = true;
  }



Do not cross-post. Other thread removed.

And threads merged.

  if (!id2.equals("") && newData2) {
    Serial.print(len2);
    Serial.print("\t");
    Serial.print(id2);
    Serial.print("\t");
    Serial.print(idhit2);
    Serial.print("\t");
    Serial.print(data2);
    Serial.print("\t\t");

    if (data2.length() < 13) {
      Serial.print("\t");
    }

    num = atoll(data2.c_str());
    numbinary = String(num, BIN);
    out = SensVal(24, 32, numbinary);
    outbin = binaryToDecimal(out);

    amp = outbin - 2147483648;
    //amp = outbin/2147483648;
    Serial.print("Theoretical Amp: ");
    Serial.print(amp);
    newData2 = false;
  }
  //else Serial.print("\t\t"); // probably empty end is not necessary

  Serial.println();
  can2.events();

maybe try something like this to unpack your received CAN frame:
(Compiles, NOT tested!)

#define ERROR 0x01 //tbc, should it be 0x80 or 0x01??

const uint32_t ZeroAmpsRaw_Ref = 0x80000000;

struct {
  uint8_t Code;
  char const  *Description;
} Errors[6] = {{0x91, "Flash_CRC_Error"}, {0x93, "AFE_Over_Range"}, {0xA1, "AFE_Error"}, {0xA3, "Internal_LUT_Error"}, {0xA9, "Min_Power_Limit"}, {0xAB, "Max_Power_Limit"}};

struct __attribute__((__packed__)) {
  int32_t IP_Value;
  uint8_t Error_Information;
  uint16_t Reserved;
  uint8_t CRC_8;
} Primary_Current;

void setup() {
  uint8_t received_frame[8] = {0xFF, 0xFF, 0xFF, 0x7F, 0xA3, 0x00, 0x00, 0xFF}; //CRC not computed for example

  Serial.begin(115200);

  memcpy(&Primary_Current, received_frame, 8);

  Serial.println(Primary_Current.IP_Value, HEX);

  Primary_Current.IP_Value -= ZeroAmpsRaw_Ref;

  Serial.print("Ip(mA): ");
  Serial.println(Primary_Current.IP_Value);

  if ((Primary_Current.Error_Information & ERROR) == ERROR) {
    for (uint8_t i = 0; i < 6; ++i) {
      if (Primary_Current.Error_Information == Errors[i].Code) {
        Serial.println(Errors[i].Description);
        break;
      }
      else if(i==5) {
        Serial.print("Unknown Failure Detected! 0x");
        Serial.println(Primary_Current.Error_Information>>1, HEX);
      }
    }
  }
  else{
    Serial.println("Condition Normal");
  }

}

void loop() {
  // put your main code here, to run repeatedly:

}

hope that helps....

would you be able to post a couple of those data sets (if you have the corresponding real current as well would be handy!) would then be able to use this real data to test the codes proposed in post#11

@jason123chat
This and your other topic on the same subject closed. I should really merge the 2 topics but this far in I think it would be pointless and produce a horrible mess.

Please do not create multiple topics on the same subject again, you risk a ban from the forum.

Thanks