<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>GearBox Project</title> <link rel="icon" href="gbx_icon_cardbox_sky.png" type="image/png"> <link rel="shortcut icon" href="gbx_icon_cardbox_sky.png" type="image/png"> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> <!-- Google Analytics --> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-3583782-1"); pageTracker._initData(); pageTracker._trackPageview(); </script> <!-- Returning you to your regular programming --> </head><body> <!-- Header --> <table border="0" cellspacing="0" cellpadding="0"> <tr><!-- create row --> <!-- spacer cell --> <!-- #CC CC CC --> <td rowspan="1" colspan="1" bgcolor="#EEEEEE"> </td> <!-- menu cell --> <td width="170" rowspan="1" colspan="1" align="left" valign="top" bgcolor="#EEEEEE"> <p> </p> <p align="center"><a href="index.html"><img src="gbx_logo_cardbox_sky_150x150.png"></a></p><br> <strong>INTRODUCTION</strong><br> <strong><a href="gbx_doc_overview.html" style="text-decoration:none">Overview</a></strong><br> <strong><a href="gbx_doc_getting.html" style="text-decoration:none">Download and Install</a></strong><br> <!-- <strong><a href="gbx_doc_quickstart.html" style="text-decoration:none">Quick Start</a></strong><br> --> <strong><a href="gbx_doc_documentation.html" style="text-decoration:none">Documentation</a></strong><br> <strong><a href="gbx_doc_publications.html" style="text-decoration:none">Publications</a></strong><br> <br> <strong>REPOSITORY</strong><br> <strong><a href="group__gbx__libs.html" style="text-decoration:none">Libraries</a></strong><br> <!-- <strong><a href="group__gbx__utils.html" style="text-decoration:none">Utilities</a></strong><br> --> <br> <strong>DEVELOPER</strong><br> <!--<strong><a href="gbx_doc_tutorials.html" style="text-decoration:none">Tutorials</a></strong><br> <strong><a href="group__gbx__examples.html" style="text-decoration:none">Examples</a></strong><br>--> <strong><a href="gbx_doc_devguide.html" style="text-decoration:none">Dev Guide</a></strong><br> <!-- <strong><a href="gbx_doc_faq.html" style="text-decoration:none">FAQ</a></strong><br> --> <strong><a href="http://cdash.acfr.usyd.edu.au/index.php?project=Gearbox" style="text-decoration:none">Dashboard</a></strong><br> <!--<strong><a href="http://wiki2.cas.edu.au/orca">Wiki</a></strong><br> login/pass: orca/orca<br>--> <br> <strong>PEOPLE</strong><br> <strong><a href="gbx_doc_contributors.html" style="text-decoration:none">Contributors</a></strong><br> <strong><a href="gbx_doc_users.html" style="text-decoration:none">Users</a></strong><br> <br> <a href="http://sourceforge.net"><img src="http://sourceforge.net/sflogo.php?group_id=216468&type=2" width="125" height="37" border="0" alt="SourceForge.net Logo" /></a><br> <strong><a href="http://sourceforge.net/projects/gearbox">Project</a></strong><br> <strong><a href="http://sourceforge.net/project/showfiles.php?group_id=216468">Download</a></strong><br> <!--<strong><a href="http://sourceforge.net/tracker/?group_id=216468">Bugs/Feedback</a></strong><br>--> <strong><a href="http://sourceforge.net/mail/?group_id=216468">Mailing lists</a></strong></p> <p> </p> </td><!-- /menu cell --> <!-- one grey spacer cells --> <td rowspan="1" colspan="1" bgcolor="#EEEEEE"> </td> <!-- one white spacer cells --> <td rowspan="1" colspan="1"> </td> <!-- main content cell --> <td rowspan="1" colspan="1" align="left" valign="top"> <!-- Generated by Doxygen 1.6.2-20100208 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_b8ba9ebcb71a95a44c4fd60b7d348fe1.html">src</a> » <a class="el" href="dir_e6faee39d20d45fac309cc35f2ddb285.html">gbxsickacfr</a> » <a class="el" href="dir_a90ec821537f39ebb95efc1f6e214043.html">gbxserialdeviceacfr</a> </div> </div> <div class="contents"> <h1>serialdevicehandler.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics</span> <a name="l00003"></a>00003 <span class="comment"> * http://gearbox.sf.net/</span> <a name="l00004"></a>00004 <span class="comment"> * Copyright (c) 2004-2008 Alex Brooks</span> <a name="l00005"></a>00005 <span class="comment"> *</span> <a name="l00006"></a>00006 <span class="comment"> * This distribution is licensed to you under the terms described in</span> <a name="l00007"></a>00007 <span class="comment"> * the LICENSE file included in this distribution.</span> <a name="l00008"></a>00008 <span class="comment"> *</span> <a name="l00009"></a>00009 <span class="comment"> */</span> <a name="l00010"></a>00010 <span class="preprocessor">#ifndef GBXSERIALDEVICEACFR_SERIALDEVICEHANDLER_H</span> <a name="l00011"></a>00011 <span class="preprocessor"></span><span class="preprocessor">#define GBXSERIALDEVICEACFR_SERIALDEVICEHANDLER_H</span> <a name="l00012"></a>00012 <span class="preprocessor"></span> <a name="l00013"></a>00013 <span class="preprocessor">#include <gbxserialacfr/serial.h></span> <a name="l00014"></a>00014 <span class="preprocessor">#include <gbxutilacfr/substatus.h></span> <a name="l00015"></a>00015 <span class="preprocessor">#include <gbxsickacfr/gbxiceutilacfr/safethread.h></span> <a name="l00016"></a>00016 <span class="preprocessor">#include <gbxsickacfr/gbxiceutilacfr/buffer.h></span> <a name="l00017"></a>00017 <span class="preprocessor">#include <IceUtil/IceUtil.h></span> <a name="l00018"></a>00018 <a name="l00019"></a>00019 <span class="keyword">namespace </span>gbxserialdeviceacfr { <a name="l00020"></a>00020 <a name="l00022"></a><a class="code" href="classgbxserialdeviceacfr_1_1IResponse.html">00022</a> <span class="keyword">class </span><a class="code" href="classgbxserialdeviceacfr_1_1IResponse.html" title="A generic Response: a message received from the device.">IResponse</a> : <span class="keyword">public</span> IceUtil::Shared <a name="l00023"></a>00023 { <a name="l00024"></a>00024 <span class="keyword">public</span>: <a name="l00025"></a>00025 ~<a class="code" href="classgbxserialdeviceacfr_1_1IResponse.html" title="A generic Response: a message received from the device.">IResponse</a>() {} <a name="l00026"></a>00026 <a name="l00027"></a>00027 <span class="comment">// Does the response indicate a warning condition?</span> <a name="l00028"></a>00028 <span class="comment">// If so, this will be reported to SerialDeviceHandler's status.</span> <a name="l00029"></a>00029 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> isWarn() <span class="keyword">const</span>=0; <a name="l00030"></a>00030 <span class="comment">// Does the response indicate an error condition?</span> <a name="l00031"></a>00031 <span class="comment">// If so, this will be reported to SerialDeviceHandler's status.</span> <a name="l00032"></a>00032 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> isError() <span class="keyword">const</span>=0; <a name="l00033"></a>00033 <a name="l00034"></a>00034 <span class="comment">// Human-readable string</span> <a name="l00035"></a>00035 <span class="keyword">virtual</span> std::string toString() <span class="keyword">const</span>=0; <a name="l00036"></a>00036 }; <a name="l00037"></a>00037 <span class="keyword">typedef</span> IceUtil::Handle<IResponse> IResponsePtr; <a name="l00038"></a>00038 <a name="l00040"></a><a class="code" href="classgbxserialdeviceacfr_1_1TimedResponse.html">00040</a> <span class="keyword">class </span><a class="code" href="classgbxserialdeviceacfr_1_1TimedResponse.html" title="Response plus a timeStamp: a simple container to keep the two together.">TimedResponse</a> { <a name="l00041"></a>00041 <span class="keyword">public</span>: <a name="l00042"></a>00042 <a name="l00043"></a>00043 <span class="comment">// Require an empty constructor to put in a buffer</span> <a name="l00044"></a>00044 <a class="code" href="classgbxserialdeviceacfr_1_1TimedResponse.html" title="Response plus a timeStamp: a simple container to keep the two together.">TimedResponse</a>() {} <a name="l00045"></a>00045 <a class="code" href="classgbxserialdeviceacfr_1_1TimedResponse.html" title="Response plus a timeStamp: a simple container to keep the two together.">TimedResponse</a>( <span class="keywordtype">int</span> s, <span class="keywordtype">int</span> us, <span class="keyword">const</span> IResponsePtr &r ) <a name="l00046"></a>00046 : timeStampSec(s), timeStampUsec(us), response(r) {} <a name="l00047"></a>00047 <a name="l00048"></a>00048 <span class="keywordtype">int</span> timeStampSec; <a name="l00049"></a>00049 <span class="keywordtype">int</span> timeStampUsec; <a name="l00050"></a>00050 IResponsePtr response; <a name="l00051"></a>00051 }; <a name="l00052"></a>00052 <a name="l00057"></a><a class="code" href="classgbxserialdeviceacfr_1_1IResponseParser.html">00057</a> <span class="keyword">class </span><a class="code" href="classgbxserialdeviceacfr_1_1IResponseParser.html">IResponseParser</a> { <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="keyword">public</span>: <a name="l00060"></a>00060 <a name="l00061"></a>00061 <span class="keyword">virtual</span> ~<a class="code" href="classgbxserialdeviceacfr_1_1IResponseParser.html">IResponseParser</a>() {} <a name="l00062"></a>00062 <a name="l00063"></a>00063 <span class="comment">// Parses the contents of the buffer.</span> <a name="l00064"></a>00064 <span class="comment">// Params:</span> <a name="l00065"></a>00065 <span class="comment">// - 'response': the parsed response</span> <a name="l00066"></a>00066 <span class="comment">// - 'numBytesParsed': this function will set this to the number of bytes parsed</span> <a name="l00067"></a>00067 <span class="comment">// (irrespective of whether or not a valid response was found)</span> <a name="l00068"></a>00068 <span class="comment">// Returns:</span> <a name="l00069"></a>00069 <span class="comment">// - true: a valid response was found.</span> <a name="l00070"></a>00070 <span class="comment">// - false: no valid response found, but we still might have parsed (and thrown out) some bytes.</span> <a name="l00071"></a>00071 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> parseBuffer( <span class="keyword">const</span> std::vector<char> &buffer, <a name="l00072"></a>00072 IResponsePtr &response, <a name="l00073"></a>00073 <span class="keywordtype">int</span> &numBytesParsed )=0; <a name="l00074"></a>00074 <a name="l00075"></a>00075 }; <a name="l00076"></a>00076 <a name="l00090"></a><a class="code" href="classgbxserialdeviceacfr_1_1SerialDeviceHandler.html">00090</a> <span class="keyword">class </span><a class="code" href="classgbxserialdeviceacfr_1_1SerialDeviceHandler.html" title="Handles the serial port.">SerialDeviceHandler</a> : <span class="keyword">public</span> gbxiceutilacfr::SafeThread <a name="l00091"></a>00091 { <a name="l00092"></a>00092 <a name="l00093"></a>00093 <span class="keyword">public</span>: <a name="l00094"></a>00094 <a name="l00095"></a>00095 <span class="comment">// Params:</span> <a name="l00096"></a>00096 <span class="comment">// - subsysName: given to Status</span> <a name="l00097"></a>00097 <span class="comment">// - unparsedBytesWarnThreshold: if we get more than this many un-parsed bytes packed into the</span> <a name="l00098"></a>00098 <span class="comment">// receive buffer, flag a warning.</span> <a name="l00099"></a>00099 <span class="comment">// - serialPort_: must have timeouts enabled</span> <a name="l00100"></a>00100 <a class="code" href="classgbxserialdeviceacfr_1_1SerialDeviceHandler.html" title="Handles the serial port.">SerialDeviceHandler</a>( <span class="keyword">const</span> std::string &subsysName, <a name="l00101"></a>00101 <a class="code" href="classgbxserialacfr_1_1Serial.html" title="Encapsulates a serial port.">gbxserialacfr::Serial</a> &serialPort, <a name="l00102"></a>00102 <a class="code" href="classgbxserialdeviceacfr_1_1IResponseParser.html">IResponseParser</a> &responseParser, <a name="l00103"></a>00103 <a class="code" href="classgbxutilacfr_1_1Tracer.html" title="Local and remote tracing.">gbxutilacfr::Tracer</a> &tracer, <a name="l00104"></a>00104 <a class="code" href="classgbxutilacfr_1_1Status.html" title="Local interface to component status.">gbxutilacfr::Status</a> &status, <a name="l00105"></a>00105 <span class="keywordtype">int</span> unparsedBytesWarnThreshold = 20000 ); <a name="l00106"></a>00106 <a name="l00107"></a>00107 ~<a class="code" href="classgbxserialdeviceacfr_1_1SerialDeviceHandler.html" title="Handles the serial port.">SerialDeviceHandler</a>(); <a name="l00108"></a>00108 <a name="l00109"></a>00109 <span class="comment">// Send the bytes to the device</span> <a name="l00110"></a>00110 <span class="keywordtype">void</span> send( <span class="keyword">const</span> <span class="keywordtype">char</span>* commandBytes, <span class="keywordtype">int</span> numCommandBytes ); <a name="l00111"></a>00111 <a name="l00112"></a>00112 <span class="comment">// allows changing of baud rates on-the-fly</span> <a name="l00113"></a>00113 <span class="keywordtype">void</span> setBaudRate( <span class="keywordtype">int</span> baudRate ); <a name="l00114"></a>00114 <a name="l00115"></a>00115 <span class="comment">// The main thread function, inherited from SubsystemThread</span> <a name="l00116"></a>00116 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classgbxserialdeviceacfr_1_1SerialDeviceHandler.html#a12d8c882444976704a7ad4a26c919b9e">walk</a>(); <a name="l00117"></a>00117 <a name="l00118"></a>00118 <span class="comment">// Allow external non-const access direct to (thread-safe) responseBuffer.</span> <a name="l00119"></a>00119 <span class="comment">// This is what you use to hear responses.</span> <a name="l00120"></a>00120 <a class="code" href="classgbxiceutilacfr_1_1Buffer.html">gbxiceutilacfr::Buffer<TimedResponse></a> &responseBuffer() { <span class="keywordflow">return</span> responseBuffer_; } <a name="l00121"></a>00121 <a name="l00122"></a>00122 <span class="keyword">private</span>: <a name="l00123"></a>00123 <a name="l00124"></a>00124 <span class="comment">// Returns: true if got data, false if timed out</span> <a name="l00125"></a>00125 <span class="keywordtype">bool</span> getDataFromSerial(); <a name="l00126"></a>00126 <span class="comment">// Returns: true if statusOK, false it something bad happened</span> <a name="l00127"></a>00127 <span class="keywordtype">bool</span> processBuffer(); <a name="l00128"></a>00128 <a name="l00129"></a>00129 <a class="code" href="classgbxserialacfr_1_1Serial.html" title="Encapsulates a serial port.">gbxserialacfr::Serial</a> &serial_; <a name="l00130"></a>00130 <a name="l00131"></a>00131 <span class="comment">// Knows how to parse for responses</span> <a name="l00132"></a>00132 <a class="code" href="classgbxserialdeviceacfr_1_1IResponseParser.html">IResponseParser</a> &responseParser_; <a name="l00133"></a>00133 <a name="l00134"></a>00134 <span class="comment">// Contains un-parsed data from the device</span> <a name="l00135"></a>00135 std::vector<char> buffer_; <a name="l00136"></a>00136 <a name="l00137"></a>00137 <span class="comment">// Thread-safe store of responses from the device</span> <a name="l00138"></a>00138 <a class="code" href="classgbxiceutilacfr_1_1Buffer.html">gbxiceutilacfr::Buffer<TimedResponse></a> responseBuffer_; <a name="l00139"></a>00139 <a name="l00140"></a>00140 <span class="keywordtype">int</span> unparsedBytesWarnThreshold_; <a name="l00141"></a>00141 <a name="l00142"></a>00142 <a class="code" href="classgbxutilacfr_1_1Tracer.html" title="Local and remote tracing.">gbxutilacfr::Tracer</a>& tracer_; <a name="l00143"></a>00143 <a class="code" href="classgbxutilacfr_1_1SubStatus.html" title="Convenience class which maniupulates the status of a subsystem.">gbxutilacfr::SubStatus</a> subStatus_; <a name="l00144"></a>00144 }; <a name="l00146"></a>00146 <span class="keyword">typedef</span> IceUtil::Handle<SerialDeviceHandler> SerialDeviceHandlerPtr; <a name="l00147"></a>00147 <a name="l00149"></a>00149 <span class="comment">// Printing Functions</span> <a name="l00151"></a>00151 <span class="comment"></span> <a name="l00152"></a>00152 std::string toHexString( <span class="keyword">const</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> bufLen ); <a name="l00153"></a>00153 <span class="keyword">inline</span> std::string toHexString( <span class="keyword">const</span> std::vector<char> &buf ) <a name="l00154"></a>00154 {<span class="keywordflow">return</span> toHexString( &(buf[0]), buf.size() );} <a name="l00155"></a>00155 <a name="l00156"></a>00156 std::string toAsciiString( <span class="keyword">const</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> bufLen ); <a name="l00157"></a>00157 <span class="keyword">inline</span> std::string toAsciiString( <span class="keyword">const</span> std::vector<char> &buf ) <a name="l00158"></a>00158 {<span class="keywordflow">return</span> toAsciiString( &(buf[0]), buf.size() );} <a name="l00159"></a>00159 <a name="l00160"></a>00160 } <span class="comment">// namespace</span> <a name="l00161"></a>00161 <a name="l00162"></a>00162 <span class="preprocessor">#endif</span> </pre></div></div> </td> <td colspan="1"> </td> </tr> </table> <!-- end of table --> <hr size="1"><address style="align: right;"><small>Generated for GearBox by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.5 </small></address> </body> </html>