ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
3

How to use serial port in Ros

asked 2013-09-29 08:02:17 -0500

Hamid Didari gravatar image

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?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
5

answered 2013-12-22 02:30:54 -0500

Hamid Didari gravatar image

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;
}
edit flag offensive delete link more
3

answered 2013-09-29 20:18:46 -0500

tfoote gravatar image

ROS does not define how to use a generic serial port. There are several libraries which implement protocols on top of serial ports such as serial as well as a way to use ROS over the serial port rosserial

But if you just want to talk to a serial port, both python and C?C++ have ways to do that.

edit flag offensive delete link more

Comments

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?

Hamid Didari gravatar image Hamid Didari  ( 2013-09-29 23:18:29 -0500 )edit
0

answered 2015-12-03 03:42:40 -0500

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.

edit flag offensive delete link more

Question Tools

6 followers

Stats

Asked: 2013-09-29 08:02:17 -0500

Seen: 7,699 times

Last updated: Dec 03 '15