I am trying to link my DUE (via a level shifter) to my UNO
The simple test program on the DUE is:
int num;
void setup()
{
Serial.begin(115200);
Serial1.begin(9600);
while (!Serial) {; }
Serial1.begin(9600);
while (!Serial1){; }
num = 1234;
}
void loop()
{
Serial1.println(num);
delay(1000);
num = num+1;
}
So the DUE is sending a series of integers increasing by one every second.
On the UNO I have the following program which works so I know that the wiring and the level shifter are all working correctly
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(115200);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.flush();
}
void loop()
{
static char buffer[80];
if (readline(mySerial.read(), buffer, 80) > 0)
{
Serial.println(buffer);
}
}
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\n': // Ignore new-lines
break;
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
This all seemed a bit like overkill so I thought I would simplify the program and use readString(0
The program below does NOT work.
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(115200);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.flush();
}
void loop()
{
Serial.println("out");
if(mySerial.available())
{
Serial.println("in");
String a= mySerial.readString();// read the incoming data as string
Serial.println(a);
}
}
The debug code shows where the program is. after opening the serial moniter it prints "out" several times and then prints "in". At that point it stops so the readString() function is not returning - not even timing out.
The string that the DUE is sending is the number characters followed by CR/LF.(13 and 10) which is what I expected.
Changing the readString() to parseInt() and the program works properly.
So the question is what is happeneing to the readString function? What terminates the string allowing the function to return?
EDIT
I have just tried the test in the opposite direction ie UNO to DUE using the same programs as above except swapping SERIAL1 to mySerial and vice versa.
ReadString then works! Which makes me think it is something to do with SoftSerial.
However using parseInt (or parseFloat) adds an additional number (0) ie the output is
12345
0
12346
0
12347
0
etc
I tried Serial1.flush() straight after the parseInt() thinking the CR/LF where somehow causing the problem but that made no difference.
Serial comms is easy - right???