0

I am using an STM32, and monitoring the serial port using CoolTerm.

I have an array, that stores the following: 0x01 0xFA 0xAA 0x05 ... etc. I want to sent that over serial and see those values in plain text.

When I use the following:

char array[10] = { 0x01, 0xFA, 0xAA, 0x05, 0x01, 0xFA, 0xAA, 0x05, 0x01, 0x01 };
HAL_UART_Transmit(&huart4, (uint8_t *)array, len, 1);

The output on CoolTerm is:

. . . . . . . . . .

Dots instead of plain text values are shown, if I switch to HEX mode I can see the values.

What I need the output to look like:

"0x01 0xFA 0xAA 0x05 0x01 0xFA 0xAA 0x05 0x01 0x01"

I imagine this is due to the array storing byte literals, but I have no idea what to use to see the hex in plain text in the ASCII decoding serial monitor.

NoOne
  • 104
  • 12
  • @KamilCuk I need all of my output to show in ASCII mode, I don't want to bounce between plain text and hex just to see the values of the array. – NoOne Jun 25 '21 at 08:05
  • Re "*What I need the output to look like:*", I highly highly doubt that. But if so, you're asking how to convert numbers to their hex representation, which you can easily look up. – ikegami Jun 25 '21 at 08:05
  • Is the serial link for debug only or used to transmit data to some other device? – Matthieu Jun 25 '21 at 08:07
  • @Matthieu It's a debug monitor so it just sends data on a serial port. – NoOne Jun 25 '21 at 08:08
  • 1
    You can do something like this using `sprintf()` and specify the format to be %x for hex. uint8_t temperature_C; //contains the computed temperature char buffer[16]; temperature_C = ComputeTemperature((uint8_t)ADC1->DR); sprintf(buffer, "%d", temperature_C); UART_Send_String((uint8_t*)buffer, sizeof(buffer)); – Sorenp Jun 25 '21 at 08:13
  • Re "*No that isn't remotely the question.*", Then why did you accept the answer that shows how to do exactly that? – ikegami Jun 25 '21 at 08:46
  • Yes it is. That is exactly what it is doing. For example, you stored the number one (as produced by the number literal `0x01`) in `array[0]`. The code takes that number and converts it to a string that is the hex representation of that number (`0x01`) – ikegami Jun 25 '21 at 09:14
  • 1
    [Docs for %x](https://en.cppreference.com/w/c/io/fprintf): "converts an unsigned integer into hexadecimal representation hhhh." – ikegami Jun 25 '21 at 09:19
  • I concede. I didn't fundamentally understand what you wrote. Sorry about that! – NoOne Jun 25 '21 at 09:26

1 Answers1

1

You'd need to use sprintf() to a char* then send it. Something like:

char* buf = malloc(5*len);
for (i = 0; i < len; i++)
    sprintf(buf+5*i, "0x%02x ", array[i]);
HAL_UART_Transmit(&huart4, buf, 5*len-1, 1);
free(buf);

You need to allocate five time the amount of bytes because a single byte will transform into 5:

0 x ? ? <space>

then you don't transmit the last space over the serial link (hence the -1 in 5*len-1).

You can also add an \n at the end instead of the last space, and then transmit it:

...
buf[5*len-1] = '\n';
HAL_UART_Transmit(&huart4, buf, 5*len, 1);
...
Matthieu
  • 2,736
  • 4
  • 57
  • 87