15.04.2014 - Version 0.3.8_beta, last version of 0.3.8 branch. Too much added and fixed...
This commit is contained in:
57
piserial.cpp
57
piserial.cpp
@@ -42,6 +42,7 @@ PISerial::PISerial(): PIIODevice("", ReadWrite) {
|
||||
ispeed = ospeed = S115200;
|
||||
vtime = 1;
|
||||
#ifdef WINDOWS
|
||||
block_write = true;
|
||||
hCom = 0;
|
||||
#endif
|
||||
dbits = 8;
|
||||
@@ -59,6 +60,7 @@ PISerial::PISerial(const PIString & device_, PISerial::Speed speed_, PIFlags<PIS
|
||||
ispeed = ospeed = speed_;
|
||||
vtime = 1;
|
||||
#ifdef WINDOWS
|
||||
block_write = true;
|
||||
hCom = 0;
|
||||
#endif
|
||||
dbits = 8;
|
||||
@@ -208,10 +210,10 @@ bool PISerial::read(void * data, int size, double timeout_ms) {
|
||||
int ret, all = 0;
|
||||
if (timeout_ms > 0.) {
|
||||
setReadIsBlocking(false);
|
||||
all = ::read(fd, data, 1);
|
||||
all = read(data, 1);
|
||||
timer.reset();
|
||||
while (all < size && timer.elapsed_m() < timeout_ms) {
|
||||
ret = ::read(fd, &((uchar * )data)[all], size - all);
|
||||
ret = read(&((uchar * )data)[all], size - all);
|
||||
if (ret > 0) all += ret;
|
||||
else msleep(1);
|
||||
}
|
||||
@@ -219,9 +221,9 @@ bool PISerial::read(void * data, int size, double timeout_ms) {
|
||||
return (all == size);
|
||||
} else {
|
||||
setReadIsBlocking(true);
|
||||
all = ::read(fd, data, 1);
|
||||
all = read(data, 1);
|
||||
while (all < size) {
|
||||
ret = ::read(fd, &((uchar * )data)[all], size - all);
|
||||
ret = read(&((uchar * )data)[all], size - all);
|
||||
if (ret > 0) all += ret;
|
||||
}
|
||||
received(data, all);
|
||||
@@ -249,13 +251,13 @@ PIString PISerial::read(int size, double timeout_ms) {
|
||||
timer.reset();
|
||||
if (size <= 0) {
|
||||
while (timer.elapsed_m() < timeout_ms) {
|
||||
ret = ::read(fd, td, 1024);
|
||||
ret = read(td, 1024);
|
||||
if (ret <= 0) msleep(1);
|
||||
else str << PIString((char*)td, ret);
|
||||
}
|
||||
} else {
|
||||
while (all < size && timer.elapsed_m() < timeout_ms) {
|
||||
ret = ::read(fd, td, size - all);
|
||||
ret = read(td, size - all);
|
||||
if (ret <= 0) msleep(1);
|
||||
else {
|
||||
str << PIString((char*)td, ret);
|
||||
@@ -265,10 +267,10 @@ PIString PISerial::read(int size, double timeout_ms) {
|
||||
}
|
||||
} else {
|
||||
setReadIsBlocking(true);
|
||||
all = ::read(fd, td, 1);
|
||||
all = read(td, 1);
|
||||
str << PIString((char*)td, all);
|
||||
while (all < size) {
|
||||
ret = ::read(fd, td, size - all);
|
||||
ret = read(td, size - all);
|
||||
if (ret <= 0) msleep(1);
|
||||
else {
|
||||
str << PIString((char*)td, ret);
|
||||
@@ -299,13 +301,13 @@ PIByteArray PISerial::readData(int size, double timeout_ms) {
|
||||
timer.reset();
|
||||
if (size <= 0) {
|
||||
while (timer.elapsed_m() < timeout_ms) {
|
||||
ret = ::read(fd, td, 1024);
|
||||
ret = read(td, 1024);
|
||||
if (ret <= 0) msleep(1);
|
||||
else str.append(td, ret);
|
||||
}
|
||||
} else {
|
||||
while (all < size && timer.elapsed_m() < timeout_ms) {
|
||||
ret = ::read(fd, td, size - all);
|
||||
ret = read(td, size - all);
|
||||
if (ret <= 0) msleep(1);
|
||||
else {
|
||||
str.append(td, ret);
|
||||
@@ -315,10 +317,10 @@ PIByteArray PISerial::readData(int size, double timeout_ms) {
|
||||
}
|
||||
} else {
|
||||
setReadIsBlocking(true);
|
||||
all = ::read(fd, td, 1);
|
||||
all = read(td, 1);
|
||||
str.append(td, all);
|
||||
while (all < size) {
|
||||
ret = ::read(fd, td, size - all);
|
||||
ret = read(td, size - all);
|
||||
if (ret <= 0) msleep(1);
|
||||
else {
|
||||
str.append(td, ret);
|
||||
@@ -336,7 +338,8 @@ bool PISerial::openDevice() {
|
||||
DWORD ds = 0, sm = 0;
|
||||
if (isReadable()) {ds |= GENERIC_READ; sm |= FILE_SHARE_READ;}
|
||||
if (isWriteable()) {ds |= GENERIC_WRITE; sm |= FILE_SHARE_WRITE;}
|
||||
hCom = CreateFileA(path_.data(), ds, sm, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
|
||||
PIString wp = "//./" + path_;
|
||||
hCom = CreateFileA(wp.data(), ds, sm, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
|
||||
if (hCom == INVALID_HANDLE_VALUE) {
|
||||
piCoutObj << "Unable to open \"" << path_ << "\"";
|
||||
fd = -1;
|
||||
@@ -370,10 +373,10 @@ void PISerial::applySettings() {
|
||||
if (fd == -1) return;
|
||||
COMMTIMEOUTS times;
|
||||
times.ReadIntervalTimeout = block_read ? vtime : MAXDWORD;
|
||||
times.ReadTotalTimeoutConstant = block_read ? 1 : 0;
|
||||
times.ReadTotalTimeoutMultiplier = 0;
|
||||
times.WriteTotalTimeoutConstant = 1;
|
||||
times.WriteTotalTimeoutMultiplier = 0;
|
||||
times.ReadTotalTimeoutConstant = block_read ? 0 : 1;
|
||||
times.ReadTotalTimeoutMultiplier = block_read ? 0 : MAXDWORD;
|
||||
times.WriteTotalTimeoutConstant = 0;
|
||||
times.WriteTotalTimeoutMultiplier = block_write ? 0 : 1;
|
||||
if (SetCommTimeouts(hCom, ×) == -1)
|
||||
piCoutObj << "Unable to set timeouts for \"" << path_ << "\"";
|
||||
GetCommMask(hCom, &mask);
|
||||
@@ -435,11 +438,11 @@ void PISerial::setReadIsBlocking(bool yes) {
|
||||
block_read = yes;
|
||||
#ifdef WINDOWS
|
||||
COMMTIMEOUTS times;
|
||||
times.ReadIntervalTimeout = yes ? vtime : MAXDWORD;
|
||||
times.ReadTotalTimeoutConstant = yes ? 1 : 0;
|
||||
times.ReadTotalTimeoutMultiplier = 0;
|
||||
times.WriteTotalTimeoutConstant = 1;
|
||||
times.WriteTotalTimeoutMultiplier = 0;
|
||||
times.ReadIntervalTimeout = block_read ? vtime : MAXDWORD;
|
||||
times.ReadTotalTimeoutConstant = block_read ? 0 : 1;
|
||||
times.ReadTotalTimeoutMultiplier = block_read ? 0 : MAXDWORD;
|
||||
times.WriteTotalTimeoutConstant = 0;
|
||||
times.WriteTotalTimeoutMultiplier = block_write ? 0 : 1;
|
||||
if (isOpened()) SetCommTimeouts(hCom, ×);
|
||||
#else
|
||||
if (isOpened()) fcntl(fd, F_SETFL, yes ? 0 : O_NONBLOCK);
|
||||
@@ -472,14 +475,12 @@ int PISerial::write(const void * data, int max_size, bool wait) {
|
||||
return -1;
|
||||
}
|
||||
#ifdef WINDOWS
|
||||
if (block_write != wait) {
|
||||
block_write = wait;
|
||||
setReadIsBlocking(block_read);
|
||||
}
|
||||
DWORD wrote;
|
||||
WriteFile(hCom, data, max_size, &wrote, 0);
|
||||
if (wait) {
|
||||
DWORD event;
|
||||
SetCommMask(hCom, EV_TXEMPTY);
|
||||
WaitCommEvent(hCom, &event, NULL);
|
||||
SetCommMask(hCom, EV_RXCHAR);
|
||||
}
|
||||
#else
|
||||
int wrote;
|
||||
wrote = ::write(fd, data, max_size);
|
||||
|
||||
Reference in New Issue
Block a user