]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/serial.cpp
Encapsulate serial device state into a struct
[libs/core.git] / source / io / serial.cpp
index 74af366856cd6ceaf99866e00d977e2108c1803c..dc623e29cc47b82426816fa780a744f474c89eec 100644 (file)
 
 using namespace std;
 
-namespace {
-
-using namespace Msp;
-using namespace Msp::IO;
+namespace Msp {
+namespace IO {
 
+struct Serial::DeviceState
+{
 #ifdef WIN32
-typedef DCB DeviceState;
+       DCB state;
 #else
-typedef termios DeviceState;
+       termios state;
 #endif
 
-void get_state(const Handle &handle, DeviceState &state)
+       void get_from(const Handle &);
+       void apply_to(const Handle &);
+       void set_baud_rate(unsigned);
+       void set_data_bits(unsigned);
+       void set_parity(Parity);
+       void set_stop_bits(unsigned);
+};
+
+void Serial::DeviceState::get_from(const Handle &handle)
 {
 #ifdef WIN32
        GetCommState(*handle, &state);
@@ -32,7 +40,7 @@ void get_state(const Handle &handle, DeviceState &state)
 #endif
 }
 
-void set_state(const Handle &handle, DeviceState &state)
+void Serial::DeviceState::apply_to(const Handle &handle)
 {
 #ifdef WIN32
        if(SetCommState(*handle, &state)==0)
@@ -43,7 +51,7 @@ void set_state(const Handle &handle, DeviceState &state)
 #endif
 }
 
-void set_baud_rate(DeviceState &state, unsigned baud)
+void Serial::DeviceState::set_baud_rate(unsigned baud)
 {
 #ifdef WIN32
        state.BaudRate = baud;
@@ -78,7 +86,7 @@ void set_baud_rate(DeviceState &state, unsigned baud)
 #endif
 }
 
-void set_data_bits(DeviceState &state, unsigned bits)
+void Serial::DeviceState::set_data_bits(unsigned bits)
 {
 #ifdef WIN32
        state.ByteSize = bits;
@@ -97,7 +105,7 @@ void set_data_bits(DeviceState &state, unsigned bits)
 #endif
 }
 
-void set_parity(DeviceState &state, Serial::Parity par)
+void Serial::DeviceState::set_parity(Serial::Parity par)
 {
 #ifdef WIN32
        switch(par)
@@ -121,7 +129,7 @@ void set_parity(DeviceState &state, Serial::Parity par)
 #endif
 }
 
-void set_stop_bits(DeviceState &state, unsigned bits)
+void Serial::DeviceState::set_stop_bits(unsigned bits)
 {
 #ifdef WIN32
        switch(bits)
@@ -143,13 +151,9 @@ void set_stop_bits(DeviceState &state, unsigned bits)
 #endif
 }
 
-}
-
 
-namespace Msp {
-namespace IO {
-
-Serial::Serial(const string &descr)
+Serial::Serial(const string &descr):
+       reader(handle, 1024)
 {
        string::size_type comma = descr.find(',');
        string port = descr.substr(0, comma);
@@ -157,7 +161,7 @@ Serial::Serial(const string &descr)
 #ifdef WIN32
        port = "\\\\.\\"+port;
 
-       *handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+       *handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
        if(!handle)
                throw system_error(format("CreateFile(%s)", port));
        mode = M_READ|M_WRITE;
@@ -180,8 +184,11 @@ Serial::Serial(const string &descr)
 
        termios t;
        tcgetattr(*handle, &t);
-       t.c_lflag &= ~(ECHO|ICANON);
-       t.c_oflag &= ~OPOST;
+       t.c_iflag &= ~(ISTRIP|INLCR|IGNCR|ICRNL|IXON);
+       t.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
+       t.c_oflag &= ~(OPOST|OCRNL|ONOCR|ONLRET);
+       t.c_cc[VMIN] = 1;
+       t.c_cc[VTIME] = 0;
        tcsetattr(*handle, TCSADRAIN, &t);
 #endif
 
@@ -227,33 +234,33 @@ void Serial::set_block(bool b)
 void Serial::set_baud_rate(unsigned rate)
 {
        DeviceState state;
-       get_state(handle, state);
-       ::set_baud_rate(state, rate);
-       set_state(handle, state);
+       state.get_from(handle);
+       state.set_baud_rate(rate);
+       state.apply_to(handle);
 }
 
 void Serial::set_data_bits(unsigned bits)
 {
        DeviceState state;
-       get_state(handle, state);
-       ::set_data_bits(state, bits);
-       set_state(handle, state);
+       state.get_from(handle);
+       state.set_data_bits(bits);
+       state.apply_to(handle);
 }
 
 void Serial::set_parity(Parity par)
 {
        DeviceState state;
-       get_state(handle, state);
-       ::set_parity(state, par);
-       set_state(handle, state);
+       state.get_from(handle);
+       state.set_parity(par);
+       state.apply_to(handle);
 }
 
 void Serial::set_stop_bits(unsigned bits)
 {
        DeviceState state;
-       get_state(handle, state);
-       ::set_stop_bits(state, bits);
-       set_state(handle, state);
+       state.get_from(handle);
+       state.set_stop_bits(bits);
+       state.apply_to(handle);
 }
 
 void Serial::set_parameters(const string &params)
@@ -270,12 +277,12 @@ void Serial::set_parameters(const string &params)
                throw invalid_argument("Serial::set_parameters stop_bits");
 
        DeviceState state;
-       get_state(handle, state);
-       ::set_baud_rate(state, lexical_cast<unsigned>(params.substr(0, i)));
-       ::set_data_bits(state, params[i+1]-'0');
-       ::set_parity(state, (params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
-       ::set_stop_bits(state, params[i+3]-'0');
-       set_state(handle, state);
+       state.get_from(handle);
+       state.set_baud_rate(lexical_cast<unsigned>(params.substr(0, i)));
+       state.set_data_bits(params[i+1]-'0');
+       state.set_parity((params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE));
+       state.set_stop_bits(params[i+3]-'0');
+       state.apply_to(handle);
 }
 
 unsigned Serial::do_write(const char *buf, unsigned size)
@@ -291,16 +298,11 @@ unsigned Serial::do_read(char *buf, unsigned size)
        if(size==0)
                return 0;
 
-       return sys_read(handle, buf, size);
-}
+       unsigned ret = reader.read(buf, size);
+       if(ret==0)
+               set_eof();
 
-const Handle &Serial::get_event_handle()
-{
-#ifdef WIN32
-       throw logic_error("Serial port events not supported on win32 yet");
-#else
-       return handle;
-#endif
+       return ret;
 }
 
 } // namespace IO