]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/serial.cpp
Add FILE_FLAG_OVERLAPPED in a couple of places
[libs/core.git] / source / io / serial.cpp
index ec6a198bee3ac0d6d3568a9c218bac0df0f7aa4f..2ed11ae9fddd900b4ab695a2acc05770deaf5e18 100644 (file)
@@ -5,8 +5,9 @@
 #include <fcntl.h>
 #include <errno.h>
 #endif
-#include <msp/strings/formatter.h>
-#include "except.h"
+#include <msp/strings/format.h>
+#include <msp/core/systemerror.h>
+#include "handle_private.h"
 #include "serial.h"
 
 using namespace std;
@@ -22,23 +23,23 @@ typedef DCB DeviceState;
 typedef termios DeviceState;
 #endif
 
-void get_state(Handle handle, DeviceState &state)
+void get_state(const Handle &handle, DeviceState &state)
 {
 #ifdef WIN32
-       GetCommState(handle, &state);
+       GetCommState(*handle, &state);
 #else
-       tcgetattr(handle, &state);
+       tcgetattr(*handle, &state);
 #endif
 }
 
-void set_state(Handle handle, DeviceState &state)
+void set_state(const Handle &handle, DeviceState &state)
 {
 #ifdef WIN32
-       if(SetCommState(handle, &state)==0)
-               throw SystemError("Cannot set serial port parameters", GetLastError());
+       if(SetCommState(*handle, &state)==0)
+               throw system_error("SetCommState");
 #else
-       if(tcsetattr(handle, TCSADRAIN, &state)==-1)
-               throw SystemError("Cannot set serial port parameters", errno);
+       if(tcsetattr(*handle, TCSADRAIN, &state)==-1)
+               throw system_error("tcsetattr");
 #endif
 }
 
@@ -69,7 +70,7 @@ void set_baud_rate(DeviceState &state, unsigned baud)
        case 57600:  speed = B57600; break;
        case 115200: speed = B115200; break;
        case 230400: speed = B230400; break;
-       default: throw InvalidParameterValue("Invalid baud rate");
+       default: throw invalid_argument("set_baud_rate");
        }
 
        cfsetospeed(&state, speed);
@@ -89,7 +90,7 @@ void set_data_bits(DeviceState &state, unsigned bits)
        case 6: flag = CS6; break;
        case 7: flag = CS7; break;
        case 8: flag = CS8; break;
-       default: throw InvalidParameterValue("Invalid data bit count");
+       default: throw invalid_argument("set_data_bits");
        }
 
        state.c_cflag = (state.c_cflag&~CSIZE)|flag;
@@ -104,7 +105,7 @@ void set_parity(DeviceState &state, Serial::Parity par)
        case Serial::NONE: state.Parity = NOPARITY; break;
        case Serial::EVEN: state.Parity = EVENPARITY; break;
        case Serial::ODD:  state.Parity = ODDPARITY; break;
-       default: throw InvalidParameterValue("Invalid parity");
+       default: throw invalid_argument("set_parity");
        }
 #else
        tcflag_t flag;
@@ -113,7 +114,7 @@ void set_parity(DeviceState &state, Serial::Parity par)
        case Serial::NONE: flag = 0; break;
        case Serial::EVEN: flag = PARENB; break;
        case Serial::ODD:  flag = PARENB|PARODD; break;
-       default: throw InvalidParameterValue("Invalid parity");
+       default: throw invalid_argument("set_parity");
        }
 
        state.c_cflag = (state.c_cflag&~(PARENB|PARODD))|flag;
@@ -127,7 +128,7 @@ void set_stop_bits(DeviceState &state, unsigned bits)
        {
        case 1: state.StopBits = ONESTOPBIT; break;
        case 2: state.StopBits = TWOSTOPBITS; break;
-       default: throw InvalidParameterValue("Invalid stop bit count");
+       default: throw invalid_argument("set_stop_bits");
        }
 #else
        tcflag_t flag;
@@ -135,7 +136,7 @@ void set_stop_bits(DeviceState &state, unsigned bits)
        {
        case 1: flag = 0; break;
        case 2: flag = CSTOPB; break;
-       default: throw InvalidParameterValue("Invalid stop bit count");
+       default: throw invalid_argument("set_stop_bits");
        }
 
        state.c_cflag = (state.c_cflag&~CSTOPB)|flag;
@@ -148,7 +149,8 @@ void set_stop_bits(DeviceState &state, unsigned bits)
 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);
@@ -156,9 +158,9 @@ 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);
-       if(handle==INVALID_HANDLE_VALUE)
-               throw SystemError(format("Can't open serial port '%s'", port), GetLastError());
+       *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;
 
        COMMTIMEOUTS timeouts;
@@ -167,21 +169,21 @@ Serial::Serial(const string &descr)
        timeouts.ReadTotalTimeoutConstant = MAXDWORD-1;
        timeouts.WriteTotalTimeoutMultiplier = 0;
        timeouts.WriteTotalTimeoutConstant = 0;
-       SetCommTimeouts(handle, &timeouts);
+       SetCommTimeouts(*handle, &timeouts);
 #else
        if(port.compare(0, 5, "/dev/"))
                port = "/dev/"+port;
 
-       handle = open(port.c_str(), O_RDWR);
-       if(handle==-1)
-               throw SystemError(format("Can't open serial port '%s'", port), errno);
+       *handle = open(port.c_str(), O_RDWR);
+       if(!handle)
+               throw system_error(format("open(%s)", port));
        mode = M_READ|M_WRITE;
 
        termios t;
-       tcgetattr(handle, &t);
+       tcgetattr(*handle, &t);
        t.c_lflag &= ~(ECHO|ICANON);
        t.c_oflag &= ~OPOST;
-       tcsetattr(handle, TCSADRAIN, &t);
+       tcsetattr(*handle, TCSADRAIN, &t);
 #endif
 
        if(comma!=string::npos)
@@ -207,11 +209,7 @@ Serial::~Serial()
 
 void Serial::close()
 {
-#ifdef WIN32
-       CloseHandle(handle);
-#else
-       ::close(handle);
-#endif
+       sys_close(handle);
 }
 
 void Serial::set_block(bool b)
@@ -222,8 +220,8 @@ void Serial::set_block(bool b)
                mode = mode&~M_NONBLOCK;
 
 #ifndef WIN32
-       int flags = fcntl(handle, F_GETFD);
-       fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
+       int flags = fcntl(*handle, F_GETFD);
+       fcntl(*handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
 #endif
 }
 
@@ -264,13 +262,13 @@ void Serial::set_parameters(const string &params)
        unsigned i;
        for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
        if(i+4!=params.size() || params[i]!=',')
-               throw InvalidParameterValue("Invalid parameter string");
+               throw invalid_argument("Serial::set_parameters");
        if(params[i+1]<'5' || params[i+1]>'8')
-               throw InvalidParameterValue("Invalid data bit count");
+               throw invalid_argument("Serial::set_parameters data_bits");
        if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
-               throw InvalidParameterValue("Invalid parity");
+               throw invalid_argument("Serial::set_parameters parity");
        if(params[i+3]!='1' && params[i+3]!='2')
-               throw InvalidParameterValue("Invalid stop bit count");
+               throw invalid_argument("Serial::set_parameters stop_bits");
 
        DeviceState state;
        get_state(handle, state);
@@ -286,22 +284,7 @@ unsigned Serial::do_write(const char *buf, unsigned size)
        if(size==0)
                return 0;
 
-#ifdef WIN32
-       DWORD ret;
-       if(WriteFile(handle, buf, size, &ret, 0)==0)
-               throw SystemError("Writing to serial port failed", GetLastError());
-#else
-       int ret = ::write(handle, buf, size);
-       if(ret==-1)
-       {
-               if(errno==EAGAIN)
-                       return 0;
-               else
-                       throw SystemError("Writing to serial port failed", errno);
-       }
-#endif
-
-       return ret;
+       return sys_write(handle, buf, size);
 }
 
 unsigned Serial::do_read(char *buf, unsigned size)
@@ -309,32 +292,12 @@ unsigned Serial::do_read(char *buf, unsigned size)
        if(size==0)
                return 0;
 
-#ifdef WIN32
-       DWORD ret;
-       if(ReadFile(handle, buf, size, &ret, 0)==0)
-               throw SystemError("Reading from serial port failed", GetLastError());
-#else
-       int ret = ::read(handle, buf, size);
-       if(ret==-1)
-       {
-               if(errno==EAGAIN)
-                       return 0;
-               else
-                       throw SystemError("Reading from serial port failed", errno);
-       }
-#endif
+       unsigned ret = reader.read(buf, size);
+       if(ret==0)
+               set_eof();
 
        return ret;
 }
 
-Handle Serial::get_event_handle()
-{
-#ifdef WIN32
-       throw Exception("Serial port events not supported on win32 yet");
-#else
-       return handle;
-#endif
-}
-
 } // namespace IO
 } // namespace Msp