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 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)
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)
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)
261 return pimpl_->write (data, length);
265 Serial::setPort (
const string &port)
269 bool was_open =
pimpl_->isOpen ();
270 if (was_open) close();
272 if (was_open) open ();
276 Serial::getPort ()
const 278 return pimpl_->getPort ();
284 pimpl_->setTimeout (timeout);
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 ()
365 void Serial::flushOutput ()
371 void Serial::sendBreak (
int duration)
373 pimpl_->sendBreak (duration);
376 void Serial::setBreak (
bool level)
381 void Serial::setRTS (
bool level)
386 void Serial::setDTR (
bool level)
391 bool Serial::waitForChange()
393 return pimpl_->waitForChange();
396 bool Serial::getCTS ()
401 bool Serial::getDSR ()
406 bool Serial::getRI ()
411 bool Serial::getCD ()
ScopedReadLock(SerialImpl *pimpl)
const ScopedReadLock & operator=(ScopedReadLock)
ScopedWriteLock(SerialImpl *pimpl)