Fix some some buffer synchronization problems under POSIX system: Issue 17. Thanks to zuck.

This commit is contained in:
Romuald Conty 2009-09-04 13:48:06 +00:00
parent be55ba2955
commit dbf48890ac

View file

@ -119,11 +119,9 @@ bool rs232_cts(const serial_port sp)
bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen)
{
int iResult;
uint32_t uiCount = 0;
int byteCount;
fd_set rfds;
while (true)
{
// Reset file descriptor
FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
@ -135,23 +133,24 @@ bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen)
return false;
}
// Read time-out
if (iResult == 0)
{
// Test if we at least have received something
if (uiCount == 0) {
DBG("RX time-out without received data.");
return false;
}
// Number of bytes in the input buffer
ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
// Store the received byte count and return succesful
*puiRxLen = uiCount;
return true;
// Read time-out or empty buffer
#ifdef DEBUG
if (iResult == 0) {
DBG("RX time-out");
}
if (byteCount == 0) {
DBG("RX empty buffer");
}
#endif
if (iResult == 0 || byteCount == 0) return false;
// There is something available, read the data
uiCount += read(((serial_port_unix*)sp)->fd,pbtRx+uiCount,*puiRxLen-uiCount);
}
*puiRxLen = read(((serial_port_unix*)sp)->fd,pbtRx,byteCount);
return (*puiRxLen > 0);
}
bool rs232_send(const serial_port sp, const byte_t* pbtTx, const uint32_t uiTxLen)