Receiving data from two serials and showing them separately on serial monitor

I am receiving two different serial streams from two different serial ports; I want to “store” them into serial monitor, so the I can copy the result into a text file and further process it with excel.
But following source does not work, data received on standard serial port appear corrupt.
Any tip? I am tryin to pre-pend a “0” or a “1” to each datum, depending on source, to be able to distinguish them, but any other suggestion would be appreciated.

#include <SoftwareSerial.h>
int counter = 0;
/* Not all pins on the Leonardo and Micro support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
 */
SoftwareSerial mySerial1(4, 2); // RX, TX
SoftwareSerial mySerial2(9, 3); // RX, TX
//SoftwareSerial mySerial3(10, 4); // RX, TX

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
  }


  Serial.print("Avvio...");

  // set the data rate for the SoftwareSerial port
  mySerial1.begin(9600);
  mySerial2.begin(9600);
  //mySerial3.begin(9600);
  Serial.print("OK, vai.\n");
}

void loop() { // run over and over
//  mySerial1.listen();
  if (mySerial1.available()) {
    //Serial.write("Ricevuto su canale 1:");
    Serial.print("C1:");
    Serial.write(mySerial1.read());
    Serial.write("\n");
  }

//mySerial2.listen();
    if (mySerial2.available()) {
    //Serial.print("C2:");
    Serial.write(mySerial2.read());
    //Serial.write("\n");
  }

  if (Serial.available()) {
    Serial.print("0");
    Serial.write(Serial.read());
    Serial.write("\n");
  }


}

You can't use two instances of Software Serial unless you can ensure that the sending device waits until the appropriate instance is listening.

If you are using a Leonardo you can use HardwareSerial Serial1 (on pins 0 and 1) for one of the inputs and SoftwareSerial for the other. An Arduino Mega which has 3 spare HardwareSerial ports would be much better.

Rather than echo each character as it arrives I suggest you receive a complete message. That will make it much easier to add a character to identify the source of the data.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text. If you have control of the format in which the data is sent the the technique in the 3rd example will be the most reliable.

...R

Yes I figured out that I must use an hardware and a software port.
But I have no control over received data, and the 2 streams are some kilobytes long at 9600bps without pauses, so the only place where I can store them using my Uno is the Serial Monitor; but in my sketch the data appear corrupt if I print them together with a "marker" character, so I'm stuck.

For the records, I am sniffing an MCU talking to a GPRS modem:

I already recorded, saved and analyzed the two separate streams, but for the communication to make sense they need to be in sync, to figure out which response is received by MCU for each packet sent to modem.

jumpjack:
But I have no control over received data, and the 2 streams are some kilobytes long ...

Do you mean that large quantities of data arrive without anything like a carriage-return or linefeed character breaking it up?

only place where I can store them using my Uno is the Serial Monitor

That's an interesting concept of storage.

You need to provide some examples of the data so we can see what you are dealing with.

...R

Robin2:
Do you mean that large quantities of data arrive without anything like a carriage-return or linefeed character breaking it up?

Packets are not separated by 0x0d-0x0a but by 0x7e-0x7e, so I manually replaced 0x7e-0x7e by 0x7e-0x0d-0x0a to get something readable…

You need to provide some examples of the data so we can see what you are dealing with.

…and here it is, but you will not like it :slight_smile:

(MCU --> G600)
7E 3F 7D 23 3F 21 7D 21 7D 21 7D 20 7D 34 7D 22 7D 26 7D 20 7D 20 7D 20 7D 20 7D 25 7D 26 65 67 6C 6A 7D 27 7D 22 7D 28 7D 22 3F 6D 7E
7E 3F 7D 23 3F 21 7D 22 7D 21 7D 20 7D 32 7D 22 7D 26 7D 20 7D 20 7D 20 7D 20 7D 23 7D 24 3F 23 7D 27 7D 22 7D 28 7D 22 3F 3F 7E
7E 3F 23 01 02 47 50 52 53 7E
7E 3F 7D 23 3F 21 7D 21 7D 23 7D 20 7D 2E 7D 22 7D 26 7D 20 7D 20 7D 20 7D 20 7D 27 7D 22 7D 28 7D 22 2A 3F 7E
7E 3F 23 01 04 7E
7E 3F 21 01 05 7E
7E 3F 21 01 06 2F 7E
7E 3F 21 01 07 03 06 7E
7E 3F 21 02 01 7E
7E 3F 21 01 08 3F 3F 7E
7E 21 45 3F 04 5F 6E 3F 16 04 2D 07 3F 01 02 08 09 28 08 3F 61 7E
7E 21 45 3F 04 5F 6E 3F 16 04 78 05 3F 01 01 04 02 6C 73 7E
7E 21 45 30 3F 16 04 02 20 3F 3F 7E
7E 21 45 7D 5D 3F 04 5F 6E 3F 16 04 12 34 56 78 02 04 05 3F 01 01 04 02 4A 67 7E
7E 21 45 6E 3F 16 04 70 02 20 3F 3F 7E
7E 21 45 06 7B 3F 04 5F 6E 3F 16 04 12 34 56 78 7E
7E 21 45 5F 6E 3F 16 04 04 02 3F 3F 7E
7E 21 45 0B 3F 12 34 56 78 7E
7E 21 45 04 5F 6E 3F 16 04 01 04 02 3F 3F 7E7E 21 45 3F 71 7E7E 21 45 3F 04 5F 6E 3F 16 04 3F 01 01 04 02 49 3A 7E
7E 21 45 04 7E
7E 21 45 74 3F 04 5F 6E 3F 16 04 78 05 3F 01 01 04 02 6F 2E 7E
7E 21 45 30 3F 16 04 02 20 3F 7E

MCU <-- G600
7E FF 7D 23 C0 21 7D 21 7D 21 7D 20 7D 32 7D 22 7D 26 7D 20 7D 20 7D 20 7D 20 7D 23 7D 24 C0 23 7D 27 7D 22 7D 28 7D 22 DD 94 7E
7E FF 7D 23 C0 21 7D 24 7D 21 7D 20 7D 2A 7D 25 7D 26 65 67 6C 6A 60 85 7E7E FF 7D 23 C0 21 7D 22 7D 23 7D 20 7D 2E 7D 22 7D 26 7D 20 7D 20 7D 20 7D 20 7D 27 7D 22 7D 28 7D 22 7D 34 BC 7E
7E   C0 23   02 04 00 05 00   AA 5E    7E
7E   80 21   03 05  0003  06 00 00 00 00   C6 A1   7E
7E   80 21   03 06  0003  06 00 00 00 00   C1 77   7E
7E   02 10   10 10 00 4B B9 9   7E
7E  80 21   03 07  0003  06B506E6   8583   7E        7E  80 21   02 08  0003  06B506E6   8A39   7E

I got these data using am USB-serial adapter connected first time to MCU output and second time to MCU input, separately.

Maybe it would be faster if I can get two serial adapters…

Try the second example in my Tutorial with the end-marker changed to 0x7E

…R