Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

dataset.h

00001 
00003 
00017 #ifndef DATASET_H
00018 #define DATASET_H
00019 
00020 #include <iostream>
00021 #include <pthread.h>
00022 
00023 #include "comms_device.h"
00024 
00025 
00026 class Dataset : public CommsDevice
00027 {
00028 
00029   public:
00030 
00032       enum { LOBAUD=4800, HIBAUD=38400 };
00033 
00035       enum { VERY_SHORT_TIMEOUT=100000, SHORT_TIMEOUT=200000,
00036              LONG_TIMEOUT=500000, VERY_LONG_TIMEOUT=2000000 };
00037 
00039       enum Mode_t { CONTROL=0xC0000000, MONITOR = 0x40000000 };
00040 
00042       enum Op_t { N8  = 0x000,  // None for F93 registers
00043               DA  = 0x000,  // Differential Analogue In
00044               SEA = 0x008,  // Single Ended Analogue In
00045               SB  = 0x040,  // Single Bit In/Out
00046           A8  = 0x060,  // Addressed 8 Bit In/Out
00047           A16 = 0x0A0,  // Addressed 16 Bit In/Out
00048           S8  = 0x0E0,  // Strobed 8 Bit In/Out
00049           S16 = 0x0E4,  // Strobed 16 Bit In/Out
00050           W   = 0x100,  // Read/Write Word
00051                   MXA = 0x120,  // Multiplexed Analogue In
00052                   OP_ERR = -1
00053                 };
00054 
00056       enum Cat_t { ANALOG, BIT1, REG8, REG16, BIT32, CAT_ERR=-1 };
00057 
00059       enum { E_NONE         = 0,
00060          W_BEL          = 1, 
00061          W_RESET        = 2, 
00062          W_END          = 3,
00063          E_READ_TIMEOUT = -1,
00064              E_OFFLINE      = -2,
00065          E_WRITE        = -3, 
00066          E_READ         = -4,
00067          E_NAK          = -5, 
00068          E_NREAD        = -6, 
00069          E_NWRIT        = -7,
00070              E_COMPARE      = -8,
00071              E_PORT         = -9,
00072              E_SOCKET       = -10,
00073          E_UNKNOWN      = -11,
00074          E_END          = -12 };
00075 
00076 
00078 
00083       Dataset(const char* id, int port, int baud);
00084 
00086 
00092       Dataset(const char* id, int port, int baud, int altPort);
00093     
00095 
00100       Dataset(const char* id, char* host, int sockPort);
00101   
00103 
00114       int init(int dsAddr, Op_t op, int ptAddr);
00115 
00117 
00126       int init(int addr);
00127 
00129       void clear();
00130 
00132       void close();
00133  
00135 
00145       int cmd(int dsAddr, Op_t op, int ptAddr, int data);
00146 
00148 
00156       int cmd(int addr, int data);
00157 
00159 
00169       int mon(int dsAddr, Op_t op, int ptAddr, int* data);
00170 
00172 
00180       int mon(int addr, int* data);
00181 
00183 
00192       int mon(int addr, int* data, int timeOut);
00193 
00195 
00205       int mon(int dsAddr, Op_t op, int ptAddr, float* volts);
00206 
00208 
00216       int mon(int addr, float* volts);
00217 
00219 
00225       int transact(char cmdBuf[10], char respBuf[5], int timeout);
00226 
00228       void setStdAnalogScaling();
00229 
00231       void setF93AnalogScaling();
00232 
00234       /*
00235       * @param mode Either CONTROL or MONITOR.
00236       * @param dsAddr   The %Dataset base address [0 to 31].
00237       * @param op   A %Dataset operation code.
00238       * @param ptAddr   The address of the %Dataset input or output.
00239       * @return     An address composed of the above parameters.
00240       * @see    Mode_t
00241       * @see    Op_t
00242       **/
00243       static int composeAddress(Mode_t mode, int dsAddr, Op_t op, int ptAddr);
00244 
00246 
00253       static int compare(int cmdVal, int monVal, Op_t op);
00254 
00256 
00261       static int bcd2dec(int bcdNum, int nBits);
00262 
00264 
00271       static const char* opToStr(Op_t op, Cat_t cat);
00272 
00274 
00278       static Op_t strToOp(const char* str);
00279 
00281 
00284       static Cat_t strToCat(const char* str);
00285 
00287       const char* const getSendMessage();
00288 
00290       const char* const getResponseMessage();
00291 
00293 
00297       const char* getErrMsg(int code);
00298 
00300       static bool isReplyProtocolChar(char c);
00301 
00303       void setDebug(bool onOff) { debug = onOff; };
00304 
00306       static void setDebugAll(bool onOff) { debugAll = onOff; };
00307 
00308 
00309   protected:
00310 
00312       enum { ESC=0x1b, SYN=0x16, NAK=0x15, ACK=0x06,
00313          BEL=0x07, DC1=0x11 };
00314 
00316       enum { TTY, NET };
00317 
00319       struct PortAccessData {
00320           bool open;
00321           int useCount, fd, portNum;
00322           char* host;
00323           pthread_mutex_t mutex;
00324           PortAccessData() : open(false), useCount(0),
00325                              fd(-1), portNum(-1), host(NULL)
00326           { pthread_mutex_init(&mutex, NULL); };
00327       };
00328 
00329       int defaultBus;           
00330       int baud;             
00331       int altBus;           
00332       int currBus;          
00333       int portType;         
00334       static PortAccessData pDat[40];   
00335       int cmdTimeout;           
00336       int monTimeout;           
00337 
00339 
00343       int openSerial(int port);
00344 
00346 
00351       int openSocket(int port, char* host);
00352  
00354 
00360       int sendMessage(int fd, int message);
00361 
00363 
00370       int readResponse(int fd, int* response, int timeOut);
00371 
00373 
00380       int comRd(int fd, char* rdBuf, int nBytes, int timeOut);
00381 
00383 
00389       int comRd(int fd, char* rdBuf, int timeOut);
00390 
00392       static void decode(char* escSeq);
00393 
00395       static void intToBytes(int num, char* bytes);
00396 
00398       static int bytesToInt(char* byte, int nbytes);
00399 
00401       void printSendMessage(char* bytes, int len);
00402 
00404       void printResponseMessage(char* bytes, int len);
00405 
00406 
00407       static const char* errTable[];    
00408       static const char* warnTable[];   
00409 
00410   private:
00411 
00412       char sendBuff[10];    
00413       char receiveBuff[5];  
00414       char junkBuff[20];    
00415       int nRead;        
00416 
00417       bool debug;
00418       static bool debugAll;
00419 
00420       float (*analogScalingFn)(int);    
00421 
00423       static float stdAnalogScalingFn(int regVal);
00424 
00426       static float f93AnalogScalingFn(int regVal);
00427 
00429       struct Arg {
00430          Dataset* obj;
00431          PortAccessData* pDat;
00432          Arg(Dataset* obj, PortAccessData* pDat) : 
00433              obj(obj), pDat(pDat) { };
00434       }; 
00435 
00437       static void* socketReconnectThread(void* arg);
00438 
00439       // Disallow copy and assignment
00440       Dataset(const Dataset&);
00441       Dataset& operator=(const Dataset&);      
00442 };
00443 
00444 // Inline Utility Methods
00445 
00446 inline float Dataset::stdAnalogScalingFn(int regVal)
00447 {
00448     return (regVal * 10.0/4095) - 5.0;
00449 }
00450 
00451 inline float Dataset::f93AnalogScalingFn(int regVal)
00452 {
00453     return (short)regVal/8192.0;
00454 }
00455 
00456 inline int Dataset::bcd2dec(int bcdNum, int nBits)
00457 {
00458     int dec = 0, weight = 1;
00459 
00460     for (int i=0; i < (nBits/4); ++i)
00461     {
00462         int digit = bcdNum & 0xF;
00463     if (digit > 0x9)
00464         {
00465             dec = -1; break;
00466         }
00467         dec += digit * weight;
00468     weight *= 10;
00469     bcdNum >>= 4;
00470     }
00471 
00472     return dec;
00473 }
00474 
00475 inline void Dataset::decode(char* escSeq)
00476 {
00477     switch (escSeq[1])
00478     {
00479         case '0':  escSeq[0] = ESC; break;
00480         case '1':  escSeq[0] = SYN; break;
00481     case '2':  escSeq[0] = ACK; break;
00482     case '3':  escSeq[0] = BEL; break;
00483     case '4':  escSeq[0] = NAK; break;
00484     default :  break;
00485     }
00486 }
00487 
00488 inline void Dataset::intToBytes(int num, char* bytes)
00489 {
00490     for (int i=0; i<=3; ++i)
00491     {
00492         bytes[i] = num;
00493         num >>= 8;
00494     }
00495 }
00496 
00497 inline int Dataset::bytesToInt(char* bytes, int nbytes)
00498 {
00499     int n = (unsigned char)bytes[0];
00500 
00501     for (int i=1; i<nbytes; ++i)
00502     {
00503         n <<= 8;
00504         //n += (unsigned char)bytes[i];
00505         n |= (unsigned char)bytes[i];
00506     }
00507     return n;
00508 }
00509 
00510 #endif

Generated on Mon Apr 30 13:32:35 2007 for Parkes M & C - PKMC C++ library API by  doxygen 1.4.4