0

I'm having trouble communicating over a serial connection. I am trying to connect to a robot using the LibSerial library. My code seems to not open the connection correctly, and I don't know why. The robot is automatically sending multiple messages per second over the serial connection. The message structure is !number:number,(an exclamation mark followed by a number, a colon, another number and a comma). I have used both putty and HTerm to verify that the robot is actually sending the data. Both receiving and sending commands works fine with HTerm, but neither receiving nor sending works with my code. This is the configuration of the serial connection:

serial_connector_.Open("/dev/ttyUSB0");
serial_connector_.SetBaudRate(LibSerial::SerialStreamBuf::BAUD_115200);
serial_connector_.SetCharSize(LibSerial::SerialStreamBuf::CHAR_SIZE_8);
serial_connector_.SetNumOfStopBits(1);
serial_connector_.SetParity(LibSerial::SerialStreamBuf::PARITY_NONE);
serial_connector_.SetFlowControl(LibSerial::SerialStreamBuf::FLOW_CONTROL_NONE);

After doing so, serial_connector_.IsOpen() returns true. Then, I try to read from the serial connection using:

char read[100] = "";
if(serial_connector_.rdbuf()->in_avail() > 0){
  serial_connector_.read(read, 100);
  std::cout << "Read: " << read <<std::endl;
}

read always stays the empty string and serial_connector_.rdbuf()->in_avail()is evaluated to 1, even though that doesn't make much sense given the message structure. Reading only a single char doesn't work either.

char next_byte;
serial_connector_.get(next_byte);

has no effect. This is the first time I am working on a serial connection, so it is possible that I made a fundamental error. Any help on how to get the communication working is very appreciated!

rebatoma
  • 734
  • 1
  • 17
  • 32
  • Why don't you use POSIX open, read and close functions to work with serial port? Read this https://www.cmrr.umn.edu/~strupp/serial.html. –  Oct 20 '15 at 13:42
  • 1
    @Zola Thanks, I just started experimenting with it and it seems to work! I would still be interested in knowing why my code didn't work, it irks me that I can't find my mistake. – lohmannm Oct 20 '15 at 16:30
  • Have you been able to make it work using libserial? I have the same issue, can't receive anything, wondering if it have something to do with signal handler compatibility with USB serial adapter... – Alexandre Lavoie Mar 22 '16 at 20:30
  • No, it immediately worked when I switched to the POSIX commands as suggested by user5387687 so I gave up on libserial. – lohmannm Mar 24 '16 at 14:08

0 Answers0