]> git.tdb.fi Git - libs/core.git/blob - source/serial.cpp
Drop copyright and license notices from files
[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::set_block(bool b)
209 {
210         if(b)
211                 mode = mode|M_NONBLOCK;
212         else
213                 mode = mode&~M_NONBLOCK;
214
215 #ifndef WIN32
216         int flags = fcntl(handle, F_GETFD);
217         fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
218 #endif
219 }
220
221 void Serial::set_baud_rate(unsigned rate)
222 {
223         DeviceState state;
224         get_state(handle, state);
225         ::set_baud_rate(state, rate);
226         set_state(handle, state);
227 }
228
229 void Serial::set_data_bits(unsigned bits)
230 {
231         DeviceState state;
232         get_state(handle, state);
233         ::set_data_bits(state, bits);
234         set_state(handle, state);
235 }
236
237 void Serial::set_parity(Parity par)
238 {
239         DeviceState state;
240         get_state(handle, state);
241         ::set_parity(state, par);
242         set_state(handle, state);
243 }
244
245 void Serial::set_stop_bits(unsigned bits)
246 {
247         DeviceState state;
248         get_state(handle, state);
249         ::set_stop_bits(state, bits);
250         set_state(handle, state);
251 }
252
253 void Serial::set_parameters(const string &params)
254 {
255         unsigned i;
256         for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
257         if(i+4!=params.size() || params[i]!=',')
258                 throw InvalidParameterValue("Invalid parameter string");
259         if(params[i+1]<'5' || params[i+1]>'8')
260                 throw InvalidParameterValue("Invalid data bit count");
261         if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
262                 throw InvalidParameterValue("Invalid parity");
263         if(params[i+3]!='1' && params[i+3]!='2')
264                 throw InvalidParameterValue("Invalid stop bit count");
265
266         DeviceState state;
267         get_state(handle, state);
268         ::set_baud_rate(state, lexical_cast<unsigned>(params.substr(0, i)));
269         ::set_data_bits(state, params[i+1]-'0');
270         ::set_parity(state, (params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
271         ::set_stop_bits(state, params[i+3]-'0');
272         set_state(handle, state);
273 }
274
275 Handle Serial::get_event_handle()
276 {
277 #ifdef WIN32
278         throw Exception("Serial port events not supported on win32 yet");
279 #else
280         return handle;
281 #endif
282 }
283
284 void Serial::close()
285 {
286 #ifdef WIN32
287         CloseHandle(handle);
288 #else
289         ::close(handle);
290 #endif
291 }
292
293 unsigned Serial::do_write(const char *buf, unsigned size)
294 {
295         if(size==0)
296                 return 0;
297
298 #ifdef WIN32
299         DWORD ret;
300         if(WriteFile(handle, buf, size, &ret, 0)==0)
301                 throw SystemError("Writing to serial port failed", GetLastError());
302 #else
303         int ret = ::write(handle, buf, size);
304         if(ret==-1)
305         {
306                 if(errno==EAGAIN)
307                         return 0;
308                 else
309                         throw SystemError("Writing to serial port failed", errno);
310         }
311 #endif
312
313         return ret;
314 }
315
316 unsigned Serial::do_read(char *buf, unsigned size)
317 {
318         if(size==0)
319                 return 0;
320
321 #ifdef WIN32
322         DWORD ret;
323         if(ReadFile(handle, buf, size, &ret, 0)==0)
324                 throw SystemError("Reading from serial port failed", GetLastError());
325 #else
326         int ret = ::read(handle, buf, size);
327         if(ret==-1)
328         {
329                 if(errno==EAGAIN)
330                         return 0;
331                 else
332                         throw SystemError("Reading from serial port failed", errno);
333         }
334 #endif
335
336         return ret;
337 }
338
339 } // namespace IO
340 } // namespace Msp