INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
serialdevicehandler.h00001 /* 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 gbxserialdeviceacfr { 00020 00022 class IResponse : public IceUtil::Shared 00023 { 00024 public: 00025 ~IResponse() {} 00026 00027 // Does the response indicate a warning condition? 00028 // If so, this will be reported to SerialDeviceHandler's status. 00029 virtual bool isWarn() const=0; 00030 // Does the response indicate an error condition? 00031 // If so, this will be reported to SerialDeviceHandler's status. 00032 virtual bool isError() const=0; 00033 00034 // Human-readable string 00035 virtual std::string toString() const=0; 00036 }; 00037 typedef IceUtil::Handle<IResponse> IResponsePtr; 00038 00040 class TimedResponse { 00041 public: 00042 00043 // Require an empty constructor to put in a buffer 00044 TimedResponse() {} 00045 TimedResponse( int s, int us, const IResponsePtr &r ) 00046 : timeStampSec(s), timeStampUsec(us), response(r) {} 00047 00048 int timeStampSec; 00049 int timeStampUsec; 00050 IResponsePtr response; 00051 }; 00052 00057 class IResponseParser { 00058 00059 public: 00060 00061 virtual ~IResponseParser() {} 00062 00063 // Parses the contents of the buffer. 00064 // Params: 00065 // - 'response': the parsed response 00066 // - 'numBytesParsed': this function will set this to the number of bytes parsed 00067 // (irrespective of whether or not a valid response was found) 00068 // Returns: 00069 // - true: a valid response was found. 00070 // - false: no valid response found, but we still might have parsed (and thrown out) some bytes. 00071 virtual bool parseBuffer( const std::vector<char> &buffer, 00072 IResponsePtr &response, 00073 int &numBytesParsed )=0; 00074 00075 }; 00076 00090 class SerialDeviceHandler : public gbxiceutilacfr::SafeThread 00091 { 00092 00093 public: 00094 00095 // Params: 00096 // - subsysName: given to Status 00097 // - unparsedBytesWarnThreshold: if we get more than this many un-parsed bytes packed into the 00098 // receive buffer, flag a warning. 00099 // - serialPort_: must have timeouts enabled 00100 SerialDeviceHandler( const std::string &subsysName, 00101 gbxserialacfr::Serial &serialPort, 00102 IResponseParser &responseParser, 00103 gbxutilacfr::Tracer &tracer, 00104 gbxutilacfr::Status &status, 00105 int unparsedBytesWarnThreshold = 20000 ); 00106 00107 ~SerialDeviceHandler(); 00108 00109 // Send the bytes to the device 00110 void send( const char* commandBytes, int numCommandBytes ); 00111 00112 // allows changing of baud rates on-the-fly 00113 void setBaudRate( int baudRate ); 00114 00115 // The main thread function, inherited from SubsystemThread 00116 virtual void walk(); 00117 00118 // Allow external non-const access direct to (thread-safe) responseBuffer. 00119 // This is what you use to hear responses. 00120 gbxiceutilacfr::Buffer<TimedResponse> &responseBuffer() { return responseBuffer_; } 00121 00122 private: 00123 00124 // Returns: true if got data, false if timed out 00125 bool getDataFromSerial(); 00126 // Returns: true if statusOK, false it something bad happened 00127 bool processBuffer(); 00128 00129 gbxserialacfr::Serial &serial_; 00130 00131 // Knows how to parse for responses 00132 IResponseParser &responseParser_; 00133 00134 // Contains un-parsed data from the device 00135 std::vector<char> buffer_; 00136 00137 // Thread-safe store of responses from the device 00138 gbxiceutilacfr::Buffer<TimedResponse> responseBuffer_; 00139 00140 int unparsedBytesWarnThreshold_; 00141 00142 gbxutilacfr::Tracer& tracer_; 00143 gbxutilacfr::SubStatus subStatus_; 00144 }; 00146 typedef IceUtil::Handle<SerialDeviceHandler> SerialDeviceHandlerPtr; 00147 00149 // Printing Functions 00151 00152 std::string toHexString( const char *buf, int bufLen ); 00153 inline std::string toHexString( const std::vector<char> &buf ) 00154 {return toHexString( &(buf[0]), buf.size() );} 00155 00156 std::string toAsciiString( const char *buf, int bufLen ); 00157 inline std::string toAsciiString( const std::vector<char> &buf ) 00158 {return toAsciiString( &(buf[0]), buf.size() );} 00159 00160 } // namespace 00161 00162 #endif |