CARMA C++
GpibUsbController.h
Go to the documentation of this file.
1 // $Id: GpibUsbController.h,v 1.1 2010/12/13 21:06:30 eml Exp $
2 
3 #ifndef SZA_UTIL_GPIBUSBCONTROLLER_H
4 #define SZA_UTIL_GPIBUSBCONTROLLER_H
5 
15 #include "carma/szautil/CondVar.h"
19 #include "carma/szautil/TimeOut.h"
20 
21 #include <sstream>
22 
23 #define GPIB_RESPONSE_HANDLER(fn) bool (fn)(void* arg)
24 
25 namespace sza {
26  namespace util {
27 
28  class GpibUsbDevice;
29 
30  // A utility class for sending messages to the GpibUsbController task
31 
32  class GpibUsbControllerMsg : public GenericTaskMsg {
33  public:
34 
35  enum MsgType {
36  GPIB_CNT_MSG,
37  GPIB_DEV_MSG,
38  CONNECT,
39  };
40 
41  unsigned char cmd_[100];
42  bool expectsResponse_;
43  GPIB_RESPONSE_HANDLER(*responseHandler_);
44 
45  CondVar* condVar_;
46  void* retVal_;
47 
48  // A type for this message
49 
50  MsgType type_;
51  };
52 
53  class GpibUsbController : public SerialClient, public SpawnableTask<GpibUsbControllerMsg> {
54  public:
55 
56  // Constructor.
57 
58  GpibUsbController(bool doSpawn=false);
59  GpibUsbController(std::string port, bool doSpawn=false);
60 
61  // Destructor.
62 
63  virtual ~GpibUsbController();
64 
65  void stop();
66 
67  // Open the psuedo-serial port
68 
69  int connect();
70  int connectAndClear();
71 
72  //------------------------------------------------------------
73  // Controller commands
74  //------------------------------------------------------------
75 
76  // Set whether or not to auto-listen after a command
77 
78  void setAuto(bool doAuto);
79 
80  // Select the operational mode for the controller
81 
82  enum Mode {
83  MODE_CONTROLLER = 0,
84  MODE_DEVICE = 1,
85  };
86 
87  void setMode(Mode mode);
88 
89  // Set or get the GPIB address
90 
91  virtual void setAddress(unsigned address);
92  virtual unsigned getAddress();
93 
94  // Set or query if we are sending EOI
95 
96  void setEoi(bool sendEoi);
97  std::string getEoi();
98 
99  // Get the version
100 
101  std::string getVersion();
102 
103  // Get the help output
104 
105  std::string getHelp();
106 
107  // Set or query the EOS characters
108 
109  enum {
110  EOS_CRLF = 0,
111  EOS_CR = 1,
112  EOS_LF = 2,
113  EOS_NONE = 3,
114  };
115 
116  void setEos(unsigned);
117  std::string getEos();
118 
119  // Clear the interfacae
120 
121  void clearInterface();
122 
123  // Reset the controller
124 
125  void resetController();
126 
127  // This command enables or disables the appending of a user
128  // specified character (specified by setEotChar()) to network
129  // output whenever EOI is detected while reading a character
130  // from the GPIB port
131 
132  void enableEot(bool enable);
133 
134  // Thie command sets the character that should be appended when
135  // EOI is asserted
136 
137  void setEotChar(char eot);
138 
139  //------------------------------------------------------------
140  // Common Device commands
141  //------------------------------------------------------------
142 
143  // Get the device identification string
144 
145  std::string getDevice();
146 
147  // Reset the device
148 
149  void resetDevice();
150 
151  // Issue a GPIB clear command
152 
153  void clearDevice();
154 
155  // Send the device the 'clear interface' command
156 
157  void clearDeviceInterface();
158 
159  // Query the status of a self test
160 
161  std::string getSelfTest();
162 
163  //------------------------------------------------------------
164  // Other members
165  //------------------------------------------------------------
166 
167  std::ostringstream response_;
168  void* retVal_;
169 
170  // Wait until all overlapping commands have been executed
171 
172  void wait();
173 
174  // Response handlers
175 
176  static GPIB_RESPONSE_HANDLER(checkString);
177 
178  protected:
179 
180  friend class GpibUsbDevice;
181 
182  enum Receiver {
183  DEVICE = 0,
184  CONTROLLER = 1
185  };
186 
187  //------------------------------------------------------------
188  // Data members
189  //------------------------------------------------------------
190 
191  TimeOut cmdTimeOut_;
192 
193  std::string devName_;
194  std::string lastCmd_;
195  bool expectingResponse_;
196  GPIB_RESPONSE_HANDLER(*responseHandler_);
197  CondVar* condVar_;
198 
199  bool eotEnabled_;
200 
201  //------------------------------------------------------------
202  // Private methods
203  //------------------------------------------------------------
204 
205  void initialize(bool doSpawn);
206  void serviceMsgQ(void);
207  void processMsg(GpibUsbControllerMsg* msg);
208 
209  // Suspend or resume processing messages
210 
211  void suspendProcessingMessages(bool suspend);
212 
213  // Watch the serial port for a response, and set a timeout
214 
215  void watchForResponse(bool watch);
216 
217  // Respond to a timeout while waiting for a response
218 
219  void respondToTimeOut();
220 
221  // Read/continue reading a response
222 
223  void readResponse();
224 
225  // Respond to unexpected input from the controller
226 
227  void respondToUnexpectedInput();
228 
229  void sendDeviceCommand(std::string cmd, bool expectsResponse=false,
230  GPIB_RESPONSE_HANDLER(*handler)=0, bool block=false,
231  void* retVal=0);
232 
233  void sendControllerCommand(std::string cmd, bool expectsResponse=false,
234  GPIB_RESPONSE_HANDLER(*handler)=0, bool block=false, void* retVal=0);
235 
236  virtual void sendCommand(std::string cmd, Receiver rx, bool expectsResponse=false,
237  GPIB_RESPONSE_HANDLER(*handler)=0, bool block=false, void* retVal=0);
238 
239  void executeCommand(std::string cmd, bool expectsResponse,
240  GPIB_RESPONSE_HANDLER(*handler), CondVar* condVar, void* retVal);
241 
242  // Private execute methods for specific commands
243 
244  void executeConnect(void* retVal);
245 
246  static GPIB_RESPONSE_HANDLER(checkHandler);
247 
248  static GPIB_RESPONSE_HANDLER(checkAddress);
249  static GPIB_RESPONSE_HANDLER(checkDevice);
250  static GPIB_RESPONSE_HANDLER(checkCompletion);
251 
252  }; // End class GpibUsbController
253 
254  } // End namespace util
255 } // End namespace sza
256 
257 
258 
259 #endif // End #ifndef SZA_UTIL_GPIBUSBCONTROLLER_H
Tagged: Fri Nov 14 12:39:34 UTC 2003.
Tagged: Mon May 10 15:32:08 PDT 2004.
Tagged: Thu Oct 18 13:20:31 PDT 2007.
Tagged: Tue May 2 16:31:46 PDT 2006.
Tagged: Fri Jan 26 16:49:57 NZDT 2007.