Flush input hardware buffer on serial port opening (WARNING: Had to be done in Windows part.)

Make sure that returned RX buffer length of rs232_receive() is set in each case. (WARNING: Had to be done in Windows part.)
Clean up dev_arygon.c to fit with coding conventions.
This commit is contained in:
Romuald Conty 2009-09-17 08:48:05 +00:00
parent 61760cc853
commit 84fd09c281
2 changed files with 20 additions and 15 deletions

View file

@ -68,14 +68,14 @@ static byte_t abtTxBuf[BUFFER_LENGTH] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00,
* @note ARYGON-APDB2UA33 (PN532 + ARYGON µC): 9600,n,8,1 * @note ARYGON-APDB2UA33 (PN532 + ARYGON µC): 9600,n,8,1
*/ */
dev_info* dev_arygon_connect(const nfc_device_desc_t* device_desc) dev_info* dev_arygon_connect(const nfc_device_desc_t* pndd)
{ {
uint32_t uiDevNr; uint32_t uiDevNr;
serial_port sp; serial_port sp;
char acConnect[BUFFER_LENGTH]; char acConnect[BUFFER_LENGTH];
dev_info* pdi = INVALID_DEVICE_INFO; dev_info* pdi = INVALID_DEVICE_INFO;
if( device_desc == NULL ) { if( pndd == NULL ) {
#ifdef DISABLE_SERIAL_AUTOPROBE #ifdef DISABLE_SERIAL_AUTOPROBE
INFO("Sorry, serial auto-probing have been disabled at compile time."); INFO("Sorry, serial auto-probing have been disabled at compile time.");
return INVALID_DEVICE_INFO; return INVALID_DEVICE_INFO;
@ -105,14 +105,14 @@ dev_info* dev_arygon_connect(const nfc_device_desc_t* device_desc)
// Test if we have found a device // Test if we have found a device
if (uiDevNr == MAX_DEVICES) return INVALID_DEVICE_INFO; if (uiDevNr == MAX_DEVICES) return INVALID_DEVICE_INFO;
} else { } else {
DBG("Connecting to: %s at %d bauds.",device_desc->port, device_desc->speed); DBG("Connecting to: %s at %d bauds.",pndd->port, pndd->speed);
strcpy(acConnect,device_desc->port); strcpy(acConnect,pndd->port);
sp = rs232_open(acConnect); sp = rs232_open(acConnect);
if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",acConnect); if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",acConnect);
if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",acConnect); if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",acConnect);
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return INVALID_DEVICE_INFO; if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return INVALID_DEVICE_INFO;
rs232_set_speed(sp, device_desc->speed); rs232_set_speed(sp, pndd->speed);
} }
DBG("Successfully connected to: %s",acConnect); DBG("Successfully connected to: %s",acConnect);

View file

@ -40,7 +40,7 @@ typedef struct {
// Set time-out on 30 miliseconds // Set time-out on 30 miliseconds
struct timeval tv = { struct timeval tv = {
.tv_sec = 0, // No seconds .tv_sec = 0, // 0 second
.tv_usec = 30000 // 30,000 micro seconds .tv_usec = 30000 // 30,000 micro seconds
}; };
@ -88,6 +88,8 @@ serial_port rs232_open(const char* pcPortName)
rs232_close(sp); rs232_close(sp);
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
tcflush(sp, TCIFLUSH);
return sp; return sp;
} }
@ -151,22 +153,25 @@ bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen)
// Read error // Read error
if (iResult < 0) { if (iResult < 0) {
DBG("RX error."); DBG("RX error.");
*puiRxLen = 0;
return false;
}
// Read time-out
if (iResult == 0) {
DBG("RX time-out.");
*puiRxLen = 0;
return false; return false;
} }
// Number of bytes in the input buffer // Number of bytes in the input buffer
ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount); ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
// Read time-out or empty buffer // Empty buffer
#ifdef DEBUG
if (iResult == 0) {
DBG("RX time-out");
}
if (byteCount == 0) { if (byteCount == 0) {
DBG("RX empty buffer"); DBG("RX empty buffer.");
*puiRxLen = 0;
return false;
} }
#endif
if (iResult == 0 || byteCount == 0) return false;
// There is something available, read the data // There is something available, read the data
*puiRxLen = read(((serial_port_unix*)sp)->fd,pbtRx,byteCount); *puiRxLen = read(((serial_port_unix*)sp)->fd,pbtRx,byteCount);