why for input hex values FF FF FF FF 0D BC CB 8E atmega2560 is giving 00 00 00 00 79 48 63 E2 on debug port i am keeping baud rate same only input is at serial 1 and debug serial is at serial 0 this is happening with mega2560 Arduino kit also
my code attached
const int PACKET_SIZE = 139;
uint8_t gpsData[PACKET_SIZE];
//uint8_t byte;
char sentenceRMC[100];
char sentenceGGA[100];
float lastHeadingDeg = 0.0f; // most recent heading to send
uint8_t calcChecksum(const char* s) {
uint8_t sum = 0;
for (int i = 1; s[i] != '*' && s[i] != '\0'; i++) {
sum ^= s[i];
}
return sum;
}
void buildNMEASentences() {
// Simulated extraction from packet
int hour = gpsData[0]; // byte 0
int minute = gpsData[1]; // byte 1
int second = gpsData[2]; // byte 2
char ns = gpsData[6]; // byte 6 (e.g., 'N')
char ew = gpsData[10]; // byte 10 (e.g., 'E')
float speed = 22.4; // Dummy values
float course = 84.4;
int day = gpsData[15];
int month = gpsData[16];
int year = gpsData[17];
// Build RMC
sprintf(sentenceRMC,
"$GPRMC,%02d%02d%02d,A,4807.038,%c,01131.000,%c,%.1f,%.1f,%02d%02d%02d",
hour, minute, second, ns, ew, speed, course, day, month, year);
uint8_t crcRMC = calcChecksum(sentenceRMC);
sprintf(sentenceRMC + strlen(sentenceRMC), "*%02X\r\n", crcRMC);
// Build GGA
sprintf(sentenceGGA,
"$GPGGA,%02d%02d%02d,4807.038,%c,01131.000,%c,1,08,0.9,545.4,M,46.9,M,,",
hour, minute, second, ns, ew);
uint8_t crcGGA = calcChecksum(sentenceGGA);
sprintf(sentenceGGA + strlen(sentenceGGA), "*%02X\r\n", crcGGA);
}
void setup() {
Serial.begin(115200); // Debug
Serial1.begin(115200); // Read 139-byte packet from Serial
Serial2.begin(115200); // Output NMEA to USB serial1
//Serial1.println("Waiting for 139-byte packet...");
}
void loop() {
static int index = 0;
uint8_t syncMatch = 0;
while (Serial1.available() && index < PACKET_SIZE) {
gpsData[index++] = Serial1.read();
/*if(2 < index < 4)
{
if (gpsData[0] == 0xFF && gpsData[1] == 0xFF &&
gpsData[2] == 0xFF && gpsData[3] == 0xFF)
{}
else
{index = 0;}
} */
/*if (syncMatch < 4) {
// Still searching for sync pattern
if (byte == 0xFF) {
syncMatch++;
if (syncMatch == 4) {
// Sync fully detected → reset buffer & store first byte
index = 0;
gpsData[index++] = byte;
gpsData[index++] = byte;
gpsData[index++] = byte;
gpsData[index++] = byte;
//rxIndex = 3;
}
} else {
syncMatch = 0; // reset if mismatch
}
}
else {
gpsData[index++] = byte;}//Serial1.read();*/
}
Serial.print("Packet: ");
for (int i = 0; i < PACKET_SIZE; i++) {
if (gpsData[i] < 0x10) Serial.print("0"); // leading zero
Serial.print(gpsData[i],HEX);
Serial.print(" ");
}
Serial.println();
uint32_t b1 = gpsData[46];
uint32_t b2 = gpsData[47];
uint32_t b3 = gpsData[48];
uint32_t headingHex = (b1 << 16) | (b2 << 8) | b3;
// float heading = headingHex * 0.000021457672119140625;
lastHeadingDeg = (float)headingHex * 0.000021457672119140625f;
char headingStr[12];
dtostrf(lastHeadingDeg, 0, 3, headingStr); // width=0, precision=3
char sentence[32];
snprintf(sentence, sizeof(sentence), "$HEHDT,%s,T", headingStr);
// Compute XOR checksum over characters after '$' up to before '*'
uint8_t cs = 0;
for (size_t i = 1; sentence[i] != '\0'; ++i) cs ^= (uint8_t)sentence[i];
// Transmit
Serial2.print(sentence);
Serial2.print('*');
if (cs < 16) Serial2.print('0');
Serial2.print(cs, HEX);
Serial2.print("\r\n");
index = 0;
/*if (index == PACKET_SIZE) {
index = 0; // Reset for next packet
buildNMEASentences();
Serial1.print(sentenceRMC);
Serial1.print(sentenceGGA);
}*/
}

