Skip to content

SoftwareSerial sets pinMode in its constructor which is too soon #3041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nickgammon opened this issue Apr 27, 2015 · 4 comments
Closed

SoftwareSerial sets pinMode in its constructor which is too soon #3041

nickgammon opened this issue Apr 27, 2015 · 4 comments
Assignees
Labels
Component: Core Related to the code for the standard Arduino API Library: SoftwareSerial The SoftwareSerial Arduino library

Comments

@nickgammon
Copy link

In version 1.6.0 (at least) of the IDE the constructor for SoftwareSerial does this:

  setTX(transmitPin);
  setRX(receivePin);

setTX does this:

  pinMode(tx, OUTPUT);
  digitalWrite(tx, _inverse_logic ? LOW : HIGH);

This is not an appropriate time to be doing pinMode or digitalWrite because init() has not been called yet. Who knows what init will do with pins? It might set them all to inputs, for example.

@mastrolinux
Copy link
Contributor

@facchinm facchinm added Component: Core Related to the code for the standard Arduino API Library: SoftwareSerial The SoftwareSerial Arduino library labels May 7, 2015
@sbrynen
Copy link

sbrynen commented Jun 11, 2015

I had a quick look, and I can't see why you couldn't just move the pinMode commands to the begin() method, You need to call begin() to set the baud rate anyway before doing IO.

@NeilMartin
Copy link

This problem can be dodged if you create your SoftwareSerial whenever you want the pins to be set. So you control when the constructor is called instead leaving it to global construction. For example, put this in your setup() function...

mySoftwareSerial = new SoftwareSerial(pin_rx, pin_tx);
mySoftwareSerial->begin(baudRate);

@nickgammon
Copy link
Author

Yes, that is a work-around, however it looks like the official version was fixed in commit 8c8b78c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API Library: SoftwareSerial The SoftwareSerial Arduino library
Projects
None yet
Development

No branches or pull requests

6 participants