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;
70 stopbits, flowcontrol))
93 Serial::isOpen ()
const 105 Serial::waitReadable ()
112 Serial::waitByteTimes (
size_t count)
118 Serial::read_ (uint8_t *buffer,
size_t size)
124 Serial::read (uint8_t *buffer,
size_t 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)
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)
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)
281 Serial::setPort (
const string &port)
286 if (was_open) close();
288 if (was_open) open ();
292 Serial::getPort ()
const 304 Serial::getTimeout ()
const {
309 Serial::setBaudrate (uint32_t baudrate)
315 Serial::getBaudrate ()
const 327 Serial::getBytesize ()
const 339 Serial::getParity ()
const 351 Serial::getStopbits ()
const 363 Serial::getFlowcontrol ()
const 368 void Serial::flush ()
375 void Serial::flushInput ()
381 void Serial::flushOutput ()
387 void Serial::sendBreak (
int duration)
392 void Serial::setBreak (
bool level)
397 void Serial::setRTS (
bool level)
402 void Serial::setDTR (
bool level)
407 bool Serial::waitForChange()
412 bool Serial::getCTS ()
417 bool Serial::getDSR ()
422 bool Serial::getRI ()
427 bool Serial::getCD ()
void setBytesize(bytesize_t bytesize)
void waitByteTimes(size_t count)
void setStopbits(stopbits_t stopbits)
Timeout getTimeout() const
void setBaudrate(unsigned long baudrate)
size_t read(uint8_t *buf, size_t size=1)
stopbits_t getStopbits() const
ScopedReadLock(SerialImpl *pimpl)
const ScopedReadLock & operator=(ScopedReadLock)
parity_t getParity() const
bytesize_t getBytesize() const
bool waitReadable(uint32_t timeout)
ScopedWriteLock(SerialImpl *pimpl)
void setParity(parity_t parity)
void sendBreak(int duration)
unsigned long getBaudrate() const
void setFlowcontrol(flowcontrol_t flowcontrol)
void setTimeout(Timeout &timeout)
flowcontrol_t getFlowcontrol() const
size_t write(const uint8_t *data, size_t length)
void setPort(const string &port)
void setBreak(bool level)