INTRODUCTION
Overview
Download and Install
Documentation
Publications

REPOSITORY
Libraries

DEVELOPER
Dev Guide
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         

serialdevicehandler.h

00001 /*
00002  * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics
00003  *               http://gearbox.sf.net/
00004  * Copyright (c) 2004-2008 Alex Brooks
00005  *
00006  * This distribution is licensed to you under the terms described in
00007  * the LICENSE file included in this distribution.
00008  *
00009  */
00010 #ifndef GBXSERIALDEVICEACFR_SERIALDEVICEHANDLER_H
00011 #define GBXSERIALDEVICEACFR_SERIALDEVICEHANDLER_H
00012 
00013 #include <gbxserialacfr/serial.h>
00014 #include <gbxutilacfr/substatus.h>
00015 #include <gbxsickacfr/gbxiceutilacfr/safethread.h>
00016 #include <gbxsickacfr/gbxiceutilacfr/buffer.h>
00017 #include <IceUtil/IceUtil.h>
00018 
00019 namespace gbxsickacfr {
00020 namespace gbxserialdeviceacfr {
00021 
00023 class IResponse : public IceUtil::Shared
00024 {
00025 public:
00026     ~IResponse() {}
00027     
00028     // Does the response indicate a warning condition?
00029     virtual bool isWarn() const=0;
00030     // Does the response indicate an error condition?
00031     virtual bool isError() const=0;
00032     
00033     // Human-readable string
00034     virtual std::string toString() const=0;
00035 };
00036 typedef IceUtil::Handle<IResponse> IResponsePtr;
00037 
00039 class TimedResponse {
00040 public:
00041 
00042     // Require an empty constructor to put in a buffer
00043     TimedResponse() {}
00044     TimedResponse( int s, int us, const IResponsePtr &r )
00045         : timeStampSec(s), timeStampUsec(us), response(r) {}
00046 
00047     int timeStampSec;
00048     int timeStampUsec;
00049     IResponsePtr response;
00050 };
00051 
00056 class IResponseParser {
00057 
00058 public:
00059 
00060     virtual ~IResponseParser() {}
00061 
00062     // Parses the contents of the buffer.
00063     // Params:
00064     //   - 'response':       the parsed response
00065     //   - 'numBytesParsed': this function will set this to the number of bytes parsed
00066     //                       (irrespective of whether or not a valid response was found)
00067     // Returns:
00068     //   - true:  a valid response was found.
00069     //   - false: no valid response found, but we still might have parsed (and thrown out) some bytes.
00070     virtual bool parseBuffer( const std::vector<char> &buffer,
00071                               IResponsePtr  &response,
00072                               int           &numBytesParsed )=0;
00073 
00074 };
00075 
00089 class SerialDeviceHandler : public gbxiceutilacfr::SafeThread
00090 {
00091 
00092 public: 
00093 
00094     // Params:
00095     //   - subsysName: given to Status
00096     //   - unparsedBytesWarnThreshold: if we get more than this many un-parsed bytes packed into the
00097     //                                 receive buffer, flag a warning.
00098     //   - serialPort_: must have timeouts enabled
00099     SerialDeviceHandler( const std::string     &subsysName,
00100                          gbxserialacfr::Serial &serialPort,
00101                          IResponseParser       &responseParser,
00102                          gbxutilacfr::Tracer   &tracer,
00103                          gbxutilacfr::Status   &status,
00104                          int                    unparsedBytesWarnThreshold = 20000 );
00105 
00106     ~SerialDeviceHandler();
00107 
00108     // Send the bytes to the device
00109     void send( const char* commandBytes, int numCommandBytes );
00110 
00111     // allows changing of baud rates on-the-fly
00112     void setBaudRate( int baudRate );
00113 
00114     // The main thread function, inherited from SubsystemThread
00115     virtual void walk();
00116 
00117     // Allow external non-const access direct to (thread-safe) responseBuffer
00118     gbxiceutilacfr::Buffer<TimedResponse> &responseBuffer() { return responseBuffer_; }
00119 
00120 private: 
00121 
00122     // Returns: true if got data, false if timed out
00123     bool getDataFromSerial();
00124     // Returns: true if statusOK, false it something bad happened
00125     bool processBuffer();
00126 
00127     gbxserialacfr::Serial &serial_;
00128 
00129     // Knows how to parse for responses
00130     IResponseParser &responseParser_;
00131 
00132     // Contains un-parsed data from the device
00133     std::vector<char> buffer_;
00134 
00135     // Thread-safe store of responses from the device
00136     gbxiceutilacfr::Buffer<TimedResponse> responseBuffer_;
00137 
00138     int unparsedBytesWarnThreshold_;
00139 
00140     gbxutilacfr::Tracer& tracer_;
00141     gbxutilacfr::SubStatus subStatus_;
00142 };
00143 
00145 // Printing Functions
00147 
00148 std::string toHexString( const char *buf, int bufLen );
00149 // inline std::string toHexString( char *buf, int bufLen )
00150 // { return toHexString( (const unsigned char *)buf, bufLen ); }
00151 inline std::string toHexString( const std::vector<char> &buf )
00152 {return toHexString( &(buf[0]), buf.size() );}
00153 
00154 }
00155 } // namespace
00156 
00157 #endif
 

Generated for GearBox by  doxygen 1.4.5