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

lanyusea
  • 143
  • 2
  • 14
  • Are you running it with high privileges? However, if you can set other baud rates with the same procedure it's pretty weird. – edmz Aug 10 '14 at 16:49
  • did you check the hardware? – Tanuki Aug 10 '14 at 17:28
  • @black yes it makes me very puzzled, I can set other baud rate with the same procedure, just change the `BAUD_115200` into others. and not quite understand what `high privileges` mean, I didn't find a parameter with this name. – lanyusea Aug 11 '14 at 00:57
  • @Tanuki the hardware works well. I just asked someone test it in Visual Stdio, Windows, and it works well under 115200. But he doesn't use the libserial library. – lanyusea Aug 11 '14 at 01:00
  • @lanyusea You're opening a system device, therefore you need high privileges; basically launched with `sudo` or as `root, although there're other ways. – edmz Aug 11 '14 at 08:54
  • @black yes of course. I `sudo -s` before running my code. Or it will say `cannot open serial port` instead of `cannot set baud rate` – lanyusea Aug 11 '14 at 13:59
  • @lanyusea Indeed, I had guessed it. [This](https://stackoverflow.com/questions/13449912/cannot-set-baud-rate-on-usb-serial-port-for-ubuntu-12-04) seems related. – edmz Aug 11 '14 at 14:24

2 Answers2

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.

seilgu
  • 364
  • 3
  • 14
  • thanks @seilgu ! I think you are right, but sorry I cannot test it now since I rebuild my system. – lanyusea Sep 29 '14 at 06:22
  • sorry for the late acception, I just discussed this issue with someone else today and we all agree you are right. – lanyusea Jun 27 '15 at 05:29
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
  • really a nice catch I should have try. btw, someone answered a question I asked two years before, it feels dame great! – lanyusea Jan 24 '17 at 10:04
  • It's a pity some times we never get some answer in really important things over here!!probably cause none face the same problem so he had no the need to solve it!!whatever that libSerial even two years later still doesn't works as It should be!!The most strange thing is that libSerial-like libraries works on windows well and not in Linux!!Usually windows sucks lol but to c++ with serial they works well! – teo tianneas p Jan 25 '17 at 07:03