Need help with converting Hex stream to LCD-data

Hello

I need help to read a hex stream from a serial port and convert HEX to decimal values to be printed on my LCD display.

I use an Arduino Duemilanove

I have mapped out all the bytes on a packet med no stuffing.

Stream: 7E FE 46 5D 5C BE 00 00 00 00 7E
Pos: 1 2 3 4 5 6 7 8 9 10 11

1,2,11 - used to mark begin&end
7,8,9,10 - unused
3 - ADC1, range is 3.3V = FF
4 - ADC2, range is 3.3V = FF
5 - link quality, 110db = 6E
6 - link quality2, 110db = 6E

I will print the decimal value of position 3,4,5 and 6

Data stream coming in continuously:
00 00 00 7E 7E FE 46 5D 5B BF 00 00 00 00 7E 7E FE 46 5D 5B C0 00 00 00 00 7E 7E FE 46 5D 5B C0 00 00 00 00 7E 7E FE 46 5D 5C BF 00 00 00 00 7E 7E FE 46 5D 5D BF 00 00 00 00 7E 7E FE 46 5D 5D BE 00 00 00 00 7E 7E FE 46 5D 5D BE 00 00 00 00 7E 7E FE 46 5D 5D BE 00 00 00 00 7E 7E FE 46 5D 5D BE 00 00 00 00 7E 7E FE 46 5D 5D BD 00 00 00 00 7E 7E FE 46 5

Writing to the display is not a problem but when I tried to get data in via Rx port on the Arduino, I haven't managed to select the desired data in the stream. You can see my setup and LCD below.

I hope some can help with a clue, and much like the necessary code to convert the data stream to int's to print.

Thanks in advance.

My setup and loop code so far: NO SERIAL INPUT SO FAR, just analog data for testing, not everything in use.

void setup()
{
  pinMode(txPin, OUTPUT);
  LCD.begin(9600);
  clearSerLcd();
  backlightSerLcd(50);

  displaySerLcdChar(1,5, "Analog data");  
  displaySerLcdChar(2,1, "AD1"); 
  displaySerLcdChar(2,10, "mV"); 
  displaySerLcdChar(2,13, "TEMP 15 C"); 
  displaySerLcdChar(2,20, " ");   
  displaySerLcdChar(3,1, "AD2"); 
  displaySerLcdChar(3,10, "mV");
  displaySerLcdChar(4,1, "RSSI"); // AV-receiver RSSI
  displaySerLcdChar(4,10, "%");
  displaySerLcdChar(4,12, "RSSI"); 
  displaySerLcdChar(4,20, "%");
}



void loop() {
  
  // read the value from the sensor:
//  sensorValue = analogRead(sensorPin); 
//  TempValue = analogRead(sensorPin1); 
  RSSIValue = analogRead(sensorPin2);
  // Convert inputdata to mV, calculation: 5V / 1.0230 = 4.8876;
//  millivolts = sensorValue * 4.8876; 
//  Tempvolts = TempValue * 4.8876; 
//  Temp =  Tempvolts / 10 - 284; 
  RSSIAVrx = RSSIValue * 0.12773; // calculation for %               : (5V / 1.0230) / 50 = 0.09775; 
                                  // calculation for % with 3.7V=100%: (RSSIValue * 4.8876) / 0,7653 / 50 = RSSIValue * 0.12773

  //Print Data  
  displaySerLcdChar(2,5, "0");
  LCD.print(" ");
  displaySerLcdChar(3,5, "0"); // displaySerLcdData(3,5, millivolts);
  LCD.print(" ");  
  displaySerLcdData(4,6, RSSIAVrx); // AV-receiver RSSI
  LCD.print(" ");

}


The LCD-display are only showing text.

You'll need to read every byte that comes in, as it comes in. Use Serial.available() to determine how many bytes are available, and Serial.read() in a loop to read them.

You only need to store 11 bytes, so create an array:

byte usefulData[11];

You'll need to know where in the array to store data, so create an index:

byte index;

When reading the stream, ignore everything until you see the start of packet marker (7E). When you see the start of packet marker, set index to 0, and store the byte in the array using index as the position:

usefulData[index] = byteRead;

If the byte read is actually the end of a packet, that won't matter, as it will be followed immediately by the start of packet marker.

Once you get the start of packet marker, wait for, and read, the next byte. Increment index, and save the byte. If the byte is FE, then wait for, read, and store the next 9 bytes.

If the last byte is 7E, you know you got a complete packet, so extract and display the appropriate bits. If not, just keep reading until a complete packet arrives.

Thank you PaulS

This brought me a little further, and I understand the process, but I find it hard to grasp how the byte sorting process must be programmed. Can you or someone else help with getting the individual bytes into the variables.

I've written the following code in the light of what you wrote, but is aware of is missing the key, wait for 7E etc.

byte usefulData[11];   // Space to store the Bytes read
byte index;   // Index into array; where to store the Bytes

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

void loop()
{
  while(Serial.available() > 0){
    
    if(index < 10) // One less than the size of the array
    {
      byteRead = Serial.read(); // Read a Byte
      usefullData[index] = byteRead; // Store it


        }
    }
  }
}

Try something like this:

byte usefulData[11];   // Space to store the Bytes read
byte index;   // Index into array; where to store the Bytes
byte someByte;
byte prevByte = 0;

#define SEOP 0x7E

boolean started = false;
boolean ended = false;

void loop()
{
   while(Serial.available() > 0)
   {
      byte someByte = Serial.read(); // Read the next byte
      if(started && !ended) // We've got a start-of-packet, but no end-of-packet
      {
         index++;
         usefulData[index] = someByte;
      }
      else if(someByte == SEOP) // It's either a start or end marker
      {
         if(!started || prevByte == SEOP) // Then this is the start marker
         {
            index = 0;
            usefulData[index] = someByte;

            started = true;
         }
         else
         {
            index++;
            usefulData[index] = someByte;

            ended = true;
            break;
         }
      }

      prevByte = someByte;
   }

   if(started && ended) // We've got both markers
   {
       // Do something with usefulData

       started = false;
       ended = false;
   }
}

This code will look at each byte that comes in. If the byte is 0x7E, it is either a start of packet marker or an end of packet marker. It is a start marker if the previous byte is also 0x7E. Start and end markers are added to the array.

It the byte is not 0x7E, then it is added to the array.

When all the available data has been read, or the end of packet marker is encountered, the while loop ends.

If both markers (start and end) have been received, then the data can be used, the pointer is reset to the start of the array, and the flags are reset.