PISerial now can configure blocking write and blocking read options from path

git-svn-id: svn://db.shs.com.ru/pip@281 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2016-11-23 07:23:37 +00:00
parent a393f93ee1
commit d3166f8fa5
3 changed files with 47 additions and 16 deletions

View File

@@ -612,6 +612,11 @@ void PISerial::setReadIsBlocking(bool yes) {
if (isOpened()) setTimeouts(); if (isOpened()) setTimeouts();
} }
void PISerial::setWriteIsBlocking(bool yes) {
block_write = yes;
if (isOpened()) setTimeouts();
}
/** \brief Basic read function /** \brief Basic read function
* \details Read to pointer "read_to" no more than "max_size". If read is * \details Read to pointer "read_to" no more than "max_size". If read is
@@ -635,18 +640,18 @@ int PISerial::read(void * read_to, int max_size) {
} }
int PISerial::write(const void * data, int max_size, bool wait) { int PISerial::write(const void * data, int max_size) {
if (fd == -1 || !canWrite()) { if (fd == -1 || !canWrite()) {
//piCoutObj << "Can`t write to uninitialized COM"; //piCoutObj << "Can`t write to uninitialized COM";
return -1; return -1;
} }
#ifdef WINDOWS #ifdef WINDOWS
if (block_write != wait) { // if (block_write != wait) {
block_write = wait; // block_write = wait;
// piCoutObj << "set timeout ..."; // piCoutObj << "set timeout ...";
setReadIsBlocking(block_read); // setReadIsBlocking(block_read);
// piCoutObj << "set timeout ok"; // piCoutObj << "set timeout ok";
} // }
DWORD wrote; DWORD wrote;
// piCoutObj << "send ...";// << max_size;// << ": " << PIString((char*)data, max_size); // piCoutObj << "send ...";// << max_size;// << ": " << PIString((char*)data, max_size);
sending = true; sending = true;
@@ -656,7 +661,7 @@ int PISerial::write(const void * data, int max_size, bool wait) {
#else #else
int wrote; int wrote;
wrote = ::write(fd, data, max_size); wrote = ::write(fd, data, max_size);
if (wait) tcdrain(fd); if (block_write) tcdrain(fd);
#endif #endif
return (int)wrote; return (int)wrote;
//piCoutObj << "Error while sending"; //piCoutObj << "Error while sending";
@@ -685,6 +690,9 @@ PIString PISerial::constructFullPath() const {
} else ret << ":N"; } else ret << ":N";
if (parameters()[TwoStopBits]) ret << ":2"; if (parameters()[TwoStopBits]) ret << ":2";
else ret << ":1"; else ret << ":1";
ret << ":block";
if (block_read) ret << "R";
if (block_write) ret << "W";
return ret; return ret;
} }
@@ -694,17 +702,37 @@ void PISerial::configureFromFullPath(const PIString & full_path) {
for (int i = 0; i < pl.size_s(); ++i) { for (int i = 0; i < pl.size_s(); ++i) {
PIString p(pl[i]); PIString p(pl[i]);
switch (i) { switch (i) {
case 0: setPath(p); break; case 0: setProperty(PIStringAscii("path"), p); break;
case 1: setSpeed((Speed)(p.toInt())); break; case 1: setProperty("outSpeed", p.toInt()); setProperty("inSpeed", p.toInt()); break;
case 2: setDataBitsCount(p.toInt()); break; case 2: setProperty("dataBitsCount", p.toInt()); break;
case 3: case 3:
p = p.toLowerCase(); p = p.toLowerCase();
if (p != "n") setParameter(ParityControl); if (p != "n") setParameter(ParityControl);
if (p == "o") setParameter(ParityOdd); if (p == "o") setParameter(ParityOdd);
break; break;
case 4: if (p.toInt() == 2) setParameter(TwoStopBits); break; case 4: if (p.toInt() == 2) setParameter(TwoStopBits); break;
case 5:
p = p.toLowerCase();
PIString bs = "block";
if (bs == p.mid(0, bs.length())) {
block_write = false;
block_read = false;
p = p.mid(bs.length());
if (p == "r") {
block_read = true;
}
if (p == "w") {
block_write = true;
}
if (p == "rw" || p == "wr") {
block_read = true;
block_write = true;
}
}
break;
} }
} }
applySettings();
} }

View File

@@ -139,9 +139,12 @@ public:
void setVTime(int t) {vtime = t; applySettings();} void setVTime(int t) {vtime = t; applySettings();}
//! Set read is blocking for function read(void * read_to, int max_size) //! Set read is blocking for function \a read
void setReadIsBlocking(bool yes); void setReadIsBlocking(bool yes);
//! Set write is blocking for functions \a write and \a send
void setWriteIsBlocking(bool yes);
//! Returns device name //! Returns device name
PIString device() const {return path();} PIString device() const {return path();}
@@ -167,11 +170,11 @@ public:
//! \brief Write to device data "data" with maximum size "max_size" and wait for data written if "wait" is \b true. //! \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 //! \returns sended bytes count
int write(const void * data, int max_size, bool wait = false); int write(const void * data, int max_size);
//! \brief Write to device data "data" with maximum size "size" and wait for data written if "wait" is \b true. //! \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" //! \returns \b true if sended bytes count = "size"
bool send(const void * data, int size, bool wait = false) {return (write(data, size, wait) == size);} bool send(const void * data, int size) {return (write(data, size) == size);}
/// NOTE: no reason to use this function, use PIString::toUtf8() or PIString::dataAscii(),lengthAscii() instead /// NOTE: no reason to use this function, use PIString::toUtf8() or PIString::dataAscii(),lengthAscii() instead
// //! \brief Write to device string "data" and wait for data written if "wait" is \b true. // //! \brief Write to device string "data" and wait for data written if "wait" is \b true.
@@ -180,7 +183,7 @@ public:
//! \brief Write to device byte array "data" and wait for data written if "wait" is \b true. //! \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 //! \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());} bool send(const PIByteArray & data) {return (write(data.data(), data.size_s()) == data.size_s());}
PIString constructFullPath() const; PIString constructFullPath() const;
@@ -218,7 +221,7 @@ protected:
PIString fullPathPrefix() const {return "ser";} PIString fullPathPrefix() const {return "ser";}
void configureFromFullPath(const PIString & full_path); void configureFromFullPath(const PIString & full_path);
bool configureDevice(const void * e_main, const void * e_parent = 0); bool configureDevice(const void * e_main, const void * e_parent = 0);
int write(const void * data, int max_size) {return write(data, max_size, true);} // int write(const void * data, int max_size) {return write(data, max_size);}
//! Executes when any read function was successful. Default implementation does nothing //! Executes when any read function was successful. Default implementation does nothing
virtual void received(const void * data, int size) {;} virtual void received(const void * data, int size) {;}

View File

@@ -3,8 +3,8 @@
#define PIVERSION_H #define PIVERSION_H
#define PIP_VERSION_MAJOR 0 #define PIP_VERSION_MAJOR 0
#define PIP_VERSION_MINOR 5 #define PIP_VERSION_MINOR 6
#define PIP_VERSION_REVISION 7 #define PIP_VERSION_REVISION 0
#define PIP_VERSION_SUFFIX "" #define PIP_VERSION_SUFFIX ""
#endif // PIVERSION_H #endif // PIVERSION_H