When does the UART callback get triggered?

Hi,

I’m reading data from a UART configured like this:
rs422 <- hardware.uart57; rs422.configure(4800, 8, PARITY_NONE, 1, NO_CTSRTS, rs422_read);

Then the rs422_read function:
`
function rs422_read() {
local response = “”;
local c = rs422.read();

// -1 is UART read empty
while (c!=-1) {
    response = response + c.tochar();
    c=rs422.read();
    if (c==-1)
        server.log("**********Got a -1");
}    
server.log(response);

}
`

This is logging:
2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] $ 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] H 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] C 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] H 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] D 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] T 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] , 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] 1 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] 8 2016-03-09 15:48:42 UTC+11 [Device] **********Got a -1 2016-03-09 15:48:42 UTC+11 [Device] 4.6,T*22

All that should be on the same line. Does the UART callback get called every time a separate byte comes in? If so, why didn’t that happen on the last line (4.6,T*22)?

I’ve also tried logging the status using CALLBACK_WITH_FLAGS, and it’s usually ‘1’ (ie READ_READY), but sometimes it’s 0 and sometimes it’s 65, values which aren’t documented here: https://electricimp.com/docs/api/hardware/uart/configure/#callback

Do they indicate what’s going on?

Thanks,
James

Squirrel or the imp CPU can be busy doing other things, meaning that multiple characters could be pending by the time that the callback is called. That’s why it makes good sense to have a while loop (like you do) in your receive handler. At even faster baud rates, it’s more likely that multiple characters will be waiting for you.

The callback flags are set/cleared independently of each other. Think of them as 8 eight boolean values crammed into a single byte. You need to use a thing called a bitmask to isolate and test the ones you are interested in. A value of 65 indicates that bit0 (0x01 READ_READY) and bit6 (0x40 LINE_IDLE) are set.

Thanks! So the call back could be called for every byte as soon as it’s available. I was wrongly assuming it wouldn’t be called until the sentence was finished.

No, you’ll need to look for the terminating character in your handler. The UART has no idea what sort of data it is dealing with.