Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SerialPortIO.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef SERIALPORTIO_HH_
26 #define SERIALPORTIO_HH_
27 
28 #include <bias_config.h>
29 
30 #include <stdio.h> /* Standard input/output definitions */
31 #include <string> /* String function definitions */
32 
33 #ifdef WIN32
34  #include <windows.h>
35 #else
36  #include <unistd.h> /* UNIX standard function definitions */
37  #include <fcntl.h> /* File control definitions */
38  #include <errno.h> /* Error number definitions */
39  #include <termios.h> /* POSIX terminal control definitions */
40 #endif
41 
42 namespace BIAS {
43 
49  };
50 
51  /**
52  * @class SerialPortIO
53  * @brief Communication over the serial port, use OpenPort with e.g. /dev/ttyUSB0 or COM0 to
54  * open a port for communication. Default Options will be set when opening the port.
55  * if you need other options use the set SetOptions function
56  * You can read or write a string to the port.
57  * For the PetScan RT100 V5 format a function to get the RFID tag from a read string
58  * is given.
59  * @ingroup g_utils
60  * @date Nov 23, 2010
61  * @author: ischiller
62  */
63  class BIASUtils_EXPORT SerialPortIO {
64  public:
65  /** \brief standard constructor
66  * @param verbose Print messages if port cannot be opened
67  */
68  SerialPortIO(bool verbose = true);
69 
70  /** \brief standard destructor */
71  virtual ~SerialPortIO();
72 
73  /** \brief Open port, e.g. /dev/ttyUSB0, or COM0
74  * \return true on success, false on failure to open*/
75  bool OpenPort(std::string port);
76 
77  /** \brief Close port
78  * \return true on success, false on failure to close*/
79  bool ClosePort();
80 
81  /**\brief Set Options for opened port, use OpenPort before
82  * \return true on success, false on failure to close
83  *
84  * \param canonicalInput[in]: set Canonical input (line oriented) */
85  bool SetOptions(unsigned baudrate=9600,
87  bool hardwareFlowControl=false,
88  bool canonicalInput=true,
89  bool softwareFlowControl=false,
90  int stopbits=1);
91 
92  /**
93  * \brief in linux all exept ReadTotalTimeoutConstant is ignored
94  * \param ReadIntervalTimeout[in]: tenths of seconds to wait for message
95  * \param ReadTotalTimeoutMultiplier[in]: multiplicative factor for ReadTotalTimeoutConstant
96  * \param ReadTotalTimeoutConstant[in]: constant to time wait for message
97  * \param WriteTotalTimeoutMultiplier[in]: multiplicative factor for WriteTotalTimeoutConstant
98  * \param WriteTotalTimeoutConstant[in]: tenths of seconds to wait befor writing message
99  */
100  bool SetTimeOuts(int ReadIntervalTimeout,
101  int ReadTotalTimeoutMultiplier,int ReadTotalTimeoutConstant,
102  int WriteTotalTimeoutMultiplier,int WriteTotalTimeoutConstant);
103 
104  /**\brief Read a string from port, port has to be open
105  * \param waitForMessage[in]: if this parameter is true the call
106  * to this function will block until something is read from the port
107  * if it is false it will return immediately
108  * \param message[in]: the read string*/
109  bool ReadString(std::string& message, bool waitForMessage=true);
110 
111  /**\brief Read bytes from the port, port has to be open
112  * \param waitForMessage[in]: if this parameter is true the call
113  * to this function will block until something is read from the port
114  * if it is false it will return immediately
115  * \param message[out]: the preallocated buffer of size nrBytes to write message to
116  * \param nrBytes[out]: the number of bytes which are read from the port
117  * \return number of bytes read */
118  int ReadBytes(unsigned char* message, int nrBytes, bool waitForMessage=true);
119 
120  /**\brief Write a string to the port, port has to be open
121  * \param message[in]: the message to write to the port
122  * \return true on success, false on failure to close*/
123  bool WriteString(std::string message);
124 
125  /**\brief Write bytes to the port, port has to be open
126  * \param message[in]: the message to write to the port
127  * \param nrBytes[in]: the number of bytes to write to the port
128  * \return true on success, false on failure to close*/
129  bool WriteBytes(unsigned char *message, int nrBytes);
130 
131  /**
132  * \brief parses values of PetScan RT100 V5 format
133  * Start Chip Value *Control
134  * U FDXB945 000000869846*FFA9
135  * \param message[in]: the message to extract the RFID from
136  * \param rfig[out]: the rfid extracted from the message string
137  * \return true on success, false on failure to close*/
138  bool GetPetScanRFIDFromString(std::string message, std::string& rfid);
139 
140  protected:
141  /** File descriptor for the port */
142 #ifdef WIN32
143  HANDLE serialFD_;
144 #else
146 #endif
147  bool bIsOpen_;
148 
149  bool verbose_;
150  };
151 }//end namespace BIAS
152 #endif /* SERIALPORTIO_HH_ */
int serialFD_
File descriptor for the port.
Communication over the serial port, use OpenPort with e.g.
Definition: SerialPortIO.hh:63
SerialPortParityMode
Definition: SerialPortIO.hh:44