From e37750edb0dd75b976c58672b577bb1a4f6079c0 Mon Sep 17 00:00:00 2001 From: schodet Date: Wed, 9 Feb 2005 13:48:35 +0000 Subject: Ajout de serial. --- 2005/i/robert/src/Makefile.defs | 2 +- 2005/i/robert/src/serial/Makefile.defs | 7 ++ 2005/i/robert/src/serial/serial.cc | 66 ++++++++++++ 2005/i/robert/src/serial/serial.hh | 62 ++++++++++++ 2005/i/robert/src/serial/serial_base.cc | 114 +++++++++++++++++++++ 2005/i/robert/src/serial/serial_base.hh | 65 ++++++++++++ 2005/i/robert/src/serial/serial_dev.cc | 169 +++++++++++++++++++++++++++++++ 2005/i/robert/src/serial/serial_dev.hh | 54 ++++++++++ 2005/i/robert/src/serial/serial_stdio.cc | 79 +++++++++++++++ 2005/i/robert/src/serial/serial_stdio.hh | 48 +++++++++ 2005/i/robert/src/serial/test_serial.cc | 88 ++++++++++++++++ 11 files changed, 753 insertions(+), 1 deletion(-) create mode 100644 2005/i/robert/src/serial/Makefile.defs create mode 100644 2005/i/robert/src/serial/serial.cc create mode 100644 2005/i/robert/src/serial/serial.hh create mode 100644 2005/i/robert/src/serial/serial_base.cc create mode 100644 2005/i/robert/src/serial/serial_base.hh create mode 100644 2005/i/robert/src/serial/serial_dev.cc create mode 100644 2005/i/robert/src/serial/serial_dev.hh create mode 100644 2005/i/robert/src/serial/serial_stdio.cc create mode 100644 2005/i/robert/src/serial/serial_stdio.hh create mode 100644 2005/i/robert/src/serial/test_serial.cc (limited to '2005') diff --git a/2005/i/robert/src/Makefile.defs b/2005/i/robert/src/Makefile.defs index 6b69819..99acf43 100644 --- a/2005/i/robert/src/Makefile.defs +++ b/2005/i/robert/src/Makefile.defs @@ -2,7 +2,7 @@ CXXFLAGS = -O2 -Wall -g CPPFLAGS = -MMD $(INCLUDES) INCLUDES = -I$(SRCDIR) -SUBDIRS = log scheduler utils utils/meta +SUBDIRS = log scheduler serial utils utils/meta LINK.o = $(CXX) $(LDFLAGS) $(TARGET_ARCH) VPATH = $(SUBDIRS:%=$(SRCDIR)/%) diff --git a/2005/i/robert/src/serial/Makefile.defs b/2005/i/robert/src/serial/Makefile.defs new file mode 100644 index 0000000..fef8679 --- /dev/null +++ b/2005/i/robert/src/serial/Makefile.defs @@ -0,0 +1,7 @@ +PROGRAMS += test_serial + +serial_OBJECTS = serial.o serial_base.o serial_dev.o serial_stdio.o + +test_serial_OBJECTS = test_serial.o $(serial_OBJECTS) + +test_serial: $(test_serial_OBJECTS) diff --git a/2005/i/robert/src/serial/serial.cc b/2005/i/robert/src/serial/serial.cc new file mode 100644 index 0000000..db2a6d2 --- /dev/null +++ b/2005/i/robert/src/serial/serial.cc @@ -0,0 +1,66 @@ +// serial.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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.hh" +#include "serial_dev.hh" +#include "serial_stdio.hh" + +/// Constructeur. +Serial::Serial (bool blocking) + : blocking_ (blocking), sb_ (0) +{ +} + +/// Destructeur. +Serial::~Serial (void) +{ + close (); +} + +/// Ouvre et paramètre le port série. \a ttyname peut être un device +/// ("/dev/tty00,115200", voir SerialDev), un programme précédé d'un tube +/// ("|simul_asserv", voir SerialPipe) ou l'entrée/sortie standard ("-"). +void +Serial::open (const std::string &ttyname) +{ + close (); + if (ttyname == "-") + sb_ = new SerialStdio (blocking_); + else if (ttyname[0] == '|') + { + // TODO + } + else + sb_ = new SerialDev (blocking_); + sb_->open (ttyname); +} + +/// Ferme le port série, appellé automatiquement dans le destructeur. +void +Serial::close (void) +{ + delete sb_; + sb_ = 0; +} + diff --git a/2005/i/robert/src/serial/serial.hh b/2005/i/robert/src/serial/serial.hh new file mode 100644 index 0000000..71fdd26 --- /dev/null +++ b/2005/i/robert/src/serial/serial.hh @@ -0,0 +1,62 @@ +#ifndef serial_hh +#define serial_hh +// serial.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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_base.hh" + +/// Classe générique qui utilise la sous-classe apropriée de SerialBase en +/// fonction du paramètre de Serial::open(). +class Serial +{ + bool blocking_; + SerialBase *sb_; + public: + /// Constructeur. + Serial (bool blocking = false); + /// Destructeur. + ~Serial (void); + /// Ouvre et paramètre le port série. \a ttyname peut être un device + /// ("/dev/tty00,115200", voir SerialDev), un programme précédé d'un tube + /// ("|simul_asserv", voir SerialPipe) ou l'entrée/sortie standard ("-"). + void open (const std::string &ttyname); + /// Ferme le port série, appellé automatiquement dans le destructeur. + void close (void); + /// Lit un bloc, retourne le nombre d'octets lus ou <= 0 en cas d'erreur + /// ou si aucun caractère n'est disponible. Pour le robot, préferer en + /// général getchar() pour du non-bloquant. + ssize_t read (void *buf, size_t size) { return sb_->read (buf, size); } + /// Lit un caractère, ou -1 en cas d'erreur ou si aucun caractère n'est + /// disponible. + int getchar (void) { return sb_->getchar (); } + /// Ecrit un bloc. + void write (const void *buf, size_t size) { sb_->write (buf, size); } + /// Ecrit un caractère. + void putchar (int c) { sb_->putchar (c); } + /// Attend que des caractères soient disponibles pendant un delay en + /// millisecondes. + bool wait (int timeout = -1) { return sb_->wait (timeout); } +}; + +#endif // serial_hh diff --git a/2005/i/robert/src/serial/serial_base.cc b/2005/i/robert/src/serial/serial_base.cc new file mode 100644 index 0000000..e2fe250 --- /dev/null +++ b/2005/i/robert/src/serial/serial_base.cc @@ -0,0 +1,114 @@ +// serial_base.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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_base.hh" +#include "utils/errno_exception.hh" + +#include +#include + +/// Constructeur. +SerialBase::SerialBase (void) + : fdIn_ (-1), fdOut_ (-1) +{ +} + +/// Destructeur. +SerialBase::~SerialBase (void) +{ +} + +/// Lit un bloc, retourne le nombre d'octets lus ou <= 0 en cas d'erreur +/// ou si aucun caractère n'est disponible. Pour le robot, préferer en +/// général getchar() pour du non-bloquant. +ssize_t +SerialBase::read (void *buf, size_t size) +{ + return ::read (fdIn_, buf, size); +} + +/// Lit un caractère, ou -1 en cas d'erreur ou si aucun caractère n'est +/// disponible. +int +SerialBase::getchar (void) +{ + char c; + if (read (&c, 1) == 1) + return c & 0xff; + else + return -1; +} + +/// Ecrit un bloc. +void +SerialBase::write (const void *buf, size_t size) +{ + ::write (fdOut_, buf, size); +} + +/// Ecrit un caractère. +void +SerialBase::putchar (int c) +{ + char cc = c; + write (&cc, 1); +} + +/// Attend que des caractères soient disponibles pendant un delay en +/// millisecondes. +bool +SerialBase::wait (int timeout/*-1*/) +{ + fd_set fds; + int r; + // Setup fd_set. + FD_ZERO (&fds); + FD_SET (fdIn_, &fds); + if (timeout == -1) + { + // Without timeout. + r = select (FD_SETSIZE, &fds, 0, 0, 0); + } + else + { + // With timeout. + struct timeval tv; + tv.tv_sec = timeout / 1000; + tv.tv_usec = (timeout % 1000) * 1000; + r = select (FD_SETSIZE, &fds, 0, 0, &tv); + } + switch (r) + { + case -1: + // Error. + throw errno_exception (errno); + case 0: + // Timeout. + return false; + default: + // Data availlable. + return true; + } +} + diff --git a/2005/i/robert/src/serial/serial_base.hh b/2005/i/robert/src/serial/serial_base.hh new file mode 100644 index 0000000..18c604b --- /dev/null +++ b/2005/i/robert/src/serial/serial_base.hh @@ -0,0 +1,65 @@ +#ifndef serial_base_hh +#define serial_base_hh +// serial_base.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 +#include + +/// Classe de base pour un port série. +class SerialBase +{ + protected: + /// Descripteur de fichier utilisé par getchar et write. + int fdIn_; + int fdOut_; + public: + /// Constructeur. + SerialBase (void); + /// Destructeur. + virtual ~SerialBase (void); + /// Ouvre et paramètre le port série. \a ttyname peut être un device + /// ("/dev/tty00,115200", voir SerialDev), un programme précédé d'un tube + /// ("|simul_asserv", voir SerialPipe) ou l'entrée/sortie standard ("-"). + virtual void open (const std::string &ttyname) = 0; + /// Ferme le port série, appellé automatiquement dans le destructeur. + virtual void close (void) = 0; + /// Lit un bloc, retourne le nombre d'octets lus ou <= 0 en cas d'erreur + /// ou si aucun caractère n'est disponible. Pour le robot, préferer en + /// général getchar() pour du non-bloquant. + ssize_t read (void *buf, size_t size); + /// Lit un caractère, ou -1 en cas d'erreur ou si aucun caractère n'est + /// disponible. + int getchar (void); + /// Ecrit un bloc. + void write (const void *buf, size_t size); + /// Ecrit un caractère. + void putchar (int c); + /// Attend que des caractères soient disponibles pendant un delay en + /// millisecondes. + bool wait (int timeout = -1); +}; + +#endif // serial_base_hh diff --git a/2005/i/robert/src/serial/serial_dev.cc b/2005/i/robert/src/serial/serial_dev.cc new file mode 100644 index 0000000..e445ef1 --- /dev/null +++ b/2005/i/robert/src/serial/serial_dev.cc @@ -0,0 +1,169 @@ +// serial_dev.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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.hh" +#include "utils/errno_exception.hh" + +#include +#include +#include + +/// Constructeur. +SerialDev::SerialDev (bool blocking/*false*/) + : blocking_ (blocking) +{ +} + +/// Destructeur. +SerialDev::~SerialDev (void) +{ + close (); +} + +/// Ouvre et paramètre le port série, accepte plusieurs paramètres séparés par +/// des virgules : tty, vitesse, parité (none, odd, even). Par exemple : +/// "/dev/ttyS00,115200,even" (par defaut). +void +SerialDev::open (const std::string &ttyname) +{ + struct termios tios; + std::string tty; + int speed = 115200; + char par = 'e'; + // Décode le ttyname. + decode (ttyname, tty, speed, par); + // Ouvre le port série. + fdIn_ = ::open (tty.c_str (), O_RDWR | O_NOCTTY | O_NONBLOCK); + if (fdIn_ < 0) + throw errno_exception (ttyname, errno); + // Rendre le port série asynchrone. + //fcntl (fd_, F_SETFL, FASYNC); + // Sauver la configuration courante du port série. + tcgetattr (fdIn_, &old_); + tcgetattr (fdIn_, &tios); + // Paramètrer les options du port série. + // Vérification de la parité en entrée, remplace toute erreur de + // transmission par un caractère nul. Pas de Xon Xoff. Entrée crue. + tios.c_iflag &= ~(IGNPAR | PARMRK | ISTRIP | IGNBRK | BRKINT | IGNCR | + ICRNL | INLCR | IXON | IXOFF | IXANY | IMAXBEL); + tios.c_iflag |= INPCK; + // Sortie crue. + tios.c_oflag &= ~(OPOST); + // Pas de status line, 1 bit de stop, 8 bits de données, règle la parité. + tios.c_cflag &= ~(HUPCL | CSTOPB | PARENB | PARODD | CSIZE); + tios.c_cflag |= CS8 | CLOCAL | CREAD; + if (par != 'n') tios.c_cflag |= PARENB; + if (par == 'o') tios.c_cflag |= PARODD; + // Cru. + tios.c_lflag &= ~(ICANON | ECHO | ISIG | NOFLSH | TOSTOP); + // 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 0: + case 9600: + cfsetspeed (&tios, B9600); + break; + case 19200: + cfsetspeed (&tios, B19200); + break; + case 38400: + cfsetspeed (&tios, B38400); + break; + case 57600: + cfsetspeed (&tios, B57600); + break; + case 115200: + cfsetspeed (&tios, B115200); + break; + case 230400: + cfsetspeed (&tios, B230400); + break; + case 460800: + cfsetspeed (&tios, B460800); + break; + default: + throw std::runtime_error ("Vitesse de port série non supportée\n"); + } + // Vider et configurer le port série. + tcflush (fdIn_, TCIFLUSH); + tcsetattr (fdIn_, TCSANOW, &tios); + fdOut_ = fdIn_; +} + +/// Ferme le port série, appellé automatiquement dans le destructeur de la +/// classe SerialBase. +void +SerialDev::close (void) +{ + if (fdIn_ != -1) + { + tcsetattr (fdIn_, TCSANOW, &old_); + ::close (fdIn_); + fdIn_ = -1; + fdOut_ = -1; + } +} + +/// Décode le ttyname, voir open. +void +SerialDev::decode (const std::string &ttyname, std::string &tty, int &speed, + char &par) const +{ + std::string::size_type a, b, d; + int i; + a = 0; + b = ttyname.find (','); + while (a != std::string::npos) + { + d = b == std::string::npos ? std::string::npos : b - a; + std::string s (ttyname, a, d); + i = atoi (s.c_str ()); + if (i) + speed = i; + else + { + if (s == "e" || s == "even") + par = 'e'; + else if (s == "o" || s == "odd") + par = 'o'; + else if (s == "n" || s == "none") + par = 'n'; + else if (tty.empty ()) + tty = s; + else + throw std::runtime_error ("error in ttyname"); + } + a = b + 1; + b = ttyname.find (',', a); + } +} + diff --git a/2005/i/robert/src/serial/serial_dev.hh b/2005/i/robert/src/serial/serial_dev.hh new file mode 100644 index 0000000..31e98b6 --- /dev/null +++ b/2005/i/robert/src/serial/serial_dev.hh @@ -0,0 +1,54 @@ +#ifndef serial_dev_hh +#define serial_dev_hh +// serial_dev.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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_base.hh" + +#include + +/// Classe de contrôle du port serie /dev/tty*. +class SerialDev : public SerialBase +{ + bool blocking_; + struct termios old_; + public: + /// Constructeur. + SerialDev (bool blocking = false); + /// Destructeur. + ~SerialDev (void); + /// Ouvre et paramètre le port série, accepte plusieurs paramètres séparés + /// par des virgules : tty, vitesse, parité (none, odd, even). Par + /// exemple : "/dev/ttyS00,115200,even" (par defaut). + void open (const std::string &ttyname); + /// Ferme le port série, appellé automatiquement dans le destructeur de la + /// classe SerialBase. + void close (void); + private: + /// Décode le ttyname, voir open. + void decode (const std::string &ttyname, std::string &tty, int &speed, + char &par) const; +}; + +#endif // serial_dev_hh diff --git a/2005/i/robert/src/serial/serial_stdio.cc b/2005/i/robert/src/serial/serial_stdio.cc new file mode 100644 index 0000000..738223f --- /dev/null +++ b/2005/i/robert/src/serial/serial_stdio.cc @@ -0,0 +1,79 @@ +// serial_stdio.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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_stdio.hh" +#include "utils/errno_exception.hh" + +#include + +/// Constructeur. +SerialStdio::SerialStdio (bool blocking/*false*/) + : blocking_ (blocking) +{ +} + +/// Destructeur. +SerialStdio::~SerialStdio (void) +{ + close (); +} + +/// Ouvre et paramètre le port série. +void +SerialStdio::open (const std::string &ttyname) +{ + struct termios tios; + fdIn_ = 0; + fdOut_ = 1; + // Sauver la configuration courante du port série. + tcgetattr (fdIn_, &old_); + // Paramètrer les options du port série. + tios.c_cflag = CS8 | CLOCAL | CREAD; + 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; + // Vider et configurer le port série. + tcflush (fdIn_, TCIFLUSH); + tcsetattr (fdIn_, TCSANOW, &tios); +} + +/// Ferme le port série, appellé automatiquement dans le destructeur de la +/// classe SerialBase. +void +SerialStdio::close (void) +{ + if (fdIn_ != -1) + { + tcsetattr (fdIn_, TCSANOW, &old_); + fdIn_ = -1; + fdOut_ = -1; + } +} + diff --git a/2005/i/robert/src/serial/serial_stdio.hh b/2005/i/robert/src/serial/serial_stdio.hh new file mode 100644 index 0000000..ea10eff --- /dev/null +++ b/2005/i/robert/src/serial/serial_stdio.hh @@ -0,0 +1,48 @@ +#ifndef serial_stdio_hh +#define serial_stdio_hh +// serial_stdio.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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_base.hh" + +#include + +/// Classe de contrôle du port serie factice sur l'entrée/sortie standart. +class SerialStdio : public SerialBase +{ + bool blocking_; + struct termios old_; + public: + /// Constructeur. + SerialStdio (bool blocking = false); + /// Destructeur. + ~SerialStdio (void); + /// Ouvre et paramètre le port série. + void open (const std::string &ttyname); + /// Ferme le port série, appellé automatiquement dans le destructeur de la + /// classe SerialBase. + void close (void); +}; + +#endif // serial_stdio_hh diff --git a/2005/i/robert/src/serial/test_serial.cc b/2005/i/robert/src/serial/test_serial.cc new file mode 100644 index 0000000..cce45d5 --- /dev/null +++ b/2005/i/robert/src/serial/test_serial.cc @@ -0,0 +1,88 @@ +// test_serial.cc - Programme de test pour le port série. +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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.hh" +//#include "date/date.hh" + +#include + +void +syntax (void) +{ + std::cout << "test_serial ttyname commandes... - Test la classe Serial.\n" + " a carte d'asservissement.\n" + " e échèle de 0 à 255 puis 0...\n" + " envois une valeur." << std::endl; +} +int +main (int argc, char **argv) +{ + try + { + if (argc < 3) + { + syntax (); + return 1; + } + Serial s; + s.open (argv[1]); + switch (argv[2][0]) + { + case 'a': + s.write ("!g1\r", 3); + //Date::getInstance ().wait (1000); + s.write ("!v0303\r", 7); + //Date::getInstance ().wait (5000); + s.write ("!s\r", 3); + break; + case 'e': + while (1) + { + for (int i = 0; i < 256; ++i) + { + std::cout << i << std::endl; + s.putchar (i); + //Date::getInstance ().wait (100); + } + for (int i = 255; i >= 0; --i) + { + std::cout << i << std::endl; + s.putchar (i); + //Date::getInstance ().wait (100); + } + } + break; + default: + int i = atoi (argv[2]); + std::cout << i << std::endl; + s.putchar (i); + break; + } + } + catch (const std::exception &e) + { + cerr << e.what () << std::endl; + return 1; + } +} -- cgit v1.2.3