git-svn-id: svn://db.shs.com.ru/pip@284 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-11-30 12:21:53 +00:00
parent e66e78ac16
commit 8b72323dd1
25 changed files with 254 additions and 227 deletions

View File

@@ -165,8 +165,6 @@ void PISerial::construct() {
fd = -1;
piMonitor.serials++;
setPriority(piHigh);
block_read = false;
block_write = true;
vtime = 10;
sending = false;
setParameters(0);
@@ -358,23 +356,25 @@ bool PISerial::read(void * data, int size, double timeout_ms) {
if (data == 0 || size <= 0) return false;
int ret, all = 0;
if (timeout_ms > 0.) {
setReadIsBlocking(false);
all = read(data, 1);
bool br = setOption(BlockingRead, false);
all = readDevice(data, 1);
tm_.reset();
while (all < size && tm_.elapsed_m() < timeout_ms) {
ret = read(&((uchar * )data)[all], size - all);
ret = readDevice(&((uchar * )data)[all], size - all);
if (ret > 0) all += ret;
else msleep(1);
}
setOption(BlockingRead, br);
received(data, all);
return (all == size);
} else {
setReadIsBlocking(true);
all = read(data, 1);
bool br = setOption(BlockingRead, true);
all = readDevice(data, 1);
while (all < size) {
ret = read(&((uchar * )data)[all], size - all);
ret = readDevice(&((uchar * )data)[all], size - all);
if (ret > 0) all += ret;
}
setOption(BlockingRead, br);
received(data, all);
return (all == size);
}
@@ -396,17 +396,17 @@ PIString PISerial::read(int size, double timeout_ms) {
int ret, all = 0;
uchar td[1024];
if (timeout_ms > 0.) {
setReadIsBlocking(false);
bool br = setOption(BlockingRead, false);
tm_.reset();
if (size <= 0) {
while (tm_.elapsed_m() < timeout_ms) {
ret = read(td, 1024);
ret = readDevice(td, 1024);
if (ret <= 0) msleep(1);
else str << PIString((char*)td, ret);
}
} else {
while (all < size && tm_.elapsed_m() < timeout_ms) {
ret = read(td, size - all);
ret = readDevice(td, size - all);
if (ret <= 0) msleep(1);
else {
str << PIString((char*)td, ret);
@@ -414,18 +414,20 @@ PIString PISerial::read(int size, double timeout_ms) {
}
}
}
setOption(BlockingRead, br);
} else {
setReadIsBlocking(true);
all = read(td, 1);
bool br = setOption(BlockingRead, true);
all = readDevice(td, 1);
str << PIString((char*)td, all);
while (all < size) {
ret = read(td, size - all);
ret = readDevice(td, size - all);
if (ret <= 0) msleep(1);
else {
str << PIString((char*)td, ret);
all += ret;
}
}
setOption(BlockingRead, br);
}
received(str.data(), str.size_s());
return str;
@@ -446,17 +448,17 @@ PIByteArray PISerial::readData(int size, double timeout_ms) {
int ret, all = 0;
uchar td[1024];
if (timeout_ms > 0.) {
setReadIsBlocking(false);
bool br = setOption(BlockingRead, false);
tm_.reset();
if (size <= 0) {
while (tm_.elapsed_m() < timeout_ms) {
ret = read(td, 1024);
ret = readDevice(td, 1024);
if (ret <= 0) msleep(1);
else str.append(td, ret);
}
} else {
while (all < size && tm_.elapsed_m() < timeout_ms) {
ret = read(td, size - all);
ret = readDevice(td, size - all);
if (ret <= 0) msleep(1);
else {
str.append(td, ret);
@@ -464,18 +466,20 @@ PIByteArray PISerial::readData(int size, double timeout_ms) {
}
}
}
setOption(BlockingRead, br);
} else {
setReadIsBlocking(true);
all = read(td, 1);
bool br = setOption(BlockingRead, true);
all = readDevice(td, 1);
str.append(td, all);
while (all < size) {
ret = read(td, size - all);
ret = readDevice(td, size - all);
if (ret <= 0) msleep(1);
else {
str.append(td, ret);
all += ret;
}
}
setOption(BlockingRead, br);
}
received(str.data(), str.size_s());
return str;
@@ -594,36 +598,25 @@ void PISerial::applySettings() {
void PISerial::setTimeouts() {
#ifdef WINDOWS
COMMTIMEOUTS times;
times.ReadIntervalTimeout = block_read ? vtime : MAXDWORD;
times.ReadTotalTimeoutConstant = block_read ? 0 : 1;
times.ReadTotalTimeoutMultiplier = block_read ? 0 : MAXDWORD;
times.WriteTotalTimeoutConstant = block_write ? 0 : 1;
times.ReadIntervalTimeout = isOptionSet(BlockingRead) ? vtime : MAXDWORD;
times.ReadTotalTimeoutConstant = isOptionSet(BlockingRead) ? 0 : 1;
times.ReadTotalTimeoutMultiplier = isOptionSet(BlockingRead) ? 0 : MAXDWORD;
times.WriteTotalTimeoutConstant = isOptionSet(BlockingWrite) ? 0 : 1;
times.WriteTotalTimeoutMultiplier = 0;
if (SetCommTimeouts(PRIVATE->hCom, &times) == -1)
piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
#else
fcntl(fd, F_SETFL, block_read ? 0 : O_NONBLOCK);
fcntl(fd, F_SETFL, isOptionSet(BlockingRead) ? 0 : O_NONBLOCK);
#endif
}
void PISerial::setReadIsBlocking(bool yes) {
block_read = yes;
if (isOpened()) setTimeouts();
}
void PISerial::setWriteIsBlocking(bool yes) {
block_write = yes;
if (isOpened()) setTimeouts();
}
/** \brief Basic read function
* \details Read to pointer "read_to" no more than "max_size". If read is
* set to blocking this function will be wait at least one byte.
* \returns Readed bytes count
* \sa \a readData() */
int PISerial::read(void * read_to, int max_size) {
int PISerial::readDevice(void * read_to, int max_size) {
#ifdef WINDOWS
if (!canRead()) return -1;
if (sending) return -1;
@@ -640,7 +633,7 @@ int PISerial::read(void * read_to, int max_size) {
}
int PISerial::write(const void * data, int max_size) {
int PISerial::writeDevice(const void * data, int max_size) {
if (fd == -1 || !canWrite()) {
//piCoutObj << "Can`t write to uninitialized COM";
return -1;
@@ -661,7 +654,7 @@ int PISerial::write(const void * data, int max_size) {
#else
int wrote;
wrote = ::write(fd, data, max_size);
if (block_write) tcdrain(fd);
if (isOptionSet(BlockingWrite)) tcdrain(fd);
#endif
return (int)wrote;
//piCoutObj << "Error while sending";
@@ -681,8 +674,8 @@ bool PISerial::configureDevice(const void * e_main, const void * e_parent) {
}
PIString PISerial::constructFullPath() const {
PIString ret(fullPathPrefix() + "://");
PIString PISerial::constructFullPathDevice() const {
PIString ret;
ret << path() << ":" << int(inSpeed()) << ":" << dataBitsCount();
if (parameters()[ParityControl]) {
if (parameters()[ParityOdd]) ret << ":O";
@@ -690,19 +683,16 @@ PIString PISerial::constructFullPath() const {
} else ret << ":N";
if (parameters()[TwoStopBits]) ret << ":2";
else ret << ":1";
ret << ":block";
if (block_read) ret << "R";
if (block_write) ret << "W";
return ret;
}
void PISerial::configureFromFullPath(const PIString & full_path) {
void PISerial::configureFromFullPathDevice(const PIString & full_path) {
PIStringList pl = full_path.split(":");
for (int i = 0; i < pl.size_s(); ++i) {
PIString p(pl[i]);
switch (i) {
case 0: setProperty(PIStringAscii("path"), p); break;
case 0: setProperty("path", p); break;
case 1: setProperty("outSpeed", p.toInt()); setProperty("inSpeed", p.toInt()); break;
case 2: setProperty("dataBitsCount", p.toInt()); break;
case 3:
@@ -711,25 +701,6 @@ void PISerial::configureFromFullPath(const PIString & full_path) {
if (p == "o") setParameter(ParityOdd);
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();
@@ -847,3 +818,8 @@ PIStringList PISerial::availableDevices(bool test) {
}
return dl;
}
void PISerial::optionsChanged() {
if (isOpened()) setTimeouts();
}