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;
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 = 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(read_so_far < eol_len)
continue;
192 if (
string (
reinterpret_cast<const char*
>
193 (buffer_ + read_so_far - eol_len), eol_len) == eol) {
196 if (read_so_far == size) {
200 buffer.append(
reinterpret_cast<const char*
> (buffer_), read_so_far);
205 Serial::readline (
size_t size,
string eol)
208 this->readline (buffer, size, eol);
213 Serial::readlines (
size_t size,
string eol)
216 std::vector<std::string> lines;
217 size_t eol_len = eol.length ();
218 uint8_t *buffer_ =
static_cast<uint8_t*
>
219 (alloca (size *
sizeof (uint8_t)));
220 size_t read_so_far = 0;
221 size_t start_of_line = 0;
222 while (read_so_far < size) {
223 size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
224 read_so_far += bytes_read;
225 if (bytes_read == 0) {
226 if (start_of_line != read_so_far) {
228 string (
reinterpret_cast<const char*
> (buffer_ + start_of_line),
229 read_so_far - start_of_line));
233 if(read_so_far < eol_len)
continue;
234 if (
string (
reinterpret_cast<const char*
>
235 (buffer_ + read_so_far - eol_len), eol_len) == eol) {
238 string(
reinterpret_cast<const char*
> (buffer_ + start_of_line),
239 read_so_far - start_of_line));
240 start_of_line = read_so_far;
242 if (read_so_far == size) {
243 if (start_of_line != read_so_far) {
245 string(
reinterpret_cast<const char*
> (buffer_ + start_of_line),
246 read_so_far - start_of_line));
255 Serial::write (
const string &data)
257 ScopedWriteLock lock(this->pimpl_);
258 return this->write_ (
reinterpret_cast<const uint8_t*
>(data.c_str()),
263 Serial::write (
const std::vector<uint8_t> &data)
266 return this->write_ (&data[0], data.size());
270 Serial::write (
const uint8_t *data,
size_t size)
273 return this->write_(data, size);
277 Serial::write_ (
const uint8_t *data,
size_t length)
279 return pimpl_->write (data, length);
283 Serial::setPort (
const string &port)
287 bool was_open = pimpl_->isOpen ();
288 if (was_open) close();
289 pimpl_->setPort (port);
290 if (was_open) open ();
294 Serial::getPort ()
const
296 return pimpl_->getPort ();
306 Serial::getTimeout ()
const {
307 return pimpl_->getTimeout ();
311 Serial::setBaudrate (uint32_t baudrate)
313 pimpl_->setBaudrate (baudrate);
317 Serial::getBaudrate ()
const
319 return uint32_t(pimpl_->getBaudrate ());
325 pimpl_->setBytesize (bytesize);
329 Serial::getBytesize ()
const
331 return pimpl_->getBytesize ();
337 pimpl_->setParity (parity);
341 Serial::getParity ()
const
343 return pimpl_->getParity ();
349 pimpl_->setStopbits (stopbits);
353 Serial::getStopbits ()
const
355 return pimpl_->getStopbits ();
361 pimpl_->setFlowcontrol (flowcontrol);
365 Serial::getFlowcontrol ()
const
367 return pimpl_->getFlowcontrol ();
370 void Serial::flush ()
377 void Serial::flushInput ()
380 pimpl_->flushInput ();
383 void Serial::flushOutput ()
386 pimpl_->flushOutput ();
389 void Serial::sendBreak (
int duration)
391 pimpl_->sendBreak (duration);
394 void Serial::setBreak (
bool level)
396 pimpl_->setBreak (level);
399 void Serial::setRTS (
bool level)
401 pimpl_->setRTS (level);
404 void Serial::setDTR (
bool level)
406 pimpl_->setDTR (level);
409 bool Serial::waitForChange()
411 return pimpl_->waitForChange();
414 bool Serial::getCTS ()
416 return pimpl_->getCTS ();
419 bool Serial::getDSR ()
421 return pimpl_->getDSR ();
424 bool Serial::getRI ()
426 return pimpl_->getRI ();
429 bool Serial::getCD ()
431 return pimpl_->getCD ();