8 #include <msp/strings/format.h>
9 #include <msp/core/systemerror.h>
10 #include "handle_private.h"
18 using namespace Msp::IO;
21 typedef DCB DeviceState;
23 typedef termios DeviceState;
26 void get_state(const Handle &handle, DeviceState &state)
29 GetCommState(*handle, &state);
31 tcgetattr(*handle, &state);
35 void set_state(const Handle &handle, DeviceState &state)
38 if(SetCommState(*handle, &state)==0)
39 throw system_error("SetCommState");
41 if(tcsetattr(*handle, TCSADRAIN, &state)==-1)
42 throw system_error("tcsetattr");
46 void set_baud_rate(DeviceState &state, unsigned baud)
49 state.BaudRate = baud;
54 case 0: speed = B0; break;
55 case 50: speed = B50; break;
56 case 75: speed = B75; break;
57 case 110: speed = B110; break;
58 case 134: speed = B134; break;
59 case 150: speed = B150; break;
60 case 200: speed = B200; break;
61 case 300: speed = B300; break;
62 case 600: speed = B600; break;
63 case 1200: speed = B1200; break;
64 case 1800: speed = B1800; break;
65 case 2400: speed = B2400; break;
66 case 4800: speed = B4800; break;
67 case 9600: speed = B9600; break;
68 case 19200: speed = B19200; break;
69 case 38400: speed = B38400; break;
70 case 57600: speed = B57600; break;
71 case 115200: speed = B115200; break;
72 case 230400: speed = B230400; break;
73 default: throw invalid_argument("set_baud_rate");
76 cfsetospeed(&state, speed);
77 cfsetispeed(&state, speed);
81 void set_data_bits(DeviceState &state, unsigned bits)
84 state.ByteSize = bits;
89 case 5: flag = CS5; break;
90 case 6: flag = CS6; break;
91 case 7: flag = CS7; break;
92 case 8: flag = CS8; break;
93 default: throw invalid_argument("set_data_bits");
96 state.c_cflag = (state.c_cflag&~CSIZE)|flag;
100 void set_parity(DeviceState &state, Serial::Parity par)
105 case Serial::NONE: state.Parity = NOPARITY; break;
106 case Serial::EVEN: state.Parity = EVENPARITY; break;
107 case Serial::ODD: state.Parity = ODDPARITY; break;
108 default: throw invalid_argument("set_parity");
114 case Serial::NONE: flag = 0; break;
115 case Serial::EVEN: flag = PARENB; break;
116 case Serial::ODD: flag = PARENB|PARODD; break;
117 default: throw invalid_argument("set_parity");
120 state.c_cflag = (state.c_cflag&~(PARENB|PARODD))|flag;
124 void set_stop_bits(DeviceState &state, unsigned bits)
129 case 1: state.StopBits = ONESTOPBIT; break;
130 case 2: state.StopBits = TWOSTOPBITS; break;
131 default: throw invalid_argument("set_stop_bits");
137 case 1: flag = 0; break;
138 case 2: flag = CSTOPB; break;
139 default: throw invalid_argument("set_stop_bits");
142 state.c_cflag = (state.c_cflag&~CSTOPB)|flag;
152 Serial::Serial(const string &descr):
155 string::size_type comma = descr.find(',');
156 string port = descr.substr(0, comma);
159 port = "\\\\.\\"+port;
161 *handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
163 throw system_error(format("CreateFile(%s)", port));
164 mode = M_READ|M_WRITE;
166 COMMTIMEOUTS timeouts;
167 timeouts.ReadIntervalTimeout = MAXDWORD;
168 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
169 timeouts.ReadTotalTimeoutConstant = MAXDWORD-1;
170 timeouts.WriteTotalTimeoutMultiplier = 0;
171 timeouts.WriteTotalTimeoutConstant = 0;
172 SetCommTimeouts(*handle, &timeouts);
174 if(port.compare(0, 5, "/dev/"))
177 *handle = open(port.c_str(), O_RDWR);
179 throw system_error(format("open(%s)", port));
180 mode = M_READ|M_WRITE;
183 tcgetattr(*handle, &t);
184 t.c_lflag &= ~(ECHO|ICANON);
186 tcsetattr(*handle, TCSADRAIN, &t);
189 if(comma!=string::npos)
193 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 invalid_argument("Serial::set_parameters");
266 if(params[i+1]<'5' || params[i+1]>'8')
267 throw invalid_argument("Serial::set_parameters data_bits");
268 if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
269 throw invalid_argument("Serial::set_parameters parity");
270 if(params[i+3]!='1' && params[i+3]!='2')
271 throw invalid_argument("Serial::set_parameters stop_bits");
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 unsigned Serial::do_write(const char *buf, unsigned size)
287 return sys_write(handle, buf, size);
290 unsigned Serial::do_read(char *buf, unsigned size)
295 unsigned ret = reader.read(buf, size);