I have two applications communicating over serial. The target has a state machine that it navigates. It will receive and throw out data until it gets a start of message byte (0xE3) and then process data until an end of message byte (0x3E). I'm having an issue with some of the data. I believe it's a processing problem and not a communication issue. I also know it's something simple, but struggling to see the problem.
The host is sending data to the target like so. The bfr data is: {0xAA, 0xFF, 0x05, 'H', 'e', 'l', 'l', 'o'};
byte = MSG_START_BYTE; // 0xE3
if (write(fd, &byte, 1) < 0)
return -1;
if (write(fd, bfr, bfr_len) < 0)
return -1;
if (write(fd, &crc, sizeof(crc)) < 0)
return -1;
byte = MSG_END_BYTE; // 0x3E
if (write(fd, &byte, 1) < 0)
return -1;
The target is using select() and read() to receive the data.
char rx_bfr[128];
uint8_t max_bytes_to_read = 1;
memset(rx_bfr, 0, strlen(rx_bfr));
...
printf("\tactivity...");
bytes_read = read(fd, rx_bfr, max_bytes_to_read);
if (bytes_read < 0)
return -1;
else if (bytes_read == 0)
return -1;
printf(" %d bytes to process...\n", bytes_read);
for (i = 0; i < bytes_read; i++)
{
printf("byte[%d] = 0x%x\n", i, rx_bfr[i]);
switch (state)
{
case SOM:
if (rx_bfr[i] == 0xE3)
...
And the target console output is:
Waiting for message...
activity... 1 bytes to process...
byte[0] = 0xffffffe3
activity... 1 bytes to process...
byte[0] = 0xffffffaa
activity... 1 bytes to process...
byte[0] = 0xffffffff
activity... 1 bytes to process...
byte[0] = 0x5
activity... 1 bytes to process...
byte[0] = 0x48
activity... 1 bytes to process...
byte[0] = 0x65
activity... 1 bytes to process...
byte[0] = 0x6c
activity... 1 bytes to process...
byte[0] = 0x6c
activity... 1 bytes to process...
byte[0] = 0x6f
activity... 1 bytes to process...
byte[0] = 0xffffffd4
activity... 1 bytes to process...
byte[0] = 0xffffff81
activity... 1 bytes to process...
byte[0] = 0x1a
activity... 1 bytes to process...
byte[0] = 0x3c
activity... 1 bytes to process...
byte[0] = 0x3b
activity... 1 bytes to process...
byte[0] = 0x3e
So in my state machine the if conditional is looking for 0xE3
as the SOM, but the first "byte" in the buffer as seen from the printf is 0xFFFF_FFE3
.