30.11.2013 - New PICollection namespace, Android support, my own PIVector implementation
This commit is contained in:
190
piserial.h
190
piserial.h
@@ -90,29 +90,35 @@
|
||||
# define CRTSCTS 020000000000
|
||||
#endif
|
||||
|
||||
|
||||
class PIP_EXPORT PISerial: public PIIODevice {
|
||||
public:
|
||||
// slot is any function format "bool <func>(void*, uchar*, int)"
|
||||
// slot_header is any function format "bool <func>(void*, uchar*, uchar*, int)"
|
||||
PISerial(const PIString & device, void * data = 0, ReadRetFunc slot = 0);
|
||||
PISerial(void * data = 0, ReadRetFunc slot = 0);
|
||||
~PISerial();
|
||||
|
||||
//! Contructs an empty %PISerial
|
||||
PISerial();
|
||||
|
||||
enum Parameters {ParityControl = 0x1, ParityOdd = 0x2, TwoStopBits = 0x4, HardwareFlowControl = 0x8};
|
||||
//! \brief Parameters of PISerial
|
||||
enum Parameters {
|
||||
ParityControl /*! Enable parity check and generate */ = 0x1,
|
||||
ParityOdd /*! Parity is odd instead of even */ = 0x2,
|
||||
TwoStopBits /*! Two stop bits instead of one */ = 0x4
|
||||
};
|
||||
|
||||
//! \brief Speed of PISerial
|
||||
enum Speed {
|
||||
S50 = 50,
|
||||
S75 = 75,
|
||||
S110 = 110,
|
||||
S300 = 300,
|
||||
S600 = 600,
|
||||
S1200 = 1200,
|
||||
S2400 = 2400,
|
||||
S4800 = 4800,
|
||||
S9600 = 9600,
|
||||
S19200 = 19200,
|
||||
S38400 = 38400,
|
||||
S57600 = 57600,
|
||||
S115200 = 115200,
|
||||
S50 /*! 50 baud */ = 50,
|
||||
S75 /*! 75 baud */ = 75,
|
||||
S110 /*! 110 baud */ = 110,
|
||||
S300 /*! 300 baud */ = 300,
|
||||
S600 /*! 600 baud */ = 600,
|
||||
S1200 /*! 1200 baud */ = 1200,
|
||||
S2400 /*! 2400 baud */ = 2400,
|
||||
S4800 /*! 4800 baud */ = 4800,
|
||||
S9600 /*! 9600 baud */ = 9600,
|
||||
S19200 /*! 19200 baud */ = 19200,
|
||||
S38400 /*! 38400 baud */ = 38400,
|
||||
S57600 /*! 57600 baud */ = 57600,
|
||||
S115200 /*! 115200 baud */ = 115200,
|
||||
S1500000 = 1500000, // Linux only
|
||||
S2000000 = 2000000, // Linux only
|
||||
S2500000 = 2500000, // Linux only
|
||||
@@ -120,21 +126,50 @@ public:
|
||||
S3500000 = 3500000, // Linux only
|
||||
S4000000 = 4000000 // Linux only
|
||||
};
|
||||
|
||||
//! Contructs %PISerial with device name "device", speed "speed" and parameters "params"
|
||||
PISerial(const PIString & device, PISerial::Speed speed = S115200, PIFlags<PISerial::Parameters> params = 0);
|
||||
|
||||
~PISerial();
|
||||
|
||||
void setData(void * d) {data = d;}
|
||||
|
||||
//! Set both input and output speed to "speed"
|
||||
void setSpeed(PISerial::Speed speed) {ospeed = ispeed = speed; applySettings();}
|
||||
|
||||
//! Set output speed to "speed"
|
||||
void setOutSpeed(PISerial::Speed speed) {ospeed = speed; applySettings();}
|
||||
|
||||
//! Set input speed to "speed"
|
||||
void setInSpeed(PISerial::Speed speed) {ispeed = speed; applySettings();}
|
||||
|
||||
//! Set device name to "dev"
|
||||
void setDevice(const PIString & dev) {path_ = dev; if (isOpened()) {close(); open();};}
|
||||
|
||||
//void setSlot(SerialFunc func) {ret_func = func;}
|
||||
void setSpeed(PISerial::Speed speed) {ospeed = ispeed = speed;}
|
||||
void setOutSpeed(PISerial::Speed speed) {ospeed = speed;}
|
||||
void setInSpeed(PISerial::Speed speed) {ispeed = speed;}
|
||||
void setDevice(const PIString & dev) {path_ = dev;}
|
||||
|
||||
void setParameters(PIFlags<PISerial::Parameters> parameters_) {params = parameters_;}
|
||||
void setParameter(PISerial::Parameters parameter, bool on = true) {params.setFlag(parameter, on);}
|
||||
|
||||
//! Set parameters to "parameters_"
|
||||
void setParameters(PIFlags<PISerial::Parameters> parameters_) {params = parameters_; applySettings();}
|
||||
|
||||
//! Set parameter "parameter" to "on" state
|
||||
void setParameter(PISerial::Parameters parameter, bool on = true) {params.setFlag(parameter, on); applySettings();}
|
||||
|
||||
//! Returns if parameter "parameter" is set
|
||||
bool isParameterSet(PISerial::Parameters parameter) const {return params[parameter];}
|
||||
|
||||
//! Returns parameters
|
||||
PIFlags<PISerial::Parameters> parameters() const {return params;}
|
||||
|
||||
|
||||
//! Set data bits count. Valid range is from 5 to 8, befault is 8
|
||||
void setDataBitsCount(int bits) {dbits = bits; applySettings();}
|
||||
|
||||
//! Returns data bits count
|
||||
int dataBitsCount() const {return dbits;}
|
||||
|
||||
|
||||
//! Set pin number "number" to logic level "on". Valid numbers are 4 (DTR) and 7 (RTS)
|
||||
bool setPin(int number, bool on);
|
||||
|
||||
//! Returns pin number "number" logic level. Valid numbers range is from 1 to 9
|
||||
bool isPin(int number) const;
|
||||
|
||||
bool setLE(bool on) {return setBit(TIOCM_LE, on, "LE");} // useless function, just formally
|
||||
@@ -157,46 +192,91 @@ public:
|
||||
bool isRNG() const {return isBit(TIOCM_RNG, "RNG");}
|
||||
bool isDSR() const {return isBit(TIOCM_DSR, "DSR");}
|
||||
|
||||
void setVTime(int t) {vtime = t;}
|
||||
void setVTime(int t) {vtime = t; applySettings();}
|
||||
|
||||
void setReadIsBlocking(bool yes) {
|
||||
#ifdef WINDOWS
|
||||
COMMTIMEOUTS times;
|
||||
times.ReadIntervalTimeout = yes ? vtime : MAXDWORD;
|
||||
times.ReadTotalTimeoutConstant = yes ? 1 : 0;
|
||||
times.ReadTotalTimeoutMultiplier = 0;
|
||||
times.WriteTotalTimeoutConstant = 1;
|
||||
times.WriteTotalTimeoutMultiplier = 0;
|
||||
if (isOpened()) SetCommTimeouts(hCom, ×);
|
||||
#else
|
||||
if (isOpened()) fcntl(fd, F_SETFL, yes ? 0 : O_NONBLOCK);
|
||||
|
||||
//! Set read is blocking for function read(void * read_to, int max_size)
|
||||
void setReadIsBlocking(bool yes);
|
||||
|
||||
|
||||
//! Returns device name
|
||||
const PIString & device() const {return path_;}
|
||||
|
||||
//! Returns output speed
|
||||
PISerial::Speed outSpeed() const {return ospeed;}
|
||||
|
||||
//! Returns input speed
|
||||
PISerial::Speed inSpeed() const {return ispeed;}
|
||||
|
||||
int VTime() const {return vtime;}
|
||||
|
||||
|
||||
//! Discard all buffered input and output data
|
||||
void flush() {
|
||||
#ifndef WINDOWS
|
||||
if (fd != -1) tcflush(fd, TCIOFLUSH);
|
||||
#endif
|
||||
}
|
||||
|
||||
const PIString & device() const {return path_;}
|
||||
PISerial::Speed outSpeed() const {return ospeed;}
|
||||
PISerial::Speed inSpeed() const {return ispeed;}
|
||||
int VTime() const {return vtime;}
|
||||
|
||||
#ifndef WINDOWS
|
||||
void flush() {if (fd != -1) tcflush(fd, TCIOFLUSH);}
|
||||
#endif
|
||||
|
||||
int read(void * read_to, int max_size);
|
||||
bool read(void * data, int size, double timeout_ms);
|
||||
bool read(void * read_to, int max_size, double timeout_ms);
|
||||
PIString read(int size = -1, double timeout_ms = 1000.);
|
||||
PIByteArray readData(int size = -1, double timeout_ms = 1000.);
|
||||
|
||||
int write(const void * data, int max_size, bool wait);
|
||||
int write(const void * data, int max_size) {return write(data, max_size, false);}
|
||||
|
||||
//! \brief Write to device data "data" with maximum size "max_size" and wait for data written if "wait" is \b true.
|
||||
//! \returns sended bytes count
|
||||
int write(const void * data, int max_size, bool wait = false);
|
||||
|
||||
//! \brief Write to device data "data" with maximum size "size" and wait for data written if "wait" is \b true.
|
||||
//! \returns \b true if sended bytes count = "size"
|
||||
bool send(const void * data, int size, bool wait = false) {return (write(data, size, wait) == size);}
|
||||
|
||||
//! \brief Write to device string "data" and wait for data written if "wait" is \b true.
|
||||
//! \returns \b true if sended bytes count = size of string
|
||||
bool send(const PIString & data, bool wait = false) {return (write(data.data(), data.lengthAscii(), wait) == data.size_s());}
|
||||
|
||||
//! \brief Write to device byte array "data" and wait for data written if "wait" is \b true.
|
||||
//! \returns \b true if sended bytes count = size of string
|
||||
bool send(const PIByteArray & data, bool wait = false) {return (write(data.data(), data.size_s(), wait) == data.size_s());}
|
||||
|
||||
|
||||
//! \brief Returns all available speeds for serial devices
|
||||
static PIVector<int> availableSpeeds();
|
||||
|
||||
//! \brief Returns all available system devices. If "test" each device will be tried to open
|
||||
static PIStringList availableDevices(bool test = false);
|
||||
|
||||
//! \ioparams
|
||||
//! \{
|
||||
#ifdef DOXYGEN
|
||||
//! \brief device, default ""
|
||||
string device;
|
||||
|
||||
//! \brief input/output speed, default 115200
|
||||
int speed;
|
||||
|
||||
//! \brief dataBitsCount, default 8
|
||||
int dataBitsCount;
|
||||
|
||||
//! \brief parityControl, default false
|
||||
bool parityControl;
|
||||
|
||||
//! \brief parityOdd, default false
|
||||
bool parityOdd;
|
||||
|
||||
//! \brief twoStopBits, default false
|
||||
bool twoStopBits;
|
||||
#endif
|
||||
//! \}
|
||||
|
||||
protected:
|
||||
bool configureDevice(const void * e_main, const void * e_parent = 0);
|
||||
virtual void received(void * data, int size) {;}
|
||||
|
||||
//! Executes when any read function was successful. Default implementation does nothing
|
||||
virtual void received(const void * data, int size) {;}
|
||||
|
||||
void applySettings();
|
||||
int convertSpeed(PISerial::Speed speed);
|
||||
bool setBit(int bit, bool on, const PIString & bname);
|
||||
bool isBit(int bit, const PIString & bname) const;
|
||||
@@ -213,10 +293,10 @@ protected:
|
||||
termios desc, sdesc;
|
||||
uint readed;
|
||||
#endif
|
||||
int fd, vtime;
|
||||
int fd, vtime, dbits;
|
||||
bool block_read;
|
||||
PISerial::Speed ospeed, ispeed;
|
||||
PITimer timer;
|
||||
void * headerPtr, * data;
|
||||
PIFlags<PISerial::Parameters> params;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user