libserial
0.6.0rc1
Main Page
Related Pages
Namespaces
Classes
Files
File List
src
SerialStreamBuf.h
Go to the documentation of this file.
1
/*
2
* Time-stamp: <02-Nov-2008 14:52:35 pagey>
3
*
4
* $Id: SerialStreamBuf.h,v 1.9 2005-10-17 00:19:12 crayzeewulf Exp $
5
*
6
*
7
*/
8
#ifndef _SerialStreamBuf_h_
9
#define _SerialStreamBuf_h_
10
11
#include <termios.h>
12
#include <unistd.h>
13
#include <iosfwd>
14
#include <streambuf>
15
#include <string>
16
#include <
SerialPort.h
>
17
18
extern
"C++"
{
19
namespace
LibSerial {
38
class
SerialStreamBuf
:
public
std::streambuf {
39
public
:
43
45
62
enum
BaudRateEnum
{
63
BAUD_50
=
SerialPort::BAUD_50
,
64
BAUD_75
=
SerialPort::BAUD_75
,
65
BAUD_110
=
SerialPort::BAUD_110
,
66
BAUD_134
=
SerialPort::BAUD_134
,
67
BAUD_150
=
SerialPort::BAUD_150
,
68
BAUD_200
=
SerialPort::BAUD_200
,
69
BAUD_300
=
SerialPort::BAUD_300
,
70
BAUD_600
=
SerialPort::BAUD_600
,
71
BAUD_1200
=
SerialPort::BAUD_1200
,
72
BAUD_1800
=
SerialPort::BAUD_1800
,
73
BAUD_2400
=
SerialPort::BAUD_2400
,
74
BAUD_4800
=
SerialPort::BAUD_4800
,
75
BAUD_9600
=
SerialPort::BAUD_9600
,
76
BAUD_19200
=
SerialPort::BAUD_19200
,
77
BAUD_38400
=
SerialPort::BAUD_38400
,
78
BAUD_57600
=
SerialPort::BAUD_57600
,
79
BAUD_115200
=
SerialPort::BAUD_115200
,
80
BAUD_230400
=
SerialPort::BAUD_230400
,
81
#ifdef __linux__
82
BAUD_460800 = SerialPort::BAUD_460800,
83
#endif
84
BAUD_DEFAULT
=
SerialPort::BAUD_DEFAULT
,
85
BAUD_INVALID
86
} ;
87
97
enum
CharSizeEnum
{
98
CHAR_SIZE_5
=
SerialPort::CHAR_SIZE_5
,
99
CHAR_SIZE_6
=
SerialPort::CHAR_SIZE_6
,
100
CHAR_SIZE_7
=
SerialPort::CHAR_SIZE_7
,
101
CHAR_SIZE_8
=
SerialPort::CHAR_SIZE_8
,
102
CHAR_SIZE_DEFAULT
=
SerialPort::CHAR_SIZE_DEFAULT
,
103
CHAR_SIZE_INVALID
104
} ;
105
114
enum
ParityEnum
{
115
PARITY_EVEN
=
SerialPort::PARITY_EVEN
,
116
PARITY_ODD
=
SerialPort::PARITY_ODD
,
117
PARITY_NONE
=
SerialPort::PARITY_NONE
,
118
PARITY_DEFAULT
=
SerialPort::PARITY_DEFAULT
,
119
PARITY_INVALID
120
} ;
121
130
enum
FlowControlEnum
{
131
FLOW_CONTROL_HARD
=
SerialPort::FLOW_CONTROL_HARD
,
132
FLOW_CONTROL_SOFT
=
SerialPort::FLOW_CONTROL_SOFT
,
133
FLOW_CONTROL_NONE
=
SerialPort::FLOW_CONTROL_NONE
,
134
FLOW_CONTROL_DEFAULT
=
SerialPort::FLOW_CONTROL_DEFAULT
,
135
FLOW_CONTROL_INVALID
136
} ;
138
139
/* ------------------------------------------------------------
140
* Public Static Members
141
* ------------------------------------------------------------
142
*/
153
static
const
BaudRateEnum
DEFAULT_BAUD
;
154
162
static
const
CharSizeEnum
DEFAULT_CHAR_SIZE
;
163
170
static
const
short
DEFAULT_NO_OF_STOP_BITS
;
171
178
static
const
ParityEnum
DEFAULT_PARITY
;
179
186
static
const
FlowControlEnum
DEFAULT_FLOW_CONTROL
;
187
196
static
const
short
DEFAULT_VMIN
;
197
206
static
const
short
DEFAULT_VTIME
;
207
209
210
217
SerialStreamBuf
() ;
218
222
~SerialStreamBuf
() ;
224
233
bool
is_open
()
const
;
234
282
SerialStreamBuf
*
open
(
const
std::string filename,
283
std::ios_base::openmode mode =
284
std::ios_base::in | std::ios_base::out ) ;
285
306
SerialStreamBuf
*
close
() ;
307
312
int
SetParametersToDefault
() ;
313
319
const
BaudRateEnum
SetBaudRate
(
const
BaudRateEnum
baudRate ) ;
320
326
const
BaudRateEnum
BaudRate
()
const
;
327
333
const
CharSizeEnum
SetCharSize
(
const
CharSizeEnum
charSize) ;
334
339
const
CharSizeEnum
CharSize
()
const
;
340
348
short
SetNumOfStopBits
(
short
numOfStopBits) ;
349
355
short
NumOfStopBits
()
const
;
356
362
const
ParityEnum
SetParity
(
const
ParityEnum
parityType) ;
363
369
const
ParityEnum
Parity
()
const
;
370
374
const
FlowControlEnum
SetFlowControl
(
const
FlowControlEnum
flowControlType) ;
375
379
const
FlowControlEnum
FlowControl
()
const
;
380
385
const
short
SetVMin
(
short
vtime ) ;
386
391
const
short
VMin
()
const
;
392
397
const
short
SetVTime
(
short
vtime ) ;
398
403
const
short
VTime
()
const
;
404
406
410
412
413
/* ------------------------------------------------------------
414
* Friends
415
* ------------------------------------------------------------
416
*/
417
protected
:
418
/* ------------------------------------------------------------
419
* Protected Data Members
420
* ------------------------------------------------------------
421
*/
426
static
const
char
CTRL_Q
= 0x11 ;
427
432
static
const
char
CTRL_S
= 0x13 ;
433
/* ------------------------------------------------------------
434
* Protected Methods
435
* ------------------------------------------------------------
436
*/
450
virtual
std::streambuf*
setbuf
( char_type*,
451
std::streamsize ) ;
452
459
virtual
std::streamsize
xsgetn
( char_type* s,
460
std::streamsize n ) ;
461
471
virtual
std::streamsize
showmanyc
();
472
480
virtual
int_type
underflow
() ;
481
490
virtual
int_type
uflow
() ;
491
498
virtual
int_type
pbackfail
(int_type c = traits_type::eof()) ;
499
506
virtual
std::streamsize
xsputn
(
const
char_type* s,
507
std::streamsize n ) ;
508
514
virtual
int_type
overflow
(int_type c) ;
515
516
private
:
517
/* ------------------------------------------------------------
518
* Private Data Members
519
* ------------------------------------------------------------
520
*/
521
//
522
// The copy constructor and the assignment operator are
523
// declared private but never defined. This allows the
524
// compiler to catch attempts to copy instances of this
525
// class.
526
//
527
SerialStreamBuf
(
const
SerialStreamBuf
& ) ;
528
SerialStreamBuf
&
operator=
(
const
SerialStreamBuf
& ) ;
529
536
char
mPutbackChar
;
537
541
bool
mPutbackAvailable
;
542
546
int
mFileDescriptor
;
547
/* ------------------------------------------------------------
548
* Private Methods
549
* ------------------------------------------------------------
550
*/
558
int
InitializeSerialPort
() ;
559
} ;
// class SerialStreamBuf
560
561
inline
562
SerialStreamBuf::SerialStreamBuf
() :
563
mPutbackChar(0),
564
mPutbackAvailable(false),
565
mFileDescriptor(-1)
566
{
567
setbuf
(0, 0) ;
568
return ;
569
}
570
571
inline
572
SerialStreamBuf::~SerialStreamBuf
()
573
{
574
if
( this->
is_open
() )
575
{
576
this->
close
() ;
577
}
578
return ;
579
}
580
581
inline
582
bool
583
SerialStreamBuf::is_open
()
const
584
{
585
return
(-1 !=
mFileDescriptor
) ;
586
}
587
588
inline
589
std::streambuf*
590
SerialStreamBuf::setbuf
(char_type *, std::streamsize)
591
{
592
return
std::streambuf::setbuf
(0, 0) ;
593
}
594
595
inline
596
SerialStreamBuf
*
597
SerialStreamBuf::close
()
598
{
599
//
600
// Return a null pointer if the serial port is not currently open.
601
//
602
if
( this->
is_open
() ==
false
) {
603
return
0 ;
604
}
605
//
606
// Otherwise, close the serial port and set the file descriptor
607
// to an invalid value.
608
//
609
if
( -1 == ::
close
(
mFileDescriptor
) ) {
610
//
611
// If the close failed then return a null pointer.
612
//
613
return
0 ;
614
}
else
{
615
//
616
// Set the file descriptor to an invalid value, -1.
617
//
618
mFileDescriptor
= -1 ;
619
//
620
// On success, return "this" as required by the C++ standard.
621
//
622
return
this ;
623
}
624
}
625
626
inline
627
std::streambuf::int_type
628
SerialStreamBuf::uflow
()
629
{
630
int_type next_ch =
underflow
() ;
631
mPutbackAvailable
= false ;
632
return
next_ch ;
633
}
634
635
}
// namespace LibSerial
636
}
// extern "C++"
637
#endif // #ifndef _SerialStreamBuf_h_
Generated on Sun Sep 30 2012 02:54:24 for libserial by
1.8.1.2