INTRODUCTION
Overview
Download and Install
Documentation
Publications

REPOSITORY
Libraries

DEVELOPER
Dev Guide
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         

serialhandler.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 
00011 #ifndef SICK_ACFR_SERIALHANDLER_H
00012 #define SICK_ACFR_SERIALHANDLER_H
00013 
00014 #include <gbxsickacfr/messages.h>
00015 #include <gbxserialacfr/serial.h>
00016 #include <gbxsickacfr/gbxiceutilacfr/thread.h>
00017 #include <gbxsickacfr/gbxiceutilacfr/buffer.h>
00018 #include <gbxsickacfr/gbxserialdeviceacfr/serialdevicehandler.h>
00019 #include <gbxsickacfr/gbxserialdeviceacfr/stickinbuffercallback.h>
00020 
00021 namespace gbxsickacfr {
00022 
00023 class RxMsgParser : public gbxserialdeviceacfr::RxMsgParser 
00024 {
00025 public:
00026     gbxserialdeviceacfr::RxMsgPtr parseBuffer( const std::vector<char>       &buffer,
00027                                                int                           &numBytesParsed )
00028         {
00029             LmsRxMsgPtr lmsRxMsg = parseBufferForRxMsgs( (const uChar*)&(buffer[0]),
00030                                                          buffer.size(),
00031                                                          numBytesParsed );
00032             return lmsRxMsg;
00033         }
00034 
00035 };
00036 
00037 // LmsRxMsg plus a timeStamp
00038 class TimedLmsRxMsg {
00039 public:
00040     TimedLmsRxMsg() {}
00041     TimedLmsRxMsg( int s, int us, const LmsRxMsgPtr &r )
00042         : timeStampSec(s), timeStampUsec(us), msg(r) {}
00043 
00044     int timeStampSec;
00045     int timeStampUsec;
00046     LmsRxMsgPtr msg;
00047 };
00048 
00049 //
00050 // @brief Handles the serial port.
00051 //
00052 // Read in this separate loop so we can hopefully grab the messages
00053 // as soon as they arrive, without relying on having Driver::read()
00054 // called by an external thread which may be doing other stuff.
00055 // This will hopefully give us more accurate timestamps.
00056 //
00057 // @author Alex Brooks
00058 //
00059 class SerialHandler
00060 {
00061 
00062 public: 
00063 
00064     SerialHandler( const std::string   &dev,
00065                    gbxutilacfr::Tracer &tracer,
00066                    gbxutilacfr::Status &status );
00067     ~SerialHandler();
00068 
00069     void send( const std::vector<uChar> &telegram )
00070         { serialDeviceHandler_->send( (const char*)&(telegram[0]), telegram.size() ); }
00071 
00072     void setBaudRate( int baudRate )
00073         { serialDeviceHandler_->setBaudRate( baudRate ); }
00074 
00075     // waits up to maxWaitMs for a RxMsg
00076     // return codes same as gbxiceutilacfr::Buffer
00077     int getNextRxMsg( TimedLmsRxMsg &timedRxMsg, int maxWaitMs )
00078         { 
00079             gbxserialdeviceacfr::TimedRxMsg genericTimedRxMsg;
00080             int ret = bufferCallback_.rxMsgBuffer().getAndPopWithTimeout( genericTimedRxMsg, maxWaitMs );
00081             if ( ret == 0 )
00082             {
00083                 timedRxMsg.timeStampSec = genericTimedRxMsg.timeStampSec;
00084                 timedRxMsg.timeStampUsec = genericTimedRxMsg.timeStampUsec;
00085 
00086                 // This cast is safe becuase the rxMsg had to have been generated by RxMsgParser
00087                 timedRxMsg.msg = LmsRxMsgPtr::dynamicCast( genericTimedRxMsg.msg );
00088             }
00089             return ret;
00090         }
00091 
00092 private: 
00093 
00094     RxMsgParser                               rxMsgParser_;
00095     gbxserialdeviceacfr::StickInBufferCallback bufferCallback_;
00096     gbxserialacfr::Serial                     serialPort_;
00097     gbxserialdeviceacfr::SerialDeviceHandler *serialDeviceHandler_;
00098     // Keep a smart pointer to the SerialDeviceHandler as a thread, for stop/start purposes
00099     gbxiceutilacfr::ThreadPtr                 serialDeviceHandlerThreadPtr_;
00100 };
00101 
00102 }
00103 
00104 #endif
 

Generated for GearBox by  doxygen 1.4.5