nidas v1.2.3
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
41namespace nidas { namespace util {
42
44{
45
46public:
47
48 SerialPort();
49
55 SerialPort(const SerialPort&);
56
62 SerialPort(const std::string& name);
63
67 SerialPort(const std::string& name, int fd);
68
72 virtual ~SerialPort();
73
79 Termios& termios() { return _termios; }
80
84 const Termios& getTermios() const { return _termios; }
85
89 void applyTermios();
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);
106
110 void close();
111
112 // int &fd() { return _fd; }
113
114 int getFd() const { return _fd; }
115
135 int getModemStatus();
136
140 void setModemStatus(int val);
141
145 void clearModemBits(int val);
146
150 void setModemBits(int val);
151
152 bool getCarrierDetect();
153
154 static std::string modemFlagsToString(int modem);
155
156 void setBlocking(bool val);
157 bool getBlocking();
158
163 void drain();
164
169 void flushInput();
170
175 void flushOutput();
176
177 void flushBoth();
178
179 int timeoutOrEOF() const { return _state == TIMEOUT_OR_EOF; }
180
186 virtual int readUntil(char *buf,int len,char term);
187
191 virtual int readLine(char *buf,int len);
192
193 virtual int read(char *buf,int len);
194
195 virtual char readchar();
196
197 virtual int write(const void *buf,int len);
198
224 static int createPtyLink(const std::string& linkname);
225
230 static void createLinkToPty(const std::string& linkname, int fd);
231
239 static int createPty(bool hup);
240
263 static bool waitForOpen(int fd, int timeout);
264
265private:
266
271
273
274 int _fd;
275
276 std::string _name;
277
279
280 char *_savep;
281
282 char *_savebuf;
283
285
287
289};
290
291}} // namespace nidas namespace util
292
293#endif
Definition SerialPort.h:44
void flushOutput()
Do a tcflush() system call on the device.
Definition SerialPort.cc:209
int _savealloc
Definition SerialPort.h:286
bool _blocking
Definition SerialPort.h:288
static int createPty(bool hup)
Create a pseudo-terminal and return the file descriptor.
Definition SerialPort.cc:368
int getModemStatus()
Get the current state of the modem bits.
Definition SerialPort.cc:138
void flushBoth()
Definition SerialPort.cc:223
static bool waitForOpen(int fd, int timeout)
Wait up to timeout seconds for pts to be opened.
Definition SerialPort.cc:438
char * _savep
Definition SerialPort.h:280
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:429
void drain()
Do a tcdrain() system call on the device.
Definition SerialPort.cc:202
const std::string & getName() const
Get device name of the SerialPort.
Definition SerialPort.h:94
state
Definition SerialPort.h:278
@ OK
Definition SerialPort.h:278
@ TIMEOUT_OR_EOF
Definition SerialPort.h:278
void setModemStatus(int val)
Set the current state of the modem bits.
Definition SerialPort.cc:147
Termios & termios()
Writable reference to the SerialPort's Termios.
Definition SerialPort.h:79
void flushInput()
Do a tcflush() system call on the device.
Definition SerialPort.cc:216
char * _savebuf
Definition SerialPort.h:282
virtual int read(char *buf, int len)
Definition SerialPort.cc:313
int timeoutOrEOF() const
Definition SerialPort.h:179
void applyTermios()
Apply the Termios settings to an opened serial port.
Definition SerialPort.cc:101
std::string _name
Definition SerialPort.h:276
virtual ~SerialPort()
close the file descriptor if is is open.
Definition SerialPort.cc:73
static std::string modemFlagsToString(int modem)
Definition SerialPort.cc:174
void close()
close the file descriptor.
Definition SerialPort.cc:80
Termios _termios
Definition SerialPort.h:272
void clearModemBits(int val)
Clear the indicated modem bits.
Definition SerialPort.cc:154
int getFd() const
Definition SerialPort.h:114
virtual char readchar()
Do a buffered read and return character read.
Definition SerialPort.cc:331
void setName(const std::string &val)
Set device name of the SerialPort.
Definition SerialPort.h:99
void setBlocking(bool val)
Definition SerialPort.cc:107
int _savelen
Definition SerialPort.h:284
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:230
bool getCarrierDetect()
Definition SerialPort.cc:168
SerialPort()
Definition SerialPort.cc:49
SerialPort & operator=(const SerialPort &)
No assignment.
void setModemBits(int val)
Set the indicated modem bits.
Definition SerialPort.cc:161
virtual int open(int mode=O_RDONLY)
open the serial port.
Definition SerialPort.cc:90
virtual int readLine(char *buf, int len)
Do a readUntil with a newline terminator.
Definition SerialPort.cc:297
enum nidas::util::SerialPort::state _state
virtual int write(const void *buf, int len)
Definition SerialPort.cc:303
static void createLinkToPty(const std::string &linkname, int fd)
Create a symbolic link to ptsname(fd).
Definition SerialPort.cc:391
bool getBlocking()
Definition SerialPort.cc:126
const Termios & getTermios() const
Readonly reference to Termios.
Definition SerialPort.h:84
int _fd
Definition SerialPort.h:274
A class providing get/set methods into a termios structure.
Definition Termios.h:48
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
int len
Definition sing.cc:948
int fd
Definition twod.c:56