Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15484

SoftwareSerial.read() returning value with all the bits set

$
0
0

I'm trying to get a sketch going using SoftwareSerial on an R4 Arduino Uno wifi.

SoftwareSerial.read() is supposed to read a byte, or -1 if no data is available. The return type of read() is an int, which on this board is an 4-byte quantity, so if no data is available it should return 0xFFFFFFFF.

What I'm finding is that occasionally read() returns 0xFFFFFFdd, where dd is the value of the byte I'm receiving. If I either AND that value with 0xFF, or cast the returned int into a uint8_t, a one-byte quantity, my program works fine.

Looking at the code for SoftwareSerial, in the class there is a member ringbuf, which is a ring buffer of type char. This is the code for read():

int SoftwareSerial::read()
{
    int chr = -1;
    if (!rx_descr.ringbuf.empty()) {
        chr = rx_descr.ringbuf.get();
    }
    return chr;
}

ringbuf.get() should return a one-byte quantity, a char, which gets cast to an int. Somehow the upper three bytes of that int are getting set to F.

Just to check, I did Serial.print(sizeof(char)), which gives 1, as expected.

Any thoughts what's going on?

6 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 15484

Trending Articles