cancel
Showing results for 
Search instead for 
Did you mean: 

I am trying to send hex values to the Uart terminal, but I am getting wrong values or nothing at terminal. I am getting the characters ok. I am using DMA and interrupt (IRQ_Handler), and STMCUBE MX to generate DMA init and uart codes. Please help.

KMINH.1
Associate III

I am using below code;

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)

{

if (huart->Instance == USART3)

{

oldPos = newPos; // Update the last position before copying new data

// If the data is large and it is about to exceed the buffer size, we have to route it to the start of the buffer

// This is to maintain the circular buffer

// The old data in the main buffer will be overlapped

if (oldPos+Size > MainBuf_SIZE) // If the current position + new data size is greater than the main buffer

{

uint16_t datatocopy = MainBuf_SIZE-oldPos; // find out how much space is left in the main buffer

memcpy ((uint8_t *)MainBuf+oldPos, RxBuf, datatocopy); // copy data in that remaining space

oldPos = 0; // point to the start of the buffer

memcpy ((uint8_t *)MainBuf, (uint8_t *)RxBuf+datatocopy, (Size-datatocopy)); // copy the remaining data

newPos = (Size-datatocopy); // update the position

}

// if the current position + new data size is less than the main buffer copy the data into the buffer and update the position  

else

{

memcpy ((uint8_t *)MainBuf+oldPos, RxBuf, Size);

newPos = Size+oldPos;

}

// start the DMA again 

HAL_UARTEx_ReceiveToIdle_DMA(&huart3, (uint8_t *) RxBuf, RxBuf_SIZE);

__HAL_DMA_DISABLE_IT(&hdma_usart3_rx, DMA_IT_HT);

}

}

1 ACCEPTED SOLUTION

Accepted Solutions

This is an issue with your terminal prog. Terminals were designed for human IO using printable ASCII chars and few control sequences. Everything else is implementation specific and can often be configured, like VT100 emulation, ANSI ESC codes,...

There is no single standard how 0x90 should look like on the receiver side. Maybe you want to convert your binaries into printable ASCII chars using sprintf(buffer, "%02x ", value ); before sending?

hth

KnarfB

View solution in original post

7 REPLIES 7
KnarfB
Principal III

> send hex values to the Uart terminal

But you're showing the receive side? The UART is agnostic to the content you are sending. If it works for (printable) chars and does not work for (binary?) hex values, check your terminal settings or use another terminal or raw serial port. Terminals usually interpret control characters for VT100 emaulation and such.

hth

KnarfB

KMINH.1
Associate III

Thanks KnarfB for response

And Sorry for that.

I am using below line for transmission, which may not use DMA, interrupt;

HAL_UART_Transmit(&huart2, (uint8_t *)0x90, 16, HAL_MAX_DELAY);

This line prints ok,

HAL_UART_Transmit(&huart2, (uint8_t *)"This works ok", 13, HAL_MAX_DELAY);

I checked with 2 terminals, Teraterm and putty.

I am not sure about the settings. Can u plz give me a clue?

(uint8_t *)0x90

This won't work. You are type casting the constant to an address and sendig the arbitrary value which is found at that address. To send a 0x90, define a local variable and pass its address to the function like

uint8_t buffer[] = { 0x90 };
HAL_UART_Transmit(&huart2, buffer, sizeof(buffer), HAL_MAX_DELAY);

which will probably print as É or such on the terminal, because the terminal will re-interpret the 0x90 as char.

hth

KnarfB

KMINH.1
Associate III

It works this way, but conditionally,

uint16_t buffer = 0x68;
HAL_UART_Transmit(&huart2, (uint8_t *)&buffer, sizeof(buffer), HAL_MAX_DELAY);

The output is h.

If I assign hex value beyond ascci range, like 0x80 or above to the buffer, then it prints nothing.

How do I see these hex values in terminal?

Thanks

Minhaj

This is an issue with your terminal prog. Terminals were designed for human IO using printable ASCII chars and few control sequences. Everything else is implementation specific and can often be configured, like VT100 emulation, ANSI ESC codes,...

There is no single standard how 0x90 should look like on the receiver side. Maybe you want to convert your binaries into printable ASCII chars using sprintf(buffer, "%02x ", value ); before sending?

hth

KnarfB

KMINH.1
Associate III

You could be right. The terminal isn't displaying them.

Actually I am trying to use UART for debug messages purpose. So displaying every hex value is important.

Using sprintf throws me this warning in Keil,

../Core/Src/main.c(203): warning: implicitly declaring library function 'sprintf' with type 'int (char *, const char *, ...)' [-Wimplicit-function-declaration]

    sprintf(buf, "%02s ", val ); 

whereas char, buf are declared as

char * buf, *val;

Thanks

Minhaj

KMINH.1
Associate III

 sprintf(buf, "%02s ", val );  worked. I missed stdio.h in the beginning.

Thanks KnarfB for the suggestions.

Minhaj