4 #if !defined(_WIN32) && !defined(__OpenBSD__)
8 #if defined (__MINGW32__)
9 # define alloca __builtin_alloca
20 using std::invalid_argument;
22 using std::numeric_limits;
69 : pimpl_(new
SerialImpl (port, baudrate, bytesize, parity,
70 stopbits, flowcontrol))
93 Serial::isOpen ()
const
95 return pimpl_->isOpen ();
101 return pimpl_->available ();
105 Serial::waitReadable ()
108 return pimpl_->waitReadable(
timeout.read_timeout_constant);
112 Serial::waitByteTimes (
size_t count)
114 pimpl_->waitByteTimes(count);
118 Serial::read_ (uint8_t *buffer,
size_t size)
120 return this->pimpl_->read (buffer, size);
124 Serial::read (uint8_t *buffer,
size_t size)
127 return this->pimpl_->read (buffer, size);
131 Serial::read (std::vector<uint8_t> &buffer,
size_t size)
134 uint8_t *buffer_ =
new uint8_t[size];
135 size_t bytes_read = this->pimpl_->read (buffer_, size);
136 buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
142 Serial::read (std::string &buffer,
size_t size)
145 uint8_t *buffer_ =
new uint8_t[size];
146 size_t bytes_read = this->pimpl_->read (buffer_, size);
147 buffer.append (
reinterpret_cast<const char*
>(buffer_), bytes_read);
153 Serial::read (
size_t size)
156 this->read (buffer, size);
161 Serial::readline (
string &buffer,
size_t size,
string eol)
163 ScopedReadLock lock(this->pimpl_);
164 size_t eol_len = eol.length ();
165 uint8_t *buffer_ =
static_cast<uint8_t*
>
166 (alloca (size *
sizeof (uint8_t)));
167 size_t read_so_far = 0;
170 size_t bytes_read = this->read_ (buffer_ + read_so_far, 1);
171 read_so_far += bytes_read;
172 if (bytes_read == 0) {
175 if (
string (
reinterpret_cast<const char*
>
176 (buffer_ + read_so_far - eol_len), eol_len) == eol) {
179 if (read_so_far == size) {
183 buffer.append(
reinterpret_cast<const char*
> (buffer_), read_so_far);
188 Serial::readline (
size_t size,
string eol)
191 this->readline (buffer, size, eol);
196 Serial::readlines (
size_t size,
string eol)
199 std::vector<std::string> lines;
200 size_t eol_len = eol.length ();
201 uint8_t *buffer_ =
static_cast<uint8_t*
>
202 (alloca (size *
sizeof (uint8_t)));
203 size_t read_so_far = 0;
204 size_t start_of_line = 0;
205 while (read_so_far < size) {
206 size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
207 read_so_far += bytes_read;
208 if (bytes_read == 0) {
209 if (start_of_line != read_so_far) {
211 string (
reinterpret_cast<const char*
> (buffer_ + start_of_line),
212 read_so_far - start_of_line));
216 if (
string (
reinterpret_cast<const char*
>
217 (buffer_ + read_so_far - eol_len), eol_len) == eol) {
220 string(
reinterpret_cast<const char*
> (buffer_ + start_of_line),
221 read_so_far - start_of_line));
222 start_of_line = read_so_far;
224 if (read_so_far == size) {
225 if (start_of_line != read_so_far) {
227 string(
reinterpret_cast<const char*
> (buffer_ + start_of_line),
228 read_so_far - start_of_line));
237 Serial::write (
const string &data)
239 ScopedWriteLock lock(this->pimpl_);
240 return this->write_ (
reinterpret_cast<const uint8_t*
>(
data.c_str()),
245 Serial::write (
const std::vector<uint8_t> &data)
248 return this->write_ (&
data[0],
data.size());
252 Serial::write (
const uint8_t *data,
size_t size)
255 return this->write_(
data, size);
259 Serial::write_ (
const uint8_t *data,
size_t length)
265 Serial::setPort (
const string &port)
269 bool was_open = pimpl_->isOpen ();
270 if (was_open) close();
271 pimpl_->setPort (port);
272 if (was_open) open ();
276 Serial::getPort ()
const
278 return pimpl_->getPort ();
288 Serial::getTimeout ()
const {
289 return pimpl_->getTimeout ();
293 Serial::setBaudrate (uint32_t baudrate)
295 pimpl_->setBaudrate (baudrate);
299 Serial::getBaudrate ()
const
301 return uint32_t(pimpl_->getBaudrate ());
307 pimpl_->setBytesize (bytesize);
311 Serial::getBytesize ()
const
313 return pimpl_->getBytesize ();
319 pimpl_->setParity (parity);
323 Serial::getParity ()
const
325 return pimpl_->getParity ();
331 pimpl_->setStopbits (stopbits);
335 Serial::getStopbits ()
const
337 return pimpl_->getStopbits ();
343 pimpl_->setFlowcontrol (flowcontrol);
347 Serial::getFlowcontrol ()
const
349 return pimpl_->getFlowcontrol ();
352 void Serial::flush ()
359 void Serial::flushInput ()
362 pimpl_->flushInput ();
365 void Serial::flushOutput ()
368 pimpl_->flushOutput ();
371 void Serial::sendBreak (
int duration)
373 pimpl_->sendBreak (duration);
376 void Serial::setBreak (
bool level)
378 pimpl_->setBreak (level);
381 void Serial::setRTS (
bool level)
383 pimpl_->setRTS (level);
386 void Serial::setDTR (
bool level)
388 pimpl_->setDTR (level);
391 bool Serial::waitForChange()
393 return pimpl_->waitForChange();
396 bool Serial::getCTS ()
398 return pimpl_->getCTS ();
401 bool Serial::getDSR ()
403 return pimpl_->getDSR ();
406 bool Serial::getRI ()
408 return pimpl_->getRI ();
411 bool Serial::getCD ()
413 return pimpl_->getCD ();