serial_communication.cpp
Go to the documentation of this file.
00001 
00026 #include "maggie_serial_comm_drivers/serial_communication.h"
00027 
00029 
00030 SerialCommunication::SerialCommunication() :
00031     _file_descriptor(-1)
00032 {
00033 }
00034 
00036 
00037 SerialCommunication::~SerialCommunication()
00038 {
00039 }
00040 
00042 
00043 void SerialCommunication::set_serial_device(std::string serial_device)
00044 {
00045     _serial_device = serial_device;
00046 }
00047 
00049 
00050 int SerialCommunication::open_port(struct termios *oldtio)
00051 {
00052     struct termios newtio;
00053     int error = 0;
00054 
00055     // open the serial port
00056     _file_descriptor = open(_serial_device.c_str(), O_RDWR | O_NOCTTY);
00057 
00058     if (_file_descriptor < 0) {
00059         ROS_ERROR("[EYELIDS] Error: opening serial device %s", _serial_device.c_str());
00060         exit(-1);
00061     }
00062 
00063     // save current port settings
00064     if (tcgetattr(_file_descriptor, oldtio) != 0) {
00065         error = -1;
00066     }
00067 
00068     bzero(&newtio, sizeof(newtio));
00069     newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
00070     newtio.c_cflag &= ~PARENB;
00071     newtio.c_cflag &= ~CSTOPB;
00072     newtio.c_cflag &= ~CSIZE;
00073     newtio.c_cflag |= CS8;
00074     newtio.c_oflag = 0;
00075 
00076     // set input mode (non-canonical, no echo,...)
00077     newtio.c_lflag = 0;
00078 
00079     newtio.c_cc[VTIME] = 0; // inter-character timer unused
00080     newtio.c_cc[VMIN] = 1;  // blocking read until 1 chars received
00081 
00082     if (tcflush(_file_descriptor, TCIFLUSH) != 0) {
00083         error = -1;
00084     }
00085     if (tcsetattr(_file_descriptor, TCSANOW, &newtio) != 0) {
00086         error = -1;
00087     }
00088 
00089     return error;
00090 }
00091 
00093 
00094 int SerialCommunication::close_port(struct termios *oldtio)
00095 {
00096     int error = 0;
00097 
00098     if (tcsetattr(_file_descriptor, TCSANOW, oldtio) != 0) {
00099         error = -1;
00100     }
00101     if (close(_file_descriptor) != 0) {
00102         error = -1;
00103     }
00104 
00105     return error;
00106 }
00107 
00109 
00110 int SerialCommunication::send_character(unsigned char *str)
00111 {
00112     int error = 0;
00113 
00114     if (write(_file_descriptor, str, 1) == -1) {
00115         ROS_ERROR("[EYELIDS] Error: sending character by the serial port");
00116         error = -1;
00117     }
00118     ROS_DEBUG("[EYELIDS] Character sent: %u", *str);
00119 
00120     return error;
00121 }
00122 


maggie_serial_comm_drivers
Author(s): Raul Perula-Martinez
autogenerated on Mon Sep 14 2015 03:06:42