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) bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen)
{ {
int iResult; int iResult;
uint32_t uiCount = 0; int byteCount;
fd_set rfds; fd_set rfds;
while (true)
{
// Reset file descriptor // Reset file descriptor
FD_ZERO(&rfds); FD_ZERO(&rfds);
FD_SET(((serial_port_unix*)sp)->fd,&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; return false;
} }
// Read time-out // Number of bytes in the input buffer
if (iResult == 0) ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
{
// Test if we at least have received something
if (uiCount == 0) {
DBG("RX time-out without received data.");
return false;
}
// Store the received byte count and return succesful // Read time-out or empty buffer
*puiRxLen = uiCount; #ifdef DEBUG
return true; 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 // 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) bool rs232_send(const serial_port sp, const byte_t* pbtTx, const uint32_t uiTxLen)