crayzeewulf/libserial

Unable to read serial data on a raspberry pi 3B

pranavlal opened this issue · 4 comments

Hi all,
I am trying to read the values of 2 ultrasound sensors that have USB serial interfaces. Both are connected to my raspberry pi running raspbian buster.
The sensors and ports are working because I can get readings in python. I am trying to get readings in C++.
I am able to open the ports but do not seem to get any data so none of the loops execute.

while(frontSensorStream.rdbuf()->in_avail() > 0 && backSensorStream.rdbuf()->in_avail() > 0)

I am pasting my entire code below. What is the error I am making?

#include #include #include #include #include using namespace LibSerial ; using namespace std; constexpr const char* const SERIAL_PORT_FRONT = "/dev/ttyUSB0" ; constexpr const char* const SERIAL_PORT_BACK = "/dev/ttyUSB1" ; void signal_callback_handler(int signum) { exit(signum); } int main() { SerialStream frontSensorStream; SerialStream backSensorStream; signal(SIGINT, signal_callback_handler); std::cout << "program started" << endl;

try
{
frontSensorStream.Open(SERIAL_PORT_FRONT);
}
catch (const OpenFailed&)
{
std::cerr << "The serial port did not open correctly." << std::endl ;
return EXIT_FAILURE ;

}
try
{
backSensorStream.Open(SERIAL_PORT_BACK);
}
catch (const OpenFailed&)
{
std::cerr << "The serial port did not open correctly." << std::endl;
return EXIT_FAILURE ;
}

frontSensorStream.SetBaudRate(BaudRate::BAUD_57600);
backSensorStream.SetBaudRate(BaudRate::BAUD_57600);
frontSensorStream.SetCharacterSize(CharacterSize::CHAR_SIZE_8);
backSensorStream.SetCharacterSize(CharacterSize::CHAR_SIZE_8);
frontSensorStream.SetStopBits(StopBits::STOP_BITS_1);
backSensorStream.SetStopBits(StopBits::STOP_BITS_1);
frontSensorStream.SetParity(Parity::PARITY_NONE) ;
backSensorStream.SetParity(Parity::PARITY_NONE) ;
frontSensorStream.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
backSensorStream.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
usleep(2000);
std::cout << "ports opened, about to start capture" << endl;
while(frontSensorStream.rdbuf()->in_avail() > 0 && backSensorStream.rdbuf()->in_avail() > 0)
{
while(frontSensorStream.IsDataAvailable() && backSensorStream.IsDataAvailable())
{
char frontSensorData;
char backSensorData;
frontSensorStream.get(frontSensorData);
backSensorStream.get(backSensorData);
std::cout << "front sensor=" << frontSensorData << endl;
std::cout << "back sensor=" << backSensorData << endl;
usleep(1000);
}
}
frontSensorStream.Close();
backSensorStream.Close();
std::cout << "program done" << endl;
return EXIT_SUCCESS ;
}

I have installed libserial from source and did not get any error messages during the make process.

Hi @pranavlal ,

I think your issue is stemming from possibly a small logic error as well as physical timing. With minor modification I am able to run the code you posted above and everything executes properly.

Please review the program I have attached here (based on your code with a few small modifications) and let us know if your error persists. Note the && change to || in your while loop and the elimination of your outer while() loop as it was somewhat a repetition of your inner loop logic.

Libserial_Example.txt

(@pranavlal , I've pushed up a branch with the example mentioned just above included in the examples directory that you can checkout, compile, and run here.)

@mcsauder,
Many many thanks. The program works!

I now need to add some more code to compare values etc. Time to get reacquainted with C.

Great to hear!