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
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);
}
}
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.
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.