And the code of the receiver isn't much more complicated:
#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10,11);
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
// Start each software serial port
portOne.begin(9600);
}
void loop()
{
int count = 0;
// By default, the last intialized port is listening.
// when you want to listen on a port, explicitly select it:
portOne.listen();
if (portOne.available()) {
Serial.println("Data from port one:");
// while there is data coming in, read it
// and send to the hardware serial port:
while (portOne.available()) {
char inByte = portOne.read();
Serial.print(inByte);
count++;
}
portOne.flush();
Serial.println(count);
}
}
The output result is a random result that is puzzling me:
Data from port one:
AT+A4
Data from port one:
0
3
Data from port one:
AT+A4
Data from port one:
0
3
Data from port one:
AT+A05
Data from port one:
2
Data from port one:
AT+A4
Data from port one:
0
3
Data from port one:
AT+A4
Data from port one:
0
3
Data from port one:
AT+A4
Data from port one:
0
3
Data from port one:
AT+A05
Data from port one:
2
Data from port one:
AT+A05
Data from port one:
2
Data from port one:
AT+A4
Data from port one:
0
3
Only a few times I get the right result, AT+A05, where 5 is the number of characters given by count... How to solve this?
You will not always have a character count of 5 because sometimes you will drop out of the loop() function and then reenter the loop() function, resetting the value of count to zero.
Also, this:
portOne.flush();
may clobber some characters as they are being received.
SoftwareSerial does not work on some pins of some Arduinos, and does not work at all on some Arduinos. The documentation describes this. What Arduino are you using SoftwareSerial on?
vaj4088:
What Arduino are you using SoftwareSerial on?
I'm using the Arduino Nano, and I removed the flush() line without any improvements! Don't know what to do mainly when the Example sketches are useless...
Calling listen empties the input buffer. Calling portOne.begin also calls listen, so you you don't have to call it again, and you certainly don't have to call it every time through loop.
Also, you should consider something besides SoftwareSerial, because it is very inefficient. It disables interrupts for long periods of time, which can interfere with other parts of your sketch or with other libraries.
Here is your sketch, modified to use other serial port libraries:
// Best choice is this:
//#include <AltSoftSerial.h>
//AltSoftSerial portOne; // no choice for the pins... must be 8 & 9 on a Nano
// Second best is this:
#include <NeoSWSerial.h>
NeoSWSerial portOne(10,11);
// Worst is this:
//#include <SoftwareSerial.h>
//SoftwareSerial portOne(10,11);
int count = 0;
char buffer[16];
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
// Start each software serial port
portOne.begin(9600); // listens by default
}
void loop()
{
if (portOne.available()) {
char inByte = portOne.read();
if (inByte == '\n') {
Serial.print ( F("Data from port one:") ); // F macro saves RAM!
buffer[ count ] = '\0'; // NUL-terminate the array to make it a standard C string
Serial.println( buffer );
count = 0; // reset for next line
} else if (count < sizeof(buffer)-1) {
buffer[ count++ ] = inByte; // save another byte
}
}
}
Also notice that this accumulates one line of characters. It won't print the line until the final newline character, '\n' is received. This loop runs all the time, without calling delay or waiting for anything. When the newline finally shows up, after thousands of iterations, it prints the whole line. See "Serial Input Basics" here for more techniques.
You'll have to download NeoSWSerial or AltSoftSerial to try them.
/dev:
Also, you should consider something besides SoftwareSerial, because it is very inefficient. It disables interrupts for long periods of time, which can interfere with other parts of your sketch or with other libraries.
After seeing other Communication Examples I discovered the function [url=https://www.arduino.cc/en/Tutorial/SerialEvent]SerialEvent()[/url]!
It uses the Arduino serial hardware and it does exactly what I want
Now I only need to refresh my knowledge about Strings...