SlashDevin / NeoSWSerial

Efficient alternative to SoftwareSerial with attachInterrupt for RX chars, simultaneous RX & TX
169 stars 42 forks source link

error: 'NeoSWSerial::NeoSWSerial(const NeoSWSerial&)' is private within this context #41

Open FlavorJ opened 3 years ago

FlavorJ commented 3 years ago

From NeoSWSerial.h:

NeoSWSerial( const NeoSWSerial & ); // Not allowed
NeoSWSerial & operator =( const NeoSWSerial & ); // Not allowed

This prevents using an array to store multiple objects, e.g. NeoSWSerial serialArray[2] = { NeoSWSerial(3,4), NeoSWSerial(5,6) }; is not allowed.

Of course, instead we can use pointers and even assign those within the global context:

NeoSWSerial serialPort1(3,4);
NeoSWSerial serialPort2(5,6);
NeoSWSerial * serialArray[2] = { &serialPort1, &serialPort2 };

which works fine, requiring minor syntax adjustments for member function calls and passing the object as an argument:

byte blockingReadByteFrom(NeoSWSerial * serialPort) {
  int r;
  serialPort->listen();
  serialPort->write(B10100101); // or however you request data
  do {
    r = serialPort->read();
  } while (r == -1);
  serialPort->ignore();
  return (byte) r;
}

void loop() {
  for (int i=0; i<2; i++) {
    Serial.print("serialArray[")
    Serial.print(i);
    Serial.print("]: ");
    Serial.println(someFunction(serialArray[i]), HEX);
  }
}

This doesn't really require a fix (maybe a readme addition), but since Arduino is geared towards beginners I thought I would share the alternative and working usage for those who would like multiple software serial ports within an iterable array and encounter this error when attempting to instantiate within the declaration.

Also thanks for creating and maintaining this. I haven't tried maxing it out (though I don't see why you couldn't have eight or nine with pins available), but I have four NeoSWSerial ports plus the hardware serial working just fine on atmega328p-based boards.