1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Fixing timeouts, there were not functioning correctly.

This commit is contained in:
John Harrison 2012-01-17 12:35:32 -06:00
parent 04f81f23ed
commit fe61b346da
2 changed files with 9 additions and 5 deletions

View File

@ -236,7 +236,7 @@ Serial::SerialImpl::read (char* buf, size_t size)
FD_SET (fd_, &readfds); FD_SET (fd_, &readfds);
struct timeval timeout; struct timeval timeout;
timeout.tv_sec = timeout_ / 1000; timeout.tv_sec = timeout_ / 1000;
timeout.tv_usec = static_cast<int> (timeout_ % 1000); timeout.tv_usec = static_cast<int> (timeout_ % 1000) * 1000;
int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
if (r == -1 && errno == EINTR) if (r == -1 && errno == EINTR)
@ -265,6 +265,10 @@ Serial::SerialImpl::read (char* buf, size_t size)
} }
break; break;
} }
else
{
break;
}
} }
return static_cast<size_t> (bytes_read); return static_cast<size_t> (bytes_read);
} }
@ -298,8 +302,6 @@ void
Serial::SerialImpl::setTimeout (long timeout) Serial::SerialImpl::setTimeout (long timeout)
{ {
timeout_ = timeout; timeout_ = timeout;
if (isOpen_)
reconfigurePort ();
} }
long long

View File

@ -11,13 +11,15 @@ using serial::SerialExecption;
int main(int argc, char **argv) { int main(int argc, char **argv) {
try { try {
Serial s("/dev/tty.usbserial-A900adHq", 115200, 2000); Serial s("/dev/tty.usbserial-A900adHq", 115200, 100);
s.flush(); s.flush();
long long count = 0; long long count = 0;
while (1) { while (1) {
// size_t available = s.available(); // size_t available = s.available();
// cout << "avialable: " << available << endl; // cout << "avialable: " << available << endl;
string line = s.readline(); string line = s.read();
if (line.empty())
cout << "Nothing\n";
cout << count << ": " << line << line.length() << endl; cout << count << ": " << line << line.length() << endl;
count++; count++;
} }