serial available() returns false

asked 2019-07-15 01:34:20 -0500

wlsdx gravatar image

updated 2019-07-15 04:14:58 -0500

Hi, I'm trying using serial to send information to a serial port.

The following is my cpp code. The result shows that ros_ser.isOpen()==true, and ros_ser.available()==false, where ros_ser is an object of serial::Serial. I've attached my device to a USB port and found the file ttyUSB0 in folder /dev.

Thanks for any help in advance.

#include "ros/ros.h"
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
#include <string>
#include <sstream>

serial::Serial ros_ser;

int main(int argc, char** argv){
 ros::init(argc, argv, "my_serial_node");
 ros::NodeHandle n;
     serial::Timeout to = serial::Timeout::simpleTimeout(1000);
 catch (serial::IOException& e)
     ROS_ERROR_STREAM("Unable to open port ");
     return -1;

     ROS_INFO_STREAM("Serial Port opened");
     return -1;

 std::stringstream str;

 ros::Rate loop_rate(10);
         ROS_INFO_STREAM("Sending Information...");
     else ROS_INFO_STREAM("ros_ser not available");
edit retag flag offensive close merge delete


Hi, I'm trying using rosserial to send information to a serial port.

the code you show doesn't look like what I would expect when you're using rosserial.

Are you confusing it with the serial package?

Those are two entirely different things (apart from the fact that they can both be used with serial ports).

gvdhoorn gravatar image gvdhoorn  ( 2019-07-15 02:46:28 -0500 )edit

Thanks. I've corrected the title.

wlsdx gravatar image wlsdx  ( 2019-07-15 03:36:16 -0500 )edit

I don't see a question in your post, but from the C++ documentation for available() it seems to return the nr of characters (or bytes) that are waiting in the receive buffer of the serial port.

What are you expecting it to return exactly? Do you have anything "on the other side" sending data? If not, available() returning 0 seems like expected behaviour.

gvdhoorn gravatar image gvdhoorn  ( 2019-07-15 03:46:19 -0500 )edit

Thanks. A motor is attached to the USB port. It needs a command to run. The command is a long string and has been tested when using the serial port assistant cutecom. So the motor should be running if the node is doing the job.

wlsdx gravatar image wlsdx  ( 2019-07-15 06:31:31 -0500 )edit

So is the motor sending anything back to your node/PC? If it isn't, available() will never return anything other than 0.

Just making sure: available() does not return whether the serial port is "available" (or present), it returns whether there is data waiting to be read (by your node).

gvdhoorn gravatar image gvdhoorn  ( 2019-07-15 06:43:05 -0500 )edit