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

后端 未结 2 1307
我寻月下人不归
我寻月下人不归 2020-12-05 21:48

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

相关标签:
2条回答
  • 2020-12-05 22:18

    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.

    0 讨论(0)
  • 2020-12-05 22:27

    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

    0 讨论(0)
提交回复
热议问题