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 
48  SerialImpl *pimpl_;
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
63  SerialImpl *pimpl_;
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 {
107  serial::Timeout timeout(pimpl_->getTimeout ());
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 = this->pimpl_->read (buffer_, size);
136  buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
137  delete[] buffer_;
138  return bytes_read;
139 }
140 
141 size_t
142 Serial::read (std::string &buffer, size_t size)
143 {
144  ScopedReadLock lock(this->pimpl_);
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);
148  delete[] buffer_;
149  return bytes_read;
150 }
151 
152 string
153 Serial::read (size_t size)
154 {
155  std::string buffer;
156  this->read (buffer, size);
157  return buffer;
158 }
159 
160 size_t
161 Serial::readline (string &buffer, size_t size, string eol)
162 {
163  ScopedReadLock lock(this->pimpl_);
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;
168  while (true)
169  {
170  size_t bytes_read = this->read_ (buffer_ + read_so_far, 1);
171  read_so_far += bytes_read;
172  if (bytes_read == 0) {
173  break; // Timeout occured on reading 1 byte
174  }
175  if (string (reinterpret_cast<const char*>
176  (buffer_ + read_so_far - eol_len), eol_len) == eol) {
177  break; // EOL found
178  }
179  if (read_so_far == size) {
180  break; // Reached the maximum read length
181  }
182  }
183  buffer.append(reinterpret_cast<const char*> (buffer_), read_so_far);
184  return read_so_far;
185 }
186 
187 string
188 Serial::readline (size_t size, string eol)
189 {
190  std::string buffer;
191  this->readline (buffer, size, eol);
192  return buffer;
193 }
194 
195 vector<string>
196 Serial::readlines (size_t size, string eol)
197 {
198  ScopedReadLock lock(this->pimpl_);
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) {
210  lines.push_back (
211  string (reinterpret_cast<const char*> (buffer_ + start_of_line),
212  read_so_far - start_of_line));
213  }
214  break; // Timeout occured on reading 1 byte
215  }
216  if (string (reinterpret_cast<const char*>
217  (buffer_ + read_so_far - eol_len), eol_len) == eol) {
218  // EOL found
219  lines.push_back(
220  string(reinterpret_cast<const char*> (buffer_ + start_of_line),
221  read_so_far - start_of_line));
222  start_of_line = read_so_far;
223  }
224  if (read_so_far == size) {
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; // Reached the maximum read length
231  }
232  }
233  return lines;
234 }
235 
236 size_t
237 Serial::write (const string &data)
238 {
239  ScopedWriteLock lock(this->pimpl_);
240  return this->write_ (reinterpret_cast<const uint8_t*>(data.c_str()),
241  data.length());
242 }
243 
244 size_t
245 Serial::write (const std::vector<uint8_t> &data)
246 {
247  ScopedWriteLock lock(this->pimpl_);
248  return this->write_ (&data[0], data.size());
249 }
250 
251 size_t
252 Serial::write (const uint8_t *data, size_t size)
253 {
254  ScopedWriteLock lock(this->pimpl_);
255  return this->write_(data, size);
256 }
257 
258 size_t
259 Serial::write_ (const uint8_t *data, size_t length)
260 {
261  return pimpl_->write (data, length);
262 }
263 
264 void
265 Serial::setPort (const string &port)
266 {
267  ScopedReadLock rlock(this->pimpl_);
268  ScopedWriteLock wlock(this->pimpl_);
269  bool was_open = pimpl_->isOpen ();
270  if (was_open) close();
271  pimpl_->setPort (port);
272  if (was_open) open ();
273 }
274 
275 string
276 Serial::getPort () const
277 {
278  return pimpl_->getPort ();
279 }
280 
281 void
282 Serial::setTimeout (serial::Timeout &timeout)
283 {
284  pimpl_->setTimeout (timeout);
285 }
286 
288 Serial::getTimeout () const {
289  return pimpl_->getTimeout ();
290 }
291 
292 void
293 Serial::setBaudrate (uint32_t baudrate)
294 {
295  pimpl_->setBaudrate (baudrate);
296 }
297 
298 uint32_t
299 Serial::getBaudrate () const
300 {
301  return uint32_t(pimpl_->getBaudrate ());
302 }
303 
304 void
305 Serial::setBytesize (bytesize_t bytesize)
306 {
307  pimpl_->setBytesize (bytesize);
308 }
309 
311 Serial::getBytesize () const
312 {
313  return pimpl_->getBytesize ();
314 }
315 
316 void
317 Serial::setParity (parity_t parity)
318 {
319  pimpl_->setParity (parity);
320 }
321 
322 parity_t
323 Serial::getParity () const
324 {
325  return pimpl_->getParity ();
326 }
327 
328 void
329 Serial::setStopbits (stopbits_t stopbits)
330 {
331  pimpl_->setStopbits (stopbits);
332 }
333 
335 Serial::getStopbits () const
336 {
337  return pimpl_->getStopbits ();
338 }
339 
340 void
341 Serial::setFlowcontrol (flowcontrol_t flowcontrol)
342 {
343  pimpl_->setFlowcontrol (flowcontrol);
344 }
345 
347 Serial::getFlowcontrol () const
348 {
349  return pimpl_->getFlowcontrol ();
350 }
351 
352 void Serial::flush ()
353 {
354  ScopedReadLock rlock(this->pimpl_);
355  ScopedWriteLock wlock(this->pimpl_);
356  pimpl_->flush ();
357 }
358 
359 void Serial::flushInput ()
360 {
361  ScopedReadLock lock(this->pimpl_);
362  pimpl_->flushInput ();
363 }
364 
365 void Serial::flushOutput ()
366 {
367  ScopedWriteLock lock(this->pimpl_);
368  pimpl_->flushOutput ();
369 }
370 
371 void Serial::sendBreak (int duration)
372 {
373  pimpl_->sendBreak (duration);
374 }
375 
376 void Serial::setBreak (bool level)
377 {
378  pimpl_->setBreak (level);
379 }
380 
381 void Serial::setRTS (bool level)
382 {
383  pimpl_->setRTS (level);
384 }
385 
386 void Serial::setDTR (bool level)
387 {
388  pimpl_->setDTR (level);
389 }
390 
391 bool Serial::waitForChange()
392 {
393  return pimpl_->waitForChange();
394 }
395 
396 bool Serial::getCTS ()
397 {
398  return pimpl_->getCTS ();
399 }
400 
401 bool Serial::getDSR ()
402 {
403  return pimpl_->getDSR ();
404 }
405 
406 bool Serial::getRI ()
407 {
408  return pimpl_->getRI ();
409 }
410 
411 bool Serial::getCD ()
412 {
413  return pimpl_->getCD ();
414 }
parity_t
Definition: serial.h:66
bytesize_t
Definition: serial.h:56
ScopedReadLock(SerialImpl *pimpl)
Definition: serial.cc:37
const ScopedReadLock & operator=(ScopedReadLock)
ScopedWriteLock(SerialImpl *pimpl)
Definition: serial.cc:53
stopbits_t
Definition: serial.h:77
flowcontrol_t
Definition: serial.h:86


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23