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 adjust_mode(mode, M_NONBLOCK, !b);
46 sys_set_blocking(handle, b);
49 void Serial::set_inherit(bool i)
51 adjust_mode(mode, M_INHERIT, i);
52 sys_set_inherit(handle, i);
55 void Serial::set_baud_rate(unsigned rate)
58 state.get_from(handle);
59 state.set_baud_rate(rate);
60 state.apply_to(handle);
63 void Serial::set_data_bits(unsigned bits)
66 state.get_from(handle);
67 state.set_data_bits(bits);
68 state.apply_to(handle);
71 void Serial::set_parity(Parity par)
74 state.get_from(handle);
75 state.set_parity(par);
76 state.apply_to(handle);
79 void Serial::set_stop_bits(unsigned bits)
82 state.get_from(handle);
83 state.set_stop_bits(bits);
84 state.apply_to(handle);
87 void Serial::set_parameters(const string ¶ms)
90 for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
91 if(i+4!=params.size() || params[i]!=',')
92 throw invalid_argument("Serial::set_parameters");
93 if(params[i+1]<'5' || params[i+1]>'8')
94 throw invalid_argument("Serial::set_parameters data_bits");
95 if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
96 throw invalid_argument("Serial::set_parameters parity");
97 if(params[i+3]!='1' && params[i+3]!='2')
98 throw invalid_argument("Serial::set_parameters stop_bits");
101 state.get_from(handle);
102 state.set_baud_rate(lexical_cast<unsigned>(params.substr(0, i)));
103 state.set_data_bits(params[i+1]-'0');
104 state.set_parity((params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
105 state.set_stop_bits(params[i+3]-'0');
106 state.apply_to(handle);
109 size_t Serial::do_write(const char *buf, size_t size)
114 return sys_write(handle, buf, size);
117 size_t Serial::do_read(char *buf, size_t size)
122 size_t ret = reader.read(buf, size);
129 const Handle &Serial::get_handle(Mode m)