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-2010 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/store.h>
00017 #include <IceUtil/IceUtil.h>
00018 
00019 namespace gbxserialdeviceacfr {
00020 
00022 class RxMsg : public IceUtil::Shared
00023 {
00024 public:
00025     ~RxMsg() {}
00026     
00027     // Does the message indicate a warning condition?
00028     // If so, this will be reported to SerialDeviceHandler's status.
00029     virtual bool isWarn() const { return false; }
00030     // Does the message indicate an error condition?
00031     // If so, this will be reported to SerialDeviceHandler's status.
00032     virtual bool isError() const { return false; }
00033     
00034     // Human-readable string
00035     virtual std::string toString() const=0;
00036 };
00037 typedef IceUtil::Handle<RxMsg> RxMsgPtr;
00038 
00043 class RxMsgParser {
00044 public:
00045     virtual ~RxMsgParser() {}
00046 
00047     // Parses the contents of the buffer.
00048     // Params:
00049     //   - 'numBytesParsed': this function sets this to the number of bytes parsed
00050     //                       (irrespective of whether or not a valid RxMsg was found)
00051     // Returns:
00052     //   - the parsed received message, if it is found, or
00053     //   - NULL if no message is found.
00054     virtual RxMsgPtr parseBuffer( const std::vector<char> &buffer,
00055                                   int                     &numBytesParsed ) = 0;
00056 };
00057 
00066 class RxMsgCallback {
00067 public:
00068     virtual ~RxMsgCallback() {}
00069 
00070     virtual void msgReceived( const RxMsgPtr &msg,
00071                               int             timeStampSec,
00072                               int             timeStampUsec )=0;
00073 };
00074 
00088 class SerialDeviceHandler : public gbxiceutilacfr::SafeThread
00089 {
00090 
00091 public: 
00092 
00093     // Params:
00094     //   - subsysName: given to Status
00095     //   - unparsedBytesWarnThreshold: if we get more than this many un-parsed bytes packed into the
00096     //                                 receive buffer, flag a warning.
00097     //   - serialPort_: must have timeouts enabled
00098     SerialDeviceHandler( const std::string     &subsysName,
00099                          gbxserialacfr::Serial &serialPort,
00100                          RxMsgParser           &rxMsgParser,
00101                          gbxutilacfr::Tracer   &tracer,
00102                          gbxutilacfr::Status   &status,
00103                          bool                   verboseDebug=false,
00104                          int                    unparsedBytesWarnThreshold = 20000 );
00105     void setRxMsgCallback( RxMsgCallback &callback )
00106         { rxMsgCallback_ = &callback; }
00107 
00108     ~SerialDeviceHandler();
00109 
00110     // Send the bytes to the device
00111     void send( const char* commandBytes, int numCommandBytes );
00112 
00113     // allows changing of baud rates on-the-fly
00114     void setBaudRate( int baudRate );
00115 
00116     // The SerialDeviceHandler runs in its own thread -- this lets you ensure that it's really running.
00117     void startAndWaitTillIsRunning();
00118 
00119 private: 
00120 
00121     // The main thread function, inherited from SubsystemThread
00122     virtual void walk();
00123 
00124     // interrogate whether or not that thread is running.
00125     bool isRunning();
00126 
00127     // Returns: true if got data, false if timed out
00128     bool getDataFromSerial();
00129     // Returns: true if statusOK, false it something bad happened
00130     bool processBuffer();
00131 
00132     gbxserialacfr::Serial &serial_;
00133 
00134     // Knows how to parse for rxMsgs
00135     RxMsgParser &rxMsgParser_;
00136     // Knows what to do with them
00137     RxMsgCallback *rxMsgCallback_;
00138 
00139     // Contains un-parsed data from the device
00140     std::vector<char> buffer_;
00141     
00142     gbxiceutilacfr::Store<bool> isRunningStore_;
00143 
00144     int unparsedBytesWarnThreshold_;
00145 
00146     gbxutilacfr::Tracer& tracer_;
00147     gbxutilacfr::SubStatus subStatus_;
00148 
00149     const bool verboseDebug_;
00150 };
00152 typedef IceUtil::Handle<SerialDeviceHandler> SerialDeviceHandlerPtr;
00153 
00154 } // namespace
00155 
00156 #endif
 

Generated for GearBox by  doxygen 1.4.5