X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Fserial.cpp;h=5d335b6faa82e0226c043d14ba3a158e8a84d8ae;hp=dbfee955f9ae85b954daf1ff11c9c689c8476d92;hb=d85465dd66a6fc80a9fb7868d1193a86abfe5b7c;hpb=8f2711fba7a2817840038630d9cf9a2060ecbe8e diff --git a/source/io/serial.cpp b/source/io/serial.cpp index dbfee95..5d335b6 100644 --- a/source/io/serial.cpp +++ b/source/io/serial.cpp @@ -5,8 +5,9 @@ #include #include #endif +#include #include -#include +#include "handle_private.h" #include "serial.h" using namespace std; @@ -22,22 +23,22 @@ 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) + if(SetCommState(*handle, &state)==0) throw system_error("SetCommState"); #else - if(tcsetattr(handle, TCSADRAIN, &state)==-1) + if(tcsetattr(*handle, TCSADRAIN, &state)==-1) throw system_error("tcsetattr"); #endif } @@ -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,8 +158,8 @@ 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) + *handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + if(!handle) throw system_error(format("CreateFile(%s)", port)); mode = M_READ|M_WRITE; @@ -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) + *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 } @@ -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 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) @@ -309,31 +292,7 @@ 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 - - return ret; -} - -Handle Serial::get_event_handle() -{ -#ifdef WIN32 - throw logic_error("Serial port events not supported on win32 yet"); -#else - return handle; -#endif + return reader.read(buf, size); } } // namespace IO