8 #include <msp/strings/format.h>
9 #include <msp/core/systemerror.h>
17 using namespace Msp::IO;
20 typedef DCB DeviceState;
22 typedef termios DeviceState;
25 void get_state(Handle handle, DeviceState &state)
28 GetCommState(handle, &state);
30 tcgetattr(handle, &state);
34 void set_state(Handle handle, DeviceState &state)
37 if(SetCommState(handle, &state)==0)
38 throw system_error("SetCommState");
40 if(tcsetattr(handle, TCSADRAIN, &state)==-1)
41 throw system_error("tcsetattr");
45 void set_baud_rate(DeviceState &state, unsigned baud)
48 state.BaudRate = baud;
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 invalid_argument("set_baud_rate");
75 cfsetospeed(&state, speed);
76 cfsetispeed(&state, speed);
80 void set_data_bits(DeviceState &state, unsigned bits)
83 state.ByteSize = bits;
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 invalid_argument("set_data_bits");
95 state.c_cflag = (state.c_cflag&~CSIZE)|flag;
99 void set_parity(DeviceState &state, Serial::Parity par)
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 invalid_argument("set_parity");
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 invalid_argument("set_parity");
119 state.c_cflag = (state.c_cflag&~(PARENB|PARODD))|flag;
123 void set_stop_bits(DeviceState &state, unsigned bits)
128 case 1: state.StopBits = ONESTOPBIT; break;
129 case 2: state.StopBits = TWOSTOPBITS; break;
130 default: throw invalid_argument("set_stop_bits");
136 case 1: flag = 0; break;
137 case 2: flag = CSTOPB; break;
138 default: throw invalid_argument("set_stop_bits");
141 state.c_cflag = (state.c_cflag&~CSTOPB)|flag;
151 Serial::Serial(const string &descr)
153 string::size_type comma = descr.find(',');
154 string port = descr.substr(0, comma);
157 port = "\\\\.\\"+port;
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 system_error(format("CreateFile(%s)", port));
162 mode = M_READ|M_WRITE;
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);
172 if(port.compare(0, 5, "/dev/"))
175 handle = open(port.c_str(), O_RDWR);
177 throw system_error(format("open(%s)", port));
178 mode = M_READ|M_WRITE;
181 tcgetattr(handle, &t);
182 t.c_lflag &= ~(ECHO|ICANON);
184 tcsetattr(handle, TCSADRAIN, &t);
187 if(comma!=string::npos)
191 set_parameters(descr.substr(comma+1));
217 void Serial::set_block(bool b)
220 mode = mode|M_NONBLOCK;
222 mode = mode&~M_NONBLOCK;
225 int flags = fcntl(handle, F_GETFD);
226 fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
230 void Serial::set_baud_rate(unsigned rate)
233 get_state(handle, state);
234 ::set_baud_rate(state, rate);
235 set_state(handle, state);
238 void Serial::set_data_bits(unsigned bits)
241 get_state(handle, state);
242 ::set_data_bits(state, bits);
243 set_state(handle, state);
246 void Serial::set_parity(Parity par)
249 get_state(handle, state);
250 ::set_parity(state, par);
251 set_state(handle, state);
254 void Serial::set_stop_bits(unsigned bits)
257 get_state(handle, state);
258 ::set_stop_bits(state, bits);
259 set_state(handle, state);
262 void Serial::set_parameters(const string ¶ms)
265 for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
266 if(i+4!=params.size() || params[i]!=',')
267 throw invalid_argument("Serial::set_parameters");
268 if(params[i+1]<'5' || params[i+1]>'8')
269 throw invalid_argument("Serial::set_parameters data_bits");
270 if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
271 throw invalid_argument("Serial::set_parameters parity");
272 if(params[i+3]!='1' && params[i+3]!='2')
273 throw invalid_argument("Serial::set_parameters stop_bits");
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);
284 unsigned Serial::do_write(const char *buf, unsigned size)
291 if(WriteFile(handle, buf, size, &ret, 0)==0)
292 throw system_error("WriteFile");
294 int ret = ::write(handle, buf, size);
300 throw system_error("write");
307 unsigned Serial::do_read(char *buf, unsigned size)
314 if(ReadFile(handle, buf, size, &ret, 0)==0)
315 throw system_error("ReadFile");
317 int ret = ::read(handle, buf, size);
323 throw system_error("read");
330 Handle Serial::get_event_handle()
333 throw logic_error("Serial port events not supported on win32 yet");