SerialDevice.cc
Go to the documentation of this file.
00001 /*
00002 *  Copyright (c) 2012, Robotnik Automation, SLL
00003 * 
00004 *   This file is part of sick-s3000-ros-pkg.
00005 *
00006 *   sick-s3000-ros-pkg is free software: you can redistribute it and/or modify
00007 *   it under the terms of the GNU General Public License as published by
00008 *   the Free Software Foundation, either version 3 of the License, or
00009 *   (at your option) any later version.
00010 *
00011 *   This program is distributed in the hope that it will be useful,
00012 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *   GNU General Public License for more details.
00015 *
00016 *   You should have received a copy of the GNU General Public License
00017 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 *
00019 */
00030 #include <s3000_laser/SerialDevice.h>
00031 #include <time.h>
00032 #include <string.h> 
00033 #include <fcntl.h>
00034 #include <stdio.h>
00035 #include <unistd.h>  /* UNIX standard function definitions */
00036 #include <sys/stat.h>
00037 #include <termios.h>
00038 #include <sys/ioctl.h>
00039 #include <iostream>
00040 #include <errno.h>   /* Error number definitions */
00041 #include <stropts.h>
00042 #include <ros/ros.h>
00043 
00047 SerialDevice::SerialDevice(const char *device, int baudrate, const char *parity, int datasize)
00048 : device_( device )
00049 , parity_( parity )
00050 , baudrate_( baudrate )
00051 , datasize_( datasize )
00052 {               
00053         ROS_INFO_STREAM( "SerialDevice: " << device 
00054             << " Parity= " << parity 
00055             << " DataSize=" << datasize 
00056             << " BaudRate=" << baudrate );
00057 }
00058 
00062 SerialDevice::~SerialDevice() 
00063 {
00064     ClosePort();
00065 }
00066 
00070 bool SerialDevice::OpenPort()
00071 {
00072         serial_port_ = open(device_.c_str(),  O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
00073         
00074         if( serial_port_ == -1 )
00075         {
00076                 ROS_WARN( "Error opening serial device=%s", device_.c_str() );
00077                 return false;  // invalid device file   
00078         }
00079         
00080         // set up comm flags
00081         struct termios comms_flags;
00082         memset( &comms_flags, 0, sizeof(termios) );
00083         
00084         switch(datasize_)
00085         {
00086                 case 5:
00087                         comms_flags.c_cflag = CS5 | CREAD;
00088                     break;
00089                 case 6:
00090                         comms_flags.c_cflag = CS6 | CREAD;
00091                     break;
00092                 case 7:
00093                         comms_flags.c_cflag = CS7 | CREAD;
00094                     break;
00095                 case 8:
00096                         comms_flags.c_cflag = CS8 | CREAD;
00097                     break;
00098                 default:
00099                         ROS_WARN( "unsupported datasize=%d", datasize_ );
00100                     return false;
00101         }
00102         
00103         comms_flags.c_iflag = INPCK;
00104         comms_flags.c_oflag = 0;
00105         comms_flags.c_lflag = 0;
00106         
00107         if( parity_ == "even" )
00108         {
00109                 comms_flags.c_cflag |= PARENB;
00110         }
00111         else if( parity_ == "odd" )
00112         {
00113                 comms_flags.c_cflag |= PARODD;
00114         }
00115         else if( parity_ == "none" )
00116         {
00117                 comms_flags.c_cflag &= ~PARODD;
00118                 comms_flags.c_cflag &= ~PARENB;
00119         }
00120         
00121         comms_flags.c_lflag &= ~ICANON; // TEST
00122         
00123         tcsetattr( serial_port_, TCSANOW, &comms_flags );
00124         tcflush( serial_port_, TCIOFLUSH );
00125                 
00126         if ( SetTermSpeed(baudrate_) == false )
00127             return false;
00128         
00129         // Make sure queue is empty
00130         tcflush( serial_port_, TCIOFLUSH );
00131         return true;
00132         
00133 }
00134 
00138 bool SerialDevice::ClosePort()
00139 {
00140         return close(serial_port_) == 0;
00141 }
00142 
00152 bool SerialDevice::ReadPort(char *buffer, int bytes_to_read, int &bytes_read ) 
00153 {
00154         bytes_read = read( serial_port_, buffer, bytes_to_read );
00155 
00156     if( bytes_read >= 0 ) 
00157     {
00158         return true;
00159     }
00160     else if ( errno == EAGAIN ) 
00161     {
00162         return true; // nothing to read
00163     }
00164     else
00165     {
00166         ROS_WARN("SERIAL read error %d %s", errno, strerror(errno) );
00167         return false;
00168     }
00169 }
00170 
00176 bool SerialDevice::SetTermSpeed(int baudrate)
00177 {
00178         int baudrate_flag;
00179         
00180         switch(baudrate)
00181         {
00182                 case 9600:
00183                         baudrate_flag = B9600;
00184                         break;
00185                 case 19200:
00186                         baudrate_flag = B19200;
00187                         break;
00188                 case 38400:
00189                         baudrate_flag = B38400;
00190                         break;
00191                 case 115200:
00192                         baudrate_flag = B115200;
00193                         break;
00194                 case 500000:
00195                         baudrate_flag = B500000;
00196                         break;
00197                 default:
00198                     ROS_WARN("unsupported baudrate=%d", baudrate );
00199                         return false;
00200         }
00201         
00202         struct termios comms_flags;
00203 
00204         if( tcgetattr( serial_port_, &comms_flags ) < 0 )
00205                 return false;
00206         
00207         if( cfsetispeed( &comms_flags, baudrate_flag ) < 0 || cfsetospeed( &comms_flags, baudrate_flag ) < 0)
00208                 return false;
00209         
00210         if( tcsetattr( serial_port_, TCSAFLUSH, &comms_flags ) < 0 )
00211                 return false;
00212 
00213         return true;
00214 }
00215 


s3000_laser
Author(s): Roman Navarro Garcia
autogenerated on Sat Jun 8 2019 20:55:59