3 This file is part of libmspio
4 Copyright © 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
15 #include <msp/strings/formatter.h>
24 using namespace Msp::IO;
27 typedef DCB DeviceState;
29 typedef termios DeviceState;
32 void get_state(Handle handle, DeviceState &state)
35 GetCommState(handle, &state);
37 tcgetattr(handle, &state);
41 void set_state(Handle handle, DeviceState &state)
44 if(SetCommState(handle, &state)==0)
45 throw SystemError("Cannot set serial port parameters", GetLastError());
47 if(tcsetattr(handle, TCSADRAIN, &state)==-1)
48 throw SystemError("Cannot set serial port parameters", errno);
52 void set_baud_rate(DeviceState &state, unsigned baud)
55 state.BaudRate = baud;
60 case 0: speed = B0; break;
61 case 50: speed = B50; break;
62 case 75: speed = B75; break;
63 case 110: speed = B110; break;
64 case 134: speed = B134; break;
65 case 150: speed = B150; break;
66 case 200: speed = B200; break;
67 case 300: speed = B300; break;
68 case 600: speed = B600; break;
69 case 1200: speed = B1200; break;
70 case 1800: speed = B1800; break;
71 case 2400: speed = B2400; break;
72 case 4800: speed = B4800; break;
73 case 9600: speed = B9600; break;
74 case 19200: speed = B19200; break;
75 case 38400: speed = B38400; break;
76 case 57600: speed = B57600; break;
77 case 115200: speed = B115200; break;
78 case 230400: speed = B230400; break;
79 default: throw InvalidParameterValue("Invalid baud rate");
82 cfsetospeed(&state, speed);
83 cfsetispeed(&state, speed);
87 void set_data_bits(DeviceState &state, unsigned bits)
90 state.ByteSize = bits;
95 case 5: flag = CS5; break;
96 case 6: flag = CS6; break;
97 case 7: flag = CS7; break;
98 case 8: flag = CS8; break;
99 default: throw InvalidParameterValue("Invalid data bit count");
102 state.c_cflag = (state.c_cflag&~CSIZE)|flag;
106 void set_parity(DeviceState &state, Serial::Parity par)
111 case Serial::NONE: state.Parity = NOPARITY; break;
112 case Serial::EVEN: state.Parity = EVENPARITY; break;
113 case Serial::ODD: state.Parity = ODDPARITY; break;
114 default: throw InvalidParameterValue("Invalid parity");
120 case Serial::NONE: flag = 0; break;
121 case Serial::EVEN: flag = PARENB; break;
122 case Serial::ODD: flag = PARENB|PARODD; break;
123 default: throw InvalidParameterValue("Invalid parity");
126 state.c_cflag = (state.c_cflag&~(PARENB|PARODD))|flag;
130 void set_stop_bits(DeviceState &state, unsigned bits)
135 case 1: state.StopBits = ONESTOPBIT; break;
136 case 2: state.StopBits = TWOSTOPBITS; break;
137 default: throw InvalidParameterValue("Invalid stop bit count");
143 case 1: flag = 0; break;
144 case 2: flag = CSTOPB; break;
145 default: throw InvalidParameterValue("Invalid stop bit count");
148 state.c_cflag = (state.c_cflag&~CSTOPB)|flag;
158 Serial::Serial(const string &descr)
160 string::size_type comma = descr.find(',');
161 string port = descr.substr(0, comma);
164 port = "\\\\.\\"+port;
166 handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
167 if(handle==INVALID_HANDLE_VALUE)
168 throw SystemError(format("Can't open serial port '%s'", port), GetLastError());
169 mode = M_READ|M_WRITE;
171 COMMTIMEOUTS timeouts;
172 timeouts.ReadIntervalTimeout = MAXDWORD;
173 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
174 timeouts.ReadTotalTimeoutConstant = MAXDWORD-1;
175 timeouts.WriteTotalTimeoutMultiplier = 0;
176 timeouts.WriteTotalTimeoutConstant = 0;
177 SetCommTimeouts(handle, &timeouts);
179 if(port.compare(0, 5, "/dev/"))
182 handle = open(port.c_str(), O_RDWR);
184 throw SystemError(format("Can't open serial port '%s'", port), errno);
185 mode = M_READ|M_WRITE;
188 tcgetattr(handle, &t);
189 t.c_lflag &= ~(ECHO|ICANON);
191 tcsetattr(handle, TCSADRAIN, &t);
194 if(comma!=string::npos)
198 set_parameters(descr.substr(comma+1));
215 void Serial::set_block(bool b)
218 mode = mode|M_NONBLOCK;
220 mode = mode&~M_NONBLOCK;
223 int flags = fcntl(handle, F_GETFD);
224 fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
228 void Serial::set_baud_rate(unsigned rate)
231 get_state(handle, state);
232 ::set_baud_rate(state, rate);
233 set_state(handle, state);
236 void Serial::set_data_bits(unsigned bits)
239 get_state(handle, state);
240 ::set_data_bits(state, bits);
241 set_state(handle, state);
244 void Serial::set_parity(Parity par)
247 get_state(handle, state);
248 ::set_parity(state, par);
249 set_state(handle, state);
252 void Serial::set_stop_bits(unsigned bits)
255 get_state(handle, state);
256 ::set_stop_bits(state, bits);
257 set_state(handle, state);
260 void Serial::set_parameters(const string ¶ms)
263 for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
264 if(i+4!=params.size() || params[i]!=',')
265 throw InvalidParameterValue("Invalid parameter string");
266 if(params[i+1]<'5' || params[i+1]>'8')
267 throw InvalidParameterValue("Invalid data bit count");
268 if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
269 throw InvalidParameterValue("Invalid parity");
270 if(params[i+3]!='1' && params[i+3]!='2')
271 throw InvalidParameterValue("Invalid stop bit count");
274 get_state(handle, state);
275 ::set_baud_rate(state, lexical_cast<unsigned>(params.substr(0, i)));
276 ::set_data_bits(state, params[i+1]-'0');
277 ::set_parity(state, (params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
278 ::set_stop_bits(state, params[i+3]-'0');
279 set_state(handle, state);
282 Handle Serial::get_event_handle()
285 throw Exception("Serial port events not supported on win32 yet");
300 unsigned Serial::do_write(const char *buf, unsigned size)
307 if(WriteFile(handle, buf, size, &ret, 0)==0)
308 throw SystemError("Writing to serial port failed", GetLastError());
310 int ret = ::write(handle, buf, size);
316 throw SystemError("Writing to serial port failed", errno);
323 unsigned Serial::do_read(char *buf, unsigned size)
330 if(ReadFile(handle, buf, size, &ret, 0)==0)
331 throw SystemError("Reading from serial port failed", GetLastError());
333 int ret = ::read(handle, buf, size);
339 throw SystemError("Reading from serial port failed", errno);