Reputation: 1
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!
Upvotes: 0
Views: 417