]> git.mxchange.org Git - simgear.git/blob - Serial/serial.cxx
Updates ...
[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     struct termios config;
54
55     if ( (fd = open(device.c_str(), O_RDWR | O_NONBLOCK)) == -1 ) {
56         FG_LOG( FG_SERIAL, FG_ALERT, "Cannot open " << device
57                 << " for serial I/O" );
58         return false;
59     } else {
60         dev_open = true;
61     }
62
63     // set software flow control 
64     if ( tcgetattr( fd, &config ) != 0 ) {
65         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
66         return false;
67     }
68
69     config.c_iflag |= IXON;
70     config.c_iflag |= IXOFF;
71
72     if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
73         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
74         return false;
75     }
76
77     return true;
78 }
79
80 bool fgSERIAL::set_baud(int baud) {
81     struct termios config;
82     speed_t speed;
83
84     if ( tcgetattr( fd, &config ) != 0 ) {
85         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to poll port settings" );
86         return false;
87     }
88
89     if ( baud == 300 ) {
90         speed = B300;
91     } else if ( baud == 1200 ) {
92         speed = B1200;
93     } else if ( baud == 2400 ) {
94         speed = B2400;
95     } else if ( baud == 4800 ) {
96         speed = B4800;
97     } else if ( baud == 9600 ) {
98         speed = B9600;
99     } else if ( baud == 19200 ) {
100         speed = B19200;
101     } else if ( baud == 38400 ) {
102         speed = B38400;
103     } else if ( baud == 57600 ) {
104         speed = B57600;
105     } else if ( baud == 115200 ) {
106         speed = B115200;
107     } else if ( baud == 230400 ) {
108         speed = B230400;
109     } else {
110         FG_LOG( FG_SERIAL, FG_ALERT, "Unsupported baud rate " << baud );
111         return false;
112     }
113
114     if ( cfsetispeed( &config, speed ) != 0 ) {
115         FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting input baud rate" );
116         return false;
117     }
118
119     if ( cfsetospeed( &config, speed ) != 0 ) {
120         FG_LOG( FG_SERIAL, FG_ALERT, "Problem setting output baud rate" );
121         return false;
122     }
123
124     if ( tcsetattr( fd, TCSANOW, &config ) != 0 ) {
125         FG_LOG( FG_SERIAL, FG_ALERT, "Unable to update port settings" );
126         return false;
127     }
128
129     return true;
130 }
131
132 string fgSERIAL::read_port() {
133     const int max_count = 1024;
134     char buffer[max_count+1];
135     int count;
136     string result;
137
138     count = read(fd, buffer, max_count);
139     // cout << "read " << count << " bytes" << endl;
140
141     if ( count < 0 ) {
142         // error condition
143         if ( errno != EAGAIN ) {
144             FG_LOG( FG_SERIAL, FG_ALERT, 
145                     "Serial I/O on read, error number = " << errno );
146         }
147
148         return "";
149     } else {
150         buffer[count] = '\0';
151         result = buffer;
152
153         return result;
154     }
155 }
156
157 int fgSERIAL::write_port(const string& value) {
158     int count;
159
160     count = write(fd, value.c_str(), value.length());
161     // cout << "write '" << value << "'  " << count << " bytes" << endl;
162
163     if ( (int)count != (int)value.length() ) {
164         FG_LOG( FG_SERIAL, FG_ALERT,
165                 "Serial I/O on write, error number = " << errno );
166     }
167
168     return count;
169 }
170
171
172 // $Log$
173 // Revision 1.2  1998/11/19 03:35:43  curt
174 // Updates ...
175 //
176 // Revision 1.1  1998/11/16 13:53:02  curt
177 // Initial revision.
178 //