How to receive hex data from the terminal using UART?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-07-11 5:44 AM
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?
Solved! Go to Solution.
- Labels:
-
UART-USART
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-07-14 6:17 AM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-07-11 6:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-07-13 5:09 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-07-13 7:16 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-07-13 9:27 AM
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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-07-14 6:17 AM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-06 3:42 AM
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?
