1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 11:44:53 +08:00

Fixing bad C++ constructors.

I am not sure if this is related to the random crashes on Linux, but the wrong C++ std::string constructors were being called because of bad use of c-style array dereferencing. The correct C++ std::string constructors are being called now.
This commit is contained in:
John Harrison 2012-02-06 18:22:14 -06:00
parent 61c193f5d8
commit 9734f943cb
3 changed files with 25 additions and 18 deletions

View File

@ -78,7 +78,7 @@ typedef enum {
*/
typedef enum {
flowcontrol_none = 0,
flowcontrol_software,
flowcontrol_software
} flowcontrol_t;
/*!

View File

@ -230,14 +230,14 @@ Serial::SerialImpl::reconfigurePort ()
// Linux Support
#elif defined(__linux__)
struct serial_struct ser;
ioctl(fd_, TIOCGSERIAL, &ser);
ioctl (fd_, TIOCGSERIAL, &ser);
// set custom divisor
ser.custom_divisor = ser.baud_base / baudrate_;
// update flags
ser.flags &= ~ASYNC_SPD_MASK;
ser.flags |= ASYNC_SPD_CUST;
if (ioctl(fd_, TIOCSSERIAL, ser) < 0)
if (ioctl (fd_, TIOCSSERIAL, ser) < 0)
{
throw IOException (errno);
}
@ -420,7 +420,7 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size)
// Calculate the time select took
struct timeval diff;
diff.tv_sec = end.tv_sec-start.tv_sec;
diff.tv_usec = (end.tv_nsec-start.tv_nsec)/1000;
diff.tv_usec = static_cast<int> ((end.tv_nsec-start.tv_nsec)/1000);
// Update the timeout
if (timeout.tv_sec <= diff.tv_sec) {
timeout.tv_sec = 0;
@ -703,7 +703,7 @@ Serial::SerialImpl::getDSR()
{
throw PortNotOpenedException ("Serial::getDSR");
}
int s = ioctl(fd_, TIOCMGET, 0);
int s = ioctl (fd_, TIOCMGET, 0);
return (s & TIOCM_DSR) != 0;
}

View File

@ -132,17 +132,19 @@ size_t
Serial::readline (string &buffer, size_t size, string eol)
{
ScopedReadLock (this->pimpl_);
size_t eol_len = eol.length();
unsigned char buffer_[size];
size_t eol_len = eol.length ();
unsigned char *buffer_ = static_cast<unsigned char*>
(alloca (size * sizeof (unsigned char)));
size_t read_so_far = 0;
while (true)
{
size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
size_t bytes_read = this->read_ (buffer_ + read_so_far, 1);
read_so_far += bytes_read;
if (bytes_read == 0) {
break; // Timeout occured on reading 1 byte
}
if (string(buffer_[read_so_far-eol_len], eol_len) == eol) {
if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
break; // EOL found
}
if (read_so_far == size) {
@ -165,8 +167,9 @@ Serial::readlines (size_t size, string eol)
{
ScopedReadLock (this->pimpl_);
std::vector<std::string> lines;
size_t eol_len = eol.length();
unsigned char buffer_[size];
size_t eol_len = eol.length ();
unsigned char *buffer_ = static_cast<unsigned char*>
(alloca (size * sizeof (unsigned char)));
size_t read_so_far = 0;
size_t start_of_line = 0;
while (read_so_far < size) {
@ -174,21 +177,25 @@ Serial::readlines (size_t size, string eol)
read_so_far += bytes_read;
if (bytes_read == 0) {
if (start_of_line != read_so_far) {
lines.push_back(
std::string(buffer_[start_of_line], read_so_far-start_of_line));
lines.push_back (
string (reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
}
break; // Timeout occured on reading 1 byte
}
if (string(buffer_[read_so_far-eol_len], eol_len) == eol) {
if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
// EOL found
lines.push_back(
std::string(buffer_[start_of_line], read_so_far-start_of_line));
string(reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
start_of_line = read_so_far;
}
if (read_so_far == size) {
if (start_of_line != read_so_far) {
lines.push_back(
std::string(buffer_[start_of_line], read_so_far-start_of_line));
string(reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
}
break; // Reached the maximum read length
}
@ -208,10 +215,10 @@ Serial::setPort (const string &port)
{
ScopedReadLock(this->pimpl_);
ScopedWriteLock(this->pimpl_);
bool was_open = pimpl_->isOpen();
bool was_open = pimpl_->isOpen ();
if (was_open) close();
pimpl_->setPort (port);
if (was_open) open();
if (was_open) open ();
}
string