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