2 serial ports within a library

Hy I'm useing the FUTABA_SBUS library.

It uses fix Serial1 to communicate with an SBUS-Receriver

In my sketch, I want to talk to 2 SBUS-Receivers. One on Serial1, the other on Serial3.

I extend the begin-function like this:

void Teensy_SBUS::begin(HardwareSerial& portPin){
    port = portPin;
    port.begin(BAUDRATE,SERIAL_8N2_RXINV_TXINV);  //this works as it is a teensy-board

port is declared in the private-area like this:

HardwareSerial& port = Serial1;

I don't like the initialisation of "= Serial1". If I don't use it, I have an error called "Teensy_SBUS.h:37:22: note: 'Teensy_SBUS::port' should be initialized"

If I use it like this, guess what: I have only the inputs from Serial1.

The Setup looks like this:

void setup(){
  sBusOwn.begin(Serial1);
  sBusForeign.begin(Serial3);
  Serial.begin(115200);
}

sBusForeign has never new data. HW is correct, veryfied by switching the cables on the RX-Pins...

What has to be done, to make the Serial-Port changeable within a library?

When I've done this sort of thing, I use a pointer to the Hardware serial class, which is what you appear to be doing here

HardwareSerial& port = Serial1;

However, what I don't see in the code that you posted is anything that makes use of the pointer you passed, as that code would look like

port->Begin(115200);

I think what you are doing is Teensy specific, as it doesn't look like standard arduino code to me, i.e with the normal Arduino libraries you don't pass the pin you want to use for the serial port

Actually in the code you posted, it looks like dodgy naming, as the & portPin looks like a pointer to a class rather than a pointer to a number of a pin, i.e not pointer to int etc

BTW. On a totally different subject. I've used a Futaba SBus library when I converted my DJI Phantom 2 to use APM 2.5

BUT. There is a feature / Bug in the SBus library I used.

If the SBus signal input is lost, the output continues to be the last received value, therefore any downstream systems e.g. your FMU has no way to know that the signal has been lost.

I don't have my modification to had at the moment, but I recall that in the Futaba library there was handling for the loss of signal, but it didnt do anything, so I ended up setting some channels (throttle I think) to zero. This then got noticed by the Arducopter APM 2.5 which goes into its panic mode (sorry I can't remember the exact terms they use) and then the FMU can do the appropriate action !

PS. I wonder why you are using a Teensy, are you using it as your whole FMU or just to convert SBus to something else?

Thanks

The fault is the incompetence of me - or missing knowledge. I'm not confirm with all the pointer stuff. And actually, I have no glue what "Serial1" is. I tried to hande as a variable - but without success.

It is not teensy-specific, because the original lib was written for arduino.

I found a way, but it is a slow and inefficient way like this:

void Teensy_SBUS::begin(uint8_t port_nr){
        port = port_nr;
   switch (port) {
      case 1:
        port1.begin(BAUDRATE,SERIAL_8N2_RXINV_TXINV);
        break;
      case 2:
        port2.begin(BAUDRATE,SERIAL_8N2_RXINV_TXINV);
        break;
      case 3:
        port3.begin(BAUDRATE,SERIAL_8N2_RXINV_TXINV);
        break;
      }

Everywhere the Lib usese some port-statements I have to add the switch-statement. That is slow and expands the code for nothing. To do some next programming with my sketch - ok. But it will be great if someone can help me to do it better.

What I want to do is the following: In my sketch, I want to add the used Serial-Port in the setup routine. The original library uses Serial1 harcoded like this:

#define port Serial1

and does later some port-stuff like this:

port.write(sbusData[i]);

It would be great, if I can tell the instance which port to use, something like this:

Teensy_SBUS sBusOwn;
Teensy_SBUS sBusForeign; 
void setup(){
  sBusOwn.begin(Serial1);
  sBusForeign.begin(Serial3);
}

Thanks for the tip about Failsafe. The code now sends 0 for all channels. There is another SBUS-Library that can handle the Filesave-Bit. I will have a look there. Its bad, thisone does only SBUS-reading. And the Port is also hardcoded-stuff =(

The teensy is to control the incoming signal from 2 SBUS-Receiver and bring the better signal to an APM. It will later also check the APM, and by inactivity it will switch the signal to a FyDOS. And at the real end, I will have to teensy checking each other. Target: eliminate single point of failure :~

Rather than using switch statements, you code re-code it to use a pointer to the Serial port.

I've used this technique and it works fine if they are all Hardware Serial, but if you have to mix Software Serial and Hardware Serial it gets more complicated as the base class that both Hardware and Software serial derive from is Stream, but Stream doesn't have a Begin method :-(

However it looks like you are using Hardware Serial for all channels, so this won't be a problem.

So just pass in the pointer to the constructor of the library and store it as a private var (property) e,g,

HardwareSerial & _serial;

Then you can just do

_serial->read(.....);

_ serial.write(...); 

etc

I'm not that familiar with pointer stuff, what goes where?

What is in the sketch, calling the lib?

sBusOwn.begin(Serial1);

What is in the definition of the begin-function?

void Teensy_SBUS::begin(HardwareSerial & _serial){...

And finnaly I can do:

_serial.begin(BAUDRATE,SERIAL_8N2_RXINV_TXINV);
_serial.write(sbusData[i]);