]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_serial.hxx
Effects framework
[simgear.git] / simgear / io / sg_serial.hxx
1 /**
2  * \file sg_serial.hxx
3  * Serial I/O routines
4  */
5
6 // Written by Curtis Olson, started November 1999.
7 //
8 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_SERIAL_HXX
28 #define _SG_SERIAL_HXX
29
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include <string>
38
39 #include <simgear/serial/serial.hxx>
40
41 #include "iochannel.hxx"
42
43 using std::string;
44
45 /**
46  * A serial I/O class based on SGIOChannel.
47  */
48 class SGSerial : public SGIOChannel {
49
50     string device;
51     string baud;
52     SGSerialPort port;
53
54     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
55     int save_len;
56
57 public:
58
59     /**
60      * Create an instance of SGSerial.
61      * This creates an instance of the SGSerial class. You need to
62      * provide the serial device name and desired baud rate.  For Unix
63      * style systems, device names will be similar to
64      * ``/dev/ttyS0''. For DOS style systems you may want to use
65      * something similar to ``COM1:''. As with the SGFile class,
66      * device is not opened immediately, but instead will be opened
67      * when the open() method is called.
68      * @param device_name name of serial device
69      * @param baud_rate speed of communication
70      */
71     SGSerial( const string& device_name, const string& baud_rate );
72
73     /** Destructor */
74     ~SGSerial();
75
76     // open the serial port based on specified direction
77     bool open( const SGProtocolDir d );
78
79     // read a block of data of specified size
80     int read( char *buf, int length );
81
82     // read a line of data, length is max size of input buffer
83     int readline( char *buf, int length );
84
85     // write data to port
86     int write( const char *buf, const int length );
87
88     // write null terminated string to port
89     int writestring( const char *str );
90
91     // close port
92     bool close();
93
94     /** @return the serial port device name */
95     inline string get_device() const { return device; }
96
97     /** @return the baud rate */
98     inline string get_baud() const { return baud; }
99 };
100
101
102 #endif // _SG_SERIAL_HXX
103
104