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

This commit is contained in:
2016-09-06 05:29:56 +00:00
parent f5e90f7a0f
commit 73cc277f23
4 changed files with 91 additions and 157 deletions

View File

@@ -165,8 +165,10 @@ void PISerial::construct() {
fd = -1;
piMonitor.serials++;
setPriority(piHigh);
block_read = block_write = true;
vtime = 1;
block_read = false;
block_write = true;
vtime = 10;
sending = false;
setParameters(0);
setSpeed(S115200);
setDataBitsCount(8);
@@ -520,17 +522,11 @@ bool PISerial::openDevice() {
void PISerial::applySettings() {
#ifdef WINDOWS
if (fd == -1) return;
COMMTIMEOUTS times;
times.ReadIntervalTimeout = block_read ? vtime : MAXDWORD;
times.ReadTotalTimeoutConstant = block_read ? 0 : 1;
times.ReadTotalTimeoutMultiplier = block_read ? 0 : MAXDWORD;
times.WriteTotalTimeoutConstant = 10;
times.WriteTotalTimeoutMultiplier = 1;// block_write ? 0 : 1;
if (SetCommTimeouts(PRIVATE->hCom, &times) == -1)
piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
setTimeouts();
GetCommMask(PRIVATE->hCom, &PRIVATE->mask);
SetCommMask(PRIVATE->hCom, EV_RXCHAR);
GetCommState(PRIVATE->hCom, &PRIVATE->sdesc);
// piCoutObj << PRIVATE->sdesc.fBinary << PRIVATE->sdesc.fAbortOnError << PRIVATE->sdesc.fDsrSensitivity << PRIVATE->sdesc.fDtrControl << PRIVATE->sdesc.fDummy2 << PRIVATE->sdesc.fErrorChar;
PRIVATE->desc = PRIVATE->sdesc;
PRIVATE->desc.DCBlength = sizeof(PRIVATE->desc);
PRIVATE->desc.BaudRate = convertSpeed(outSpeed());
@@ -544,6 +540,16 @@ void PISerial::applySettings() {
PRIVATE->desc.Parity = params[PISerial::ParityOdd] ? 1 : 2;
}
PRIVATE->desc.StopBits = params[PISerial::TwoStopBits] ? TWOSTOPBITS : ONESTOPBIT;
PRIVATE->desc.fOutxCtsFlow = 0;
PRIVATE->desc.fOutxDsrFlow = 0;
PRIVATE->desc.fDtrControl = 0;
PRIVATE->desc.fRtsControl = 0;
PRIVATE->desc.fInX = 0;
PRIVATE->desc.fOutX = 0;
PRIVATE->desc.fBinary = 1;
PRIVATE->desc.fAbortOnError = 0;
PRIVATE->desc.fNull = 0;
if (SetCommState(PRIVATE->hCom, &PRIVATE->desc) == -1) {
piCoutObj << "Unable to set comm state for \"" << path() << "\"";
return;
@@ -575,7 +581,7 @@ void PISerial::applySettings() {
cfsetospeed(&PRIVATE->desc, convertSpeed(outSpeed()));
tcflush(fd, TCIOFLUSH);
fcntl(fd, F_SETFL, block_read ? 0 : O_NONBLOCK);
setTimeouts();
if(tcsetattr(fd, TCSANOW, &PRIVATE->desc) < 0) {
piCoutObj << "Can`t set attributes for \"" << path() << "\"";
@@ -585,22 +591,28 @@ void PISerial::applySettings() {
}
void PISerial::setReadIsBlocking(bool yes) {
block_read = yes;
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 = 10;
times.WriteTotalTimeoutMultiplier = 1;//block_write ? 0 : 1;
if (isOpened()) SetCommTimeouts(PRIVATE->hCom, &times);
times.WriteTotalTimeoutConstant = block_write ? 0 : 1;
times.WriteTotalTimeoutMultiplier = 0;
if (SetCommTimeouts(PRIVATE->hCom, &times) == -1)
piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
#else
if (isOpened()) fcntl(fd, F_SETFL, yes ? 0 : O_NONBLOCK);
fcntl(fd, F_SETFL, block_read ? 0 : O_NONBLOCK);
#endif
}
void PISerial::setReadIsBlocking(bool yes) {
block_read = 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.
@@ -609,8 +621,12 @@ void PISerial::setReadIsBlocking(bool yes) {
int PISerial::read(void * read_to, int max_size) {
#ifdef WINDOWS
if (!canRead()) return -1;
if (sending) return -1;
// piCoutObj << "com event ...";
WaitCommEvent(PRIVATE->hCom, 0, 0);
// piCoutObj << "read ...";
ReadFile(PRIVATE->hCom, read_to, max_size, &PRIVATE->readed, 0);
// piCoutObj << "read ok";
return PRIVATE->readed;
#else
if (!canRead()) return -1;
@@ -620,7 +636,6 @@ int PISerial::read(void * read_to, int max_size) {
int PISerial::write(const void * data, int max_size, bool wait) {
piCoutObj << "send " << max_size;// << ": " << PIString((char*)data, max_size);
if (fd == -1 || !canWrite()) {
//piCoutObj << "Can`t write to uninitialized COM";
return -1;
@@ -628,16 +643,21 @@ int PISerial::write(const void * data, int max_size, bool wait) {
#ifdef WINDOWS
if (block_write != wait) {
block_write = wait;
// piCoutObj << "set timeout ...";
setReadIsBlocking(block_read);
// piCoutObj << "set timeout ok";
}
DWORD wrote;
// piCoutObj << "send ...";// << max_size;// << ": " << PIString((char*)data, max_size);
sending = true;
WriteFile(PRIVATE->hCom, data, max_size, &wrote, 0);
sending = false;
// piCoutObj << "send ok";// << wrote << " bytes in " << path();
#else
int wrote;
wrote = ::write(fd, data, max_size);
if (wait) tcdrain(fd);
#endif
piCoutObj << "Wrote " << wrote << " bytes in " << path();
return (int)wrote;
//piCoutObj << "Error while sending";
}
@@ -757,8 +777,8 @@ PIStringList PISerial::availableDevices(bool test) {
if (test) {
for (int i = 0; i < dl.size_s(); ++i) {
#ifdef WINDOWS
void * hCom = CreateFileA(dl[i].data(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
if (hCom == INVALID_HANDLE_VALUE) {
void * hComm = CreateFileA(dl[i].data(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
if (hComm == INVALID_HANDLE_VALUE) {
#else
int fd = ::open(dl[i].data(), O_NOCTTY | O_RDONLY);
if (fd == -1) {
@@ -776,8 +796,8 @@ PIStringList PISerial::availableDevices(bool test) {
times.ReadTotalTimeoutMultiplier = 0;
times.WriteTotalTimeoutConstant = 1;
times.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts(hCom, &times);
if (ReadFile(hCom, &void_, 1, &readed_, 0) == 0)
SetCommTimeouts(hComm, &times);
if (ReadFile(hComm, &void_, 1, &readed_, 0) == 0)
rok = GetLastError() == ;*/
#else
fcntl(fd, F_SETFL, O_NONBLOCK);
@@ -791,7 +811,7 @@ PIStringList PISerial::availableDevices(bool test) {
continue;
}
#ifdef WINDOWS
CloseHandle(hCom);
CloseHandle(hComm);
#else
::close(fd);
#endif