From 463240ea92d65693f76c0e0991c322b3c4527ffe Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 9 Apr 2010 08:23:48 +0000 Subject: [PATCH] Add a class for serial port communications No win32 support yet --- source/serial.cpp | 269 ++++++++++++++++++++++++++++++++++++++++++++++ source/serial.h | 55 ++++++++++ 2 files changed, 324 insertions(+) create mode 100644 source/serial.cpp create mode 100644 source/serial.h diff --git a/source/serial.cpp b/source/serial.cpp new file mode 100644 index 0000000..e6eff51 --- /dev/null +++ b/source/serial.cpp @@ -0,0 +1,269 @@ +/* $Id$ + +This file is part of libmspio +Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#include +#include +#include +#include "except.h" +#include "serial.h" + +using namespace std; + +namespace { + +using namespace Msp; +using namespace Msp::IO; + +#ifndef WIN32 +void set_baud_rate(termios &t, unsigned baud) +{ + speed_t speed; + switch(baud) + { + case 0: speed = B0; break; + case 50: speed = B50; break; + case 75: speed = B75; break; + case 110: speed = B110; break; + case 134: speed = B134; break; + case 150: speed = B150; break; + case 200: speed = B200; break; + case 300: speed = B300; break; + case 600: speed = B600; break; + case 1200: speed = B1200; break; + case 1800: speed = B1800; break; + case 2400: speed = B2400; break; + case 4800: speed = B4800; break; + case 9600: speed = B9600; break; + case 19200: speed = B19200; break; + case 38400: speed = B38400; break; + case 57600: speed = B57600; break; + case 115200: speed = B115200; break; + case 230400: speed = B230400; break; + default: throw InvalidParameterValue("Invalid baud rate"); + } + + cfsetospeed(&t, speed); + cfsetispeed(&t, speed); +} + +void set_data_bits(termios &t, unsigned bits) +{ + tcflag_t flag; + switch(bits) + { + case 5: flag = CS5; break; + case 6: flag = CS6; break; + case 7: flag = CS7; break; + case 8: flag = CS8; break; + default: throw InvalidParameterValue("Invalid data bit count"); + } + + t.c_cflag = (t.c_cflag&~CSIZE)|flag; +} + +void set_parity(termios &t, Serial::Parity par) +{ + tcflag_t flag; + switch(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"); + } + + t.c_cflag = (t.c_cflag&~(PARENB|PARODD))|flag; +} + +void set_stop_bits(termios &t, unsigned bits) +{ + tcflag_t flag; + switch(bits) + { + case 1: flag = 0; break; + case 2: flag = CSTOPB; break; + default: throw InvalidParameterValue("Invalid stop bit count"); + } + + t.c_cflag = (t.c_cflag&~CSTOPB)|flag; +} +#endif + +} + + +namespace Msp { +namespace IO { + +Serial::Serial(const string &descr) +{ + string::size_type comma = descr.find(','); + string port = descr.substr(0, comma); + +#ifndef WIN32 + 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); + mode = M_READ|M_WRITE; + + termios t; + tcgetattr(handle, &t); + t.c_lflag &= ~(ECHO|ICANON); + tcsetattr(handle, TCSADRAIN, &t); +#else + throw Exception("Serial ports not supported in win32 yet"); +#endif + + if(comma!=string::npos) + { + try + { + set_parameters(descr.substr(comma+1)); + } + catch(...) + { + close(); + throw; + } + } + + set_events(P_INPUT); +} + +Serial::~Serial() +{ + close(); +} + +void Serial::set_block(bool b) +{ + if(b) + mode = mode|M_NONBLOCK; + else + mode = mode&~M_NONBLOCK; + +#ifndef WIN32 + int flags = fcntl(handle, F_GETFD); + fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); +#endif +} + +void Serial::set_baud_rate(unsigned rate) +{ +#ifndef WIN32 + termios t; + tcgetattr(handle, &t); + ::set_baud_rate(t, rate); + tcsetattr(handle, TCSADRAIN, &t); +#endif +} + +void Serial::set_data_bits(unsigned bits) +{ +#ifndef WIN32 + termios t; + tcgetattr(handle, &t); + ::set_data_bits(t, bits); + tcsetattr(handle, TCSADRAIN, &t); +#endif +} + +void Serial::set_parity(Parity par) +{ +#ifndef WIN32 + termios t; + tcgetattr(handle, &t); + ::set_parity(t, par); + tcsetattr(handle, TCSADRAIN, &t); +#endif +} + +void Serial::set_stop_bits(unsigned bits) +{ +#ifndef WIN32 + termios t; + tcgetattr(handle, &t); + ::set_stop_bits(t, bits); + tcsetattr(handle, TCSADRAIN, &t); +#endif +} + +void Serial::set_parameters(const string ¶ms) +{ + unsigned i; + for(i=0; i'8') + throw InvalidParameterValue("Invalid data bit count"); + if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O') + throw InvalidParameterValue("Invalid parity"); + if(params[i+3]!='1' && params[i+3]!='2') + throw InvalidParameterValue("Invalid stop bit count"); + +#ifndef WIN32 + termios t; + tcgetattr(handle, &t); + ::set_baud_rate(t, lexical_cast(params.substr(0, i))); + ::set_data_bits(t, params[i+1]-'0'); + ::set_parity(t, (params[i+2]=='E' ? EVEN : params[i+2]=='O' ? ODD : NONE)); + ::set_stop_bits(t, params[i+3]-'0'); + tcsetattr(handle, TCSADRAIN, &t); +#endif +} + +void Serial::close() +{ +#ifndef WIN32 + ::close(handle); +#endif +} + +unsigned Serial::do_write(const char *buf, unsigned size) +{ + if(size==0) + return 0; + +#ifndef WIN32 + 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; +} + +unsigned Serial::do_read(char *buf, unsigned size) +{ + if(size==0) + return 0; + +#ifndef WIN32 + int ret = ::read(handle, buf, size); + if(ret==-1) + { + if(errno==EAGAIN) + return 0; + else + throw SystemError("Reading from serial port failed", errno); + } +#endif + + return ret; +} + +} // namespace IO +} // namespace Msp diff --git a/source/serial.h b/source/serial.h new file mode 100644 index 0000000..0e87361 --- /dev/null +++ b/source/serial.h @@ -0,0 +1,55 @@ +/* $Id$ + +This file is part of libmspio +Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_IO_SERIAL_H_ +#define MSP_IO_SERIAL_H_ + +#include "base.h" + +namespace Msp { +namespace IO { + +class Serial: public Base +{ +public: + enum Parity + { + NONE, + EVEN, + ODD + }; + +private: + struct Private; + + Handle handle; + Private *priv; + +public: + Serial(const std::string &); + virtual ~Serial(); + + virtual void set_block(bool); + + void set_baud_rate(unsigned); + void set_data_bits(unsigned); + void set_parity(Parity); + void set_stop_bits(unsigned); + void set_parameters(const std::string &); + + virtual Handle get_event_handle() { return handle; } + +private: + void close(); + virtual unsigned do_write(const char *, unsigned); + virtual unsigned do_read(char *, unsigned); +}; + +} // namespace IO +} // namespace Msp + +#endif -- 2.45.2