I\'m using LibSerial on Ubuntu to read and write data on serial port.
At the moment, I\'m able to write and receive strings over the serial port, but my code does no
I think you just need to use while( serial_port.rdbuf()->in_avail() > 0 )
as a condition for your last while loop. Then it'll read out all the available data (“answer”) and you can send the second command after that.
Using try and catch would be helpful. :)
Try this: A global pointer SerialPort *pu was already declared and the port was opened.
int rxstring(char *cstr, unsigned int len, bool print_str)
{
char temp=0;
int i=0;
while(temp!='\n')
{
try
{
temp=pu->ReadByte(100);
}
catch(SerialPort::ReadTimeout &e)
{
//cout<<"Read Timeout"<<endl;
return 1;
}
if((temp!='\n')&&(temp!=0)&&(temp!=' '))
{
cstr[i]=temp;
++i;
//cout<<i++<<temp<<'x'<<endl;
}
}
cstr[i]='\0';
if(print_str)
puts(cstr);
return 0;
}
Reference: http://libserial.sourceforge.net/doxygen/class_serial_port.html#a15