serial available() returns false
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;
try
{
ros_ser.setPort("/dev/ttyUSB0");
ros_ser.setBaudrate(115200);
serial::Timeout to = serial::Timeout::simpleTimeout(1000);
ros_ser.setTimeout(to);
ros_ser.open();
}
catch (serial::IOException& e)
{
ROS_ERROR_STREAM("Unable to open port ");
return -1;
}
if(ros_ser.isOpen()){
ROS_INFO_STREAM("Serial Port opened");
}else{
return -1;
}
std::stringstream str;
ros::Rate loop_rate(10);
while(ros::ok()){
ros::spinOnce();
if(ros_ser.available()){
ROS_INFO_STREAM("Sending Information...");
str<<"myInformation"<<std::endl;
ros_ser.write(str.str());
}
else ROS_INFO_STREAM("ros_ser not available");
loop_rate.sleep();
}
}
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).
Thanks. I've corrected the title.
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()
returning0
seems like expected behaviour.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.
So is the motor sending anything back to your node/PC? If it isn't,
available()
will never return anything other than0
.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).