Imp UART Seems to be looping over an incoming byte

Hi,

I’m working on a project where my imp needs to talk to another device over a serial port.
I’m using uart57, which is working well for sending data. However when it reads incoming bytes it seems to get stuck in a loop.

here is my read function:

`
// This function reads any data from the serial port
function readSerial(){

server.log("Reading Serial");
local byte = matrix.read();

while (byte != -1) {

    server.log("serial: " + byte);
}

}
`

And the configuration:

matrix <-hardware.uart57; matrix.configure(115200,8, PARITY_NONE,1, NO_CTSRTS, readSerial);

Now when I send a command to the device, it will echo back the characters to the serial port. And then once it’s completed the command it sends <done> back.

But the imp seems to keep printing this constantly until I unplug it:
2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115 2014-05-08 19:13:27 UTC+1: [Device] serial: 115

I’ve checked the RX and TX with my logic analyser and I can confirm that nothing strange is being sent from the device.

Does anyone know what might be causing this?

Thanks,

James

Your matrix.read() is only called once. Try
while (byte=matrix.read()) != -1) server.log("serial: " + byte);

Thanks coverdriven, that worked great!

In case anyone else is interested, this is how I’m using it:

`

input_string <- “”;

function chr(ascii_value)
{
// Convert passed integer value Ascii code into a character string

if (ascii_value < 32) return "";
return format("%c", ascii_value);

}

// This function reads any data from the serial port
function readSerial(){
local byte = matrix.read();

while ((byte=matrix.read()) != -1){
    if(byte == 10){
        server.log("serial: " + input_string);
        input_string = "";
    } else {
        input_string = input_string +chr(byte);
    }

}

}

`

(I used https://electricimp.com/docs/api/hardware/uart/ for the chr function)

You still need to tweak your code a bit, otherwise you’ll drop the first byte received.
I suggest the following:

EITHER
`
function readSerial(){
local byte;

while ((byte=matrix.read()) != -1){
    if(byte == 10){
        server.log("serial: " + input_string);
        input_string = "";
    } else {
        input_string = input_string +chr(byte);
    }

}

}
OR
function readSerial(){
local byte = matrix.read();

while (byte != -1){
    if(byte == 10){
        server.log("serial: " + input_string);
        input_string = "";
    } else {
        input_string = input_string +chr(byte);
    }
    byte = matrix.read();
}

}
`