]> git.mxchange.org Git - simgear.git/blob - Serial/serial.cxx
Initial revision.
[simgear.git] / Serial / serial.cxx
1 // serial.cxx -- Unix serial I/O support
2 //
3 // Written by Curtis Olson, started November 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #include <termios.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include <Debug/logstream.hxx>
32
33 #include "serial.hxx"
34
35
36 fgSERIAL::fgSERIAL() {
37     dev_open = false;
38 }
39
40 fgSERIAL::fgSERIAL(const string& device, int baud) {
41     open_port(device);
42     
43     if ( dev_open ) {
44         set_baud(baud);
45     }
46 }
47
48 fgSERIAL::~fgSERIAL() {
49     close(fd);
50 }
51
52 bool fgSERIAL::open_port(const string& device) {
53     if ( (fd = open(device.c_str(), O_RDWR | O_NONBLOCK)) == -1 ) {
54         FG_LOG( FG_SERIAL, FG_ALERT, "Cannot open " << device
55                 << " for serial I/O" );
56         return false;
57     } else {
58         dev_open = true;
59         return true;
60     }
61 }
62
63 bool fgSERIAL::set_baud(int baud) {
64     struct termios config;
65     speed_t speed;
66
67     cout << "attempting to set baud rate to: " << baud << endl;
68
69     if ( tcgetattr( fd, &config ) != 0 ) {
70         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
71         return false;
72     }
73
74     if ( baud == 300 ) {
75         speed = B300;
76     } else if ( baud == 1200 ) {
77         speed = B1200;
78     } else if ( baud == 2400 ) {
79         speed = B2400;
80     } else if ( baud == 4800 ) {
81         speed = B4800;
82     } else if ( baud == 9600 ) {
83         speed = B9600;
84     } else if ( baud == 19200 ) {
85         speed = B19200;
86     } else if ( baud == 38400 ) {
87         speed = B38400;
88     } else if ( baud == 57600 ) {
89         speed = B57600;
90     } else if ( baud == 115200 ) {
91         speed = B115200;
92     } else if ( baud == 230400 ) {
93         speed = B230400;
94     } else {
95         FG_LOG( FG_SERIAL, FG_ALERT, "Unsupported baud rate " << baud );
96         return false;
97     }
98
99     if ( cfsetispeed( &config, speed ) != 0 ) {
100         FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting input baud rate" );
101         return false;
102     }
103
104     if ( cfsetospeed( &config, speed ) != 0 ) {
105         FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting output baud rate" );
106         return false;
107     }
108
109     if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
110         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
111         return false;
112     }
113
114     cout << "successfully set baud to " << baud << endl;
115
116     return true;
117 }
118
119 string fgSERIAL::read_port() {
120     const int max_count = 1024;
121     char buffer[max_count+1];
122     int count;
123     string result;
124
125     count = read(fd, buffer, max_count);
126     // cout << "read " << count << " bytes" << endl;
127
128     if ( count < 0 ) {
129         // error condition
130         if ( errno != EAGAIN ) {
131             FG_LOG( FG_SERIAL, FG_ALERT, 
132                     "Serial I/O on read, error number = " << errno );
133         }
134
135         return "";
136     } else {
137         buffer[count] = '\0';
138         result = buffer;
139
140         return result;
141     }
142 }
143
144 int fgSERIAL::write_port(const string& value) {
145     int count;
146
147     count = write(fd, value.c_str(), value.length());
148     // cout << "write '" << value << "'  " << count << " bytes" << endl;
149
150     if ( (int)count != (int)value.length() ) {
151         FG_LOG( FG_SERIAL, FG_ALERT,
152                 "Serial I/O on write, error number = " << errno );
153     }
154
155     return count;
156 }
157
158
159 // $Log$
160 // Revision 1.1  1998/11/16 13:53:02  curt
161 // Initial revision.
162 //