This line is pointless/nonsense...
You cannot clear the RCIF like that, it is readonly. The only way to clear a RCIF is read RCREG (like you do in the very next line). If the byte you read is the last one in the FIFO receive buffer then the flag will be cleared automatically.
Where are you resetting rxcount to zero after the first command has been received?
Eventually you will have a buffer overrun, because buf is only defined as 4 bytes.
Please explain your serial protocol.
It looks like you are expecting a 4 byte packet starting 0xEA and ending 0x55?
Somewhat strange choices, but okay, let's roll with it.
Things you need to think about....
1. What happens to bytes before you receive an 0xEA header byte? Currently they are added to the buffer until it overflows. Probably not wise. It would be better to ignore them.
2. What happens to bytes after you receive 0xEA? Specifically what happens if you receive more than the 2 payload bytes the buffer can hold? Currently they are also added to the buffer until it overflows. Again you'd do well to ignore them and probably reject the command as invalid (as it doesn't fit the protocol).
3. What happens after you receive the 0x55 trailer? Currently sflag gets set to 1, but then there's nothing to to reset rxcount or rxheader for the next packet. That's probably the reason your second command packet gets ignored.
post edited by pcbbc - 2019/09/11 09:16:23