crayzeewulf / libserial

Serial Port Programming in C++
BSD 3-Clause "New" or "Revised" License
398 stars 141 forks source link

[Patch] showmanyc method to allow non-blocking read [sf#1] #44

Closed crayzeewulf closed 9 years ago

crayzeewulf commented 9 years ago

Reported by wedesoft on 2005-07-30 14:52 UTC One would be able to check for availability of input using 's.rdbuf()->in_avail() > 0', if the method 'SerialStreamBuf::showmanyc()' is implemented:

std::streamsize SerialStreamBuf::showmanyc() {

int retval = -1;

if ( -1 == mFileDescriptor ) { return -1; };

if ( mPutbackAvailable ) {

// We still have a character left in the buffer. retval = 1;

} else {

// Switch to non-blocking read. int flags = fcntl(this->mFileDescriptor, F_GETFL, 0) ; if( -1 == fcntl( this->mFileDescriptor, F_SETFL, flags | O_NONBLOCK ) ) { return -1; }

// Try to read a character. retval = read(mFileDescriptor, &mPutbackChar, 1);

if ( retval == 1 ) { mPutbackAvailable = true; } else retval = 0;

// Switch back to blocking read. if( -1 == fcntl( this->mFileDescriptor, F_SETFL, flags ) ) { return -1; } }; return retval;

}

The patch furthermore fixes the missing return-statement in 'SerialStreamBuf::open'.

crayzeewulf commented 9 years ago

Commented by wedesoft on 2005-08-03 16:28 UTC 'rdbuf()->in_avail()' and timeouts for libserial-0.3.2 Attached file showmanyc_timeout.patch.gz:

The file could not be attached: 'ascii' codec can't decode byte 0x8b in position 5: ordinal not in range(128)

crayzeewulf commented 9 years ago

Commented by wedesoft on 2005-08-03 16:28 UTC Logged In: YES user_id=1106827

I've furthermore added the functions "SetTimeout" and "Timeout" to specify a timeout for read-operations. The new patch "showmanyc_timeout.patch.gz" contains all changes. Apply patch by doing: gunzip showmanyc_timeout.patch.gz cd libserial-0.3.2 patch -p1 < ../showmanyc_timeout.patch

crayzeewulf commented 9 years ago

Updated by wedesoft on 2005-09-22 10:24 UTC