UART .read difficulty

I currently have my electric imp connected (through a level shifter circuit and USB to Serial Adapter) to my computer. I’m want to use goSerial (essentially, a terminal) to communicate to my Imp.

Using the code below, I can successfully write “Hello, World!” to goSerial. But my readSerial function does nothing even after entering text into goSerial…

`
// UART Communication Code

// function that reads data from Serial port
function readserial()
{
local b = hardware.uart1289.read();
if(b >= 0)
{
server.log(b);
}else{
server.log(“no data to retrieve”);
}
}

// configure a pin pair for UART TX/RX
hardware.uart12.configure(38400, 8, PARITY_NONE, 1, NO_CTSRTS);

// Connect to server
imp.configure(“Serial RX”, [], []);

//Write hello world
hardware.uart12.write(“Hello, World!\r
”);
hardware.uart12.flush();

readserial();

`

Everyone has been so knowledgeable and helpful here!!

I just realized what was wrong. My code should have been:

local b = hardware.uart12.read();

NOT:

local b = hardware.uart1289.read();

Thanks all!

Have you tried using a callback for the uart?

http://devwiki.electricimp.com/doku.php?id=electricimpapi:uart:configure

I love it!

I just used the callback. It’s great!! Thanks!

Along those lines, when I enter text like “hi”, my Imp prints the following:

Wed Jul 17 2013 13:49:09 GMT-0400 (EDT): 13 Wed Jul 17 2013 13:49:09 GMT-0400 (EDT): 48

Is this hex??

You’re logging it in decimal (you can do it in hex if you want - change your server.log(b) to server.log(format("%02x", b)) …)

13 = newline (you hit return at some point, or had a “\r” in the data from the arduino)
48 = ‘0’… unless you are logging hex already and then thats ‘H’.

See http://www.asciitable.com/

If you want to see them as Squirrel strings:
server.log(b.tochar());

Peter

Ah. I totally understand now. Thanks so much Hugo and Peter!