I have been Fiddling about (for ages....) trying to get software serial to talk between two UNO's .
For various reasons I'm running these from within "setup()" ; now working but the delays shown in the code are needed for it to work; if I take either of them out " recvWithEndMarker();" is not called or that code doesn't run.
Why are the delays needed for it to work,?
//software serialplay1
//TRANSMITTER
#include <SoftwareSerial.h>
const byte startMarker = 0x3C;
const byte endMarker = 0x3E;
byte SEND[4] = {0xFF, 0x10, 0x22, 0x33};
/*
SEND[4]==
0 Data: 255 Decimal
1 Data: 16
2 Data: 34
3 Data: 51
*/
SoftwareSerial mySerial(4,3); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
mySerial.begin(19200);
Serial.begin(9600);
Serial.println ("OK !");
Serial.println("");
for (int i=0; i<=3;i++)
{
Serial.print (i);
Serial.print(" Data: ");
Serial.println (SEND[i], DEC);
}
delay(200);
mySerial.write(startMarker);
mySerial.write(startMarker);
for (int i = 0; i <= 3; i++)
{
mySerial.write(SEND[i]);
}
mySerial.write(endMarker);
}
void loop()
{
}
and....
//software serialplay6
//RECEIVER.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 4); // RX, TX
const byte numBytes = 8;
byte recievedBytes[numBytes]; // an array to store the received data
boolean newData = false;
byte startmarkerCount = 0;
const byte startMarker = 0x3C;
const byte endMarker = 0x3E;
static boolean recvInProgress = false;
void setup()
{
Serial.begin(9600);
mySerial.begin(19200);
Serial.println("<Arduino is ready>");
while (mySerial.available() == 0)
delay(8);// at least 5 needed
recvWithEndMarker();
showNewData();
}
void loop()
{
}
void recvWithEndMarker()
{
delay(3);// at least 2 needed
static byte ndx = 0;
byte rc;
while (mySerial.available() > 0)
{
rc = mySerial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
recievedBytes[ndx] = rc;
ndx++;
if (ndx >= numBytes)
{
ndx = numBytes - 1;
}
}
if (rc == endMarker)
{
//Serial.print("IN else");
recievedBytes[ndx] = '\0'; // terminate the string
ndx = 0;
recvInProgress == false;
newData = true;
}
//***********
}
else if ((rc == startMarker) && (recvInProgress == false))
{
delay(2);// needs at least 1mS delay !!
if ((rc = mySerial.read()) == startMarker)// check next one.
{
recvInProgress = true;
ndx = 0;
}
else
{
startmarkerCount = 0;
}
}
}
}
//*************************************************************
void showNewData()
{
if (newData == true)
{
Serial.println("This just in ... ");
for (int i = 0; i <= 3; i++)
{
Serial.print (i);
Serial.print (" Data =");
Serial.println(recievedBytes[i]);
}
}
newData = false;
}
The output on the serial monitor being correct and ...
<Arduino is ready>
This just in ...
0 Data =255
1 Data =16
2 Data =34
3 Data =51
Because without them the receiver code calls recvWithEndMarker() as soon as a single byte is available to read. Adding a delay() gives time for the full message to arrive before trying to read it
In Robin's original examples recvWithEndMarker() is called repeatedly while something is available to be read and only ends when the end marker is received
This sort of communication synchronization can be done easily, if one builds a state machine in the code at each end, such that proceeding to process a message in either direction can only be done in controlled circumstances.
Getting there, of course, is most of the fun!
in the data being sent, in the real code, there is a small chance of the
data being sent/received being the same as the start or end maker. The chances of two sets of such data appearing together is smaller . In this minimalised code its crept in by mistake.
it was for a setup of scaling parameters , and i wanted to do it in setup - during this post Ive abandoned that as I cant get it to work and have moved it to loop and trying to manipulate ( double use) some digitals to signal when to send data etc to make a better job of it .
So for now ( I'll be back ....) I'll make this as closed as you answered the inital question on delays.
I was doing that , but for some unknown reason I couldn’t get it to work with software serial .
In a while loop it wouldn’t do the “ serial.available “ bit . Possibly (?) my errors , but in loop working …Nearly done in loop now , so no longer an issue I’m looking to solve !
( if at first you don’t succeed , do something else ).
(I didn’t have a sync method , but ensured the receiver was ahead of the transmitter, waiting in a while loop for buffered data ).
while ( mySerial.available()==O)
delay (100);
Etc etc
I did try a delay too to allow all message to be received .
As said , not sure why it didn’t work - you (@UKHeliBob ) advised the delay , but now moved on , hey ho .