Question

I need some help with the serial port (UART) read function. I need to read in and display the hex value, but currently I'm reading in the ASCII values.

to send the hex over the UART I use

char data [] = {0xFF};
write(fd, data, sizeof(data));

which does the work well. But I have a problem with the reading in. I want to be able to read hex values and use the read values later.

Currently I'm reading with this function

read(fd, buffer, sizeof(buffer));

But as a result it displays me the ASCII values, I know I need to make some kind of conversion, but I have searched and was unsuccessful of finding how to do it.

Thanks in advance.

Was it helpful?

Solution

Code is likely reading the data OK, but need to use "%X" for subsequent display.
For each byte:

If recent compiler,

printf("%02hhX", buffer[i]);

otherwise

printf("%02X", (unsigned char) buffer[i]);

OTHER TIPS

Since your question lack the information needed to properly answer you, these are my assumtions:

  1. You are aware that you are sending a single byte over the line (which can be represented as 0xff in hex and 255 in decimal and 11111111 in binary)
  2. You know that there are no distinction, that everything is just numbers.
  3. That you want to represent this single number as a string to the user, this string describing the number in hexadecimal notation.

    printf( "%X", buffer );

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top