1

I'm trying to communicate with an USB-uart module using Libserial.

Following is my code for initial part:

serial_port.Open("/dev/ttyUSB0");
if ( ! serial_port.good() )
{
    std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
        << "Error: Could not open serial port."
        << std::endl ;
    exit(1) ;
}

serial_port.SetBaudRate( SerialStreamBuf::BAUD_115200 ) ;
if ( ! serial_port.good() )
{
    std::cerr << "Error: Could not set the baud rate." <<
        std::endl ;
    exit(1) ;
}

When I run it on Ubuntu 12.04 and 13.04 with the same USB module, they all say

Error: Could not set the baud rate.

I did some tests and finally found this error would occur if I set the baud rate as or higher than 115200. It works well on 57600 and 19200.

But I'm wondering is there any possible way for me to set the baud rate as 115200? I downloaded a serial test tool, it can work as the 115200(but I didn't checked the msg content, I just notice the transmit led is flash).

Or is it the hardware limit so I need to buy another module if I want a higher baud rate?

Thanks

===========

UPDATE:

  1. There is no problem with the hardware. I tested it in Windows VS using 115200 and it works well. But it failed on two Ubuntu desktop(12.04 and 13.04).

  2. I print the baudrate out after I set it

    serial_port.SetBaudRate( SerialStreamBuf::BAUD_115200) ;

    int rate = serial_port.BaudRate();

    cout << SerialStreamBuf::BAUD_115200 << endl;

    cout << rate << endl;

the result shows their values are the same, both are 4098.

Then I tried to comment all the .good() part with and after the SetBaudRate part, the program start successfully but the transmit LED doesn't flash. So I think there is really something wrong with the baudrate set so the serial initial failed, although the baudrate it returns is correct.

Now I have no idea what to do next...

in case you need to see all my code

4

2 回答 2

2

I'm guessing it's this bug, but haven't verified it.

http://ehc.ac/p/libserial/bugs/10/

Now in SerialStreamBuf.h

enum BaudRateEnum {
                BAUD_50    = SerialPort::BAUD_50,
                BAUD_75    = SerialPort::BAUD_75,
                BAUD_110   = SerialPort::BAUD_110,
                BAUD_134   = SerialPort::BAUD_134,
                BAUD_150   = SerialPort::BAUD_150,
                BAUD_200   = SerialPort::BAUD_200,
                BAUD_300   = SerialPort::BAUD_300,
                BAUD_600   = SerialPort::BAUD_600,
                BAUD_1200  = SerialPort::BAUD_1200,
                BAUD_1800  = SerialPort::BAUD_1800,
                BAUD_2400  = SerialPort::BAUD_2400,
                BAUD_4800  = SerialPort::BAUD_4800,
                BAUD_9600  = SerialPort::BAUD_9600,
                BAUD_19200 = SerialPort::BAUD_19200,
                BAUD_38400 = SerialPort::BAUD_38400,
                BAUD_57600 = SerialPort::BAUD_57600,
                BAUD_115200 = SerialPort::BAUD_115200, // 4098
                BAUD_230400 = SerialPort::BAUD_230400,
#ifdef __linux__
                BAUD_460800 = SerialPort::BAUD_460800,
#endif
                BAUD_DEFAULT = SerialPort::BAUD_DEFAULT, // 4097
                BAUD_INVALID
            } ;

So BAUD_INVALID will be 4098, exactly the same as BAUD_115200. That's why you get error.

于 2014-09-28T05:59:20.823 回答
0

hello i had the same problem and even i tried everything using c++ API for libSerial couldn't solve until i used the bellow code in my serial initialization!! I used the system call once at the initialization and that worked GREAT!! NOTE instead of /dev/ttyACM0 use the name of your serial device /dev/ttyXXX

LibSerial::SerialStream serial;
//serial.SetBaudRate(LibSerial::SerialStreamBuf::BAUD_9600);//THAT DOESNT WORKS
serial.SetCharSize( LibSerial::SerialStreamBuf::CHAR_SIZE_8);
serial.Open("/dev/ttyACM0");
system("sudo stty -F /dev/ttyACM0 115200");//YOU HAVE TO RUN THE EXCECUTABLE FROM COMMAND LINE WITH SU PRIVILEGES
于 2017-01-23T09:09:53.300 回答