#ifndef _Brooks0254Port_ #define _Brooks0254Port_ #include #include #include #include #include #include #include #include #define _OPEN_THREADS #include #include #include #include #include #include #include #include using namespace std; #include "MtxLock.hh" #include "SemLock.hh" //! Class to add function for calculating checksum for Brooks0254 //! message which inherits from std:string class Brooks0254Msg : virtual public std::string { public: Brooks0254Msg() : string() {;} Brooks0254Msg( const string& str ) : string( str ) {;} Brooks0254Msg( const Brooks0254Msg& msg ) : string( static_cast( msg ) ) {;} Brooks0254Msg operator=( const Brooks0254Msg& msg ) { return string::operator=( static_cast( msg ) ); } unsigned CheckSum(); }; class Brooks0254Port { public: class FailedOpenPort { public: string msg; FailedOpenPort( string errMsg ) : msg( errMsg ) {}; }; class FailedClosePort { public: string msg; FailedClosePort( string errMsg ) : msg( errMsg ) {}; }; class FailedWritePort { public: string msg; FailedWritePort( string errMsg ) : msg( errMsg ) {}; }; class FailedReadPort { public: string msg; FailedReadPort( string errMsg ) : msg( errMsg ) {}; }; protected: unsigned bpID; //! Port ID string bpDev; //! Device name int bpFD; //! Port file descriptor sem_t bpSemPos; //! Posix semaphore struct termios bpOpt; //! serial port configuration Brooks0254Port& operator=( const Brooks0254Port& port ); //! Not allowed Brooks0254Port( const Brooks0254Port& port ); //! Not allowed void Init(); //! Method to initialize the serial port void ClosePort(); //! Close the serial connection //! Method to read the response from the serial port. The call to //! this function needs to be timed properly after a request for a //! response has been send, otherwise the part of the responce //! message can be lost. string Read( unsigned nReadLine = 1 ); //! Method to write to the serial device and then to delay before //! returning the control int Write( string strOut ); public: static unsigned bpDebFlag; //! Debug flag static unsigned bpReadbackTimeoutCount; //! # of time to check port for data static unsigned bpReadDelay; //! time between char reads in microseconds static unsigned bpWriteDelay; //! time delay between write and read static double bpCharTime; //! time to transfer a byte static unsigned bpResponseLine; //! Line where the response is expected static long bpMaxReadAttempt; //! Max number of attmepts to read static const int bpSuccess; // Success constant static const int bpFail; // Fail constant static const int bpError; // Error constant static pthread_mutex_t bpGlobMutex; //! Global mutex static pthread_mutexattr_t bpGlobMtxAttr; //! Global mutex attributes static int bpDummyInt; //! Dummy integer static map bpPortMap; //! Hash table to keep //! track of devices Brooks0254Port( int portID, string devName ); ~Brooks0254Port(); //! Reset serial connection on the driver side void Reset(); //! Reset serial connection //! Initiliaze global mutex static int InitGlobalMutex(); //! Initialize global mutex //! Send a meassage to serial port. If second argument is true then //! receive the readback. The method will try to send and receive //! untill either he checksum is correct or the maximum number of //! attempts is reached. string Send( const string, bool checkResponce = true, unsigned nRespLine = 1 ); inline unsigned GetID() { SemLock sLock( bpSemPos ); return bpID; } inline string GetDevice() { SemLock sLock( bpSemPos ); return bpDev ; } inline int GetFD() { SemLock s( bpSemPos ); return bpFD; } }; #endif