summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/serial/serial.cc
diff options
context:
space:
mode:
Diffstat (limited to '2004/i/nono/src/serial/serial.cc')
-rw-r--r--2004/i/nono/src/serial/serial.cc97
1 files changed, 97 insertions, 0 deletions
diff --git a/2004/i/nono/src/serial/serial.cc b/2004/i/nono/src/serial/serial.cc
new file mode 100644
index 0000000..ab047ce
--- /dev/null
+++ b/2004/i/nono/src/serial/serial.cc
@@ -0,0 +1,97 @@
+// serial.cc - Contrôle du port serie.
+// 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.h"
+#include "erreur/erreur.h"
+
+#include <fcntl.h>
+
+// Constructeur.
+Serial::Serial (void)
+ : fd_ (-1)
+{
+}
+
+// Destructeur.
+Serial::~Serial (void)
+{
+ close ();
+}
+
+// Ouvre et paramètre le port série.
+void
+Serial::open (const char *ttyname)
+{
+ 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.
+ tios.c_cc[VMIN] = 0;
+ tios.c_cc[VTIME] = 0;
+ //cfsetspeed (&tios, B115200);
+ //cfsetspeed (&tios, B9600);
+ cfsetspeed (&tios, B19200);
+ // Vider et configuer le port série.
+ tcflush (fd_, TCIFLUSH);
+ tcsetattr (fd_, TCSANOW, &tios);
+}
+
+// Ferme le port série, appellé dans le destructeur.
+void
+Serial::close (void)
+{
+ tcsetattr (fd_, TCSANOW, &old_);
+ ::close (fd_);
+ fd_ = -1;
+}
+
+// Lit un caractère, ou -1.
+int
+Serial::getchar (void)
+{
+ char c;
+ if (read (fd_, &c, 1) == 1)
+ return c;
+ else
+ return -1;
+}
+
+// Ecrit un bloc.
+ssize_t
+Serial::write (const void *buf, size_t size)
+{
+ return ::write (fd_, buf, size);
+}
+