Flushing the serial port

I realize that this may be too specialized a cpp question but I thought I'd just give it a try in case there are any serial programming gurus here. Every now and then, I have to restart the create because i am getting bad serial data. What is the proper way to reset the serial buffer?

This is how I connect:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int Biscuit::biscConnect(char const *device) {
    if(device == NULL) return BISC_ERR;

    struct termios tty;

    // Try to open the device
    if((deviceDescriptor = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
        return BISC_ERR;
    }

    tcgetattr(deviceDescriptor, &tty);
    tty.c_iflag     = IGNBRK | IGNPAR;
    tty.c_lflag     = 0;
    tty.c_oflag     = 0;
    tty.c_cflag     = CREAD | CS8 | CLOCAL;
    cfsetispeed(&tty, BISC_BAUD_RATE);
    cfsetospeed(&tty, BISC_BAUD_RATE);
    tcsetattr(deviceDescriptor, TCSANOW, &tty);
    
    // Initialize file descriptor sets
    
    FD_ZERO(&read_fds);
    FD_ZERO(&write_fds);
    FD_ZERO(&except_fds);
    FD_SET(deviceDescriptor, &read_fds);
    
    
    
    // Set timeout to 20 microseconds for serialport stream reading
    timeout.tv_sec = 0;
    timeout.tv_usec = 40;
    
    return deviceDescriptor;
}


And I have tried:

1
2
3
4
5
6
7
8
int Biscuit::biscserialFlush() {
    usleep(2000000);
    tcflush(deviceDescriptor,TCIOFLUSH);
    return BISC_SUCCESS;
    
}



But it doesn't work.
Thanks
Chris

Last edited on
I have to restart the create
What is 'the create'?

i am getting bad serial data
How do you identify 'bad data'?

What is the proper way to reset the serial buffer?
close() -> open(...). It is a stream like a file.

But it doesn't work.
What doesn't work?
Last edited on
Thanks for your interest.

The create is an Irobot Create 1. It is a programmable robot using a serial programming interface.

While the question I posted here is more general. The issue is a consequence of my failure to request streaming data from the robot. Somehow, starting and stopping the stream results in corrupted sensor data packets in the read line. Not to mention that I couldn't get any data from the irobot create using the streaming sensor method. I can parse the start byte and the packet ID byte but the data byte is zero.

I was trying to find a not so drastic reset of the serial buffer in this thread to see If I can make the system recover from a bad sensor stream attempt. That would at least shed some more light into the issue. The tcflush did not restore the system to before the sensor read. A shutdown and restart fixes the issue.

May I refer you to this thread and anybody else who wish to reply:

http://www.cplusplus.com/forum/general/198170/#msg949744

When I get a working solution, I will make sure to update this post for future readers.
Sorry for the late reply I almost missed this.

Let me guess the scenario:
The sensor permanently sends data to the serial port. Then some times later the device is reading from the serial port, correct?

The would mean that it is rather unlikely that you get valid data at the beginning. So you have to read until you determine a correct dataset.

Flushing doesn't help here. It is actually only for write operations.
Thanks for your response. I will continue to study the problem. I will post as soon as I have more info. My real life work has gotten busier at the moment. I am also leaning away from the serial streaming solution at this time in favor of sensor packet requests. It would be nice to figure out the problem though.
Thanks
Chris
Topic archived. No new replies allowed.