Difficulties with SoftwareSerial.h Two Port Receive sketch!

Hi,

While I’m trying to use the sketch Two Port Receive to do something useful, like portTwo sending the “test” text to portOne, I can’t see any “test” received by portOne despite “test” being detected in my Logic Analyzer!

The code is this:

/*
 Software serial multple serial test

 Receives from the two software serial ports,
 sends to the hardware serial port.

 In order to listen on a software port, you call port.listen().
 When using two software serial ports, you have to switch ports
 by listen()ing on each one in turn. Pick a logical time to switch
 ports, like the end of an expected transmission, or when the
 buffer is empty. This example switches ports when there is nothing
 more to read from a port

 The circuit:
 Two devices which communicate serially are needed.
 * First serial device's TX attached to digital pin 10(RX), RX to pin 11(TX)
 * Second serial device's TX attached to digital pin 8(RX), RX to pin 9(TX)

 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

 Not all pins on the Leonardo support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

 created 18 Apr. 2011
 modified 19 March 2016
 by Tom Igoe
 based on Mikal Hart's twoPortRXExample

 This example code is in the public domain.

 */

#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 10, TX = digital pin 11
SoftwareSerial portOne(10, 11);

// software serial #2: RX = digital pin 8, TX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
SoftwareSerial portTwo(8, 9);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // Start each software serial port
  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop() {
  // By default, the last intialized port is listening.
  // when you want to listen on a port, explicitly select it:
  portOne.listen();
  Serial.println("Data from port one:");
  
  // my personal added line
  portTwo.write("test");
  
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (portOne.available() > 0) {
    char inByte = portOne.read();
    Serial.write(inByte);
  }

  // blank line to separate data from the two ports:
  Serial.println();

  // Now listen on the second port
  portTwo.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  Serial.println("Data from port two:");
  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
  }

  // blank line to separate data from the two ports:
  Serial.println();
}

The serial output I get from Serial Monitor is this:

…ñData from port one:



Data from port two:



Data from port one:



Data from port two:



Data from port one:



Data from port two:



Data from port one:



Data from port two:



Data from port one:



Data from port two:

So, despite my following line:

  // my personal added line
  portTwo.write("test");

“test” string are nowhere in the serial monitor output despite I have wired 9(TX) to 10(RX) and 11(TX) to 8(RX)! What’s wrong?

ruiseixas:
What's wrong?

Your expectations.

SoftwareSerial is half-duplex IIRC.

Whandall:
Your expectations.

SoftwareSerial is half-duplex IIRC.

So, do you mean that it's impossible to simulate a Serial Communication in a single Arduino board? If so, what's the point of the Two Port Receive Example?

ruiseixas:
So, do you mean that it's impossible to simulate a Serial Communication in a single Arduino board?

No. I mean that SoftwareSerial can not be used in a loopback configuration.

Whandall:
No. I mean that SoftwareSerial can not be used in a loopback configuration.

In that case how to put portOne communicating with portTwo (half-duplex)? By other words, how to simulate a serial communication in a single Arduino board?

ruiseixas:
By other words, how to simulate a serial communication in a single Arduino board?

Don't send stuff, but pretend you had received it on the other port.

Whandall:
Don't send stuff, but pretend you had received it on the other port.

Pretending I received something sounds like don't use portOne.read() which defeats the purpose of Two Port Receive Example!

I'm starting to realize that I have to use two boards to do a real serial simulation. :confused:

Isn't simulation always pretending something?

I see a differnce between performing serial communication and simulating it.

Whandall:
Isn't simulation always pretending something?

I see a differnce between performing serial communication and simulating it.

You know there are many specificities surrounding serial.read and serial.write... If I don't use those functions I'm not simulating those specificities!

If you are using them you do not simulate.

ruiseixas:
You know there are many specificities surrounding serial.read and serial.write...

Are there? I feel those functions are very straightforward, simple and easy to use.

Whandall:
If you are using them you do not simulate.

Are there? I feel those functions are very straightforward, simple and easy to use.

For instance, you handle char variables and not strings or full text, meaning you have to use loops like while and similar tricks to read all input to a variable like an integer one!

The purpose is to develop my own AT commands so that one board extracts information from another board... Example, AT+A1, reads sensor A1 and sends value, like, AT+A1=1023...

ruiseixas:
For instance, you handle char variables and not strings or full text, meaning you have to use loops like while and similar tricks to read all input to a variable like an integer one!

I would call that normal, standard programming, no tricks neccessary.

For your application I would not try to simulate both sides of the communication in one Arduino.

If you use an ASCII based protocoll (like AT…) then the serial monitor does a good job as a simulator of the other side.