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

Revision history [back]

click to hide/show revision 1
initial version

In order to use qexserialport in ros I suggest you this cmake.This program writes "h" on the port an receives some data from micro. Cmake

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 the 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;
}