Softserial missing zero's from a data string

I'm new to this so hopefully a simple issue!
I'm trying to use softserial to read a hex data string but it's dropping zero's compared to using the UART
UART string reads:- AA10002174100001113809005400051D5A00382205463822087C0D147158087C0F237158
Softserial reads:- AA1002174100111382615A047A6003822540382200124715887CF237158

It's a fixed format string and lining up the characters there are random zero's missing from the Softserial string????

AA10002174100001113809005400051D5A00382205463822087C0D147158087C0F237158
AA100 217410 011138 2615A0 47A600 3822 5403822 001 247158 87C F237158

Any solutions???

Sketch is:

#include <Arduino.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 5);

#define LED2 2 // yellow - 2

void setup()
{
  Serial.begin(76800);
   delay(100);
  Serial.println("Start");

  // Set LEDs as outputs
  pinMode(LED2, OUTPUT);

  while (!Serial)
  {
    /* code */
  }

  mySerial.begin(9600);
}

void loop()
{
  byte buffer[42];

  byte duncan = 0

  // Turn LED on
  delay(500);
  digitalWrite(LED2, LOW);

  // Print line to give easy debug if where we are in the pr  ogram
  Serial.println("waiting");

  // Wait until any data available on serial
  while (!mySerial.available())
  {
  }

  // Turn off LED, this will only happen if there is any data
  digitalWrite(LED2, HIGH);

  // 1
byte duncan = mySerial.readBytes( buffer,1);
  
  If (buffer[1] = 170)

  int data1 = mySerial.readBytes( buffer ,42);
  

  Serial.println("data read - ");
  delay(50);
  
  for (int i = 0 ; i < data1 ; i++) {
   Serial.print(buffer[i],HEX);
 }

 //Serial.print('\n');
  //  Serial.println(data1);
  delay(50);
 // Serial.println(char(data1));
  delay(50);
   //Serial.println(data1,BIN);

  // // 2
  // int data2 = mySerial.read();

  // Serial.println(String(data2));

  // // 3
  // int data2 = mySerial.readStringUntil(',');

  // Serial.println(String(data2));
}

Welcome to the forum

Nowhere in the sketch that you posted does SoftSerial read or write any data. Did you post the right sketch ?

Presumably not as you have edited your post and changed the sketch

Please post the actual code that demonstrates the problem you described. The above is not it. It won't even compile.

Apologies, I've tried several iterations so I need to back out the changes!!

You may not realise that

Serial.print(buffer[i],HEX);

will suppress the leading zero if the value is less than 0x10. But that's true for both hardware and software serial.

There is more than one way to fix that

if (buffer[i] < 0x10) Serial.print("0");
Serial.print(buffer[i],HEX);

or

char buffer2[3];
sprintf(buffer2, "%02X", buffer[i]);
Serial.print(buffer2);

Why does "Serial.print(buffer[i],HEX); " supress leading zero's?
The issue with the "If " statement is that some xx10's are being replaced?
It looses the xxAA sync byte from the string?? I need to decode specific bytes of the string into decimal numbers but if the zero's are dropped then the byte count goes out and potentially will decimalize the wrong byte!!
Is the issue with the Serial.print but the byte count in the serial buffer is correct??

PS I tried the "If" statement and it lost the xxAA sync byte!

Because that is how it works

You and I might think that it should not do that but we are stuck with it, hence the suggested workaround

The issue with the "If" is that C/C++ is case sensitive. "if" is good but "If" will give an error. That's how I knew the code you posted wasn't the version you tested.

You can tell I'm a C++ virgin :slight_smile:

If read a single byte to a buffer, the result will be in buffer[0] and not in buffer[1]

Also take the note about the difference between = and ==

PS
This is not code but some kind of mess.
Please show a correct code

1 Like

Is it really necessary to go via hex format?

Perhaps if you explain what you are ultimately trying to achieve, the forum can suggest a better/easier way.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.