Problem with Flush(), clear buffer input in serial communication

Hi,

First of all, sorry for my limited knowledge. I'm quite new to C++ and I'm having trouble connecting the Raspberry Pi to the Koala car using RS232.

The problem I'm facing is that I know everytime I send the command to the car, there will be a random signal sent from the Pi and it will affect my commands. For every first command I send I always get the "Command not found" response from the car.

I'm trying to use the Flush() function to clear off that random signal but it seems to not working :(. Here is my code, please help me :(

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
bool res;
	std::string data;
	std::string temp;

    _serial = new ClsSerialCom(COM.c_str());
     _serial->Flush();
     res = _serial->Write("B\r", 2);
	//usleep(2000);
	if (res == true)
	// Read the Acknowledge
	//usleep(2000);
	res = _serial->Read(data);
	//usleep(2000);

	//std::cout << data << std::endl;

	if (res != true || data[0] != 'b')
	{
		_serial->Flush();
		return false;
	}
	cout << data;

	// return 1 means successful in opening the serial port
	return true;


And this is how I set my Flush();

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
bool ClsSerialCom::Read(std::string &data)
{
    int dwBytesRead = 0;

    //Read 25 bytes (trial and error!!! result in different reading speed)
    char pbuff[100];

    usleep(3000);
    dwBytesRead = read(iDevice, pbuff, 100);

    if (dwBytesRead <= 0)
        return -1;

    pbuff[dwBytesRead++] = 0;
    data = pbuff;
    //std::cout<<std::endl<<pbuff<<std::endl;

	return dwBytesRead;
}
void ClsSerialCom::Flush()
{

    //usleep(200000);
    tcflush(iDevice,TCIOFLUSH);
}


Sorry for the rubbish comments as I'm trying to test whether it works :(

Thank you
tcflush(iDevice, TCIOFLUSH); is supposed to have a return value you can check that to see what happened.

You might also want to check what the TCIOFLUSH queue_selector does and if there isn't another appropriate one for flushing your device I/O.
Last edited on
Topic archived. No new replies allowed.