CSerialPort::CSerialPort()
{
m_fd = -1;
+ m_tostdout = false;
}
CSerialPort::~CSerialPort()
int32_t byteswritten = 0;
+ struct timeval timeout, *tv;
+ if (data->transmit_timeout <= 0)
+ {
+ tv = NULL;
+ }
+ else
+ {
+ timeout.tv_sec = (long int)data->transmit_timeout / (long int)1000.;
+ timeout.tv_usec = (long int)data->transmit_timeout % (long int)1000.;
+ tv = &timeout;
+ }
+
while (byteswritten < (int32_t) data->size())
{
FD_ZERO(&port);
FD_SET(m_fd, &port);
- int returnv = select(m_fd + 1, NULL, &port, NULL, NULL);
- if (returnv == -1)
+ int returnv = select(m_fd + 1, NULL, &port, NULL, tv);
+ if (returnv < 0)
{
m_error = strerror(errno);
return -1;
}
+ else if (returnv == 0)
+ {
+ m_error = "timeout";
+ return -1;
+ }
returnv = write(m_fd, data->packet.data + byteswritten, data->size() - byteswritten);
if (returnv == -1)
}
//print what's written to stdout for debugging
-// if (m_tostdout)
-// {
-// printf("%s write:", m_name.c_str());
-// for (int i = 0; i < byteswritten; i++)
-// printf(" %02x", (unsigned int)data[i]);
-//
-// printf("\n");
-// }
+ if (m_tostdout)
+ {
+ printf("%s write:", m_name.c_str());
+ for (int i = 0; i < byteswritten; i++)
+ printf(" %02x", data->at(i));
+
+ printf("\n");
+ }
return byteswritten;
}
}
//print what's read to stdout for debugging
-// if (m_tostdout && bytesread > 0)
-// {
-// printf("%s read:", m_name.c_str());
-// for (int i = 0; i < bytesread; i++)
-// printf(" %02x", (unsigned int)data[i]);
-//
-// printf("\n");
-// }
+ if (m_tostdout && bytesread > 0)
+ {
+ printf("%s read:", m_name.c_str());
+ for (int i = 0; i < bytesread; i++)
+ printf(" %02x", data[i]);
+
+ printf("\n");
+ }
return bytesread;
}