nidas  v1.2-1520
SerialPort.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_UTIL_SERIALPORT_H
28 #define NIDAS_UTIL_SERIALPORT_H
29 
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 
35 #include <string>
36 #include <iostream>
37 
38 #include "Termios.h"
39 #include "IOException.h"
40 
41 namespace nidas { namespace util {
42 
44 {
45 
46 public:
47 
48  SerialPort();
49 
55  SerialPort(const SerialPort&);
56 
62  SerialPort(const std::string& name);
63 
67  SerialPort(const std::string& name, int fd) throw (IOException);
68 
72  virtual ~SerialPort();
73 
79  Termios& termios() { return _termios; }
80 
84  const Termios& getTermios() const { return _termios; }
85 
89  void applyTermios() throw(IOException);
90 
94  const std::string& getName() const { return _name; }
95 
99  void setName(const std::string& val) { _name = val; }
100 
105  virtual int open(int mode = O_RDONLY) throw (IOException);
106 
110  void close();
111 
112  // int &fd() { return _fd; }
113 
114  int getFd() const { return _fd; }
115 
135  int getModemStatus() throw (IOException);
136 
140  void setModemStatus(int val) throw (IOException);
141 
145  void clearModemBits(int val) throw (IOException);
146 
150  void setModemBits(int val) throw (IOException);
151 
152  bool getCarrierDetect() throw (IOException);
153 
154  static std::string modemFlagsToString(int modem);
155 
156  void setBlocking(bool val) throw(IOException);
157  bool getBlocking() throw(IOException);
158 
163  void drain() throw(IOException);
164 
169  void flushInput() throw(IOException);
170 
175  void flushOutput() throw(IOException);
176 
177  void flushBoth() throw(IOException);
178 
179  int timeoutOrEOF() const { return _state == TIMEOUT_OR_EOF; }
180 
186  virtual int readUntil(char *buf,int len,char term) throw(IOException);
187 
191  virtual int readLine(char *buf,int len) throw(IOException);
192 
193  virtual int read(char *buf,int len) throw(IOException);
194 
195  virtual char readchar() throw(IOException);
196 
197  virtual int write(const void *buf,int len) throw(IOException);
198 
216  static int createPtyLink(const std::string& linkname) throw(IOException);
217 
218 private:
219 
223  SerialPort& operator=(const SerialPort&);
224 
226 
227  int _fd;
228 
229  std::string _name;
230 
232 
233  char *_savep;
234 
235  char *_savebuf;
236 
237  int _savelen;
238 
240 
241  bool _blocking;
242 };
243 
244 }} // namespace nidas namespace util
245 
246 #endif
char * _savebuf
Definition: SerialPort.h:235
void flushInput()
Do a tcflush() system call on the device.
Definition: SerialPort.cc:215
int _fd
Definition: SerialPort.h:227
Definition: SerialPort.h:231
bool _blocking
Definition: SerialPort.h:241
virtual int write(const void *buf, int len)
Definition: SerialPort.cc:302
void applyTermios()
Apply the Termios settings to an opened serial port.
Definition: SerialPort.cc:100
std::string _name
Definition: SerialPort.h:229
bool getBlocking()
Definition: SerialPort.cc:125
const std::string & getName() const
Get device name of the SerialPort.
Definition: SerialPort.h:94
void setName(const std::string &val)
Set device name of the SerialPort.
Definition: SerialPort.h:99
int getModemStatus()
Get the current state of the modem bits.
Definition: SerialPort.cc:137
SerialPort()
Definition: SerialPort.cc:48
virtual int readUntil(char *buf, int len, char term)
Read bytes until either the term character is read, or len-1 number of characters have been read...
Definition: SerialPort.cc:229
int timeoutOrEOF() const
Definition: SerialPort.h:179
int _savelen
Definition: SerialPort.h:237
Termios _termios
Definition: SerialPort.h:225
void clearModemBits(int val)
Clear the indicated modem bits.
Definition: SerialPort.cc:153
void drain()
Do a tcdrain() system call on the device.
Definition: SerialPort.cc:201
virtual ~SerialPort()
close the file descriptor if is is open.
Definition: SerialPort.cc:72
bool getCarrierDetect()
Definition: SerialPort.cc:167
void setModemStatus(int val)
Set the current state of the modem bits.
Definition: SerialPort.cc:146
Definition: SerialPort.h:43
enum nidas::util::SerialPort::state _state
int _savealloc
Definition: SerialPort.h:239
int len
Definition: sing.cc:934
virtual char readchar()
Do a buffered read and return character read.
Definition: SerialPort.cc:330
int fd
Definition: twod.c:56
virtual int read(char *buf, int len)
Definition: SerialPort.cc:312
Definition: IOException.h:37
state
Definition: SerialPort.h:231
void close()
close the file descriptor.
Definition: SerialPort.cc:79
int getFd() const
Definition: SerialPort.h:114
const Termios & getTermios() const
Readonly reference to Termios.
Definition: SerialPort.h:84
void flushBoth()
Definition: SerialPort.cc:222
virtual int readLine(char *buf, int len)
Do a readUntil with a newline terminator.
Definition: SerialPort.cc:296
static std::string modemFlagsToString(int modem)
Definition: SerialPort.cc:173
void setBlocking(bool val)
Definition: SerialPort.cc:106
A class providing get/set methods into a termios structure.
Definition: Termios.h:48
void setModemBits(int val)
Set the indicated modem bits.
Definition: SerialPort.cc:160
virtual int open(int mode=O_RDONLY)
open the serial port.
Definition: SerialPort.cc:89
Termios & termios()
Writable reference to the SerialPort&#39;s Termios.
Definition: SerialPort.h:79
Definition: SerialPort.h:231
char * _savep
Definition: SerialPort.h:233
static int createPtyLink(const std::string &linkname)
Static utility that creates a pseudo-terminal, returning the file descriptor of the master side and c...
Definition: SerialPort.cc:351
void flushOutput()
Do a tcflush() system call on the device.
Definition: SerialPort.cc:208