How to use serial port in Ros
Hi dears I want to connect micro atmega8 to Ros with RS232 could you please help me to know how can I use serial port in ros?
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
I solve my problem with qexserialport this is cmake and my program
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
find_package(Qt4 COMPONENTS QtCore QtGui)
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
link_directories(/usr/include/qt4/QtExtSerialPort)
rosbuild_init()
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
rosbuild_add_executable(first_test src/first_test.cpp)
target_link_libraries(first_test ${QT_LIBRARIES} -lqextserialport -lpthread)
and program
/*
* first_test.cpp
*
* Created on: Dec 19, 2013
* Author: hamid
*/
#include "ros/ros.h"
#include <QtExtSerialPort/qextserialport.h>
#include "QDebug"
#include "QCoreApplication"
int main(int argc, char** argv)
{
ros::init(argc, argv, "first_test");
QCoreApplication app(argc, argv);
ros::NodeHandle n;
QextSerialPort *port;
QByteArray bytes;
QByteArray bytes2;
QString portName = QLatin1String("ttyUSB0");
port = new QextSerialPort(QString(portName), QextSerialPort::EventDriven);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
if (port->open(QIODevice::ReadWrite) == true)
{
qDebug() << "listening for data on" << port->portName();
}
else
{
qDebug() << "device failed to open:" << port->errorString();
}
bytes[0]='h';
int total = port->write(bytes,bytes.size());
qDebug() << total;
sleep(1);
int a = port->bytesAvailable();
qDebug() << a;
bytes2.resize(a);
port->read(bytes2.data(),bytes2.size());
qDebug() <<(QString::fromAscii(bytes2).toUcs4());
return 0;
}
I want to create a node that gets data from micro and publish that data. how can i use serial port or how can i use serial port in c++? and dose ros support serial port in c++ or not?
I'm using "cereal_port" package, it works fine with Indigo. But ROS now recommend using "serial" so I'm planning to use it instead.
Asked: 2013-09-29 08:02:17 -0600
Seen: 7,986 times
Last updated: Dec 03 '15