Really simple squirrel function with string return

Someone will probably be able to answer this in two seconds…
I have a bluegiga module connected to my IMP via UART, and its working properly, I’m just trying to get my Squirrel “Read()” function correct.

I have this:

`function blueRead() {
local readValue = “”;
local b = uart.read();
// block until data available
while(b==-1){b = uart.read();}
while(b!=-1) {
readValue += format("%c", b);
b = uart.read();
}
return readValue;
}

local result = “”;
…and I call it like this…
uart.write(“AT
”);
imp.sleep(0.000002);
result = blueRead();
server.log("response: " + readValue);

uart.write(“AT
”);
imp.sleep(0.000002);
result = blueRead();
server.log("response: " + readValue);

uart.write(“AT
”);
imp.sleep(0.000002);
result = blueRead();
server.log("response: " + readValue);`

…The interesting thing, is - the module replies with “>ATOK”, but this is the program output

2014-03-05 21:15:09 UTC+13: [Device] response: > 2014-03-05 21:15:10 UTC+13: [Device] response: A 2014-03-05 21:15:10 UTC+13: [Device] response: T

I know that all my data is being read back in my the blueRead() method (I logged the individual chars) - but for some reason, each “server.log(result)” is just… reading a single char from the readValue variable…

What am I doing wrong here?

Thanks

not quite able to see anything wrong. You can use a callback in order to read serial data. You do not have to have a blocking command.

Is it possible that your while loop executes so quickly that the next character is not yet in the buffer?

Your log is stated to read out “response: >” but there isn’t any code that would print “response”.

Looks like mjkuwp94 has it figured out. Your blueRead function stops executing when it empties the UART receive FIFO; it can read the FIFO empty faster than the BLE module can write in the next character, so you’re still only reading one byte per read.

It does look like your output log doesn’t match the code you provided.

You don’t need a blocking read. You can actually configure the UART so that it calls a callback whenever new data appears in the FIFO.

You’ll have to assemble and parse your data outside of your UART read callback, because your UART read function won’t automatically line up with commands or words coming back from the BLE. You can sorta fake it by pausing at the top of your UART callback to give the BLE a chance to finish writing the command, but that’s not terribly robust.

Here’s some code to get you started.