4 #if !defined(_WIN32) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
8 #if defined (__MINGW32__)
9 # define alloca __builtin_alloca
20 using std::invalid_argument;
22 using std::numeric_limits;
41 this->
pimpl_->readUnlock();
57 this->
pimpl_->writeUnlock();
66 Serial::Serial (
const string &port, uint32_t baudrate,
serial::Timeout timeout,
69 : pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
70 stopbits, flowcontrol))
72 pimpl_->setTimeout(timeout);
93 Serial::isOpen ()
const
95 return pimpl_->isOpen ();
101 return pimpl_->available ();
105 Serial::waitReadable ()
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 = 0;
138 bytes_read = this->pimpl_->read (buffer_, size);
140 catch (
const std::exception &e) {
145 buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
151 Serial::read (std::string &buffer,
size_t size)
154 uint8_t *buffer_ =
new uint8_t[size];
155 size_t bytes_read = 0;
157 bytes_read = this->pimpl_->read (buffer_, size);
159 catch (
const std::exception &e) {
163 buffer.append (
reinterpret_cast<const char*
>(buffer_), bytes_read);
169 Serial::read (
size_t size)
172 this->read (buffer, size);
177 Serial::readline (
string &buffer,
size_t size,
string eol)
179 ScopedReadLock lock(this->pimpl_);
180 size_t eol_len =
eol.length ();
181 uint8_t *buffer_ =
static_cast<uint8_t*
>
182 (alloca (size *
sizeof (uint8_t)));
183 size_t read_so_far = 0;
186 size_t bytes_read = this->read_ (buffer_ + read_so_far, 1);
187 read_so_far += bytes_read;
188 if (bytes_read == 0) {
191 if (
string (
reinterpret_cast<const char*
>
192 (buffer_ + read_so_far - eol_len), eol_len) ==
eol) {
195 if (read_so_far == size) {
199 buffer.append(
reinterpret_cast<const char*
> (buffer_), read_so_far);
204 Serial::readline (
size_t size,
string eol)
207 this->readline (buffer, size,
eol);
212 Serial::readlines (
size_t size,
string eol)
215 std::vector<std::string> lines;
216 size_t eol_len =
eol.length ();
217 uint8_t *buffer_ =
static_cast<uint8_t*
>
218 (alloca (size *
sizeof (uint8_t)));
219 size_t read_so_far = 0;
220 size_t start_of_line = 0;
221 while (read_so_far < size) {
222 size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
223 read_so_far += bytes_read;
224 if (bytes_read == 0) {
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));
232 if (
string (
reinterpret_cast<const char*
>
233 (buffer_ + read_so_far - eol_len), eol_len) ==
eol) {
236 string(
reinterpret_cast<const char*
> (buffer_ + start_of_line),
237 read_so_far - start_of_line));
238 start_of_line = read_so_far;
240 if (read_so_far == size) {
241 if (start_of_line != read_so_far) {
243 string(
reinterpret_cast<const char*
> (buffer_ + start_of_line),
244 read_so_far - start_of_line));
253 Serial::write (
const string &data)
255 ScopedWriteLock lock(this->pimpl_);
256 return this->write_ (
reinterpret_cast<const uint8_t*
>(data.c_str()),
261 Serial::write (
const std::vector<uint8_t> &data)
264 return this->write_ (&data[0], data.size());
268 Serial::write (
const uint8_t *data,
size_t size)
271 return this->write_(data, size);
275 Serial::write_ (
const uint8_t *data,
size_t length)
277 return pimpl_->write (data, length);
281 Serial::setPort (
const string &port)
285 bool was_open = pimpl_->isOpen ();
286 if (was_open) close();
287 pimpl_->setPort (port);
288 if (was_open) open ();
292 Serial::getPort ()
const
294 return pimpl_->getPort ();
300 pimpl_->setTimeout (timeout);
304 Serial::getTimeout ()
const {
305 return pimpl_->getTimeout ();
309 Serial::setBaudrate (uint32_t baudrate)
311 pimpl_->setBaudrate (baudrate);
315 Serial::getBaudrate ()
const
317 return uint32_t(pimpl_->getBaudrate ());
323 pimpl_->setBytesize (bytesize);
327 Serial::getBytesize ()
const
329 return pimpl_->getBytesize ();
335 pimpl_->setParity (parity);
339 Serial::getParity ()
const
341 return pimpl_->getParity ();
347 pimpl_->setStopbits (stopbits);
351 Serial::getStopbits ()
const
353 return pimpl_->getStopbits ();
359 pimpl_->setFlowcontrol (flowcontrol);
363 Serial::getFlowcontrol ()
const
365 return pimpl_->getFlowcontrol ();
368 void Serial::flush ()
375 void Serial::flushInput ()
378 pimpl_->flushInput ();
381 void Serial::flushOutput ()
384 pimpl_->flushOutput ();
392 void Serial::setBreak (
bool level)
394 pimpl_->setBreak (level);
397 void Serial::setRTS (
bool level)
399 pimpl_->setRTS (level);
402 void Serial::setDTR (
bool level)
404 pimpl_->setDTR (level);
407 bool Serial::waitForChange()
409 return pimpl_->waitForChange();
412 bool Serial::getCTS ()
414 return pimpl_->getCTS ();
417 bool Serial::getDSR ()
419 return pimpl_->getDSR ();
422 bool Serial::getRI ()
424 return pimpl_->getRI ();
427 bool Serial::getCD ()
429 return pimpl_->getCD ();