mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
Implemented write, readline, readlines and available
This commit is contained in:
parent
0a6aabe719
commit
f14ac390bf
@ -105,6 +105,7 @@ private:
|
||||
bool isOpen_;
|
||||
|
||||
int interCharTimeout_;
|
||||
int writeTimeout_;
|
||||
int xonxoff_;
|
||||
int rtscts_;
|
||||
|
||||
|
||||
@ -39,12 +39,8 @@
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#ifdef CXX_11
|
||||
#include <memory>
|
||||
#else
|
||||
// #include <tr1/boost_shared_ptr.h>
|
||||
#endif
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
|
||||
namespace serial {
|
||||
|
||||
@ -159,6 +155,11 @@ public:
|
||||
void
|
||||
close ();
|
||||
|
||||
/* Return the number of characters in the buffer.
|
||||
*/
|
||||
size_t
|
||||
available();
|
||||
|
||||
/*! Read a given amount of bytes from the serial port.
|
||||
*
|
||||
* If a timeout is set it may return less characters than requested. With
|
||||
@ -172,8 +173,8 @@ public:
|
||||
*
|
||||
* \return A size_t representing the number of bytes actually read.
|
||||
*/
|
||||
size_t
|
||||
read (unsigned char* buffer, size_t size = 1);
|
||||
//size_t
|
||||
//read (unsigned char* buffer, size_t size = 1);
|
||||
|
||||
/*! Read a given amount of bytes from the serial port.
|
||||
*
|
||||
@ -188,6 +189,9 @@ public:
|
||||
std::string
|
||||
read (size_t size = 1);
|
||||
|
||||
std::string readline(size_t size = std::numeric_limits<std::size_t>::max(), std::string eol = "\n");
|
||||
std::vector<std::string> readlines(std::string eol = "\n");
|
||||
|
||||
/*! Read a given amount of bytes from the serial port.
|
||||
*
|
||||
* Reads into a std::string by reference rather than returning it.
|
||||
@ -199,8 +203,8 @@ public:
|
||||
*
|
||||
* \see Serial::read(size_t)
|
||||
*/
|
||||
size_t
|
||||
read (std::string &buffer, size_t size = 1);
|
||||
//size_t
|
||||
//read (std::string &buffer, size_t size = 1);
|
||||
|
||||
/*! Write bytes from the data to the serial port by given length.
|
||||
*
|
||||
@ -211,8 +215,8 @@ public:
|
||||
*
|
||||
* \return A size_t representing the number of bytes actually written.
|
||||
*/
|
||||
size_t
|
||||
write (unsigned char* data, size_t length);
|
||||
//size_t
|
||||
//write (unsigned char* data, size_t length);
|
||||
|
||||
/*! Write a string to the serial port.
|
||||
*
|
||||
|
||||
@ -219,8 +219,8 @@ Serial::SerialImpl::read (size_t size) {
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(fd_, &readfds);
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = timeout_ / 1000000;
|
||||
timeout.tv_usec = timeout_ % 1000000;
|
||||
timeout.tv_sec = timeout_ / 1000;
|
||||
timeout.tv_usec = timeout_ % 1000;
|
||||
int r = select(1, &readfds, NULL, NULL, &timeout);
|
||||
|
||||
if (r == -1 && errno == EINTR)
|
||||
@ -253,7 +253,15 @@ Serial::SerialImpl::read (size_t size) {
|
||||
|
||||
size_t
|
||||
Serial::SerialImpl::write (const string &data) {
|
||||
|
||||
if (isOpen_ == false) {
|
||||
throw "portNotOpenError";
|
||||
}
|
||||
size_t t = data.length();
|
||||
size_t n = ::write(fd_, data.c_str(), data.length());
|
||||
if (n == -1) {
|
||||
throw "Write error";
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void
|
||||
@ -268,12 +276,12 @@ Serial::SerialImpl::getPort () const {
|
||||
|
||||
void
|
||||
Serial::SerialImpl::setTimeout (long timeout) {
|
||||
|
||||
timeout_ = timeout;
|
||||
}
|
||||
|
||||
long
|
||||
Serial::SerialImpl::getTimeout () const {
|
||||
|
||||
return timeout_;
|
||||
}
|
||||
|
||||
void
|
||||
@ -289,42 +297,42 @@ Serial::SerialImpl::getBaudrate () const {
|
||||
|
||||
void
|
||||
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
|
||||
|
||||
bytesize_ = bytesize;
|
||||
}
|
||||
|
||||
serial::bytesize_t
|
||||
Serial::SerialImpl::getBytesize () const {
|
||||
|
||||
return bytesize_;
|
||||
}
|
||||
|
||||
void
|
||||
Serial::SerialImpl::setParity (serial::parity_t parity) {
|
||||
|
||||
parity_ = parity;
|
||||
}
|
||||
|
||||
serial::parity_t
|
||||
Serial::SerialImpl::getParity () const {
|
||||
|
||||
return parity_;
|
||||
}
|
||||
|
||||
void
|
||||
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
|
||||
|
||||
stopbits_ = stopbits;
|
||||
}
|
||||
|
||||
serial::stopbits_t
|
||||
Serial::SerialImpl::getStopbits () const {
|
||||
|
||||
return stopbits_;
|
||||
}
|
||||
|
||||
void
|
||||
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
|
||||
|
||||
flowcontrol_ = flowcontrol;
|
||||
}
|
||||
|
||||
serial::flowcontrol_t
|
||||
Serial::SerialImpl::getFlowcontrol () const {
|
||||
|
||||
return flowcontrol_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@ using serial::parity_t;
|
||||
using serial::stopbits_t;
|
||||
using serial::flowcontrol_t;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
Serial::Serial (const string &port, int baudrate,
|
||||
long timeout, bytesize_t bytesize,
|
||||
@ -42,18 +43,65 @@ Serial::isOpen () {
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::read (unsigned char* buffer, size_t size) {
|
||||
// return this->pimpl->read (buffer, size);
|
||||
Serial::available () {
|
||||
return this->pimpl->available();
|
||||
}
|
||||
|
||||
//size_t
|
||||
//Serial::read (unsigned char* buffer, size_t size) {
|
||||
// return this->pimpl->read (buffer, size);
|
||||
//}
|
||||
|
||||
string
|
||||
Serial::read (size_t size) {
|
||||
return this->pimpl->read (size);
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::read (string &buffer, size_t size) {
|
||||
// return this->pimpl->read (buffer, size);
|
||||
string
|
||||
Serial::readline(size_t size, string eol) {
|
||||
size_t leneol = eol.length();
|
||||
string line;
|
||||
while (true) {
|
||||
string c = read(1);
|
||||
if (c.empty()) {
|
||||
line += c;
|
||||
if (line.substr(line.length() - leneol, leneol) == eol) {
|
||||
break;
|
||||
}
|
||||
if (line.length() >= size) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Timeout
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
vector<string>
|
||||
Serial::readlines(string eol) {
|
||||
if (this->pimpl->getTimeout() < 0) {
|
||||
throw "Error, must be set for readlines";
|
||||
}
|
||||
size_t leneol = eol.length();
|
||||
vector<string> lines;
|
||||
while (true) {
|
||||
string line = readline(std::numeric_limits<std::size_t>::max(), eol);
|
||||
if (!line.empty()) {
|
||||
lines.push_back(line);
|
||||
if (line.substr(line.length() - leneol, leneol) == eol)
|
||||
break;
|
||||
}
|
||||
else {
|
||||
// Timeout
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
//size_t
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user