1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Working on pimpl implementation of serial.

This commit is contained in:
William Woodall 2012-01-10 15:08:57 -06:00
parent 18284ae764
commit c8e7841223
4 changed files with 168 additions and 1 deletions

1
.gitignore vendored
View File

@ -7,7 +7,6 @@
*.pyc
*.pyo
*.zip
*.cc
*/files/*
*/tmp/*
*.hwm*

View File

@ -83,6 +83,9 @@ public:
void setFlowcontrol (flowcontrol_t flowcontrol);
flowcontrol_t getFlowcontrol () const;
private:
int fd;
};
}

134
src/serial.cc Normal file
View File

@ -0,0 +1,134 @@
#include "serial/serial.h"
#ifdef _WIN32
#include "serial/impl/win.h"
#else
#include "serial/impl/unix.h"
#endif
Serial::Serial (const std::string &port, int baudrate,
long timeout, bytesize_t bytesize,
parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol)
: impl(new Serial_pimpl(port,baudrate,timeout,bytesize,parity,stopbits,
flowcontrol))
{
}
Serial::~Serial () {
delete impl;
}
void
Serial::open () {
this->impl->open ();
}
void
Serial::close () {
this->impl->close ();
}
bool
Serial::isOpen () {
return this->impl->isOpen ();
}
size_t
Serial::read (unsigned char* buffer, size_t size = 1) {
return this->impl->read (buffer, size);
}
std::string
Serial::read (size_t size = 1) {
return this->impl->read (size);
}
size_t
Serial::read (std::string &buffer, size_t size = 1) {
return this->impl->read (buffer, size);
}
size_t
Serial::write (unsigned char* data, size_t length) {
return this->impl->write (data, length);
}
size_t
Serial::write (const std::string &data) {
return this->impl->write (data);
}
void
Serial::setPort (const std::string &port) {
this->impl->setPort (port);
}
std::string
Serial::getPort () const {
return this->impl->getPort ();
}
void
Serial::setTimeout (long timeout) {
this->impl->setTimeout (timeout);
}
long
Serial::getTimeout () const {
return this->impl->getTimeout ();
}
void
Serial::setBaudrate (int baudrate) {
this->impl->setBaudrate (baudrate);
}
int
Serial::getBaudrate () const {
return this->impl->getBaudrate ();
}
void
Serial::setBytesize (bytesize_t bytesize) {
this->impl->setBytesize (bytesize);
}
bytesize_t
Serial::getBytesize () const {
return this->impl->getBytesize ();
}
void
Serial::setParity (parity_t parity) {
this->impl->setParity (parity);
}
parity_t
Serial::getParity () const {
return this->impl->getParity ();
}
void
Serial::setStopbits (stopbits_t stopbits) {
this->impl->setStopbits (stopbits);
}
stopbits_t
Serial::getStopbits () const {
return this->impl->getStopbits ();
}
void
Serial::setFlowcontrol (flowcontrol_t flowcontrol) {
this->impl->setFlowcontrol (flowcontrol);
}
flowcontrol_t
Serial::getFlowcontrol () const {
return this->impl->getFlowcontrol ();
}

View File

@ -0,0 +1,31 @@
#include <iostream>
#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
void
_delimeter_tokenizer (std::string &data, std::vector<std::string> &tokens,
std::string delimeter)
{
boost::split(tokens, data, boost::is_any_of(delimeter));
}
typedef boost::function<void(std::string&,std::vector<std::string>&)> TokenizerType;
int main(void) {
std::string data = "a\rb\rc\r";
std::vector<std::string> tokens;
std::string delimeter = "\r";
TokenizerType f = boost::bind(_delimeter_tokenizer, _1, _2, delimeter);
f(data, tokens);
BOOST_FOREACH(std::string token, tokens)
std::cout << token << std::endl;
return 0;
}