From 614c4c0403b85014e016ad4923e27ecfad74b0f8 Mon Sep 17 00:00:00 2001 From: Sidney Leung Date: Mon, 21 Dec 2015 12:12:18 -0800 Subject: [PATCH] Jira-462: Call Available() after calling end() indicated data available. Code Modifications: 1. Added checking of device open in these routines: available, read, peek. Will return 0 or -1 if device is not opened to indicate invalid operation. --- cores/arduino/CDCSerialClass.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cores/arduino/CDCSerialClass.cpp b/cores/arduino/CDCSerialClass.cpp index ad5c569e..56c50e89 100644 --- a/cores/arduino/CDCSerialClass.cpp +++ b/cores/arduino/CDCSerialClass.cpp @@ -84,6 +84,10 @@ void CDCSerialClass::end( void ) int CDCSerialClass::available( void ) { #define SBS SERIAL_BUFFER_SIZE + + if (!_shared_data->device_open) + return (0); + else return (int)(SBS + _rx_buffer->head - _rx_buffer->tail) % SBS; } @@ -102,7 +106,7 @@ int CDCSerialClass::availableForWrite(void) int CDCSerialClass::peek(void) { - if ( _rx_buffer->head == _rx_buffer->tail ) + if ((!_shared_data->device_open) || ( _rx_buffer->head == _rx_buffer->tail )) return -1; return _rx_buffer->data[_rx_buffer->tail]; @@ -110,7 +114,7 @@ int CDCSerialClass::peek(void) int CDCSerialClass::read( void ) { - if ( _rx_buffer->head == _rx_buffer->tail ) + if ((!_shared_data->device_open) || ( _rx_buffer->head == _rx_buffer->tail )) return -1; uint8_t uc = _rx_buffer->data[_rx_buffer->tail];