Serial com between 2 arduinos

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;

The loop prints one character the one ;.

10 is two characters. So your loop cannot print what you expect.

void onNetworkMessageTransmit() {
 for (byte n = 0; n < gNetworkPacketSize; n++) {
  Serial.print((byte)n); /////// ROFL!!!!!
 }
 Serial.println();
}

How many bytes do you think your sending?

Mark

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

@robin2 The issues not at the RX end!

Mark

You're mixing up ASCII and binary.

Serial.print(10) sends the characters 1 and 0 (two bytes).

Serial.write(10) sends the binary value 10 (single byte).

So replace

  Serial.print((byte)n);

by

  Serial.write(n);

in the master for starters. Then you send the byte values instead of trying to send the ASCII representation of the number.

Then in the slave replace

    ConsoleSerial.print((char)gNetworkBuffer[n]);

by

    ConsoleSerial.print(gNetworkBuffer[n]);

In this case you actually want to see the ASCII representation of the number, rather than trying to convert this number to an ASCII character (ASCII characters <32 are non-printable).

Also do make sure on the slave side that the output Serial is at a higher baudrate than the input Serial as otherwise the output can't keep up, and you may get problems with buffers overflowing when data volume gets large.

That said, my preference would be I2C or ISP when trying to communicate between two boards. Much easier to deal with as the protocols have the start/stop conditions built in, and both allow for much faster communications.

holmes4:
@robin2 The issues not at the RX end!

I had the impression from this statement by the OP

I.e. this is the data that slave is showing (10 first bytes seams to be correct):

that the problem was on the receiving end.

In the sense that the receiver must match the sender perhaps it is impossible to say which end has issues. Making either one match the other will solve the problem.

My strong preference is for sending data in human readable form for easier debugging unless the greater efficiency of binary is absolutely essential.

...R

Just look at the output OP posted... Take out the separators:

01234567891011121314...

And that's exactly what is expected, adding other separators:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

Tx is doing exactly what it should. Rx is receiving perfectly. OP just needs to have Rx parse the data better.

No, the OP said that he was sending 60 bytes expecting each number to be one byte!

The Tx was there wrong as it sends over a 100 bytes.

If the OP did send each number as a bytes using write then he will get a problem with his end of message detection!

Mark

holmes4:
No, the OP said that he was sending 60 bytes expecting each number to be one byte!

That is a different issue. The code as such is performing well, the actual communication between the boards works without a hitch. The problem is that OP has a different (and incorrect) expectation of what this communication is exactly.