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_iflag &= ~(ISTRIP|INLCR|IGNCR|ICRNL|IXON);
185 t.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
186 t.c_oflag &= ~(OPOST|OCRNL|ONOCR|ONLRET);
189 tcsetattr(*handle, TCSADRAIN, &t);
192 if(comma!=string::npos)
196 set_parameters(descr.substr(comma+1));
218 void Serial::set_block(bool b)
221 mode = mode|M_NONBLOCK;
223 mode = mode&~M_NONBLOCK;
226 int flags = fcntl(*handle, F_GETFD);
227 fcntl(*handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
231 void Serial::set_baud_rate(unsigned rate)
234 get_state(handle, state);
235 ::set_baud_rate(state, rate);
236 set_state(handle, state);
239 void Serial::set_data_bits(unsigned bits)
242 get_state(handle, state);
243 ::set_data_bits(state, bits);
244 set_state(handle, state);
247 void Serial::set_parity(Parity par)
250 get_state(handle, state);
251 ::set_parity(state, par);
252 set_state(handle, state);
255 void Serial::set_stop_bits(unsigned bits)
258 get_state(handle, state);
259 ::set_stop_bits(state, bits);
260 set_state(handle, state);
263 void Serial::set_parameters(const string ¶ms)
266 for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
267 if(i+4!=params.size() || params[i]!=',')
268 throw invalid_argument("Serial::set_parameters");
269 if(params[i+1]<'5' || params[i+1]>'8')
270 throw invalid_argument("Serial::set_parameters data_bits");
271 if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
272 throw invalid_argument("Serial::set_parameters parity");
273 if(params[i+3]!='1' && params[i+3]!='2')
274 throw invalid_argument("Serial::set_parameters stop_bits");
277 get_state(handle, state);
278 ::set_baud_rate(state, lexical_cast<unsigned>(params.substr(0, i)));
279 ::set_data_bits(state, params[i+1]-'0');
280 ::set_parity(state, (params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
281 ::set_stop_bits(state, params[i+3]-'0');
282 set_state(handle, state);
285 unsigned Serial::do_write(const char *buf, unsigned size)
290 return sys_write(handle, buf, size);
293 unsigned Serial::do_read(char *buf, unsigned size)
298 unsigned ret = reader.read(buf, size);