]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_serial.hxx
Introduce SGBinaryFile
[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 /**
44  * A serial I/O class based on SGIOChannel.
45  */
46 class SGSerial : public SGIOChannel {
47
48     std::string device;
49     std::string baud;
50     SGSerialPort port;
51
52     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
53     int save_len;
54
55 public:
56
57     /**
58      * Create an instance of SGSerial.
59      * This creates an instance of the SGSerial class. You need to
60      * provide the serial device name and desired baud rate.  For Unix
61      * style systems, device names will be similar to
62      * ``/dev/ttyS0''. For DOS style systems you may want to use
63      * something similar to ``COM1:''. As with the SGFile class,
64      * device is not opened immediately, but instead will be opened
65      * when the open() method is called.
66      * @param device_name name of serial device
67      * @param baud_rate speed of communication
68      */
69     SGSerial( const std::string& device_name, const std::string& baud_rate );
70
71     /** Destructor */
72     ~SGSerial();
73
74     // open the serial port based on specified direction
75     bool open( const SGProtocolDir d );
76
77     // read a block of data of specified size
78     int read( char *buf, int length );
79
80     // read a line of data, length is max size of input buffer
81     int readline( char *buf, int length );
82
83     // write data to port
84     int write( const char *buf, const int length );
85
86     // write null terminated string to port
87     int writestring( const char *str );
88
89     // close port
90     bool close();
91
92     /** @return the serial port device name */
93     inline string get_device() const { return device; }
94
95     /** @return the baud rate */
96     inline string get_baud() const { return baud; }
97 };
98
99
100 #endif // _SG_SERIAL_HXX
101
102