]> git.tdb.fi Git - libs/core.git/blob - source/serial.cpp
ec6a198bee3ac0d6d3568a9c218bac0df0f7aa4f
[libs/core.git] / source / serial.cpp
1 #ifdef WIN32
2 #include <windows.h>
3 #else
4 #include <termios.h>
5 #include <fcntl.h>
6 #include <errno.h>
7 #endif
8 #include <msp/strings/formatter.h>
9 #include "except.h"
10 #include "serial.h"
11
12 using namespace std;
13
14 namespace {
15
16 using namespace Msp;
17 using namespace Msp::IO;
18
19 #ifdef WIN32
20 typedef DCB DeviceState;
21 #else
22 typedef termios DeviceState;
23 #endif
24
25 void get_state(Handle handle, DeviceState &state)
26 {
27 #ifdef WIN32
28         GetCommState(handle, &state);
29 #else
30         tcgetattr(handle, &state);
31 #endif
32 }
33
34 void set_state(Handle handle, DeviceState &state)
35 {
36 #ifdef WIN32
37         if(SetCommState(handle, &state)==0)
38                 throw SystemError("Cannot set serial port parameters", GetLastError());
39 #else
40         if(tcsetattr(handle, TCSADRAIN, &state)==-1)
41                 throw SystemError("Cannot set serial port parameters", errno);
42 #endif
43 }
44
45 void set_baud_rate(DeviceState &state, unsigned baud)
46 {
47 #ifdef WIN32
48         state.BaudRate = baud;
49 #else
50         speed_t speed;
51         switch(baud)
52         {
53         case 0:      speed = B0; break;
54         case 50:     speed = B50; break;
55         case 75:     speed = B75; break;
56         case 110:    speed = B110; break;
57         case 134:    speed = B134; break;
58         case 150:    speed = B150; break;
59         case 200:    speed = B200; break;
60         case 300:    speed = B300; break;
61         case 600:    speed = B600; break;
62         case 1200:   speed = B1200; break;
63         case 1800:   speed = B1800; break;
64         case 2400:   speed = B2400; break;
65         case 4800:   speed = B4800; break;
66         case 9600:   speed = B9600; break;
67         case 19200:  speed = B19200; break;
68         case 38400:  speed = B38400; break;
69         case 57600:  speed = B57600; break;
70         case 115200: speed = B115200; break;
71         case 230400: speed = B230400; break;
72         default: throw InvalidParameterValue("Invalid baud rate");
73         }
74
75         cfsetospeed(&state, speed);
76         cfsetispeed(&state, speed);
77 #endif
78 }
79
80 void set_data_bits(DeviceState &state, unsigned bits)
81 {
82 #ifdef WIN32
83         state.ByteSize = bits;
84 #else
85         tcflag_t flag;
86         switch(bits)
87         {
88         case 5: flag = CS5; break;
89         case 6: flag = CS6; break;
90         case 7: flag = CS7; break;
91         case 8: flag = CS8; break;
92         default: throw InvalidParameterValue("Invalid data bit count");
93         }
94
95         state.c_cflag = (state.c_cflag&~CSIZE)|flag;
96 #endif
97 }
98
99 void set_parity(DeviceState &state, Serial::Parity par)
100 {
101 #ifdef WIN32
102         switch(par)
103         {
104         case Serial::NONE: state.Parity = NOPARITY; break;
105         case Serial::EVEN: state.Parity = EVENPARITY; break;
106         case Serial::ODD:  state.Parity = ODDPARITY; break;
107         default: throw InvalidParameterValue("Invalid parity");
108         }
109 #else
110         tcflag_t flag;
111         switch(par)
112         {
113         case Serial::NONE: flag = 0; break;
114         case Serial::EVEN: flag = PARENB; break;
115         case Serial::ODD:  flag = PARENB|PARODD; break;
116         default: throw InvalidParameterValue("Invalid parity");
117         }
118
119         state.c_cflag = (state.c_cflag&~(PARENB|PARODD))|flag;
120 #endif
121 }
122
123 void set_stop_bits(DeviceState &state, unsigned bits)
124 {
125 #ifdef WIN32
126         switch(bits)
127         {
128         case 1: state.StopBits = ONESTOPBIT; break;
129         case 2: state.StopBits = TWOSTOPBITS; break;
130         default: throw InvalidParameterValue("Invalid stop bit count");
131         }
132 #else
133         tcflag_t flag;
134         switch(bits)
135         {
136         case 1: flag = 0; break;
137         case 2: flag = CSTOPB; break;
138         default: throw InvalidParameterValue("Invalid stop bit count");
139         }
140
141         state.c_cflag = (state.c_cflag&~CSTOPB)|flag;
142 #endif
143 }
144
145 }
146
147
148 namespace Msp {
149 namespace IO {
150
151 Serial::Serial(const string &descr)
152 {
153         string::size_type comma = descr.find(',');
154         string port = descr.substr(0, comma);
155
156 #ifdef WIN32
157         port = "\\\\.\\"+port;
158
159         handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
160         if(handle==INVALID_HANDLE_VALUE)
161                 throw SystemError(format("Can't open serial port '%s'", port), GetLastError());
162         mode = M_READ|M_WRITE;
163
164         COMMTIMEOUTS timeouts;
165         timeouts.ReadIntervalTimeout = MAXDWORD;
166         timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
167         timeouts.ReadTotalTimeoutConstant = MAXDWORD-1;
168         timeouts.WriteTotalTimeoutMultiplier = 0;
169         timeouts.WriteTotalTimeoutConstant = 0;
170         SetCommTimeouts(handle, &timeouts);
171 #else
172         if(port.compare(0, 5, "/dev/"))
173                 port = "/dev/"+port;
174
175         handle = open(port.c_str(), O_RDWR);
176         if(handle==-1)
177                 throw SystemError(format("Can't open serial port '%s'", port), errno);
178         mode = M_READ|M_WRITE;
179
180         termios t;
181         tcgetattr(handle, &t);
182         t.c_lflag &= ~(ECHO|ICANON);
183         t.c_oflag &= ~OPOST;
184         tcsetattr(handle, TCSADRAIN, &t);
185 #endif
186
187         if(comma!=string::npos)
188         {
189                 try
190                 {
191                         set_parameters(descr.substr(comma+1));
192                 }
193                 catch(...)
194                 {
195                         close();
196                         throw;
197                 }
198         }
199
200         set_events(P_INPUT);
201 }
202
203 Serial::~Serial()
204 {
205         close();
206 }
207
208 void Serial::close()
209 {
210 #ifdef WIN32
211         CloseHandle(handle);
212 #else
213         ::close(handle);
214 #endif
215 }
216
217 void Serial::set_block(bool b)
218 {
219         if(b)
220                 mode = mode|M_NONBLOCK;
221         else
222                 mode = mode&~M_NONBLOCK;
223
224 #ifndef WIN32
225         int flags = fcntl(handle, F_GETFD);
226         fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
227 #endif
228 }
229
230 void Serial::set_baud_rate(unsigned rate)
231 {
232         DeviceState state;
233         get_state(handle, state);
234         ::set_baud_rate(state, rate);
235         set_state(handle, state);
236 }
237
238 void Serial::set_data_bits(unsigned bits)
239 {
240         DeviceState state;
241         get_state(handle, state);
242         ::set_data_bits(state, bits);
243         set_state(handle, state);
244 }
245
246 void Serial::set_parity(Parity par)
247 {
248         DeviceState state;
249         get_state(handle, state);
250         ::set_parity(state, par);
251         set_state(handle, state);
252 }
253
254 void Serial::set_stop_bits(unsigned bits)
255 {
256         DeviceState state;
257         get_state(handle, state);
258         ::set_stop_bits(state, bits);
259         set_state(handle, state);
260 }
261
262 void Serial::set_parameters(const string &params)
263 {
264         unsigned i;
265         for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
266         if(i+4!=params.size() || params[i]!=',')
267                 throw InvalidParameterValue("Invalid parameter string");
268         if(params[i+1]<'5' || params[i+1]>'8')
269                 throw InvalidParameterValue("Invalid data bit count");
270         if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
271                 throw InvalidParameterValue("Invalid parity");
272         if(params[i+3]!='1' && params[i+3]!='2')
273                 throw InvalidParameterValue("Invalid stop bit count");
274
275         DeviceState state;
276         get_state(handle, state);
277         ::set_baud_rate(state, lexical_cast<unsigned>(params.substr(0, i)));
278         ::set_data_bits(state, params[i+1]-'0');
279         ::set_parity(state, (params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
280         ::set_stop_bits(state, params[i+3]-'0');
281         set_state(handle, state);
282 }
283
284 unsigned Serial::do_write(const char *buf, unsigned size)
285 {
286         if(size==0)
287                 return 0;
288
289 #ifdef WIN32
290         DWORD ret;
291         if(WriteFile(handle, buf, size, &ret, 0)==0)
292                 throw SystemError("Writing to serial port failed", GetLastError());
293 #else
294         int ret = ::write(handle, buf, size);
295         if(ret==-1)
296         {
297                 if(errno==EAGAIN)
298                         return 0;
299                 else
300                         throw SystemError("Writing to serial port failed", errno);
301         }
302 #endif
303
304         return ret;
305 }
306
307 unsigned Serial::do_read(char *buf, unsigned size)
308 {
309         if(size==0)
310                 return 0;
311
312 #ifdef WIN32
313         DWORD ret;
314         if(ReadFile(handle, buf, size, &ret, 0)==0)
315                 throw SystemError("Reading from serial port failed", GetLastError());
316 #else
317         int ret = ::read(handle, buf, size);
318         if(ret==-1)
319         {
320                 if(errno==EAGAIN)
321                         return 0;
322                 else
323                         throw SystemError("Reading from serial port failed", errno);
324         }
325 #endif
326
327         return ret;
328 }
329
330 Handle Serial::get_event_handle()
331 {
332 #ifdef WIN32
333         throw Exception("Serial port events not supported on win32 yet");
334 #else
335         return handle;
336 #endif
337 }
338
339 } // namespace IO
340 } // namespace Msp