Read and Write on serial port in Ubuntu with C/C++ and LibSerial

风格不统一 提交于 2019-11-27 21:40:28

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.

Bazooka

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!