IMP & Arduino Serial Connection/Communication

I just managed to set up my hardware serial connection arduino - logic converter - impee. Shifting numbers between 0-255 sort of works back n firth, however, how do I transfer larger data packages, e.g. strings, etc.?

On the imp side the simple answer should be to use blobs for reading and strings for writing. On the arduino side I’ll leave that as an exercise for you :wink: (I’m not all too familiar with the Wiring language…) I haven’t tested either of these functions but I think they should both work fine…

The write is super simple:
`
function write(writeStr){
local serialConn = hardware.uart57 //This should be whatever UART you are using and should already have been configured with baud rate, parity, etc.

           serialConn.write(writeStr.tostring());

}`

The read is a little more complicated but the code goes something like:
`
function read(){
local serialConn = hardware.uart57 //This should be whatever UART you are using and should already have been configured with baud rate, parity, etc.
local readBuffer = blob(0);
local byte;

    while((byte=serialConn.read())!=-1){
		readBuffer.writen(byte, 'b');
	}

    return readBuffer.tostring();   //Convenience method provided by Electric Imp not present in standard squirrel

}`

The blob class has all kinds of useful functions to swap endianess, read out different data types, etc.

A string is basically an array of characters and a character is really just a 2-byte integer typically stored in ASCII Format. For instance a decimal value of 65 can be understood as the character ‘A’. The string “test” is stored in memory as [116, 101, 115, 116]. A blob gives you the ability to change the integer values the UART transports between the imp and arduino very easily back into a string. I can expand on this if its confusing.

eh, do I have to add the blob class? all I get with the blog code is : (BST): ERROR: index out of range
(BST): ERROR: at readSerial:27

My apologies - I am part of the Agent beta and typically use Agents to do all of my logging - the blob.tostring() method appears to only be available inside of an agent and not on the device code… (Electric Imp team - The documentation doesn’t make this clear. Can it be updated or is blob.tostring() part of Release 23 for the device?)

Using blobs makes your data FAR more flexible and memory efficient but they aren’t strictly necessary. Here’s a tested and working code example:
`function write(writeStr) {
local serialConn = hardware.uart57 //This should be whatever UART you are using and should already have been configured with baud rate, parity, etc.
serialConn.write(writeStr.tostring());
}

function read(){
local serialConn = hardware.uart57 //This should be whatever UART you are using and should already have been configured with baud rate, parity, etc.
local readBuffer = blob(1);
local byte;
local byteIndex = 0;
local str = “”

while((byte=serialConn.read())!=-1){
    readBuffer.writen(byte, 'b');   //Can comment if you don't want to use the blob
    server.log(format("Byte %d = %d = 0x%2X = %c", byteIndex++, byte, byte, byte)); //Can comment if you don't want individual byte logging
    str += byte.tochar();   //Converts integer read by UART into ASCII char for string representation
}

if(str.len() > 0){  //Function may get called by callback when no data is present - no need to log
    server.log("Read String Representation = " + str)   
}
//return readBuffer.tostring(); //Convenience method provided by Electric Imp not present in standard squirrel OR on the device in Release 14.
return str;

}

hardware.uart57.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS, read.bindenv(this));
imp.configure(“Simple Serial”, [], [], {});

//Write data to the UART every 5 seconds
loop <- 0
function writeLoop(){
write("Loop - " + loop++ + “\r
”);
imp.wakeup(5.0, writeLoop);
}
writeLoop();
`