BufferedAsyncSerial.h
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.h
39  * Author: Terraneo Federico
40  * Distributed under the Boost Software License, Version 1.0.
41  * Created on January 6, 2011, 3:31 PM
42  */
43 
44 #include <serialcom/AsyncSerial.h>
45 
46 #ifndef BUFFEREDASYNCSERIAL_H
47 #define BUFFEREDASYNCSERIAL_H
48 
54 {
55  public:
57  :AsyncSerial()
58  {
59  setReadCallback(boost::bind(&BufferedAsyncSerial::readCallback,this,_1,_2));
60  }
61 
73  BufferedAsyncSerial ( const std::string & devname, unsigned int baud_rate,
74  boost::asio::serial_port_base::parity opt_parity =
75  boost::asio::serial_port_base::parity ( boost::asio::serial_port_base::parity::
76  none ),
77  boost::asio::serial_port_base::character_size opt_csize =
78  boost::asio::serial_port_base::character_size ( 8 ),
79  boost::asio::serial_port_base::flow_control opt_flow =
80  boost::asio::serial_port_base::flow_control ( boost::asio::serial_port_base::flow_control::
81  none ),
82  boost::asio::serial_port_base::stop_bits opt_stop =
83  boost::asio::serial_port_base::stop_bits ( boost::asio::serial_port_base::stop_bits::
84  one ) )
85  :AsyncSerial ( devname, baud_rate, opt_parity, opt_csize, opt_flow, opt_stop )
86  {
87  setReadCallback ( boost::bind ( &BufferedAsyncSerial::readCallback, this, _1, _2 ) );
88  }
89 
96  size_t read ( char *data, size_t size )
97  {
98  boost::lock_guard < boost::mutex > l ( readQueueMutex );
99  size_t result = std::min ( size, readQueue.size ( ) );
100 
101  std::vector < char >::iterator it = readQueue.begin ( ) + result;
102 
103  copy ( readQueue.begin ( ), it, data );
104  readQueue.erase ( readQueue.begin ( ), it );
105  return result;
106  }
107 
112  std::vector < char >read ( )
113  {
114  boost::lock_guard < boost::mutex > l ( readQueueMutex );
115  std::vector < char >result;
116 
117  result.swap ( readQueue );
118  return result;
119  }
120 
128  std::string readString ( )
129  {
130  boost::lock_guard < boost::mutex > l ( readQueueMutex );
131  std::string result ( readQueue.begin ( ), readQueue.end ( ) );
132 
133  readQueue.clear ( );
134  return result;
135  }
136 
146  std::string readStringUntil ( const std::string delim = "\n" )
147  {
148  boost::lock_guard < boost::mutex > l ( readQueueMutex );
149  std::vector < char >::iterator it = findStringInVector ( readQueue, delim );
150 
151  if ( it == readQueue.end ( ) )
152  return "";
153 
154  std::string result ( readQueue.begin ( ), it );
155 
156  it += delim.size ( ); //Do remove the delimiter from the queue
157  readQueue.erase ( readQueue.begin ( ), it );
158  return result;
159  }
160 
162  {
164  }
165 
166  private:
167 
171  void readCallback ( const char *data, size_t len )
172  {
173  boost::lock_guard < boost::mutex > l ( readQueueMutex );
174  readQueue.insert ( readQueue.end ( ), data, data + len );
175  }
183  static std::vector < char >::iterator findStringInVector ( std::vector < char >&v,const std::string & s )
184  {
185  if ( s.size ( ) == 0 )
186  return v.end ( );
187 
188  std::vector < char >::iterator it = v.begin ( );
189 
190  for ( ;; )
191  {
192  std::vector < char >::iterator result = find ( it, v.end ( ), s[0] );
193 
194  if ( result == v.end ( ) )
195  return v.end ( ); //If not found return
196 
197  for ( size_t i = 0; i < s.size ( ); i++ )
198  {
199  std::vector < char >::iterator temp = result + i;
200 
201  if ( temp == v.end ( ) )
202  return v.end ( );
203  if ( s[i] != *temp )
204  goto mismatch;
205  }
206 
207  //Found
208  return result;
209 
210  mismatch:
211  it = result + 1;
212  }
213  }
214 
215  std::vector < char >readQueue;
216  boost::mutex readQueueMutex;
217 };
218 
219 #endif //BUFFEREDASYNCSERIAL_H
static std::vector< char >::iterator findStringInVector(std::vector< char > &v, const std::string &s)
BufferedAsyncSerial(const std::string &devname, unsigned int baud_rate, boost::asio::serial_port_base::parity opt_parity=boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none), boost::asio::serial_port_base::character_size opt_csize=boost::asio::serial_port_base::character_size(8), boost::asio::serial_port_base::flow_control opt_flow=boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none), boost::asio::serial_port_base::stop_bits opt_stop=boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one))
void readCallback(const char *data, size_t len)
Asyncronous serial class Asyncronous serial class. Intended to be a base class.
Definition: AsyncSerial.h:108
Asynchronous serial class w/ buffer Asynchronous serial class that sends data to buffer after reading...
std::string readStringUntil(const std::string delim="\n")
Class found to work with asyncronous Serial communications ()
void setReadCallback(const boost::function< void(const char *, size_t) > &callback)
Definition: AsyncSerial.h:426
std::vector< char > read()
std::vector< char > readQueue
size_t read(char *data, size_t size)
void clearReadCallback()
Definition: AsyncSerial.h:437


serialcom
Author(s): Ricardo Pascoal
autogenerated on Mon Mar 2 2015 01:32:51