LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiSerial.h

00001 /*
00002  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiSerial.h
00027  * authors ....: Guy Wafo
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 11.4.2001
00030  * revisions ..: $Id: ltiSerial.h,v 1.13 2006/02/07 20:48:50 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_SERIAL_H_
00034 #define _LTI_SERIAL_H_
00035 
00036 #include "ltiObject.h"
00037 #include "ltiIoObject.h"
00038 #include "ltiIoHandler.h"
00039 #include "ltiTypes.h"
00040 #include "ltiVector.h"
00041 
00042 #ifdef _LTI_MSC_VER
00043 // headers required in visual (or perhaps windows?)
00044 #include <windows.h>
00045 #include <winbase.h>
00046 
00047 #else
00048 
00049 // for posix systems
00050 #include <unistd.h>
00051 #include <fcntl.h>
00052 #include <errno.h>
00053 #include <termios.h>
00054 #include <cstdio>
00055 
00056 #endif
00057 
00058 #include <cstdlib>
00059 #include <string>
00060 
00061 namespace lti {
00062   /**
00063    * This serial class provides an unified interface to access serial
00064    * ports in posix and windows operating systems.  At this time only
00065    * Linux and Windows are supported.
00066    *
00067    * You just need to set the parameters if you do not want the default ones,
00068    * and send or receive data with the send() and receive() methods.
00069    *
00070    * If you use a Linux system, do not forget to make the users who
00071    * needs access to the serial ports to be members of the group
00072    * <code>uucp</code> or the corresponding group of the
00073    * <code>/dev/ttyS*</code> devices.  */
00074   class serial : public object {
00075   public:
00076     /**
00077      * the parameters for the class serial
00078      */
00079     class parameters : public ioObject {
00080     public:
00081       /**
00082        * default constructor
00083        */
00084       parameters();
00085 
00086       /**
00087        * copy constructor
00088        * @param other the parameters object to be copied
00089        */
00090       parameters(const parameters& other);
00091 
00092       /**
00093        * destructor
00094        */
00095       virtual ~parameters();
00096 
00097       /**
00098        * returns name of this type
00099        */
00100       const char* getTypeName() const;
00101 
00102       /**
00103        * copy the contents of a parameters object
00104        * @param other the parameters object to be copied
00105        * @return a reference to this parameters object
00106        */
00107       parameters& copy(const parameters& other);
00108 
00109       /**
00110        * copy the contents of a parameters object
00111        * @param other the parameters object to be copied
00112        * @return a reference to this parameters object
00113        */
00114       parameters& operator=(const parameters& other);
00115 
00116 
00117       /**
00118        * returns a pointer to a clone of the parameters
00119        */
00120       virtual parameters* clone() const;
00121 
00122       /**
00123        * write the parameters in the given ioHandler
00124        * @param handler the ioHandler to be used
00125        * @param complete if true (the default) the enclosing begin/end will
00126        *        be also written, otherwise only the data block will be written.
00127        * @return true if write was successful
00128        */
00129       virtual bool write(ioHandler& handler,const bool& complete=true) const;
00130 
00131       /**
00132        * write the parameters in the given ioHandler
00133        * @param handler the ioHandler to be used
00134        * @param complete if true (the default) the enclosing begin/end will
00135        *        be also written, otherwise only the data block will be written.
00136        * @return true if write was successful
00137        */
00138       virtual bool read(ioHandler& handler,const bool& complete=true);
00139 
00140 #     ifdef _LTI_MSC_6
00141       /**
00142        * this function is required by MSVC only, as a workaround for a
00143        * very awful bug, which exists since MSVC V.4.0, and still by
00144        * V.6.0 with all bugfixes (so called "service packs") remains
00145        * there...  This method is also public due to another bug, so please
00146        * NEVER EVER call this method directly: use read() instead
00147        */
00148       bool readMS(ioHandler& handler,const bool& complete=true);
00149 
00150       /**
00151        * this function is required by MSVC only, as a workaround for a
00152        * very awful bug, which exists since MSVC V.4.0, and still by
00153        * V.6.0 with all bugfixes (so called "service packs") remains
00154        * there...  This method is also public due to another bug, so please
00155        * NEVER EVER call this method directly: use write() instead
00156        */
00157       bool writeMS(ioHandler& handler,const bool& complete=true) const;
00158 #     endif
00159 
00160       // ------------------------------------------------
00161       // the parameters
00162       // ------------------------------------------------
00163 
00164       /**
00165        * Type to specificy the port to be used
00166        */
00167       enum ePortType {
00168         Com1=0, /**< first port usually called COM1 in Windows or
00169                      /dev/ttyS0 in Linux */
00170         Com2,   /**< first port usually called COM2 in Windows or
00171                      /dev/ttyS1 in Linux */
00172         Com3,   /**< first port usually called COM3 in Windows or
00173                      /dev/ttyS2 in Linux */
00174         Com4,   /**< first port usually called COM4 in Windows or
00175                      /dev/ttyS3 in Linux */
00176         Com5,   /**< first port usually called COM5 in Windows or
00177                      /dev/ttyS4 in Linux */
00178         Com6,   /**< first port usually called COM6 in Windows or
00179                      /dev/ttyS5 in Linux */
00180         Com7,   /**< first port usually called COM7 in Windows or
00181                      /dev/ttyS6 in Linux */
00182         Com8,   /**< first port usually called COM8 in Windows or
00183                      /dev/ttyS7 in Linux */
00184         USB0,   /**< first USB port */
00185         
00186         USB1    /**< second USB port */
00187 
00188       };
00189 
00190       /**
00191        * Type to specify the baud rate.
00192        *
00193        * Baud rate is a measure of the number of times per second a
00194        * signal in a communications channel varies, or makes a
00195        * transition between states (states being frequencies, voltage
00196        * levels, or phase angles). One baud is one such change. Thus,
00197        * a 1200-baud modem's signal changes state 1200 times each
00198        * second.  The number of bits transmitted in a second (baudrate
00199        * in bits-per-second (bps) ) depends on the modulation
00200        * technique being used.  A 2400 baud modem can transmit 9400
00201        * bps if there are 4 bits/baud.
00202        */
00203       enum eBaudRateType {
00204         Baud0,      /**<     0 baud */
00205         Baud300,    /**<   300 baud */
00206         Baud600,    /**<   600 baud */
00207         Baud1200,   /**<  1200 baud */
00208         Baud1800,   /**<  1800 baud */
00209         Baud2400,   /**<  2400 baud */
00210         Baud4800,   /**<  4800 baud */
00211         Baud9600,   /**<  9600 baud */
00212         Baud19200,  /**< 19200 baud */
00213         Baud38400,  /**< 38400 baud */
00214         Baud57600,  /**< 57600 baud */
00215         Baud76800,  /**< 76800 baud */
00216         Baud115200  /**<115200 baud */
00217       };
00218 
00219       /**
00220        * Type to specify the number of bits of a character.
00221        * The first element Cs4 means for example 4 bits/character.
00222        */
00223       enum eCharBitSizeType {
00224         Cs4=4, /**< 4 bits per character */
00225         Cs5,   /**< 5 bits per character */
00226         Cs6,   /**< 6 bits per character */
00227         Cs7,   /**< 7 bits per character */
00228         Cs8    /**< 8 bits per character */
00229       };
00230 
00231       /**
00232        * definiton of the stop bit
00233        * This is also called the remaining bits.  Stop bits serve to
00234        * synchronise the receiving computer to the incoming characters.
00235        */
00236       enum eStopBitType {
00237         One,     /**< one stop bit             */
00238         OneFive, /**< one and a half stop bits */
00239         Two      /**< two stop bits            */
00240       };
00241 
00242       /**
00243        * definition of the parity bit
00244        * the optional parity bit is a simple checksum of the data bits
00245        * indicating whether or not the data contains an even or odd
00246        * number of "1" bits.
00247        */
00248       enum eParityType {
00249         No,    /**< no parity   */
00250         Even,  /**< even parity */
00251         Odd,   /**< odd parity  */
00252         Space, /**< space       */
00253         Mark   /**< mark        */
00254       };
00255 
00256       /**
00257        * Baud rate.
00258        * Default value: Baud9600
00259        */
00260       eBaudRateType baudRate;
00261 
00262       /**
00263        * Which serial port.
00264        * Default value: Com1
00265        */
00266       ePortType port;
00267 
00268       /**
00269        * Character size.
00270        * Default value: Cs8
00271        */
00272       eCharBitSizeType characterSize;
00273 
00274       /**
00275        * Parity.
00276        * Default value: No
00277        */
00278       eParityType parity;
00279 
00280       /**
00281        * Stop bits
00282        * Default: One
00283        */
00284       eStopBitType stopBits;
00285 
00286       /**
00287        * Receive timeout in milliseconds.
00288        *
00289        * If you specify zero, then no timeout will exist.  The receive method
00290        * will check if there is something in the buffer.  If so, then it
00291        * will normally return (reading the first character in the serial FIFO).
00292        * If there is nothing in the buffer, the receive() method will return
00293        * inmediatly a "false" value.
00294        *
00295        * You should consider that for Linux systems the expected timeout
00296        * precision is in deciseconds.  The given value will be rounded
00297        * to the best decisecond precision.  For example 1 ms will be rounded
00298        * to 0 ms, 80ms will be rounded to 0.1s.
00299        *
00300        * Windows OS accepts the ms value.
00301        *
00302        * Default value: 5000 (i.e. 5 seconds)
00303        */
00304       int receiveTimeout;
00305     };
00306 
00307 
00308     /**
00309      * default constructor
00310      */
00311     serial();
00312 
00313     /**
00314      * construct a serial instance with the given parameters
00315      */
00316     serial(const parameters& serialPar);
00317 
00318     /**
00319      * copy constructor
00320      * @param other the object to be copied
00321      */
00322     serial(const serial& other);
00323 
00324     /**
00325      * destructor
00326      */
00327     virtual ~serial();
00328 
00329     /**
00330      * returns the name of this type ("serial")
00331      */
00332     virtual const char* getTypeName() const;
00333 
00334     /**
00335      * copy data of "other" functor.
00336      * @param other the functor to be copied
00337      * @return a reference to this functor object
00338      */
00339     serial& copy(const serial& other);
00340 
00341     /**
00342      * returns used parameters
00343      */
00344     const parameters& getParameters() const;
00345 
00346     /**
00347      * set the parameters for the serial object
00348      */
00349     bool setParameters(const parameters& theParameters);
00350 
00351     /**
00352      * sends a character c through the port specified in the
00353      * parameters.
00354      *
00355      * @return true if the character could be send successfully, or
00356      *              false otherwise.
00357      */
00358     bool send(const char c);
00359 
00360     /**
00361      * sends an unsigned character c through the port specified in the
00362      * parameters.
00363      *
00364      * @return true if the character could be send successfully, or
00365      *              false otherwise.
00366      */
00367     bool send(const ubyte c);
00368 
00369     /**
00370      * sends a string through the port specified in the parameters.
00371      *
00372      * @return true if all the string characters are send
00373      * successfully, or false otherwise.
00374      */
00375     bool send(const std::string& theString);
00376 
00377     /**
00378      * sends all ubytes contained in the vector through the port
00379      * specified in the parameters.  The vector is more appropriate if
00380      * the data to be send has a more "binary" nature than an
00381      * ASCII string.
00382      *
00383      * @return true if all the string characters are send
00384      * successfully, or false otherwise.  
00385      */
00386     bool send(const vector<ubyte>& theVector);
00387 
00388     /**
00389      * wait for a character to be received in the port specified in
00390      * the parameters.
00391      *
00392      * @return true if the character has been received, or false if a
00393      * problem occured.  In this case you can read the status string
00394      * to check the source of the problem.  Note that false will also
00395      * be returned if a timeout occurs.
00396      */
00397     bool receive(char& c);
00398 
00399     /**
00400      * wait for an unsigned character to be received in the port specified in
00401      * the parameters.
00402      *
00403      * @return true if the character has been received, or false if a
00404      * problem occured.  In this case you can read the status string
00405      * to check the source of the problem.  Note that false will also
00406      * be returned if a timeout occurs.
00407      */
00408     bool receive(ubyte& c);
00409 
00410     /**
00411      * read all characters until the next new line or zero character, and
00412      * store them in the given string.
00413      */
00414     bool receive(std::string& theString);
00415 
00416     /**
00417      * read exactly theVector.size() characters from the port specified in
00418      * the parameters.
00419      *
00420      * @return true if the character has been received, or false if a
00421      * problem occured.  In this case you can read the status string
00422      * to check the source of the problem.  Note that false will also
00423      * be returned if a timeout occurs.
00424      */
00425     bool receive(vector<ubyte>& theVector);
00426 
00427     /**
00428      * return the last message set with setStatusString().  This will
00429      * never return 0.  If no status-string has been set yet an empty string
00430      * (pointer to a string with only the char(0)) will be returned.
00431      */
00432     const char* getStatusString() const;
00433 
00434     /**
00435      * set a status string.
00436      *
00437      * @param msg the const string to be reported next time by
00438      * getStatusString().
00439      *
00440      * This message will be usually set within the send/receive
00441      * methods to indicate an error cause.
00442      *
00443      * Note that the change of the status string is not considered as
00444      * a change in the class status (this method is const).
00445      */
00446     void setStatusString(const char* msg) const;
00447 
00448   protected:
00449 
00450     /**
00451      * open the port using the parameters
00452      */
00453     bool openPort();
00454 
00455     /**
00456      * the parameters in use
00457      */
00458     parameters param;
00459 
00460   private:
00461 
00462 #ifdef _LTI_MSC_VER
00463     /**
00464      * handler used in windows
00465      */
00466     HANDLE hHandle;
00467 
00468     /**
00469      * sets and query the time-out parameters for a communications device
00470      */
00471     COMMTIMEOUTS timeOut;
00472 #else
00473 
00474     /**
00475      * File descriptor for the port in POSIX.
00476      * get the value -1 in case of error,while opening the port
00477      */
00478     int hHandle;
00479 
00480 #endif
00481 
00482     /**
00483      * contains true if the port is opened, else false
00484      */
00485     bool isPortOpened;
00486 
00487     /**
00488      * close the port
00489      */
00490     void closePort();
00491 
00492     /**
00493      * the empty string returned if the statusString is empty
00494      */
00495     static const char *const emptyString;
00496 
00497     /**
00498      * the status string written with setStatusString
00499      */
00500     mutable char* statusString;
00501   };
00502 }
00503 
00504 #endif

Generated on Sat Apr 10 15:26:08 2010 for LTI-Lib by Doxygen 1.6.1