]> git.mxchange.org Git - simgear.git/blob - simgear/serial/serial.hxx
Remove SVN sync code.
[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 - http://www.flightgear.org/~curt
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 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 _SERIAL_HXX
28 #define _SERIAL_HXX
29
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #ifdef _WIN32
36 #  include <windows.h>
37 #endif
38
39 #include <simgear/compiler.h>
40 #include <string>
41 using std::string;
42
43 // if someone know how to do this all with C++ streams let me know
44 // #include <stdio.h>
45
46
47 /**
48  * A class to encapsulate low level serial port IO.
49  */
50 class SGSerialPort
51 {
52 #ifdef  _WIN32
53     typedef HANDLE fd_type;
54 #else
55     typedef int fd_type;
56 #endif
57
58 private:
59
60     fd_type fd;
61     bool dev_open;
62
63 public:
64
65     /** Default constructor */
66     SGSerialPort();
67
68     /**
69      * Constructor
70      * @param device device name
71      * @param baud baud rate
72      */
73     SGSerialPort(const string& device, int baud);
74
75     /** Destructor */
76     ~SGSerialPort();
77
78     /** Open a the serial port
79      * @param device name of device
80      * @return success/failure
81      */
82     bool open_port(const string& device);
83
84     /** Close the serial port
85      * @return success/failure 
86      */
87     bool close_port();
88
89     /** Set baud rate
90      * @param baud baud rate
91      * @return success/failure
92      */
93     bool set_baud(int baud);
94
95     /** Read from the serial port
96      * @return line of data
97      */
98     string read_port();
99
100     /** Read from the serial port
101      * @param buf input buffer
102      * @param len length of buffer (i.e. max number of bytes to read
103      * @return number of bytes read
104      */
105     int read_port(char *buf, int len);
106
107     /** Write to the serial port
108      * @param value output string
109      * @return number of bytes written
110      */
111     int write_port(const string& value);
112
113     /** Write to the serial port
114      * @param buf pointer to character buffer containing output data
115      * @param len number of bytes to write from the buffer
116      * @return number of bytes written
117      */
118     int write_port(const char *buf, int len);
119
120     /** @return true if device open */
121     inline bool is_enabled() { return dev_open; }
122 };
123
124
125 #endif // _SERIAL_HXX
126
127