]> git.mxchange.org Git - simgear.git/blob - simgear/serial/serial.hxx
Doxygen ...
[simgear.git] / simgear / serial / serial.hxx
1 /**
2  * \file serial.hxx
3  * Low level serial I/O support (for unix/cygwin and windows)
4  */
5
6 // Written by Curtis Olson, started November 1998.
7 //
8 // Copyright (C) 1998  Curtis L. Olson - curt@flightgear.org
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA  02111-1307, USA.
24 //
25 // $Id$
26
27
28 #ifndef _SERIAL_HXX
29 #define _SERIAL_HXX
30
31
32 #ifndef __cplusplus
33 # error This library requires C++
34 #endif
35
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39
40 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
41 #  include <windows.h>
42 #endif
43
44 #include <simgear/compiler.h>
45 #include STL_STRING
46 SG_USING_STD(string);
47
48 // if someone know how to do this all with C++ streams let me know
49 // #include <stdio.h>
50
51
52 /**
53  * A class to encapsulate low level serial port IO.
54  */
55 class FGSerialPort
56 {
57 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
58     typedef HANDLE fd_type;
59 #else
60     typedef int fd_type;
61 #endif
62
63 private:
64
65     fd_type fd;
66     bool dev_open;
67
68 public:
69
70     /** Default constructor */
71     FGSerialPort();
72
73     /**
74      * Constructor
75      * @param device device name
76      * @param baud baud rate
77      */
78     FGSerialPort(const string& device, int baud);
79
80     /** Destructor */
81     ~FGSerialPort();
82
83     /** Open a the serial port
84      * @param device name of device
85      * @return success/failure
86      */
87     bool open_port(const string& device);
88
89     /** Close the serial port
90      * @return success/failure 
91      */
92     bool close_port();
93
94     /** Set baud rate
95      * @param baud baud rate
96      * @return success/failure
97      */
98     bool set_baud(int baud);
99
100     /** Read from the serial port
101      * @return line of data
102      */
103     string read_port();
104
105     /** Read from the serial port
106      * @param buf input buffer
107      * @param len length of buffer (i.e. max number of bytes to read
108      * @return number of bytes read
109      */
110     int read_port(char *buf, int len);
111
112     /** Write to the serial port
113      * @param value output string
114      * @return number of bytes written
115      */
116     int write_port(const string& value);
117
118     /** Write to the serial port
119      * @param buf pointer to character buffer containing output data
120      * @param len number of bytes to write from the buffer
121      * @return number of bytes written
122      */
123     int write_port(const char *buf, int len);
124
125     /** @return true if device open */
126     inline bool is_enabled() { return dev_open; }
127 };
128
129
130 #endif // _SERIAL_HXX
131
132