Go Down

Topic: Elegant method for calling multiple softSerial ports? (Read 514 times) previous topic - next topic

scottdrums89

Hello again,

I did some research into pointers but apparently my understanding is still not quite there as I ran into an issue while trying to rewrite my code in the format Peter suggested, so I'm hoping that one of you might be able to help me out.

I want to call a function within doSomethingToSerialPort, doSomethingRepeatedlyToSerialPort, that needs to get serialPort passed into it, but I can't quite figure out how to do so successfully.  Can someone help me out?

Code: [Select]

const int SERIAL_COUNT = 5;
int serialRxPins[SERIAL_COUNT] = { 2, 3, 4, 5, 6};
int serialTxPins[SERIAL_COUNT] = { 7, 8, 9, 10, 11};
SoftwareSerial*serial[SERIAL_COUNT];

void setup() {
  for(int i = 0; i < SERIAL_COUNT; i++) {
    serial[i] = new SoftwareSerial(serialRxPins[i], serialTxPins[i]);
    serial[i]->begin(9600);
  }
}

void loop() {
  doSomethingToSerialPort(serial[currentSerialPort]);
}

void doSomethingToSerialPort(SoftwareSerial *serialPort) {
  while(true) {
    doSomethingRepeatedlyToSerialPort(__something_goes_here__);
  }
}

void doSomethingRepeatedlyToSerialPort(__something_goes_here__) {
  serialPort->read();
}


-Scott

GoForSmoke

And hopefully whatever Arduino you're using has enough RAM for buffers for 4 soft serial (plus the regular serial?) objects?
Hey, run fast enough and 16 byte buffers might do it but I dunno how much control you have there.



I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

PaulS

Code: [Select]
    doSomethingRepeatedlyToSerialPort(__something_goes_here__);
serialPort goes there.

Code: [Select]
void doSomethingRepeatedlyToSerialPort(__something_goes_here__) {
Just like the other function - SoftwareSerial *serialPort goes there.

Go Up