serial.cc
Go to the documentation of this file.
1 /* Copyright 2012 William Woodall and John Harrison */
2 #include <algorithm>
3 
4 #if !defined(_WIN32) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
5 # include <alloca.h>
6 #endif
7 
8 #if defined (__MINGW32__)
9 # define alloca __builtin_alloca
10 #endif
11 
12 #include "serial/serial.h"
13 
14 #ifdef _WIN32
15 #include "serial/impl/win.h"
16 #else
17 #include "serial/impl/unix.h"
18 #endif
19 
20 using std::invalid_argument;
21 using std::min;
22 using std::numeric_limits;
23 using std::vector;
24 using std::size_t;
25 using std::string;
26 
27 using serial::Serial;
30 using serial::bytesize_t;
31 using serial::parity_t;
32 using serial::stopbits_t;
34 
36 public:
37  ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl) {
38  this->pimpl_->readLock();
39  }
41  this->pimpl_->readUnlock();
42  }
43 private:
44  // Disable copy constructors
47 
49 };
50 
52 public:
53  ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl) {
54  this->pimpl_->writeLock();
55  }
57  this->pimpl_->writeUnlock();
58  }
59 private:
60  // Disable copy constructors
64 };
65 
66 Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout timeout,
67  bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
68  flowcontrol_t flowcontrol)
69  : pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
70  stopbits, flowcontrol))
71 {
72  pimpl_->setTimeout(timeout);
73 }
74 
75 Serial::~Serial ()
76 {
77  delete pimpl_;
78 }
79 
80 void
81 Serial::open ()
82 {
83  pimpl_->open ();
84 }
85 
86 void
87 Serial::close ()
88 {
89  pimpl_->close ();
90 }
91 
92 bool
93 Serial::isOpen () const
94 {
95  return pimpl_->isOpen ();
96 }
97 
98 size_t
99 Serial::available ()
100 {
101  return pimpl_->available ();
102 }
103 
104 bool
105 Serial::waitReadable ()
106 {
108  return pimpl_->waitReadable(timeout.read_timeout_constant);
109 }
110 
111 void
112 Serial::waitByteTimes (size_t count)
113 {
114  pimpl_->waitByteTimes(count);
115 }
116 
117 size_t
118 Serial::read_ (uint8_t *buffer, size_t size)
119 {
120  return this->pimpl_->read (buffer, size);
121 }
122 
123 size_t
124 Serial::read (uint8_t *buffer, size_t size)
125 {
126  ScopedReadLock lock(this->pimpl_);
127  return this->pimpl_->read (buffer, size);
128 }
129 
130 size_t
131 Serial::read (std::vector<uint8_t> &buffer, size_t size)
132 {
133  ScopedReadLock lock(this->pimpl_);
134  uint8_t *buffer_ = new uint8_t[size];
135  size_t bytes_read = 0;
136 
137  try {
138  bytes_read = this->pimpl_->read (buffer_, size);
139  }
140  catch (const std::exception &e) {
141  delete[] buffer_;
142  throw;
143  }
144 
145  buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
146  delete[] buffer_;
147  return bytes_read;
148 }
149 
150 size_t
151 Serial::read (std::string &buffer, size_t size)
152 {
153  ScopedReadLock lock(this->pimpl_);
154  uint8_t *buffer_ = new uint8_t[size];
155  size_t bytes_read = 0;
156  try {
157  bytes_read = this->pimpl_->read (buffer_, size);
158  }
159  catch (const std::exception &e) {
160  delete[] buffer_;
161  throw;
162  }
163  buffer.append (reinterpret_cast<const char*>(buffer_), bytes_read);
164  delete[] buffer_;
165  return bytes_read;
166 }
167 
168 string
169 Serial::read (size_t size)
170 {
171  std::string buffer;
172  this->read (buffer, size);
173  return buffer;
174 }
175 
176 size_t
177 Serial::readline (string &buffer, size_t size, string eol)
178 {
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;
184  while (true)
185  {
186  size_t bytes_read = this->read_ (buffer_ + read_so_far, 1);
187  read_so_far += bytes_read;
188  if (bytes_read == 0) {
189  break; // Timeout occured on reading 1 byte
190  }
191  if (string (reinterpret_cast<const char*>
192  (buffer_ + read_so_far - eol_len), eol_len) == eol) {
193  break; // EOL found
194  }
195  if (read_so_far == size) {
196  break; // Reached the maximum read length
197  }
198  }
199  buffer.append(reinterpret_cast<const char*> (buffer_), read_so_far);
200  return read_so_far;
201 }
202 
203 string
204 Serial::readline (size_t size, string eol)
205 {
206  std::string buffer;
207  this->readline (buffer, size, eol);
208  return buffer;
209 }
210 
211 vector<string>
212 Serial::readlines (size_t size, string eol)
213 {
214  ScopedReadLock lock(this->pimpl_);
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) {
226  lines.push_back (
227  string (reinterpret_cast<const char*> (buffer_ + start_of_line),
228  read_so_far - start_of_line));
229  }
230  break; // Timeout occured on reading 1 byte
231  }
232  if (string (reinterpret_cast<const char*>
233  (buffer_ + read_so_far - eol_len), eol_len) == eol) {
234  // EOL found
235  lines.push_back(
236  string(reinterpret_cast<const char*> (buffer_ + start_of_line),
237  read_so_far - start_of_line));
238  start_of_line = read_so_far;
239  }
240  if (read_so_far == size) {
241  if (start_of_line != read_so_far) {
242  lines.push_back(
243  string(reinterpret_cast<const char*> (buffer_ + start_of_line),
244  read_so_far - start_of_line));
245  }
246  break; // Reached the maximum read length
247  }
248  }
249  return lines;
250 }
251 
252 size_t
253 Serial::write (const string &data)
254 {
255  ScopedWriteLock lock(this->pimpl_);
256  return this->write_ (reinterpret_cast<const uint8_t*>(data.c_str()),
257  data.length());
258 }
259 
260 size_t
261 Serial::write (const std::vector<uint8_t> &data)
262 {
263  ScopedWriteLock lock(this->pimpl_);
264  return this->write_ (&data[0], data.size());
265 }
266 
267 size_t
268 Serial::write (const uint8_t *data, size_t size)
269 {
270  ScopedWriteLock lock(this->pimpl_);
271  return this->write_(data, size);
272 }
273 
274 size_t
275 Serial::write_ (const uint8_t *data, size_t length)
276 {
277  return pimpl_->write (data, length);
278 }
279 
280 void
281 Serial::setPort (const string &port)
282 {
283  ScopedReadLock rlock(this->pimpl_);
284  ScopedWriteLock wlock(this->pimpl_);
285  bool was_open = pimpl_->isOpen ();
286  if (was_open) close();
287  pimpl_->setPort (port);
288  if (was_open) open ();
289 }
290 
291 string
292 Serial::getPort () const
293 {
294  return pimpl_->getPort ();
295 }
296 
297 void
298 Serial::setTimeout (serial::Timeout &timeout)
299 {
300  pimpl_->setTimeout (timeout);
301 }
302 
304 Serial::getTimeout () const {
305  return pimpl_->getTimeout ();
306 }
307 
308 void
309 Serial::setBaudrate (uint32_t baudrate)
310 {
311  pimpl_->setBaudrate (baudrate);
312 }
313 
314 uint32_t
315 Serial::getBaudrate () const
316 {
317  return uint32_t(pimpl_->getBaudrate ());
318 }
319 
320 void
321 Serial::setBytesize (bytesize_t bytesize)
322 {
323  pimpl_->setBytesize (bytesize);
324 }
325 
327 Serial::getBytesize () const
328 {
329  return pimpl_->getBytesize ();
330 }
331 
332 void
333 Serial::setParity (parity_t parity)
334 {
335  pimpl_->setParity (parity);
336 }
337 
338 parity_t
339 Serial::getParity () const
340 {
341  return pimpl_->getParity ();
342 }
343 
344 void
345 Serial::setStopbits (stopbits_t stopbits)
346 {
347  pimpl_->setStopbits (stopbits);
348 }
349 
351 Serial::getStopbits () const
352 {
353  return pimpl_->getStopbits ();
354 }
355 
356 void
357 Serial::setFlowcontrol (flowcontrol_t flowcontrol)
358 {
359  pimpl_->setFlowcontrol (flowcontrol);
360 }
361 
363 Serial::getFlowcontrol () const
364 {
365  return pimpl_->getFlowcontrol ();
366 }
367 
368 void Serial::flush ()
369 {
370  ScopedReadLock rlock(this->pimpl_);
371  ScopedWriteLock wlock(this->pimpl_);
372  pimpl_->flush ();
373 }
374 
375 void Serial::flushInput ()
376 {
377  ScopedReadLock lock(this->pimpl_);
378  pimpl_->flushInput ();
379 }
380 
381 void Serial::flushOutput ()
382 {
383  ScopedWriteLock lock(this->pimpl_);
384  pimpl_->flushOutput ();
385 }
386 
387 void Serial::sendBreak (int duration)
388 {
389  pimpl_->sendBreak (duration);
390 }
391 
392 void Serial::setBreak (bool level)
393 {
394  pimpl_->setBreak (level);
395 }
396 
397 void Serial::setRTS (bool level)
398 {
399  pimpl_->setRTS (level);
400 }
401 
402 void Serial::setDTR (bool level)
403 {
404  pimpl_->setDTR (level);
405 }
406 
407 bool Serial::waitForChange()
408 {
409  return pimpl_->waitForChange();
410 }
411 
412 bool Serial::getCTS ()
413 {
414  return pimpl_->getCTS ();
415 }
416 
417 bool Serial::getDSR ()
418 {
419  return pimpl_->getDSR ();
420 }
421 
422 bool Serial::getRI ()
423 {
424  return pimpl_->getRI ();
425 }
426 
427 bool Serial::getCD ()
428 {
429  return pimpl_->getCD ();
430 }
void setBytesize(bytesize_t bytesize)
Definition: unix.cc:736
void waitByteTimes(size_t count)
Definition: unix.cc:526
void setRTS(bool level)
Definition: unix.cc:852
string getPort() const
Definition: unix.cc:704
void setStopbits(stopbits_t stopbits)
Definition: unix.cc:764
Timeout getTimeout() const
Definition: unix.cc:716
void setBaudrate(unsigned long baudrate)
Definition: unix.cc:722
parity_t
Definition: serial.h:66
size_t read(uint8_t *buf, size_t size=1)
Definition: unix.cc:533
bytesize_t
Definition: serial.h:56
stopbits_t getStopbits() const
Definition: unix.cc:772
ScopedReadLock(SerialImpl *pimpl)
Definition: serial.cc:37
void setDTR(bool level)
Definition: unix.cc:878
const ScopedReadLock & operator=(ScopedReadLock)
parity_t getParity() const
Definition: unix.cc:758
bytesize_t getBytesize() const
Definition: unix.cc:744
bool waitReadable(uint32_t timeout)
Definition: unix.cc:495
bool isOpen() const
Definition: unix.cc:475
ScopedWriteLock(SerialImpl *pimpl)
Definition: serial.cc:53
void setParity(parity_t parity)
Definition: unix.cc:750
void sendBreak(int duration)
Definition: unix.cc:819
unsigned long getBaudrate() const
Definition: unix.cc:730
stopbits_t
Definition: serial.h:77
void setFlowcontrol(flowcontrol_t flowcontrol)
Definition: unix.cc:778
void setTimeout(Timeout &timeout)
Definition: unix.cc:710
flowcontrol_t getFlowcontrol() const
Definition: unix.cc:786
size_t write(const uint8_t *data, size_t length)
Definition: unix.cc:610
void setPort(const string &port)
Definition: unix.cc:698
void setBreak(bool level)
Definition: unix.cc:828
flowcontrol_t
Definition: serial.h:86


serial
Author(s): William Woodall , John Harrison
autogenerated on Thu Jan 9 2020 07:18:58