The answer is: Yes
Setting the RX and TX pins is done in the constructor.
You have to call the constructor to change it.
You also have to keep the line high when it is idle. It should not float while changing its function.
As you can see here, the SoftwareSerial::end() turns the pins into inputs:
and here: https://github.com/PaulStoffregen/SoftwareSerial/blob/master/SoftwareSerial.cpp#L649
I think you need a pullup resistor to keep the line high, or else the receiver might see a start-bit when the line is low by noise.
You can use
delete with a pointer to a SoftwareSerial object in global memory.
Or you can let the C++ language do that for you by using the constructor in a function. But then it gets deleted as soon as you leave the function.
// default: RX
pMySW = new SoftwareSerial( 8, -1); // does -1 disable the TX ? I'm not sure.
// switch to TX
pMySW->end(); // nicely close the serial port
delete pMySW; // remove the object
pMySW = new SoftwareSerial( -1, 8); // does -1 disable the RX ? I'm not sure.
The destructor calls the
.end() function. A
delete on a object should automatically call the
.end() function. I think that is correctly implemented, so you don't have to call
.end() in the sketch.
Perhaps the pin -1 is only for the SoftwareSerial on the ESP8266. You could assign a dummy pin that you don't use.
Have you tried AltSoftSerial() : https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
The pins are set in the constructor, so that the
.begin() function is compatible with the normal Serial library. I think that is the only reason. Perhaps there is a easier solution out there.
Which Arduino board do you use ?