0

I have a one line serial communication interface, and the problem is that I send in 01010101 and the echo that I receive is 8 out of 10 times 01010101 but 2 out of 10 I receive 01110101.

Code example:

void checkVersion(int fd) {
    tcflush(fd, TCIFLUSH);
    unsigned char checkVersion[] = {0x55, 0x02, 0x00, 0x02};
    int n = write(fd, &checkVersion, 4); //Send data
    if (n < 0) cout << "BM: WRITE FAILED" << endl;

    char read_bytes[10] = {0};
    char c;
    int aantalBytes = 0;
    bool foundU = false;
    int res;
    while (aantalBytes < 7) {
        res = read(fd, &c, 200);
        if (res != 0) {
            cout << "Byte received: " << bitset < 8 > (c) << endl;
            if (c == 'U')foundU = true;
            if (foundU)
                read_bytes[aantalBytes++] = c;
        }
        if (aantalBytes > 2 && !foundU) break;
    }
    if (!foundU) checkVersionSucceeded = false;
    if (read_bytes[aantalBytes - 3] == 0x02 && read_bytes[aantalBytes - 2] == 0x04 && read_bytes[aantalBytes - 1] == 0x06)
       cout << "BM Version 4" << endl;
}

How I configure my port:

int configure_port(int fd) // configure the port
{
    struct termios port_settings; // structure to store the port settings in

    cfsetispeed(&port_settings, B9600); // set baud rates
    cfsetospeed(&port_settings, B9600);

    port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
    port_settings.c_cflag &= ~CSTOPB;
    port_settings.c_cflag &= ~CSIZE;
    port_settings.c_cflag |= CS8;

    tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
    return (fd);
} 

What is the problem? How is it possible that the echo is mixed up 2 out 10 times?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
NeViXa
  • 73
  • 7
  • What are you communicating with? How is it configured? – Oliver Charlesworth May 29 '12 at 08:16
  • Maybe you should fix the following: char c; ... res = read(fd, &c, 200); It may cause overflow. And that can cause nasty problems. – SKi May 29 '12 at 08:25
  • 4
    `res = read(fd, &c, 200);` You attempt to read 200 bytes into a one-byte character? – wildplasser May 29 '12 at 08:28
  • As @wildplasser already illustrated, your code has a few quirks. E.g. it also looks like you wanted to hunt for the 'U' character in the first 7 bytes, but `aantalBytes` is only incremented if an 'U' is found. The `if (!foundU) {}` statement is useless because the loop won't exit without `foundU` being `true`. Maybe try doing something realy simple first like `tcflush();write(1 char);read(1 char);` and see if the problem still exists. – Bart May 29 '12 at 08:38
  • Thank you for the answers, but not solved yet. I am indeed 'hunting' for the U because that is what I'm sending in as first byte (0x55 = U) and therefore should be received as first byte, everything before the U (if there is any) is garbage. The loop won't exit if it fails indeed, in the real function I have a 'loop killer counter'. – NeViXa May 29 '12 at 08:56
  • @Bart With only tcflush();write(1 char);read(1 char); I have the same problem. Could it be that 0x55 is not interpreted as 01010101? – NeViXa May 29 '12 at 08:59
  • 0x55 is not wrongly interpreted. The checkVersion array sends 01010101 but the first read byte is 01110101 – NeViXa May 29 '12 at 09:29
  • The difference is 32, which also happens to be the difference between 'U' and 'u', in ASCII ... – unwind May 29 '12 at 09:38
  • If you try to read 200 bytes into a one byte array, then all bets are off until you fix it. You could be overwriting your whole program data with junk, putting your program in la-la-land, giving any random output and behavior. – Lundin May 29 '12 at 09:47
  • Anyway, you should check for buffer overruns and other UART errors. If you get UART errors, the problem is related to UART settings. Otherwise, it is in your program. – Lundin May 29 '12 at 09:49

1 Answers1

1

Perhaps you should try the function bzero() when you configure the connection.

bzero(&port_settings, sizeof (port_settings));

This clears the struct for new port settings, which might help to stop the irregular answers you receive over the serial port.

diip_thomas
  • 1,531
  • 13
  • 25