serialIO.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2010
00004  *
00005  * Fraunhofer Institute for Manufacturing Engineering   
00006  * and Automation (IPA)
00007  *
00008  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009  *
00010  * Project name: care-o-bot
00011  * ROS stack name: cob_driver
00012  * ROS package name: cob_light
00013  * Description: Switch robots led color by sending data to
00014  * the led-µC over serial connection.
00015  *                                                              
00016  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00017  *                      
00018  * Author: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de
00019  * Supervised by: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de
00020  *
00021  * Date of creation: August 2012
00022  * ToDo:
00023  *
00024  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00025  *
00026  * Redistribution and use in source and binary forms, with or without
00027  * modification, are permitted provided that the following conditions are met:
00028  *
00029  *     * Redistributions of source code must retain the above copyright
00030  *       notice, this list of conditions and the following disclaimer.
00031  *     * Redistributions in binary form must reproduce the above copyright
00032  *       notice, this list of conditions and the following disclaimer in the
00033  *       documentation and/or other materials provided with the distribution.
00034  *     * Neither the name of the Fraunhofer Institute for Manufacturing 
00035  *       Engineering and Automation (IPA) nor the names of its
00036  *       contributors may be used to endorse or promote products derived from
00037  *       this software without specific prior written permission.
00038  *
00039  * This program is free software: you can redistribute it and/or modify
00040  * it under the terms of the GNU Lesser General Public License LGPL as 
00041  * published by the Free Software Foundation, either version 3 of the 
00042  * License, or (at your option) any later version.
00043  * 
00044  * This program is distributed in the hope that it will be useful,
00045  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00046  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00047  * GNU Lesser General Public License LGPL for more details.
00048  * 
00049  * You should have received a copy of the GNU Lesser General Public 
00050  * License LGPL along with this program. 
00051  * If not, see <http://www.gnu.org/licenses/>.
00052  *
00053  ****************************************************************/
00054 
00055 #include "serialIO.h"
00056 
00057 SerialIO::SerialIO() :
00058          _fd(-1)
00059 {
00060 }
00061 
00062 SerialIO::~SerialIO()
00063 {
00064         closePort();
00065 }
00066 
00067 // Open Serial Port
00068 int SerialIO::openPort(std::string devicestring, int baudrate)
00069 {
00070         if(_fd != -1) return _fd;
00071 
00072         _fd = open(devicestring.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
00073         if(_fd != -1)
00074         {
00075                 speed_t baud = getBaudFromInt(baudrate);
00076                 fcntl(_fd, F_SETFL, 0);
00077                 tcgetattr(_fd, &port_settings);
00078                 port_settings.c_cflag &= ~PARENB;
00079                 port_settings.c_cflag &= ~CSTOPB;
00080                 port_settings.c_cflag &= ~CSIZE;
00081                 port_settings.c_cflag = baud | CS8 | CLOCAL | CREAD;
00082                 port_settings.c_iflag = IGNPAR;
00083                 tcsetattr(_fd, TCSANOW, &port_settings);
00084         }
00085         return _fd;
00086 }
00087 
00088 // Send Data to Serial Port
00089 int SerialIO::sendData(std::string value)
00090 {
00091         int wrote = -1;
00092         if(_fd != -1)
00093                 wrote = write(_fd, value.c_str(), value.length());
00094         return wrote;
00095 }
00096 
00097 // Read Data from Serial Port
00098 int SerialIO::readData(std::string &value, size_t nBytes)
00099 {
00100         char buffer[32];
00101         size_t rec = -1;
00102         rec = read(_fd, buffer, nBytes);
00103         value = std::string(buffer);
00104         return rec;
00105 }
00106 
00107 // Check if Serial Port is opened
00108 bool SerialIO::isOpen()
00109 {
00110         return (_fd != -1);
00111 }
00112 
00113 // Close Serial Port
00114 void SerialIO::closePort()
00115 {
00116         if(_fd != -1)
00117                 close(_fd);
00118 }
00119 
00120 
00121 speed_t SerialIO::getBaudFromInt(int baud)
00122 {
00123         speed_t ret;
00124         switch(baud)
00125         {
00126                 case 0:         ret=B0;         break;
00127                 case 50:        ret=B50;        break;
00128                 case 75:        ret=B75;        break;
00129                 case 110:       ret=B110;       break;
00130                 case 134:       ret=B134;       break;
00131                 case 150:       ret=B150;       break;
00132                 case 200:       ret=B200;       break;
00133                 case 300:       ret=B300;       break;
00134                 case 1200:      ret=B1200;      break;
00135                 case 1800:      ret=B1800;      break;
00136                 case 2400:      ret=B2400;      break;
00137                 case 4800:      ret=B4800;      break;
00138                 case 9600:      ret=B9600;      break;
00139                 case 19200: ret=B19200; break;
00140                 case 38400: ret=B38400; break;
00141                 case 57600: ret=B57600; break;
00142                 case 115200: ret=B115200; break;
00143                 case 230400: ret=B230400; break;
00144                 default:
00145                         ret=B230400;
00146                         break;
00147         }
00148         return ret;
00149 }


cob_light
Author(s): Benjamin Maidel
autogenerated on Sun Oct 5 2014 23:09:07