![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Serial buffer problem
SerialBuffered.cpp@0:56a10c93329f, 2010-10-11 (annotated)
- Committer:
- pd0wm
- Date:
- Mon Oct 11 18:46:18 2010 +0000
- Revision:
- 0:56a10c93329f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pd0wm | 0:56a10c93329f | 1 | |
pd0wm | 0:56a10c93329f | 2 | #include "mbed.h" |
pd0wm | 0:56a10c93329f | 3 | #include "SerialBuffered.h" |
pd0wm | 0:56a10c93329f | 4 | |
pd0wm | 0:56a10c93329f | 5 | extern Serial loggerSerial; |
pd0wm | 0:56a10c93329f | 6 | |
pd0wm | 0:56a10c93329f | 7 | SerialBuffered::SerialBuffered( size_t bufferSize, PinName tx, PinName rx ) : Serial( tx, rx ) |
pd0wm | 0:56a10c93329f | 8 | { |
pd0wm | 0:56a10c93329f | 9 | m_buffSize = 0; |
pd0wm | 0:56a10c93329f | 10 | m_contentStart = 0; |
pd0wm | 0:56a10c93329f | 11 | m_contentEnd = 0; |
pd0wm | 0:56a10c93329f | 12 | m_timeout = 1.0; |
pd0wm | 0:56a10c93329f | 13 | |
pd0wm | 0:56a10c93329f | 14 | |
pd0wm | 0:56a10c93329f | 15 | attach( this, &SerialBuffered::handleInterrupt ); |
pd0wm | 0:56a10c93329f | 16 | |
pd0wm | 0:56a10c93329f | 17 | m_buff = (uint8_t *) malloc( bufferSize ); |
pd0wm | 0:56a10c93329f | 18 | if( m_buff == NULL ) |
pd0wm | 0:56a10c93329f | 19 | { |
pd0wm | 0:56a10c93329f | 20 | //loggerSerial.printf("SerialBuffered - failed to alloc buffer size %d\r\n", (int) bufferSize ); |
pd0wm | 0:56a10c93329f | 21 | } |
pd0wm | 0:56a10c93329f | 22 | else |
pd0wm | 0:56a10c93329f | 23 | { |
pd0wm | 0:56a10c93329f | 24 | m_buffSize = bufferSize; |
pd0wm | 0:56a10c93329f | 25 | } |
pd0wm | 0:56a10c93329f | 26 | } |
pd0wm | 0:56a10c93329f | 27 | |
pd0wm | 0:56a10c93329f | 28 | |
pd0wm | 0:56a10c93329f | 29 | SerialBuffered::~SerialBuffered() |
pd0wm | 0:56a10c93329f | 30 | { |
pd0wm | 0:56a10c93329f | 31 | if( m_buff ) |
pd0wm | 0:56a10c93329f | 32 | free( m_buff ); |
pd0wm | 0:56a10c93329f | 33 | } |
pd0wm | 0:56a10c93329f | 34 | |
pd0wm | 0:56a10c93329f | 35 | void SerialBuffered::setTimeout( float seconds ) |
pd0wm | 0:56a10c93329f | 36 | { |
pd0wm | 0:56a10c93329f | 37 | m_timeout = seconds; |
pd0wm | 0:56a10c93329f | 38 | } |
pd0wm | 0:56a10c93329f | 39 | |
pd0wm | 0:56a10c93329f | 40 | size_t SerialBuffered::readBytes( uint8_t *bytes, size_t requested ) |
pd0wm | 0:56a10c93329f | 41 | { |
pd0wm | 0:56a10c93329f | 42 | int i = 0; |
pd0wm | 0:56a10c93329f | 43 | |
pd0wm | 0:56a10c93329f | 44 | for( ; i < requested; ) |
pd0wm | 0:56a10c93329f | 45 | { |
pd0wm | 0:56a10c93329f | 46 | int c = getc(); |
pd0wm | 0:56a10c93329f | 47 | if( c < 0 ) |
pd0wm | 0:56a10c93329f | 48 | break; |
pd0wm | 0:56a10c93329f | 49 | bytes[i] = c; |
pd0wm | 0:56a10c93329f | 50 | i++; |
pd0wm | 0:56a10c93329f | 51 | } |
pd0wm | 0:56a10c93329f | 52 | |
pd0wm | 0:56a10c93329f | 53 | return i; |
pd0wm | 0:56a10c93329f | 54 | |
pd0wm | 0:56a10c93329f | 55 | } |
pd0wm | 0:56a10c93329f | 56 | |
pd0wm | 0:56a10c93329f | 57 | |
pd0wm | 0:56a10c93329f | 58 | int SerialBuffered::getc() |
pd0wm | 0:56a10c93329f | 59 | { |
pd0wm | 0:56a10c93329f | 60 | m_timer.reset(); |
pd0wm | 0:56a10c93329f | 61 | m_timer.start(); |
pd0wm | 0:56a10c93329f | 62 | while( m_contentStart == m_contentEnd ) |
pd0wm | 0:56a10c93329f | 63 | { |
pd0wm | 0:56a10c93329f | 64 | |
pd0wm | 0:56a10c93329f | 65 | |
pd0wm | 0:56a10c93329f | 66 | wait_ms( 1 ); |
pd0wm | 0:56a10c93329f | 67 | if( m_timeout > 0 && m_timer.read() > m_timeout ) |
pd0wm | 0:56a10c93329f | 68 | return EOF; |
pd0wm | 0:56a10c93329f | 69 | } |
pd0wm | 0:56a10c93329f | 70 | |
pd0wm | 0:56a10c93329f | 71 | m_timer.stop(); |
pd0wm | 0:56a10c93329f | 72 | |
pd0wm | 0:56a10c93329f | 73 | uint8_t result = m_buff[m_contentStart++]; |
pd0wm | 0:56a10c93329f | 74 | m_contentStart = m_contentStart % m_buffSize; |
pd0wm | 0:56a10c93329f | 75 | |
pd0wm | 0:56a10c93329f | 76 | |
pd0wm | 0:56a10c93329f | 77 | return result; |
pd0wm | 0:56a10c93329f | 78 | } |
pd0wm | 0:56a10c93329f | 79 | |
pd0wm | 0:56a10c93329f | 80 | |
pd0wm | 0:56a10c93329f | 81 | int SerialBuffered::readable() |
pd0wm | 0:56a10c93329f | 82 | { |
pd0wm | 0:56a10c93329f | 83 | return m_contentStart != m_contentEnd ; |
pd0wm | 0:56a10c93329f | 84 | } |
pd0wm | 0:56a10c93329f | 85 | |
pd0wm | 0:56a10c93329f | 86 | void SerialBuffered::handleInterrupt() |
pd0wm | 0:56a10c93329f | 87 | { |
pd0wm | 0:56a10c93329f | 88 | |
pd0wm | 0:56a10c93329f | 89 | while( Serial::readable()) |
pd0wm | 0:56a10c93329f | 90 | { |
pd0wm | 0:56a10c93329f | 91 | if( m_contentStart == (m_contentEnd +1) % m_buffSize) |
pd0wm | 0:56a10c93329f | 92 | { |
pd0wm | 0:56a10c93329f | 93 | loggerSerial.printf("SerialBuffered - buffer overrun, data lost!\r\n" ); |
pd0wm | 0:56a10c93329f | 94 | Serial::getc(); |
pd0wm | 0:56a10c93329f | 95 | |
pd0wm | 0:56a10c93329f | 96 | } |
pd0wm | 0:56a10c93329f | 97 | else |
pd0wm | 0:56a10c93329f | 98 | { |
pd0wm | 0:56a10c93329f | 99 | |
pd0wm | 0:56a10c93329f | 100 | m_buff[ m_contentEnd ++ ] = Serial::getc(); |
pd0wm | 0:56a10c93329f | 101 | m_contentEnd = m_contentEnd % m_buffSize; |
pd0wm | 0:56a10c93329f | 102 | |
pd0wm | 0:56a10c93329f | 103 | |
pd0wm | 0:56a10c93329f | 104 | |
pd0wm | 0:56a10c93329f | 105 | } |
pd0wm | 0:56a10c93329f | 106 | } |
pd0wm | 0:56a10c93329f | 107 | } |
pd0wm | 0:56a10c93329f | 108 |