11 years, 2 months ago.

write int array or byte array to Serial port

Is there a method for write bytes on the Serial object? I am using a nucleo board. I want to write either of these:

int onCommand[] = {254,108,1}; unsigned char offCommand[] = {0xFE,0x6C,0x01};

it seems the Serial class only has methods for writing a single int or a string. Am I completely missing something? I can write one int at a time which is fine but a singe write command for an array would be handy.

1 Answer

11 years, 2 months ago.

Hi Travis - That seems like a reasonable request to add to the API. I wrote something called BufferedSerial that exposes this. Maybe it will work for you, maybe just give you inspiration to make these contributions to the mbed SDK. http://developer.mbed.org/users/sam_grove/code/BufferedSerial/

At the very least I'd suggest you make a request for this on the development page for the mbed SDK. https://github.com/mbedmicro/mbed/issues

Accepted Answer

Addition to Sam's answer,

almost all API is byte oriented at the moment, the new version of mbed should change this approach.

posted by Martin Kojtal 08 Oct 2014

Thanks Sam,

It seems like a very simple method to add to the Serial object really. It would just need a for loop to individually write all bytes or ints to the port. Anyway I can add it to my code just seemed strange that it was not available. I will put in a request/suggestion as you recommended. Just wanted to make sure I wasn't missing something before I submitted the request.

Thank you, Travis Elliott

posted by Travis Elliott 08 Oct 2014