Skip to content

SoftwareSerial returns impossible values #2858

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
Goz3rr opened this issue Mar 31, 2015 · 8 comments
Closed

SoftwareSerial returns impossible values #2858

Goz3rr opened this issue Mar 31, 2015 · 8 comments
Assignees
Labels
Library: SoftwareSerial The SoftwareSerial Arduino library Type: Bug

Comments

@Goz3rr
Copy link

Goz3rr commented Mar 31, 2015

I have the following code (Arduino 1.6.1):

if(_bt.available() > 0) {
    int data = _bt.peek();
    bool found = true;


    if(data == 'a') {
        // do something
    } else if(data == 'b') {
        // do something else
    } else {
        found = false;
    }

    if(found) _bt.read();
}

(It's set up like this because it's a library, and if unknown messages are found they should be passed to the code handling other commands, if that makes sense)

At random points the Arduino would stop reacting to any received messages, and after adding some debug code I found out that while _bt.available() would keep incrementing with each message received, _bt.peek() returns some a nonsense negative value but _bt.read() will return something completely different, these are some i've encountered so far:

-1 == 255
-76 == 180
-1 == 255
-36 == 220
-1 == 255
-5 == 251
-72 == 184
-36 == 220
-18 == 238

I'm not sure where these values originate from because I'm never sending them to the HC-05 attached to this SerialPort. Worth noting is that once this happens even if i disconnect the HC-05 the SoftwareSerial will keep returning the last values, so I assume some buffering is done internally.

@matthijskooijman
Copy link
Collaborator

What board are you using? Could you see if you could reproduce this problem using a minimal sketch, preferably without any external hardware attached?

Are you sure you're using the official SoftwareSerial library, and not some other library inside your sketchbook? Perhaps you should post the full verbose compilation output, which should show this.

@Goz3rr
Copy link
Author

Goz3rr commented Apr 2, 2015

These are school supplied Arduino Uno's and HC05 Bluetooth modules, I'm not sure if they're genuine Arduinos but I have managed to reproduce it with multiple Arduinos and HC05's and a minimal sketch, I haven't been able to recreate it without external hardware because it doesn't seem to happen while no data is being sent, however it does seem to happen more often when messages are sent more often, baud rate is 9600 so that shouldn't be too high. I'm currently on a bus but I'll post compilation output in a moment.

@matthijskooijman
Copy link
Collaborator

Cool, thanks!

If you can reproduce it by using output from another Arduino (connect its TX pin to the softwareserial RX pin), that would also help a lot. I don't think genuine or clone Arduino would make much of a difference here, btw.

@Goz3rr
Copy link
Author

Goz3rr commented Apr 8, 2015

So far I haven't been able to reproduce this between two Arduinos, it only seems to happen with this specific bluetooth module

@sbrynen
Copy link

sbrynen commented Jun 15, 2015

You're looking at the same thing; just the difference between signed and unsigned numbers.
byte signed: -1 = byte unsigned 255

If you look carefully at all the numbers you supplied, you'll notice that the difference between the two numbers is always 256.

@sandeepmistry
Copy link
Contributor

There is a subtle difference between SoftwareSerial::read() and SoftwareSerial::peek(), read converts the buffered byte to a uint8_t before returning, peek does not.

I'll prepare a pull request to correct this.

@Goz3rr
Copy link
Author

Goz3rr commented Mar 9, 2016

Cool to see someone bothered to look at old issues, and in retrospect that makes a lot of sense

@sandeepmistry
Copy link
Contributor

I've submitted #4669 to resolve this.

@sandeepmistry sandeepmistry added Type: Bug Library: SoftwareSerial The SoftwareSerial Arduino library labels Mar 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Library: SoftwareSerial The SoftwareSerial Arduino library Type: Bug
Projects
None yet
Development

No branches or pull requests

5 participants