2020-12-02 10:35 AM
Hi all, I've recently been teached about how to manage streamed data in this case for managing commands, then my code is like the following explanation: I've programmed the uart2 to receive the streamed data byte to byte and I've been storing it in a buffer, every time arrives a byte then a flag is set and the function that analizes the streamed data takes the address of the reception buffer, inside the function that analizes the streamed data I declared several pointers to a struct to manage differente kind of commands.
All the commands has a code in the first byte and a termination suffix, the suffix is 0xff 0xf 0xff and the command code are the following:
NEX_RET_CMD_FINISHED (0x01)
NEX_RET_SLEEP_CURRENT (0x86) (134)
NEX_RET_AWAKE_CURRENT (0x87) (135)
NEX_RET_EVENT_TOUCH_HEAD (0x65) (101)
NEX_RET_CURRENT_PAGE_ID_HEAD (0x66) 102)
NEX_RET_EVENT_POSITION_HEAD (0x67) (103)
NEX_RET_EVENT_SLEEP_POSITION_HEAD (0x68) (104)
the code in the stream analizer is the following
typedef uint8_t cCode;
typedef struct
{
cCode prefix;
uint32_t suffix : 24;
} recvMessage;
typedef struct
{
cCode prefix;
uint8_t page;
} head;
typedef struct
{
head header;
uint32_t suffix;
} pageEvent;
typedef struct
{
head header;
uint8_t ident;
uint8_t event;
uint32_t suffix : 24;
} eventID;
typedef struct
{
cCode prefix;
uint8_t page;
uint8_t ID;
uint8_t event;
uint32_t suffix : 24;
} eventBody;
typedef struct
{
cCode prefix;
uint16_t x;
uint16_t y;
uint8_t event;
} touchC;
uint8_t analize_string(uint8_t * incomming)
{
static uint8_t incCounter = 0;
recvMessage *confirm;
confirm = (recvMessage *) incomming;
pageEvent *pEvent;
pEvent = (pageEvent *) incomming;
eventID * identif;
identif = (eventID *) incomming;
touchEvent *sleepingEvent;
sleepingEvent = (touchEvent *) incomming;
// data *message;
if((confirm->prefix == NEX_RET_CMD_FINISHED)
&& (confirm->suffix == 0xffffff))
{
interface.aHMI = true;
}
if((confirm->prefix == NEX_RET_SLEEP_CURRENT)
&& (confirm->suffix == 0xffffff))
{
interface.aHMI = true;
interface.pSleep = true;
}
if((confirm->prefix == NEX_RET_AWAKE_CURRENT)
&& (confirm->suffix == 0xffffff))
{
interface.aHMI = true;
interface.pSleep = false;
}
if((identif->header.prefix == NEX_RET_EVENT_TOUCH_HEAD)
&& (identif->suffix == 0xffffff))
{
interface.aHMI = true;
}
if((sleepingEvent->prefix == NEX_RET_EVENT_POSITION_HEAD)
&& (sleepingEvent->suffix == 0xffffff))
{
interface.aHMI = true;
}
if((pEvent->header.prefix == NEX_RET_CURRENT_PAGE_ID_HEAD)
&& (pEvent->suffix == 0xffffff))
{
interface.aHMI = true;
interface.page = pEvent->header.page;
}
if( if((sleepingEvent->prefix == NEX_RET_EVENT_POSITION_HEAD)
&& (sleepingEvent->suffix == 0xffffff))
{
interface.aHMI = true;
}
if((sleepingEvent->prefix == NEX_RET_EVENT_SLEEP_POSITION_HEAD)
&& sleepingEvent->suffix)
{
interface.aHMI = true;
}
HAL_UART_Transmit_IT(&dbSerial, (uint8_t *) &sleepingEvent[incCounter], 1);
if(interface.aHMI)
{
////// screen_handler();
interface.aHMI = false;
}
incCounter++;
if(incCounter == Long_Rec)
{
incCounter = 0;
}
receive_data((char *) &U2rxBuffer[incCounter], (uint16_t) sizeof(uint8_t));
return incCounter;
}
in main the call to the function is treated like follows
receive_data(U2rxBuffer, sizeof(uint8_t));/* Inicializa la recepción de 1
* caracter. */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(received == true)
{
analize_string((uint8_t *) U2rxBuffer);
received = false;
}
}
and the flag received is set in the RxCpltCallback for the uart2like follows
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
received = true;
}
and some of the conditions to analize the stream works, some other doesn't works; by example, the conditions that compares with the macros NEX_RET_CMD_FINISHED and
NEX_RET_SLEEP_CURRENT works perfectly and those commands only has four bytes being the prefix and the suffix, the condition that works with the macro NEX_RET_EVENT_SLEEP_POSITION_HEAD also works well but the condition that works with the macro NEX_RET_EVENT_POSITION_HEAD doesn't works well thoug both commands has the same 9 bytes long, when I send to the other uart the content of the pointer sleepingEvent then it prints this
the text in the upper console s what I sent through the uart2 and the lower console is what I suposse is the content of the pointer to a struct sleepingEvent, now seeing that I can note that the content of the stream beyond the prefix is lost or not properly stored (I don't know which of that causes are) and I don't know how to solve it.
If I print directly the content of the pointer incomming then it prints what is in the lower console of the following image
what I see there is that the uart_transmit function is printing bad what is received.
So, can someone help my to solve the problem?
Thanks so much in advance for the help.