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
1 /*
2  * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics
3  * http://gearbox.sf.net/
4  * Copyright (c) 2004-2010 Alex Brooks
5  *
6  * This distribution is licensed to you under the terms described in
7  * the LICENSE file included in this distribution.
8  *
9  */
10 #ifndef GBXSERIALDEVICEACFR_SERIALDEVICEHANDLER_H
11 #define GBXSERIALDEVICEACFR_SERIALDEVICEHANDLER_H
12 
13 #include <gbxserialacfr/serial.h>
14 #include <gbxutilacfr/substatus.h>
15 #include <gbxsickacfr/gbxiceutilacfr/safethread.h>
16 #include <gbxsickacfr/gbxiceutilacfr/store.h>
17 #include <IceUtil/IceUtil.h>
18 
19 namespace gbxserialdeviceacfr {
20 
22 class RxMsg : public IceUtil::Shared
23 {
24 public:
25  ~RxMsg() {}
26 
27  // Does the message indicate a warning condition?
28  // If so, this will be reported to SerialDeviceHandler's status.
29  virtual bool isWarn() const { return false; }
30  // Does the message indicate an error condition?
31  // If so, this will be reported to SerialDeviceHandler's status.
32  virtual bool isError() const { return false; }
33 
34  // Human-readable string
35  virtual std::string toString() const=0;
36 };
37 typedef IceUtil::Handle<RxMsg> RxMsgPtr;
38 
43 class RxMsgParser {
44 public:
45  virtual ~RxMsgParser() {}
46 
47  // Parses the contents of the buffer.
48  // Params:
49  // - 'numBytesParsed': this function sets this to the number of bytes parsed
50  // (irrespective of whether or not a valid RxMsg was found)
51  // Returns:
52  // - the parsed received message, if it is found, or
53  // - NULL if no message is found.
54  virtual RxMsgPtr parseBuffer( const std::vector<char> &buffer,
55  int &numBytesParsed ) = 0;
56 };
57 
67 public:
68  virtual ~RxMsgCallback() {}
69 
70  virtual void msgReceived( const RxMsgPtr &msg,
71  int timeStampSec,
72  int timeStampUsec )=0;
73 };
74 
89 {
90 
91 public:
92 
93  // Params:
94  // - subsysName: given to Status
95  // - unparsedBytesWarnThreshold: if we get more than this many un-parsed bytes packed into the
96  // receive buffer, flag a warning.
97  // - serialPort_: must have timeouts enabled
98  SerialDeviceHandler( const std::string &subsysName,
99  gbxserialacfr::Serial &serialPort,
100  RxMsgParser &rxMsgParser,
101  gbxutilacfr::Tracer &tracer,
102  gbxutilacfr::Status &status,
103  bool verboseDebug=false,
104  int unparsedBytesWarnThreshold = 20000 );
105  void setRxMsgCallback( RxMsgCallback &callback )
106  { rxMsgCallback_ = &callback; }
107 
109 
110  // Send the bytes to the device
111  void send( const char* commandBytes, int numCommandBytes );
112 
113  // allows changing of baud rates on-the-fly
114  void setBaudRate( int baudRate );
115 
116  // The SerialDeviceHandler runs in its own thread -- this lets you ensure that it's really running.
117  void startAndWaitTillIsRunning();
118 
119 private:
120 
121  // The main thread function, inherited from SubsystemThread
122  virtual void walk();
123 
124  // interrogate whether or not that thread is running.
125  bool isRunning();
126 
127  // Returns: true if got data, false if timed out
128  bool getDataFromSerial();
129  // Returns: true if statusOK, false it something bad happened
130  bool processBuffer();
131 
132  gbxserialacfr::Serial &serial_;
133 
134  // Knows how to parse for rxMsgs
135  RxMsgParser &rxMsgParser_;
136  // Knows what to do with them
137  RxMsgCallback *rxMsgCallback_;
138 
139  // Contains un-parsed data from the device
140  std::vector<char> buffer_;
141 
142  gbxiceutilacfr::Store<bool> isRunningStore_;
143 
144  int unparsedBytesWarnThreshold_;
145 
146  gbxutilacfr::Tracer& tracer_;
147  gbxutilacfr::SubStatus subStatus_;
148 
149  const bool verboseDebug_;
150 };
152 typedef IceUtil::Handle<SerialDeviceHandler> SerialDeviceHandlerPtr;
153 
154 } // namespace
155 
156 #endif
Definition: serialdevicehandler.h:43
Definition: serialdevicehandler.h:66
Encapsulates a serial port.
Definition: serial.h:43
A version of the Thread class which catches all possible exceptions.
Definition: safethread.h:43
Local and remote tracing.
Definition: tracer.h:111
A base-class for a message received from the device on the other end.
Definition: serialdevicehandler.h:22
Convenience class which maniupulates the status of a subsystem.
Definition: substatus.h:39
Definition: printutil.cpp:8
Handles the serial port.
Definition: serialdevicehandler.h:88
Local interface to component status.
Definition: status.h:253
 

Generated for GearBox by  doxygen 1.4.5