How to Arduino read the HEXA data stream

How to Arduino read the HEXA data stream this format FF 07 01 01 35 00 00 00 3D from the sensor using RX TX pin and exact data and my sensor output is TTL 3,3 Logic Level, Kindly guide me

const byte BUFFER_SIZE=30;
char buf[BUFFER_SIZE];

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // check if data is available
  if (Serial.available() > 0) {
    // read the incoming bytes:
    int rlen = Serial.readBytes(buf, BUFFER_SIZE);

    // prints the received data
    Serial.println(" I received: ");
    for(int i = 0; i < rlen; i++)
      Serial.print(buf[i],HEX);
  }

}

I would suggest to study Serial Input Basics to get a better grasp on how to listen to the Serial port and then apply your knowledge to a binary stream

1 Like

I used it but no getting the desired result

You should set your buffer as being equal to the message size and then you should check for the message size being available before reading it.

#define MESSAGE_SIZE 9
uint8_t message[MESSAGE_SIZE];
..
if (Serial.available() >= MESSAGE_SIZE)
{
  //Read and process..
}
1 Like

Which Arduino you are using?

Which byte belongs to which field when compared with following chart you have provided?

1 Like

The StreamDevice class I mentioned recently (Purpose of delay in sim800l at commands - #5 by J-M-L) could help you

Facing problem on sending SMS on different condition Using GSM module and Arduino mega - #17 by sreekanthmp

1 Like

i am using Arduino Uno

Is the problem that you are using Serial both to read messages from your device AND send messages to your PC? Perhaps you should use SoftwareSerial to talk to your device.

#include <SoftwareSerial.h>

SoftwareSerial device(2, 3);  // Pin 2 = RX, Pin 3 = TX

void setup()
{
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  delay(200);
  device.begin(9600);
}

void loop()
{
  // check if data is available
  if (device.available())
  {
    char inChar = device.read();

    if (inChar < 0x10)
      Serial.print('0');
    Serial.println(inChar, HEX);
  }
}
2 Likes

if (inChar < 0x10)
what is meaning of this statement especially 0x10 why I use this

    // Add a leading zero if the byte is shorter than 
    // two hex digits.
    if (inChar < 0x10)
      Serial.print('0');
1 Like

Please, state the type number of your sensor or post a link in order to suggest if you need level shifter to operate by UNO.

1 Like

You might want to go through some basic development guides in order to learn the basics..

1 Like



i test the code but getting not same result as on docklight terminal the snapshops of both attached kindly guide me to read exact data


this my sensor spec

can we need shifter level for arduino because or sensor output is on tx rx is 3.3 volt TTL

I see the problem. My sketch is doing a sign-extension on the 'FF' byte so it shows up as 'FFFFFFFF'. That would have been obvious if you had left it as one byte per line or had put a space between bytes.

Change:
char inChar = device.read();
To:
unsigned char inChar = device.read();

And add a Serial.print(' '); after the Serial.println(inChar, HEX); that you changed to Serial.print(inChar, HEX);. That will put a space between bytes to make it easier to see the boundaries.

1 Like

using this code i am getting values wrong

Thank this method working but need the code to take the 5th and 6th position hexa value to perform calculation

"Wrong" in what way, exactly?

1 Like

sending random Hexa format