2 #include "serial_private.h"
9 Serial::Serial(const string &descr):
12 string::size_type comma = descr.find(',');
13 string port = descr.substr(0, comma);
17 if(comma!=string::npos)
21 set_parameters(descr.substr(comma+1));
43 void Serial::set_block(bool b)
45 mode = b?(mode&~M_NONBLOCK):(mode|M_NONBLOCK);
46 sys_set_blocking(handle, b);
49 void Serial::set_baud_rate(unsigned rate)
52 state.get_from(handle);
53 state.set_baud_rate(rate);
54 state.apply_to(handle);
57 void Serial::set_data_bits(unsigned bits)
60 state.get_from(handle);
61 state.set_data_bits(bits);
62 state.apply_to(handle);
65 void Serial::set_parity(Parity par)
68 state.get_from(handle);
69 state.set_parity(par);
70 state.apply_to(handle);
73 void Serial::set_stop_bits(unsigned bits)
76 state.get_from(handle);
77 state.set_stop_bits(bits);
78 state.apply_to(handle);
81 void Serial::set_parameters(const string ¶ms)
84 for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
85 if(i+4!=params.size() || params[i]!=',')
86 throw invalid_argument("Serial::set_parameters");
87 if(params[i+1]<'5' || params[i+1]>'8')
88 throw invalid_argument("Serial::set_parameters data_bits");
89 if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
90 throw invalid_argument("Serial::set_parameters parity");
91 if(params[i+3]!='1' && params[i+3]!='2')
92 throw invalid_argument("Serial::set_parameters stop_bits");
95 state.get_from(handle);
96 state.set_baud_rate(lexical_cast<unsigned>(params.substr(0, i)));
97 state.set_data_bits(params[i+1]-'0');
98 state.set_parity((params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
99 state.set_stop_bits(params[i+3]-'0');
100 state.apply_to(handle);
103 unsigned Serial::do_write(const char *buf, unsigned size)
108 return sys_write(handle, buf, size);
111 unsigned Serial::do_read(char *buf, unsigned size)
116 unsigned ret = reader.read(buf, size);