Send hex command with Serial

13 May 2011

Hello,

I need to send command throught Serial in format hex.

For example, I need to send : AA FF

But with a RS-232 monitoring software, I saw that mbed always send in ASCII format.

How to change this ?

Thank you in advance for your help.

13 May 2011

I think it is the way the serial command is formatted, can you show some code please?

13 May 2011

#include "mbed.h"

Serial pc(USBTX, USBRX);

char fromPC ;

int main() {

    pc.printf("Ok ok");

    while (1) {

        fromPC = pc.getc();

        pc.printf("%x",fromPC);

    }

}

Yes of course, thanks,

This code, it's a echo test.

For example, I send "AA 23 14 FF" in hexa format from my software.

And mbed return, "aa2314ff" but in ascii format. If I look to HEX format, it's "61 61 32 33 31 34 66 66", ( this is hexa format for each character "aa 23 14 ff".

13 May 2011

have you tried it with a terminal emulator? terra term or putty for example?

13 May 2011

Yes, with a development tools for serial communications,

it's docklight v1.9

13 May 2011

But I want to send directly from mbed a hex command, how to do that ?

Maybe as you said, it's like this with serial ?

edit : Sorry i made a mistake, it doesn't work. :s

13 May 2011

try the .putc() funciton instead of printf as that state it is formatting a string

I think I'm mixing it up with Arduino but im sure you could do something like

pc.putc(data, HEX);

to send it as hex

13 May 2011

Dean Moore wrote:

try the .putc() funciton instead of printf as that state it is formatting a string

I think I'm mixing it up with Arduino but im sure you could do something like

pc.putc(data, HEX);

to send it as hex

ok, i will try this,

I saw this for Arduino yeah ^^

EDIT :

Compilation failed, because prototype of function putc it's :

int putc(int c)

I change this

pc.printf("%x",fromPC);

to this

pc.printf("%c",fromPC);

Now mbed return exactly what I send,

but it's more difficult to send hex directly from mbed

13 May 2011

Hi,

If you want to send hex it's always two characters per byte (like 'aa' becoms 61 61, the character code of 'a').

I think you want is called sending binary (so like the printf("%c", fromPC) example). Your PC terminal emulator may then show this as hex depending on the software used.

18 May 2011

printf sends ascii string putc send binary data

so just putc back what ever you receive with getc. That will echo back exactly what it received. by doing printf, you convert the binary data read with getc into ascii string representation.

18 May 2011

Thank you guys,

I understand now.

As you said, putc work fine, for example to send hex command i put this line :

    pc.putc(0xAA);
    pc.putc(0x00);
    pc.putc(0x0A);

And in my software, I can see the right hex command.

Thank you :D