prevent busy loop when tty could not be opened
This commit is contained in:
parent
93043c25b8
commit
423704f63d
1 changed files with 18 additions and 4 deletions
|
@ -34,6 +34,7 @@ comm_posix_tty::comm_posix_tty(const std::string & device, const int bitrate)
|
||||||
if (tcgetattr(fd, &tty) == -1) {
|
if (tcgetattr(fd, &tty) == -1) {
|
||||||
DOLOG(warning, false, "com_posix_tty tcgetattr failed: %s", strerror(errno));
|
DOLOG(warning, false, "com_posix_tty tcgetattr failed: %s", strerror(errno));
|
||||||
close(fd);
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,22 +62,27 @@ comm_posix_tty::comm_posix_tty(const std::string & device, const int bitrate)
|
||||||
if (tcsetattr(fd, TCSANOW, &tty) == -1) {
|
if (tcsetattr(fd, TCSANOW, &tty) == -1) {
|
||||||
DOLOG(warning, false, "com_posix_tty tcsetattr failed: %s", strerror(errno));
|
DOLOG(warning, false, "com_posix_tty tcsetattr failed: %s", strerror(errno));
|
||||||
close(fd);
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
comm_posix_tty::~comm_posix_tty()
|
comm_posix_tty::~comm_posix_tty()
|
||||||
{
|
{
|
||||||
close(fd);
|
if (fd != -1)
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool comm_posix_tty::is_connected()
|
bool comm_posix_tty::is_connected()
|
||||||
{
|
{
|
||||||
return true;
|
return fd != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool comm_posix_tty::has_data()
|
bool comm_posix_tty::has_data()
|
||||||
{
|
{
|
||||||
|
if (fd == -1)
|
||||||
|
return false;
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
WSAPOLLFD fds[] { { fd, POLLIN, 0 } };
|
WSAPOLLFD fds[] { { fd, POLLIN, 0 } };
|
||||||
int rc = WSAPoll(fds, 1, 0);
|
int rc = WSAPoll(fds, 1, 0);
|
||||||
|
@ -91,7 +97,11 @@ bool comm_posix_tty::has_data()
|
||||||
uint8_t comm_posix_tty::get_byte()
|
uint8_t comm_posix_tty::get_byte()
|
||||||
{
|
{
|
||||||
uint8_t c = 0;
|
uint8_t c = 0;
|
||||||
read(fd, &c, 1); // TODO error handling
|
if (read(fd, &c, 1) <= 0) {
|
||||||
|
DOLOG(warning, false, "com_posix_tty cannot read");
|
||||||
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
@ -103,8 +113,12 @@ void comm_posix_tty::send_data(const uint8_t *const in, const size_t n)
|
||||||
|
|
||||||
while(len > 0) {
|
while(len > 0) {
|
||||||
int rc = write(fd, p, len);
|
int rc = write(fd, p, len);
|
||||||
if (rc <= 0) // TODO error checking
|
if (rc <= 0) { // TODO error checking
|
||||||
|
DOLOG(warning, false, "com_posix_tty cannot write");
|
||||||
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
p += rc;
|
p += rc;
|
||||||
len -= rc;
|
len -= rc;
|
||||||
|
|
Loading…
Add table
Reference in a new issue