SoftwareSerial on uno

Hi!

Is my code wrong or is SoftwareSerial not so reliable?
When I’m reading/sending from the rx&tx everything works fine but when I changed to pin 2&3 it doesn’t.

Arduino code:

#include <SoftwareSerial.h>
#include <AnotherClass.h>

SoftwareSerial softSerial = SoftwareSerial(2, 3);
AnotherClass aClass;

void setup()
{
    pinMode(2, INPUT); // Tried with this line and without it
    aClass = new AnotherClass(&softSerial); // This will get values on update, but not the right values (pin 2&3).
    aClass = new AnotherClass(NULL); // This will get the right values on update (using rx&tx).
}

void loop()
{
    aClass.update();
}

AnotherClass.cpp

// Private: SoftwareSerial *serial;
// Public
AnotherClass::AnotherClass(SoftwareSerial *softwareSerial)
{
    if(softwareSerial == NULL)
    {
        this->serial = NULL;
        Serial.begin(57600);
        while(!Serial) {}
        Serial.write(99); // Start
    }
    else
    {
        this->serial = softwareSerial;
        this->serial->begin(57600);
        while(!this->serial) {}
        this->serial->write(99); // Start
    }
}
bool AnotherClass::update()
{
    byte b;
    if(this->serial == NULL)
    {
        if(!Serial.available()) return false;
        b = Serial.read();
    }
    else
    {
        if(!this->serial->available()) return false;
        b = this->serial->read();
    }
    // Some other code
}

Is my code wrong or is SoftwareSerial not so reliable?

I don't see this as an either/or question. The code has some issues, but they don't relate to serial, and SoftwareSerial is quite reliable.

What is not defined is what is connected to what pins, and what the problem is.

AnotherClass aClass;

The aClass variable is an object.

aClass = new AnotherClass(&softSerial); // This will get values on update, but not the right values (pin 2&3).
aClass = new AnotherClass(NULL); // This will get the right values on update (using rx&tx).

The new operator returns a pointer to an instance, not an object.

AnotherClass soft(&softSerial);
AnotherClass hard(NULL);

is how the instances should be created.

Yes, sorry.
It should be like this of course:
AnotherClass *aClass;

But thats not my problem here.

But thats not my problem here.

But, you're not going to tell us what it is. Well, I think I'll pass on making any more guesses until you provide some facts.

What I don't understand is why the data differs when I change between these lines:

aClass = new AnotherClass(&softSerial); // This will get values on update, but not the right values (pin 2&3).
aClass = new AnotherClass(NULL); // This will get the right values on update (using rx&tx).

If I use the first line I wont receive valid data. But if I use the second line everything works great.

What I don't understand is why the data differs when I change between these lines:

aClass = new AnotherClass(&softSerial); // This will get values on update, but not the right values (pin 2&3).
aClass = new AnotherClass(NULL); // This will get the right values on update (using rx&tx).

If I use the first line I wont receive valid data. But if I use the second line everything works great.

So. what about this:

What is not defined is what is connected to what pins, and what the problem is.

You don't say that you are moving whatever is connected to the Rx/TX pins to the software serial pins, so I'll have to assume that you are not. Which explains why you can't read anything from them.

The reliability of the SoftwareSerial implementation depends a lot on the other side of the communication. Usually if the communication partner uses some kind of UART which does timing adaption based on the received signal your fine with SoftwareSerial to about 38400 baud in many cases, sometimes even 57600. But with devices that are picky about the serial timing or which may even have some timing problems themselves you're lucky if you get SoftwareSerial running at 9600 baud. The hardware serial does not just pipe some bits out of a pin or reads the status of another pin at regular intervalls, it does quite a bit more to get a better communication quality. This stuff is not possible in the SoftwareSerial because the processor is simply much too slow for doing such stuff, at higher baud rates it's even too slow to get an acceptably timed signal out.

The SoftwareSerial class is a highly optimized peace of code which tries to get the best result possible in a software implementation of something that's better done in hardware. I never got any device to communicate reliably at 57600 baud with SoftwareSerial on an Arduino. If the one hardware USART is not enough for you, get a Mega or if you wanna use the USB connection for debugging, get a Leonardo because there you have an USB debugging serial interface and the USART free for other purposes.

Thank you pylon!
I'll try with a mega.