weired data when I commented out several (irrelevant) lines of codes

Hei guys.
I was trying to access FS A8s RC serial receiver, using IBUS protocol. The protocol itself wasn't that hard to figure out. Each data packet come in 32byte. 2 first bytes are headers 0X20 and 0X40. next 28 bytes are stick value. and the last 2 bytes are checksum.

CODE1:

#include <SoftwareSerial.h>
#define HEADER_1 0x20
#define HEADER_2 0x40

SoftwareSerial softS(10, 11); // RX, TX
uint8_t buff;
uint8_t buffArr[32];
uint8_t index = 0;
uint16_t ch[10];

uint16_t ckSum = 0;
uint16_t buffSum = 0;



void setup() {
	// Open serial communications and wait for port to open:
	Serial.begin(9600);
	Serial.println("SERIAL_TEST 1");

	softS.begin(115200);
	buffArr[0] = HEADER_1;
	buffArr[1] = HEADER_2;
}

void loop() { // run over and over
	buff = softS.read();

	switch (index) {
		case 0: //check header 1
			if (buff == HEADER_1) index = 1;
			break;
		case 1: //check header 2
			if (buff == HEADER_2) index = 2;
			else index = 0;
			break;
		default: 
			if (index < 32) { //assigning value from serial read to buffer array
				buffArr[index] = buff;
				index++;
			}
			else { //reached end of data packet
				
				buffSum = 0xFFFF;
				for (int i = 0; i < 30; i++) buffSum -= buffArr[i];
				ckSum = (buffArr[31] << 8) | buffArr[30];

				// when I commented out these few lines of codes none of my data match it's checksum
				for (int i = 0; i <32; i++) { 
					Serial.print(buffArr[i]);
					Serial.print(' ');
				}
				////////////////////////////////////////////

				Serial.print("CKSUM = ");
				Serial.print(ckSum);
				Serial.print(" ");
				Serial.println(buffSum);

				if (buffSum == ckSum) Serial.println(" MATCH");
				index = 0;
			}
			break;
	}
}

CODE1 output:

32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 4 220 5 220 5 220 5 51 243 CKSUM = 62259 62260
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 193 220 5 220 5 51 243 CKSUM = 62259 62071
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 255 255 255 255 255 255 255 CKSUM = 65535 61439
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 193 175 176 102 254 CKSUM = 65126 61945
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 232 3 220 5 232 3 232 3 CKSUM = 1000 62239
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 244 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62247
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259
 MATCH
32 64 220 5 220 5 232 3 220 5 232 3 232 3 208 7 208 7 232 3 232 3 220 5 220 5 220 5 220 5 51 243 CKSUM = 62259 62259

but when I commented a few lines of code like so:
CODE2:

#include <SoftwareSerial.h>
#define HEADER_1 0x20
#define HEADER_2 0x40

SoftwareSerial softS(10, 11); // RX, TX
uint8_t buff;
uint8_t buffArr[32];
uint8_t index = 0;
uint16_t ch[10];

uint16_t ckSum = 0;
uint16_t buffSum = 0;



void setup() {
	// Open serial communications and wait for port to open:
	Serial.begin(9600);
	Serial.println("SERIAL_TEST 1");

	softS.begin(115200);
	buffArr[0] = HEADER_1;
	buffArr[1] = HEADER_2;
}

void loop() { // run over and over
	buff = softS.read();

	switch (index) {
		case 0: //check header 1
			if (buff == HEADER_1) index = 1;
			break;
		case 1: //check header 2
			if (buff == HEADER_2) index = 2;
			else index = 0;
			break;
		default: 
			if (index < 32) { //assigning value from serial read to buffer array
				buffArr[index] = buff;
				index++;
			}
			else { //reached end of data packet
				
				buffSum = 0xFFFF;
				for (int i = 0; i < 30; i++) buffSum -= buffArr[i];
				ckSum = (buffArr[31] << 8) | buffArr[30];

				// when I commented out these few lines of codes none of my data match it's checksum
				/*for (int i = 0; i <32; i++) { 
					Serial.print(buffArr[i]);
					Serial.print(' ');
				}*/
				////////////////////////////////////////////

				Serial.print("CKSUM = ");
				Serial.print(ckSum);
				Serial.print(" ");
				Serial.println(buffSum);

				if (buffSum == ckSum) Serial.println(" MATCH");
				index = 0;
			}
			break;
	}
}

none of my data match its checksum
CODE2 output:

CKSUM = 65535 59452
CKSUM = 65512 59429
CKSUM = 65512 59429
CKSUM = 65535 59452
CKSUM = 65512 59429
CKSUM = 65535 59452
CKSUM = 65535 59452
CKSUM = 65512 59429
CKSUM = 65535 59452
CKSUM = 59647 59429
CKSUM = 65535 59452
CKSUM = 65535 59452
CKSUM = 65512 59429
CKSUM = 65535 59452
CKSUM = 1023 59452
CKSUM = 65512 59429
CKSUM = 65512 59429
CKSUM = 65535 59452
CKSUM = 59647 59429
CKSUM = 65535 59452
CKSUM = 1023 59452
CKSUM = 65512 59429
CKSUM = 65535 59452
CKSUM = 65535 59452
CKSUM = 65512 59429
CKSUM = 65512 59429
CKSUM = 65535 59452

isn't it kind of weird because what I commented out is just some code to print out raw data value I get from serial buffer, or am I just plain stupid to miss out something obvious ?. I was trying to figure this out for the last couple of days and came out with nothing.

What Arduino are you using? SoftwareSerial won't work at 115200 baud on a 16MHz Arduino.

My guess is that when you comment out the print statements you upset the timing of a piece of code that was only working properly by accident.

...R

Robin2:
What Arduino are you using? SoftwareSerial won't work at 115200 baud on a 16MHz Arduino.

My guess is that when you comment out the print statements you upset the timing of a piece of code that was only working properly by accident.

...R

hey Robin2 thanks for your reply.
I'm using Arduino nano, I believe its 16MHz. I'll just have to switch to my one and only hardware serial then :frowning:

Abizhar:
I'm using Arduino nano, I believe its 16MHz. I'll just have to switch to my one and only hardware serial then :frowning:

If you need to use HardwareSerial for your project then if you have a USB-TTL cable you can use SoftwareSerial at 9600 baud for debug messages to the Serial Monitor.

Note that you will need to disconnect from HardwareSerial whenever you need to upload a program - which can be a PITA. Maybe use a Mega which has 3 spare HardwareSerial ports? Or use a Micro which has 1 spare HwS port.

...R