]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/serial.cpp
Tell the TTY driver to not mess with serial port data
[libs/core.git] / source / io / serial.cpp
index 9df797109d840cc3554136035410977926bc802e..26ef92ed9015ae4f04aa1f96670a38f27dfcacfc 100644 (file)
@@ -149,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);
@@ -157,7 +158,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 +181,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
 
@@ -208,11 +212,7 @@ Serial::~Serial()
 
 void Serial::close()
 {
-#ifdef WIN32
-       CloseHandle(*handle);
-#else
-       ::close(*handle);
-#endif
+       sys_close(handle);
 }
 
 void Serial::set_block(bool b)
@@ -287,22 +287,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 system_error("WriteFile");
-#else
-       int ret = ::write(*handle, buf, size);
-       if(ret==-1)
-       {
-               if(errno==EAGAIN)
-                       return 0;
-               else
-                       throw system_error("write");
-       }
-#endif
-
-       return ret;
+       return sys_write(handle, buf, size);
 }
 
 unsigned Serial::do_read(char *buf, unsigned size)
@@ -310,32 +295,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 system_error("ReadFile");
-#else
-       int ret = ::read(*handle, buf, size);
-       if(ret==-1)
-       {
-               if(errno==EAGAIN)
-                       return 0;
-               else
-                       throw system_error("read");
-       }
-#endif
+       unsigned ret = reader.read(buf, size);
+       if(ret==0)
+               set_eof();
 
        return ret;
 }
 
-const Handle &Serial::get_event_handle()
-{
-#ifdef WIN32
-       throw logic_error("Serial port events not supported on win32 yet");
-#else
-       return handle;
-#endif
-}
-
 } // namespace IO
 } // namespace Msp