Skip to main content
MAlam.2
Associate II
July 11, 2021
Solved

How to receive hex data from the terminal using UART?

I want to receive hex data from terminal like 0x66, 0x55, 0x44, 0x23, 0x11, 0xaa, 0xbb, 0xcc. Normally we can receive messages in string over UART. But I want to receive hex data instead of character or string from terminal. I am using stm32cubemx HAL library.

Can anyone give me a solution on how I can implement this?

This topic has been closed for replies.
MAlam.2
MAlam.2Best answer
Associate II
July 14, 2021

Finally it was succesful. here is the code that works

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Receive_IT(&huart2,&recvd_data,1);

if(recvd_data == '\r')

{

reception_complete = TRUE;

char ascii_str[10];

int len = strlen(ascii_str);

char hex_str[(len*2)+1];

          char *args[15];

    int i =0; int j=0;

    char *token = strtok(data_buffer, " \t\n");

    while (token != NULL) {

      args[i++] = token;

      token = strtok(NULL, " \t\n");

    }

    //to print the array

    for (j = 0; j < i; j++) {

      printf("%s\r\n", args[j]);

    }

int hexbase0=(int) strtol (args[0],NULL,16);

int hexbase1=(int) strtol (args[1],NULL,16);

int hexbase2=(int) strtol (args[2],NULL,16);

int hexbase3=(int) strtol (args[3],NULL,16);

int hexbase4=(int) strtol (args[4],NULL,16);

int hexbase5=(int) strtol (args[5],NULL,16);

int hexbase6=(int) strtol (args[6],NULL,16);

int hexbase7=(int) strtol (args[7],NULL,16);

int hexbase8=(int) strtol (args[8],NULL,16);

       //printf("%x\r\n",hexbase);

//asciitobinary(data_buffer);

TxData[0] = hexbase1;

TxData[1] = hexbase2;

TxData[2] = hexbase3;

TxData[3] = hexbase4 ;

TxData[4] = hexbase5;

TxData[5] = hexbase6;

TxData[6] = hexbase7;

TxData[7] = hexbase8;

TxHeader.IDE = CAN_ID_STD;

TxHeader.StdId = hexbase0;

TxHeader.RTR = CAN_RTR_DATA;

TxHeader.DLC = 8;

       if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, (uint8_t*)TxData, &TxMailbox) != HAL_OK)

{

Error_Handler ();

}

       //HAL_UART_Transmit(huart,data_buffer,count,HAL_MAX_DELAY);

data_buffer[count++]='\r';

for(count=10; count>0; count--)            //buffer leer machen

 {

         data_buffer[count]=0;

 }

}

else

{

data_buffer[count++] = recvd_data;

}

}

6 replies

TDK
July 11, 2021

Set your terminal program to display hex values instead of ASCII characters.

UART just sends raw data. The receiving program that decides how to interpret it.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MAlam.2
MAlam.2Author
Associate II
July 13, 2021

you are right. if i set my terminal to display hex value then its perfect. but is there any other way actually? because i am not allowed to use treminal settings.

TDK
July 13, 2021

You could convert values to strings on the STM32 before sending, which is inefficient but would work.

But without changing the data, no, there's nothing you can do on the STM32 side to cause the terminal program to display its data differently.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
July 13, 2021

The STM32 will output whatever character codes you choose to generate, and can similarly receive and parse data in whatever forms.

char Buffer[16];

HAL_UART_Transmit(hUart, Buffer, sprintf(Buffer, "%02X,%02X\n", 0x11, 0x22), 1000);

On the receive side you'll need to accumulate characters, recognizing data separation, and use things like sscanf() or atoi(), etc.

Basic C Programming stuff.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
MAlam.2
MAlam.2AuthorBest answer
Associate II
July 14, 2021

Finally it was succesful. here is the code that works

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Receive_IT(&huart2,&recvd_data,1);

if(recvd_data == '\r')

{

reception_complete = TRUE;

char ascii_str[10];

int len = strlen(ascii_str);

char hex_str[(len*2)+1];

          char *args[15];

    int i =0; int j=0;

    char *token = strtok(data_buffer, " \t\n");

    while (token != NULL) {

      args[i++] = token;

      token = strtok(NULL, " \t\n");

    }

    //to print the array

    for (j = 0; j < i; j++) {

      printf("%s\r\n", args[j]);

    }

int hexbase0=(int) strtol (args[0],NULL,16);

int hexbase1=(int) strtol (args[1],NULL,16);

int hexbase2=(int) strtol (args[2],NULL,16);

int hexbase3=(int) strtol (args[3],NULL,16);

int hexbase4=(int) strtol (args[4],NULL,16);

int hexbase5=(int) strtol (args[5],NULL,16);

int hexbase6=(int) strtol (args[6],NULL,16);

int hexbase7=(int) strtol (args[7],NULL,16);

int hexbase8=(int) strtol (args[8],NULL,16);

       //printf("%x\r\n",hexbase);

//asciitobinary(data_buffer);

TxData[0] = hexbase1;

TxData[1] = hexbase2;

TxData[2] = hexbase3;

TxData[3] = hexbase4 ;

TxData[4] = hexbase5;

TxData[5] = hexbase6;

TxData[6] = hexbase7;

TxData[7] = hexbase8;

TxHeader.IDE = CAN_ID_STD;

TxHeader.StdId = hexbase0;

TxHeader.RTR = CAN_RTR_DATA;

TxHeader.DLC = 8;

       if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, (uint8_t*)TxData, &TxMailbox) != HAL_OK)

{

Error_Handler ();

}

       //HAL_UART_Transmit(huart,data_buffer,count,HAL_MAX_DELAY);

data_buffer[count++]='\r';

for(count=10; count>0; count--)            //buffer leer machen

 {

         data_buffer[count]=0;

 }

}

else

{

data_buffer[count++] = recvd_data;

}

}

TMuka.1
Associate II
December 6, 2021

where are the variables char ascii_str[10], int len, char hex_str[(len*2)+1] being used?

can you send the full code please?