Sending hex string via serial

24 Oct 2012

I am trying to connect a GPS module to the mbed. I need to send a hex command string via serial to configure the GPS module. e.g. 0x2525F1040001000000000000730D0A.

Is there an easy way to send this all at once using a single function? At the moment I am using individual .putc methods to write two hex characters of the string at a time but this isn't very efficient!

Thanks.

Paul

24 Oct 2012

Paul Harris wrote:

Need to send a hex command string via serial to configure the GPS module. e.g. 0x2525F1040001000000000000730D0A.

I assume you need to send a sequence of bytes: 0x25, 0x25, 0xF1, etc You could write a function to send a number of bytes that are stored in an array. The function would then check pc.writable() and call pc.putc(array[i]) in a loop.

That would clean up your code, but you still need to fill the array byte by byte...

It would be easier if the data were static. You could then define a constant array and just send that using your function.

const char commandstring[] = { 0x25, 0x25, 0xF1, ...};

sendcommand (commandstring, sizeof(commandstring)); // pseudocode
24 Oct 2012

Thanks! Yes I need to send the bytes one by one. I had thought of doing something like that with a loop sending the character bytes one by one from the array so that's reassuring. I was unsure if there was a way of sending a whole string array at once (similar to .printf) without the need for a loop, but from what you say this is not possible.

Thanks for your help!

Paul

25 Oct 2012

I am working on the very same issue using the Novatel OEM615 receiver. Its RX/TX pins (LVTTL) are just connected to the p9, p10 of the mbed. I need to send a string of bytes based on Novatel's binary message structure. The also have an ASCII message structure, but I want to use the more compact binary structure. The binary message is a fairly complex set of chars, shorts ad longs.

My current strategy (not complete yet) is to form a struct using the mbed C++. I have added a #pragma pack(1) above the struct so that the struct internal storage doesnt have any extra packing. I have used this before with Visual Studio C++ and it worked nicely. If you dont add the #Pragme Pack(1), the compiler adds padding so the word boundaries are on 4-byte boundaries.

If I set up the struct with the various correcly named ints, shorts, longs, then I can just define these variables and the bytes will be laid out correctly. The code is very readable w/o any bit/byte manipulation logic.

When I want to send the byte string to the receiver, I just set up a putc loop that increments the pointer to the first element of the struct. So far this compiles but I havent actually tested it!

Any thoughts on this idea woule be appreciated.

- Jim