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