Skip to content

SoftwareSerial: Being able to set the receive and transmit pin at runtime #146

Open
@tinkerspy

Description

@tinkerspy

I'm trying to create a chain of attiny's that use SoftwareSerial to communicate in a one-to-many fashion with one sender and many listeners. The attiny's should all be running the same firmware only the role is different (sender of receiver). Because of the limited number of pins on an attiny only one wire should be used.

If I use SoftwareSerial for this I run into problems:

This won't work:

SoftwareSerial mySerial( 4, 4 );

OK, fair enough, I can just use two different objects, right?

SoftwareSerial myTx( -1, 4 );
SoftwareSerial myRx( 4, -1 );

if ( role == SENDER ) {
  myTx.println( "msg" );
} else {
  char c = myRx.read();
}

This works great for receiving but not for sending!
If I swap the declarations like this:

SoftwareSerial myRx( 4, -1 );
SoftwareSerial myTx( -1, 4 );

Then it works for sending but not for receiving!

Could the interface be changed so that the receive and transmit pins can be set after declaration?

I fixed it for now by moving the setRX() and setTX() methods from the private to the public section.

if ( role == SENDER ) {
  mySerial.setTX( 4 );
  mySerial.setRX( -1 );
} else {
  mySerial.setTX( -1 );
  mySerial.setRX( 4 );
}

but the most elegant way to solve this would perhaps be to add optional receivePin and transmitPin arguments to the begin() method so we could just do this:

SoftwareSerial mySerial( -1, -1 );

void setup() 
{
  if ( role == SENDER ) {
    mySerial.begin( 9600, -1, 4 );
  } else {
    mySerial.begin( 9600, 4, -1 ); 
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions