// serial_dev.cc // nono - programme du robot 2004. {{{ // // Copyright (C) 2004 Nicolas Schodet // // Robot APB Team/Efrei 2004. // Web: http://assos.efrei.fr/robot/ // Email: robot AT efrei DOT fr // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // }}} #include "serial_dev.h" #include "erreur/erreur.h" #include /// Constructeur. SerialDev::SerialDev (bool blocking/*false*/) : blocking_ (blocking) { } /// Destructeur. SerialDev::~SerialDev (void) { close (); } /// Ouvre et paramètre le port série. void SerialDev::open (const char *ttyname, int speed/*0*/) { struct termios tios; // Ouvre le port série. fd_ = ::open (ttyname, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd_ < 0) throw ErreurFatale ("Erreur d'ouverture du port série.\n"); // Rendre le port série asynchrone. //fcntl (fd_, F_SETFL, FASYNC); // Sauver la configuration courante du port série. tcgetattr (fd_, &old_); // Paramètrer les options du port série. tios.c_cflag = CS8 | CLOCAL | CREAD /*| BAUDRATE*/; tios.c_iflag = IGNBRK | IGNPAR; tios.c_oflag = 0; tios.c_lflag = 0; // On n'attend jamais si on est en non-block. On attend sinon. tios.c_cc[VTIME] = 0; if (blocking_) tios.c_cc[VMIN] = 1; else tios.c_cc[VMIN] = 0; switch (speed) { case 1200: cfsetspeed (&tios, B1200); break; case 9660: cfsetspeed (&tios, B9600); break; case 0: case 19200: cfsetspeed (&tios, B19200); break; case 38400: cfsetspeed (&tios, B38400); break; case 57600: cfsetspeed (&tios, B57600); break; case 115200: cfsetspeed (&tios, B115200); break; default: throw ErreurFatale ("Vitesse de port série non supportée\n"); } // Vider et configurer le port série. tcflush (fd_, TCIFLUSH); tcsetattr (fd_, TCSANOW, &tios); } /// Ferme le port série, appellé automatiquement dans le destructeur de la /// classe SerialBase. void SerialDev::close (void) { if (fd_ != -1) { tcsetattr (fd_, TCSANOW, &old_); ::close (fd_); fd_ = -1; } }