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_;
|
bool isOpen_;
|
||||||
|
|
||||||
int interCharTimeout_;
|
int interCharTimeout_;
|
||||||
|
int writeTimeout_;
|
||||||
int xonxoff_;
|
int xonxoff_;
|
||||||
int rtscts_;
|
int rtscts_;
|
||||||
|
|
||||||
|
|||||||
@ -39,12 +39,8 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
#ifdef CXX_11
|
#include <limits>
|
||||||
#include <memory>
|
|
||||||
#else
|
|
||||||
// #include <tr1/boost_shared_ptr.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace serial {
|
namespace serial {
|
||||||
|
|
||||||
@ -159,6 +155,11 @@ public:
|
|||||||
void
|
void
|
||||||
close ();
|
close ();
|
||||||
|
|
||||||
|
/* Return the number of characters in the buffer.
|
||||||
|
*/
|
||||||
|
size_t
|
||||||
|
available();
|
||||||
|
|
||||||
/*! Read a given amount of bytes from the serial port.
|
/*! Read a given amount of bytes from the serial port.
|
||||||
*
|
*
|
||||||
* If a timeout is set it may return less characters than requested. With
|
* 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.
|
* \return A size_t representing the number of bytes actually read.
|
||||||
*/
|
*/
|
||||||
size_t
|
//size_t
|
||||||
read (unsigned char* buffer, size_t size = 1);
|
//read (unsigned char* buffer, size_t size = 1);
|
||||||
|
|
||||||
/*! Read a given amount of bytes from the serial port.
|
/*! Read a given amount of bytes from the serial port.
|
||||||
*
|
*
|
||||||
@ -188,6 +189,9 @@ public:
|
|||||||
std::string
|
std::string
|
||||||
read (size_t size = 1);
|
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.
|
/*! Read a given amount of bytes from the serial port.
|
||||||
*
|
*
|
||||||
* Reads into a std::string by reference rather than returning it.
|
* Reads into a std::string by reference rather than returning it.
|
||||||
@ -199,8 +203,8 @@ public:
|
|||||||
*
|
*
|
||||||
* \see Serial::read(size_t)
|
* \see Serial::read(size_t)
|
||||||
*/
|
*/
|
||||||
size_t
|
//size_t
|
||||||
read (std::string &buffer, size_t size = 1);
|
//read (std::string &buffer, size_t size = 1);
|
||||||
|
|
||||||
/*! Write bytes from the data to the serial port by given length.
|
/*! 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.
|
* \return A size_t representing the number of bytes actually written.
|
||||||
*/
|
*/
|
||||||
size_t
|
//size_t
|
||||||
write (unsigned char* data, size_t length);
|
//write (unsigned char* data, size_t length);
|
||||||
|
|
||||||
/*! Write a string to the serial port.
|
/*! Write a string to the serial port.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -219,8 +219,8 @@ Serial::SerialImpl::read (size_t size) {
|
|||||||
FD_ZERO(&readfds);
|
FD_ZERO(&readfds);
|
||||||
FD_SET(fd_, &readfds);
|
FD_SET(fd_, &readfds);
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
timeout.tv_sec = timeout_ / 1000000;
|
timeout.tv_sec = timeout_ / 1000;
|
||||||
timeout.tv_usec = timeout_ % 1000000;
|
timeout.tv_usec = timeout_ % 1000;
|
||||||
int r = select(1, &readfds, NULL, NULL, &timeout);
|
int r = select(1, &readfds, NULL, NULL, &timeout);
|
||||||
|
|
||||||
if (r == -1 && errno == EINTR)
|
if (r == -1 && errno == EINTR)
|
||||||
@ -253,7 +253,15 @@ Serial::SerialImpl::read (size_t size) {
|
|||||||
|
|
||||||
size_t
|
size_t
|
||||||
Serial::SerialImpl::write (const string &data) {
|
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
|
void
|
||||||
@ -268,12 +276,12 @@ Serial::SerialImpl::getPort () const {
|
|||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::setTimeout (long timeout) {
|
Serial::SerialImpl::setTimeout (long timeout) {
|
||||||
|
timeout_ = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
long
|
long
|
||||||
Serial::SerialImpl::getTimeout () const {
|
Serial::SerialImpl::getTimeout () const {
|
||||||
|
return timeout_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -289,42 +297,42 @@ Serial::SerialImpl::getBaudrate () const {
|
|||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
|
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
|
||||||
|
bytesize_ = bytesize;
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::bytesize_t
|
serial::bytesize_t
|
||||||
Serial::SerialImpl::getBytesize () const {
|
Serial::SerialImpl::getBytesize () const {
|
||||||
|
return bytesize_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::setParity (serial::parity_t parity) {
|
Serial::SerialImpl::setParity (serial::parity_t parity) {
|
||||||
|
parity_ = parity;
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::parity_t
|
serial::parity_t
|
||||||
Serial::SerialImpl::getParity () const {
|
Serial::SerialImpl::getParity () const {
|
||||||
|
return parity_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
|
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
|
||||||
|
stopbits_ = stopbits;
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::stopbits_t
|
serial::stopbits_t
|
||||||
Serial::SerialImpl::getStopbits () const {
|
Serial::SerialImpl::getStopbits () const {
|
||||||
|
return stopbits_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
|
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
|
||||||
|
flowcontrol_ = flowcontrol;
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::flowcontrol_t
|
serial::flowcontrol_t
|
||||||
Serial::SerialImpl::getFlowcontrol () const {
|
Serial::SerialImpl::getFlowcontrol () const {
|
||||||
|
return flowcontrol_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@ using serial::parity_t;
|
|||||||
using serial::stopbits_t;
|
using serial::stopbits_t;
|
||||||
using serial::flowcontrol_t;
|
using serial::flowcontrol_t;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
using std::vector;
|
||||||
|
|
||||||
Serial::Serial (const string &port, int baudrate,
|
Serial::Serial (const string &port, int baudrate,
|
||||||
long timeout, bytesize_t bytesize,
|
long timeout, bytesize_t bytesize,
|
||||||
@ -42,18 +43,65 @@ Serial::isOpen () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
Serial::read (unsigned char* buffer, size_t size) {
|
Serial::available () {
|
||||||
// return this->pimpl->read (buffer, size);
|
return this->pimpl->available();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//size_t
|
||||||
|
//Serial::read (unsigned char* buffer, size_t size) {
|
||||||
|
// return this->pimpl->read (buffer, size);
|
||||||
|
//}
|
||||||
|
|
||||||
string
|
string
|
||||||
Serial::read (size_t size) {
|
Serial::read (size_t size) {
|
||||||
return this->pimpl->read (size);
|
return this->pimpl->read (size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
string
|
||||||
Serial::read (string &buffer, size_t size) {
|
Serial::readline(size_t size, string eol) {
|
||||||
// return this->pimpl->read (buffer, size);
|
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
|
//size_t
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user