Reputation: 1
I'm trying to transmit with UART, from an stm32 Nucleo to Matlab Simulink, a certain amount of data. More precisely I want to transmit two uint16 variables. I splitted them in two packeges of 1 byte each. The problem is that when I receive them on Simulink, it's not garanteed that SImulink take the packages in the correct order, so I have to implement something that allows me to understand where is the start of the message. I'm trying to do that, but at the moment I'm not succeeding. I want to specify that in Simulink I can write the header of the message, so I think that I just have to set it in my stm32 board.
Here it is my code:
//Serial Stream Routine
if ((streamActive != 0) && (dataReady == 1))
{
do{
//Sending the first byte of counter
firstbyte = counter & 0xFF;
memcpy(str, &firstbyte, 1);
HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);
//Sending the second byte of counter
secondbyte =(counter >> 8) & 0xFF ;
memcpy(str, &secondbyte, 1);
HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);
if(streamActive == 2){ //We transmit the counter with an offset
offset = counter + OFFSET_VALUE;
//Sending the first byte of offset
firstbyte = offset & 0xFF;
memcpy(str, &firstbyte, 1);
HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);
//Sending the second byte of offset
secondbyte =(offset >> 8) & 0xFF ;
memcpy(str, &secondbyte, 1);
HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);
}
counter++;
dataReady = 0;
}while(counter < MAX_VALUE);
So I want to trasmit the variables counter and offset. Help me, thanks.
Upvotes: 0
Views: 959
Reputation: 67476
Now I want to receive them in the correct order
Add header & PC & ARM Corex are both little-endian so it is enough to:
do{
HAL_UART_Transmit(&huart2, (void *)"START", 5, HAL_MAX_DELAY);
HAL_UART_Transmit(&huart2, (void *)&counter, 2, HAL_MAX_DELAY);
if(streamActive == 2){ //We transmit the counter with an offset
offset = counter + OFFSET_VALUE;
HAL_UART_Transmit(&huart2, (void *)"START", 5, HAL_MAX_DELAY);
HAL_UART_Transmit(&huart2, (void *)&offset, 2, HAL_MAX_DELAY);
}
counter++;
dataReady = 0;
}while(counter < MAX_VALUE);
And in simulink (matlab command): uint16('START')
Upvotes: 1