BufferedAsyncSerial.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
36 // ORIGINAL BELOW
37 /*
38  * @File: BufferedAsyncSerial.cpp
39  * @Author: Terraneo Federico
40  * @Brief:
41  * Distributed under the Boost Software License, Version 1.0.
42  * Created on January 6, 2011, 3:31 PM
43  */
44 
46 #include <string>
47 #include <algorithm>
48 #include <iostream>
49 #include <boost/bind.hpp>
50 
51 using namespace std;
52 
53 using namespace boost;
54 
55 //
56 //Class BufferedAsyncSerial
57 //
58 
60 {
61  setReadCallback ( boost::bind ( &BufferedAsyncSerial::readCallback, this, _1, _2 ) );
62 }
63 
64 BufferedAsyncSerial::BufferedAsyncSerial ( const std::string & devname,
65  unsigned int baud_rate,
66  asio::serial_port_base::parity opt_parity,
67  asio::
68  serial_port_base::character_size opt_csize,
69  asio::serial_port_base::flow_control opt_flow,
70  asio::serial_port_base::stop_bits opt_stop ):
71 AsyncSerial ( devname, baud_rate, opt_parity, opt_csize, opt_flow, opt_stop )
72 {
73  setReadCallback ( boost::bind ( &BufferedAsyncSerial::readCallback, this, _1, _2 ) );
74 }
75 
76 size_t BufferedAsyncSerial::read ( char *data, size_t size )
77 {
78  lock_guard < mutex > l ( readQueueMutex );
79  size_t
80  result = min ( size, readQueue.size ( ) );
81 
82  vector < char >::iterator
83  it = readQueue.begin ( ) + result;
84 
85  copy ( readQueue.begin ( ), it, data );
86  readQueue.erase ( readQueue.begin ( ), it );
87  return result;
88 }
89 
90 std::vector < char >
92 {
93  lock_guard < mutex > l ( readQueueMutex );
94  vector < char >result;
95 
96  result.swap ( readQueue );
97  return result;
98 }
99 
101 {
102  lock_guard < mutex > l ( readQueueMutex );
103  string result ( readQueue.begin ( ), readQueue.end ( ) );
104 
105  readQueue.clear ( );
106  return result;
107 }
108 
109 std::string BufferedAsyncSerial::readStringUntil ( const std::string delim )
110 {
111  lock_guard < mutex > l ( readQueueMutex );
112  vector < char >::iterator
113  it = findStringInVector ( readQueue, delim );
114 
115  if ( it == readQueue.end ( ) )
116  return "";
117  string result ( readQueue.begin ( ), it );
118 
119  it += delim.size ( ); //Do remove the delimiter from the queue
120  readQueue.erase ( readQueue.begin ( ), it );
121  return result;
122 }
123 
124 void
125  BufferedAsyncSerial::readCallback ( const char *data, size_t len )
126 {
127  lock_guard < mutex > l ( readQueueMutex );
128  readQueue.insert ( readQueue.end ( ), data, data + len );
129 }
130 
131 std::vector < char >::iterator
132 BufferedAsyncSerial::findStringInVector ( std::vector < char >&v, const std::string & s )
133 {
134  if ( s.size ( ) == 0 )
135  return v.end ( );
136 
137  vector < char >::iterator it = v.begin ( );
138 
139  for ( ;; )
140  {
141  vector < char >::iterator result = find ( it, v.end ( ), s[0] );
142 
143  if ( result == v.end ( ) )
144  return v.end ( ); //If not found return
145 
146  for ( size_t i = 0; i < s.size ( ); i++ )
147  {
148  vector < char >::iterator temp = result + i;
149 
150  if ( temp == v.end ( ) )
151  return v.end ( );
152  if ( s[i] != *temp )
153  goto mismatch;
154  }
155  //Found
156  return result;
157 
158  mismatch:
159  it = result + 1;
160  }
161 }
162 
164 {
165  clearReadCallback ( );
166 }
void readCallback(const char *data, size_t len)
Asyncronous serial class Asyncronous serial class. Intended to be a base class.
Definition: AsyncSerial.h:66
std::string readStringUntil(const std::string delim="\n")
void setReadCallback(const boost::function< void(const char *, size_t) > &callback)
std::vector< char > read()
std::vector< char > readQueue
Class found to make buffers for Serial communications ()
static std::vector< char >::iterator findStringInVector(std::vector< char > &v, const std::string &s)
void clearReadCallback()


pressure_cells
Author(s): Emilio Estrelinha
autogenerated on Mon Mar 2 2015 01:32:47