summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/serial
diff options
context:
space:
mode:
authorschodet2004-02-07 16:55:41 +0000
committerschodet2004-02-07 16:55:41 +0000
commit9fdd6704947174fae292dfc6e14cc1029a7f7a6c (patch)
tree40a4fe775845ececdaf27e82d99d6a06f6b3803e /2004/i/nono/src/serial
parentb673946d76e436c067d24e535b1e0a167b9dfc47 (diff)
Initial revision
Diffstat (limited to '2004/i/nono/src/serial')
-rw-r--r--2004/i/nono/src/serial/Makefile.defs8
-rw-r--r--2004/i/nono/src/serial/serial.cc97
-rw-r--r--2004/i/nono/src/serial/serial.h50
-rw-r--r--2004/i/nono/src/serial/test_serial.cc48
4 files changed, 203 insertions, 0 deletions
diff --git a/2004/i/nono/src/serial/Makefile.defs b/2004/i/nono/src/serial/Makefile.defs
new file mode 100644
index 0000000..ddafe5c
--- /dev/null
+++ b/2004/i/nono/src/serial/Makefile.defs
@@ -0,0 +1,8 @@
+LIBS += serial.a
+TARGETS += test_serial
+test_serial_SOURCES = test_serial.cc serial.a erreur.a
+serial_a_SOURCES = serial.cc
+
+serial.a: ${serial_a_SOURCES:%.cc=serial.a(%.o)}
+
+test_serial: $(test_serial_SOURCES:%.cc=%.o)
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);
+}
+
diff --git a/2004/i/nono/src/serial/serial.h b/2004/i/nono/src/serial/serial.h
new file mode 100644
index 0000000..4c91b55
--- /dev/null
+++ b/2004/i/nono/src/serial/serial.h
@@ -0,0 +1,50 @@
+#ifndef serial_h
+#define serial_h
+// serial.h - 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 <unistd.h>
+#include <termios.h>
+
+class Serial
+{
+ struct termios old_;
+ int fd_;
+ public:
+ // Constructeur.
+ Serial (void);
+ // Destructeur.
+ ~Serial (void);
+ // Ouvre et paramètre le port série.
+ void open (const char *ttyname);
+ // Ferme le port série, appellé dans le destructeur.
+ void close (void);
+ // Lit un caractère, ou -1.
+ int getchar (void);
+ // Ecrit un bloc.
+ ssize_t write (const void *buf, size_t size);
+};
+
+#endif // serial_h
diff --git a/2004/i/nono/src/serial/test_serial.cc b/2004/i/nono/src/serial/test_serial.cc
new file mode 100644
index 0000000..7404997
--- /dev/null
+++ b/2004/i/nono/src/serial/test_serial.cc
@@ -0,0 +1,48 @@
+// test_serial.cc - Programme de test pour le port série.
+// 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 <iostream>
+
+int
+main (void)
+{
+ try
+ {
+ Serial s;
+ s.open ("/dev/tty00");
+ s.write ("!g\r", 3);
+ sleep (1);
+ s.write ("!v0303\r", 7);
+ sleep (5);
+ s.write ("!s\r", 3);
+ }
+ catch (Erreur &e)
+ {
+ cerr << e.what ();
+ return 1;
+ }
+}