0

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.

linsek
  • 3,334
  • 9
  • 42
  • 55
  • TL;DR It's often misconfiguration of the serial port parameters. – Iharob Al Asimi Jan 08 '16 at 21:45
  • @iharob then why would some data come through as expected? – linsek Jan 08 '16 at 21:46
  • I would say that it's another sign of misconfiguration. – Iharob Al Asimi Jan 08 '16 at 21:47
  • I'm not sure what kind of configuration you could be referring to. I copied the attrib and blocking setup from this thread (http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c). Only difference is my applications are setup for blocking instead of non-blocking. – linsek Jan 08 '16 at 21:49
  • Please post the code where you setup the serial port, and what is the expected setup, parity bits, baud rate and other settings. – Iharob Al Asimi Jan 08 '16 at 21:49

1 Answers1

5
char rx_bfr[128];

On your platform char is signed. A signed char with value 0xE3 is negative but an integer with value 0xE3 is positive, so they won't compare as equal. Use unsigned char instead.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278