Hi guys,
I have 2 arduino unos connected like this:
Arduino 1 Rx -> Arduino 2 Tx
Arduino 1 Tx -> Arduino 2 Rx
One arduino is configured as master (only sends) and the other one as slave (only receives). Master is sending 60 bytes on 3 second intervals. Slave is able to receive all the 60 bytes BUT only the first 10 bytes are correct. And that is always so. Even if I send fewer bytes than 60. And the question goes, what could be causing that kind of behavior? It has baffled me couple of evenings already.
I can confirm that master is sending correct data because I have connected serial->usb module directly to master arduino TX pin and serial monitor is showing exactly the same data that master is sending. I have also tried adding 2ms delays between each byte send, but without no effect. Also tried different baud rates and smaller packet sizes, jet again without luck.
Master code:
#include <SoftwareSerial.h>
//---
SoftwareSerial ConsoleSerial(8, 9); // RX, TX
int RunLedState = 0;
const byte gNetworkPacketSize = 60; //in bytes
//---
void setup() {
Serial.begin(9600);
ConsoleSerial.begin(9600);
ConsoleSerial.println("MASTER"); //remove
}
//---
void loop() {
onNetworkMessageTransmit();
//toggle run led
digitalWrite(LED_BUILTIN, (RunLedState) ? HIGH : LOW);
RunLedState = !RunLedState;
delay(3000);
}
//---
void onNetworkMessageTransmit() {
for (byte n = 0; n < gNetworkPacketSize; n++) {
Serial.print((byte)n);
}
Serial.println();
}
Slave code:
#include <SoftwareSerial.h>
//---
SoftwareSerial ConsoleSerial(8, 9); // RX, TX
int RunLedState = 0;
const byte gNetworkPacketSize = 60; //in bytes
byte gNetworkBuffer[gNetworkPacketSize];
byte gNetworkBufferReadPos = 0;
//---
void setup() {
Serial.begin(9600);
ClearNetworkBuffer();
ConsoleSerial.begin(9600);
ConsoleSerial.println("SLAVE"); //remove
}
//---
void loop() {
//network messages receive event
while (Serial.available() > 0) {
if (onReadNetworkBuffer(Serial.read()) == 1) { //end marker found, process data
for (byte n = 0; n < gNetworkPacketSize; n++) { //remove
ConsoleSerial.print((char)gNetworkBuffer[n]);
ConsoleSerial.print(";");
}
ConsoleSerial.println();
ClearNetworkBuffer();
}
}
//toggle run led
digitalWrite(LED_BUILTIN, (RunLedState) ? HIGH : LOW);
RunLedState = !RunLedState;
}
//---
byte onReadNetworkBuffer(const byte inByte) {
if (inByte == '\n') return 1; //end of transmission, proccess received data
if (inByte == '\r') return 2; //discard carriage return
if (gNetworkBufferReadPos < gNetworkPacketSize) gNetworkBuffer[gNetworkBufferReadPos++] = inByte; //keep adding to buffer if not full
//if (gNetworkBufferReadPos >= gNetworkPacketSize) //overflow
return 0;
}
//---
void ClearNetworkBuffer() {
gNetworkBufferReadPos = 0;
for (byte n = 0; n < gNetworkPacketSize; n++) {
gNetworkBuffer[n] = 0;
}
}
I.e. this is the data that slave is showing (10 first bytes seams to be correct):
0;1;2;3;4;5;6;7;8;9;1;0;1;1;1;2;1;3;1;4;1;5;1;6;1;7;1;8;1;9;2;0;2;1;2;2;2;3;2;4;2;5;2;6;2;7;2;8;2;9;3;0;3;1;3;2;3;3;3;4;
but I expected it to be like that:
0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;