UART String outputting

In case this helps anyone who is trying to output a string from the uart and is wondering how to do this using the uart.write() which needs an integer, and like me you are a squirrel newbie - I did it like this:

-------------------

class RespInput extends InputPort
{

i=0;

function set(serverresponse)
    {
        foreach(i, val in serverresponse){
            hardware.uart1289.write(val);
            server.show(“letter :” + val);
            server.log(“letter:” + val);
        }
    }
}

local input = RespInput(“response”, “string”);
local output = OutputPort(“request”, “string”);

//then use the input like
imp.configure(“UART Test”, [input], [output]);
hardware.uart1289.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);

-------------------

This outputs each character in the string as an ascii decimal which you can easily convert to it’s ascii character on whatever you are sending to from the uart output.

For example on Arduino:

char inByte = (char)Serial.read();

Sorry if this is blatantly obvious to most of you guys - maybe it’ll be helpful to somebody else.

Thanks, I’m sure this will be helpful for people.


We will be adding string a blob writing capability to the UART shortly (you can already write strings and blobs with SPI). It’s currently a bit strange only being able to do it byte by byte!

@Hugo - awesome stuff, keep up the great work!

String and blob writing to the UART will be cool for microcontroller chaps like me that probably use it more than SPI, which like you say has it implemented already.




@mrjonandrews

Thanks for posting the code. I was searching high and low for casting, conversions, etc. Basically, I was modifying the Sparkfun Imp example to work with Http in and with the code you posted, it all worked out nicely when the data got over to the Arduino side.

Thanks for posting this example, I was trying to do the same thing. Are there any examples of writing to and from an array? The serial buffer on the imp is 80 bytes, right? Not quite sure how to do this with Squirrel.

I think you might look at writing to and from a blob as that is probably more what you are looking for. To quote Peter (arbiter of all things Squirrel)

That isn't an array of bytes: it's an array of Squirrel objects, each of which happens to represent an integer. What you probably want is Squirrel's "blob" class, which is represented much more efficiently: it really is an array of bytes (readable and writable in word, halfword, and byte units).

Here’s some beginner Squirrel code I wrote:

// Collect bytes local s = blob(20); local byte = hardware.uart57.read(); //READ IN a character from the uart while(byte != -1) { s.writen(byte,'b'); //put read character in the buffer byte = hardware.uart57.read(); //and read the next (if any) character toggleTxLED(); // Toggle the TX LED }

and //not tested but might work :slight_smile:

`s.seek(0); //s is a blob, back to start

    while(!s.eos()) 
    {
        hardware.uart57.write( s.readn('b').tochar() );  //TODO not sure if the tochar is needed, more testing needed 
    }`

the instantiated blob object will expand as needed. I think it allocates by half of the existing space. So if blob(20) was the original the next increase would be by 10.

Bruce,

Thanks I will give it a try when I get a chance. I am new to the Squirrel language. Why is a blob better to use than an array?

-diesel.

A blob is more like the c arrays we are used to. Its a raw buffer of arbitrary binary data. there is little overhead for the object as a whole and no extra overhead for each individual byte enclosed.

It should be lower overhead when using it and eventually there will be a method to write a blob directly to the uart without doing it a character at a time that I’ve tried to do above.

Something like

s = blob(20); //put something into s and then send it hardware.uart.write(s); //doesn't exist yet **coming someday**

will eventually exist allowing writing a buffer to the uart.

Any of you guys tried writing larger data using the UART yet?

I’ve got an idea I’m working on now and want to start pulling down some mp3’s from the web and would love to hear if any of you have tried doing this kind of thing with data bigger than just a few bytes :slight_smile:

@Hugo - any update up on when you guys are planning on implementing the string and blob writing capability?

String and blob writing is in release-9, which is currently in test. Hopefully it’ll escape this week :slight_smile:

Squirrels are hard to keep caged. We’re starting a ‘Free Squirrel’ movement. I’m looking forward to getting back home next later this week and hook everything back together.

@Hugo

Amazing, looking forward to that!!! Keep up the good work :slight_smile:

Any chance release-9 will have the UART stuff on callbacks (rather than polling)? With all the new found knowledge in best practices for squirrel it is time to re-write my code (again), but I plan on re-writing when the way I do my serial reads changes.

It doesn’t, I’m afraid. That’s top of the queue for rel10 though!

Is there a way to write out a constant blob? I have certain packets I need to send via uart, and right now, I basically make an array like:

[ 0x12, 0x34, 0x45, 0x56]

But if I am moving to blobs for my packets, it would be nice to say

sendPacket(Packet(blob( 0x12, 0x34, 0x45, 0x56)));

Or in this case, is an array ok? Use an array to create a the blob ok?

The best way is:

const blob = “\x12\x34\x45\x56”;

…then send that. A const string is stored in flash and is packed, unlike an array (see Peter’s efficientsquirrel page on the wiki). You’ll be able to write these directly in release 9.

Hi, just started with agents and uart and sending data to an Arduino Mega.
I have a test agent that gets data from a php file and it contains about 600 characters. I added a ‘*’ character at the end as a marker.

On the Imp I have this:

`
agent.on(“display”,function(msg){
server.log(“from agent=” + msg);

hardware.uart57.write(msg);

});`

On the Arduino I keep checking until Serial1.read() contains ‘*’. But on the Arduino I get maybe between 80 and 115 characters maximum. And a bunch of garbage characters.

I’m struggling with this for hours and totally forgot the Man U - Chelsea match :slight_smile: (well gonna watch the 2nd half)

Anyone has an example sending a large string from Imp to Arduino?

Got the arduino code? What speed are you sending from the imp to the arduino, and what speed are you logging out of the arduino? It’s possible you’re missing characters on the arduino due to doing other stuff (like logging).

It is working now. I was doing something inside the loop of receiving data on the Arduino.
I was drawing to a tft screen inside the loop. I moved it outside of it.
As a test I send 25000 bytes without problem now. Using UART at 115200.
In practice I only need maximum of 200 bytes per screen of data on the tft.

Also on the Imp hardware.uart57.write(msg + '*'); did not work for me.

Had to change to this to make it work:
`
foreach (idx, val in msg) {
hardware.uart57.write(val);
}

hardware.uart57.write('*');

`

This reliably sends all data for me up to 25000 bytes.

hardware.uart57.write(msg + '*') will temporarily need storage for a second copy of the “msg” string but with a “*” on the end. If the string is enormous (in imp-land, 25000 bytes is enormous), then you may not have enough memory to do that. The code with the loop never materialises the entire string at once, so runs in much less memory.

Peter