software serial on Nano

Hello,

I am trying to use an Arduino Nano to monitor serial data being sent from a drone and to send it serial data based commands.

I figured I would need SoftwareSerial so that I could keep messages sent/received from the drone separate from the hardware serial messages I planned to use to monitor and control the drone.

I have tried making a simple sketch just to get the software serial functionality working but it doesn’t seem to work, it never prints what I believe I am writing to the software serial. I tried using write instead of print, neither seems to work. Also changing the pins from (0,1) doesnt work.

/*

*/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <HardwareSerial.h>

SoftwareSerial portOne(0,1); // RX, TX

int variable = 0;

void setup() {
    //Start hardware serial
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }
    //Start software serial connection
  portOne.begin(19200);

}

void loop() {
  Serial.println("Data from port one:");

  //write something on soft port to be read
  portOne.print(variable);
  
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (portOne.available() > 0) {
    portOne.listen();
    char inByte = portOne.read();


    //print what was read
    Serial.println(inByte);

    //update thing to be read
    variable = variable++;
  }


  }

I guess "seem" is not a good word for it. You are using software serial on pins 0,1 which is fatal, as well as hardware serial on pins 0,1 which is correct but should be exclusive. Get the software serial onto some other pair of pins.

It also seems that you are using ancient code, which looks like junk and probably is. The libraries Arduino.h and Hardware Serial.h are almost certainly redundant, and may even be a problem. It might be a good idea to upgrade your Arduino IDE to something less than about eight years old.

Thank you Nick. I did some more reading and now have the code below. However nothing prints in serial monitor other than the “test begin” line. I am expecting a series of integers to print in the 9600 Baud window.

I do not have any connectors on pins 8,9 right now. I assumed Serial() can read AltSerial() internally.

#include <AltSoftSerial.h>


AltSoftSerial altSerial(8,9); // RX, TX

  int data = 1;
  int message = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("AltSoftSerial Test Begin");
  altSerial.begin(19200);
  altSerial.println("Hello World");
}

void loop() {
  
  //if (altSerial.available()) {
    altSerial.write(data);
    message = altSerial.read();
    Serial.print("alt serial port says" + message);
    data++;
  //}
}

I’m afraid I don’t know anything about AltSoft serial, but I see you have some lines commented out that I think should not be.

I am also far from clear about what you are really doing. How is the drone connected to Arduino? More to the point, what is Arduino actually communicating with? Have you got the wiring to the serial ports the right way round?

Tx>Rx and Rx<Tx

baud rates properly configured to match?

Even when I run the example from the SoftwareSerial page I just get "goodnight moon", no "hello world" message. I have nothing connected to the Arduino except the main USB but the example makes it look like Serial() can read SoftwareSerial() internally. My question is do I need something wired to the SoftwareSerial ports or should I be seeing "Hello World" on the IDE serial monitor with no additional wiring?

The following is regarding my project specifically (because you asked) but not necessary for answering my question.

The drone has a flight controller which will connect over telemetry radio but for now its just on my desk with a serial cable. The communication between them is with a protocol called MAVlink which there are good examples of with arduino. Right now though I just want to be able to see what my Arduino is writing to the pins that will be connected to the flight controller.

Goonbunny: I have nothing connected to the Arduino except the main USB but the example makes it look like Serial() can read SoftwareSerial() internally.

This nonsense. Serial is Serial, software serial is software serial, and the two have nothing to do with each other. The only way you will see anything on the software serial port is by connecting something to it.

I don't know anything about MAVlink, so I'm still not sure of what you are doing, but it is clear that what I asked is absolutely essential to answering the question. IF Arduino has ONE serial link to the peripheral AND assuming that there is no connection to PC while the drone is in use, then I submit it is entirely appropriate to have the peripheral on hardware serial, pins 0,1. This means all serial commands can be sent from and seen by the serial monitor, i.e serial monitor acts as a dummy flight controller - which I understand is your intention.

Once you have sorted what needs sorting, you may then simply replace the monitor with the controller on pins 0,1.

I get it now. I'm sorry my confusion was based on the assumption that softwareSerial and Serial could communicate.

I can now go forward with building my project.

Thank you for all your help Nick, really means a lot to have someone answering questions when you're stuck!!!

Side note. I used the arduino emulator on TinkerCAD to test the solution to this issue. TinkerCAD is really helpful when you are stuck someplace without the wires ore components you need (like I am right now in the airport).

Goonbunny: I am right now in the airport).

Not Wuhan, I hope....