Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __CXUTILS_SERIAL_H
00041 #define __CXUTILS_SERIAL_H
00042
00043 #include <string>
00044 #include "packet.h"
00045 #include "mutex.h"
00046
00047 namespace CxUtils
00048 {
00049 const int CX_SERIAL_NO_PARITY = 0;
00050 const int CX_SERIAL_EVEN_PARITY = 1;
00051 const int CX_SERIAL_ODD_PARITY = 2;
00052
00062 class CX_UTILS_DLL Serial
00063 {
00064 public:
00065 Serial();
00066 virtual ~Serial();
00067 int Connect(const std::string& port,
00068 const unsigned int baud = 9600,
00069 const unsigned int bits = 8,
00070 const unsigned int partiy = 0,
00071 const unsigned int stop = 1);
00072 int Initialize(const std::string& port,
00073 const unsigned int baud = 9600,
00074 const unsigned int bits = 8,
00075 const unsigned int parity = 0,
00076 const unsigned int stop = 1)
00077 {
00078 return Connect(port, baud, bits, parity, stop);
00079 }
00080 int Disconnect();
00081 int Shutdown() { return Disconnect(); }
00082 int Reconnect();
00083 int Send(const char *buff, const unsigned int size);
00084 int Send(const std::string& message);
00085 int Send(const Packet& p);
00086 int Recv(char* buff, unsigned int size, const unsigned int waitTimeMs = 1000);
00087 int Recv(Packet& p, const unsigned int len, const unsigned int waitTimeMs = 1000);
00088 void Reset(void);
00089 void Flush(void);
00090 void FlushReadBuffer(void);
00091 void FlushWriteBuffer(void);
00092 unsigned int ReadBytesAvailable(void);
00093 unsigned int WriteBytesPending(void);
00094 #ifdef WIN32
00095 unsigned long GetSystemError(void) const { return mLastError; };
00096 #endif
00097 inline int GetBaudRate() const { return mBaudRate; }
00098 inline int GetStopBits() const { return mStopBits; }
00099 inline int GetParity() const { return mParity; }
00100 inline int GetDataBits() const { return mDataBits; }
00101 inline const char* GetPortName() const { return mPortName.c_str(); }
00102 inline bool IsConnected() const { return mConnected; }
00103 protected:
00104 #ifdef WIN32
00105 void* mCom;
00106 unsigned long mErrors;
00107 unsigned long mLastError;
00108 #else
00109 int mCom;
00110 #endif
00111 std::string mPortName;
00112 char mParity;
00113 int mBaudRate;
00114 int mStopBits;
00115 int mDataBits;
00116 bool mConnected;
00117 Mutex mSerialMutex;
00118 };
00119 }
00120
00121 #endif
00122
00123