Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BufferSerial.cpp@7:0bc2bc07f2fe, 2020-12-02 (annotated)
- Committer:
- tetsu_0207
- Date:
- Wed Dec 02 08:06:45 2020 +0000
- Revision:
- 7:0bc2bc07f2fe
- Parent:
- 6:17e3a28338dc
[Debug] success buffer serial library
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tetsu_0207 | 6:17e3a28338dc | 1 | #include "BufferSerial.h" |
| tetsu_0207 | 6:17e3a28338dc | 2 | |
| tetsu_0207 | 6:17e3a28338dc | 3 | BufferSerial::BufferSerial(PinName tx, PinName rx,int baud,int timeout) |
| tetsu_0207 | 6:17e3a28338dc | 4 | :RawSerial(tx,rx,baud) |
| tetsu_0207 | 6:17e3a28338dc | 5 | { |
| tetsu_0207 | 6:17e3a28338dc | 6 | // initialize functions |
| tetsu_0207 | 6:17e3a28338dc | 7 | _rx_buffer_head = 0; |
| tetsu_0207 | 6:17e3a28338dc | 8 | _rx_buffer_tail = 0; |
| tetsu_0207 | 6:17e3a28338dc | 9 | // time out |
| tetsu_0207 | 6:17e3a28338dc | 10 | timer_ms = timeout; |
| tetsu_0207 | 6:17e3a28338dc | 11 | timer.start(); |
| tetsu_0207 | 6:17e3a28338dc | 12 | // attach |
| tetsu_0207 | 6:17e3a28338dc | 13 | RawSerial::attach(callback(this,&BufferSerial::serial_rx), Serial::RxIrq); |
| tetsu_0207 | 6:17e3a28338dc | 14 | } |
| tetsu_0207 | 6:17e3a28338dc | 15 | |
| tetsu_0207 | 6:17e3a28338dc | 16 | int BufferSerial::available(void) |
| tetsu_0207 | 6:17e3a28338dc | 17 | { |
| tetsu_0207 | 6:17e3a28338dc | 18 | return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE; |
| tetsu_0207 | 6:17e3a28338dc | 19 | } |
| tetsu_0207 | 6:17e3a28338dc | 20 | |
| tetsu_0207 | 6:17e3a28338dc | 21 | int BufferSerial::read(void) |
| tetsu_0207 | 6:17e3a28338dc | 22 | { |
| tetsu_0207 | 6:17e3a28338dc | 23 | if (_rx_buffer_head == _rx_buffer_tail) { |
| tetsu_0207 | 6:17e3a28338dc | 24 | return -1; |
| tetsu_0207 | 6:17e3a28338dc | 25 | } else { |
| tetsu_0207 | 6:17e3a28338dc | 26 | unsigned char c = _rx_buffer[_rx_buffer_tail]; |
| tetsu_0207 | 6:17e3a28338dc | 27 | _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE; |
| tetsu_0207 | 6:17e3a28338dc | 28 | return c; |
| tetsu_0207 | 6:17e3a28338dc | 29 | } |
| tetsu_0207 | 6:17e3a28338dc | 30 | } |
| tetsu_0207 | 6:17e3a28338dc | 31 | |
| tetsu_0207 | 6:17e3a28338dc | 32 | size_t BufferSerial::readBytesUntil(char charactor,char* buffer,int length) |
| tetsu_0207 | 6:17e3a28338dc | 33 | { |
| tetsu_0207 | 6:17e3a28338dc | 34 | size_t index = 0; |
| tetsu_0207 | 6:17e3a28338dc | 35 | while (index < length) { |
| tetsu_0207 | 6:17e3a28338dc | 36 | int c = timedRead(); |
| tetsu_0207 | 6:17e3a28338dc | 37 | if (c < 0 || c == charactor){ |
| tetsu_0207 | 6:17e3a28338dc | 38 | break; |
| tetsu_0207 | 6:17e3a28338dc | 39 | } |
| tetsu_0207 | 6:17e3a28338dc | 40 | *buffer++ = (char)c; |
| tetsu_0207 | 6:17e3a28338dc | 41 | index++; |
| tetsu_0207 | 6:17e3a28338dc | 42 | } |
| tetsu_0207 | 6:17e3a28338dc | 43 | return index; |
| tetsu_0207 | 6:17e3a28338dc | 44 | } |
| tetsu_0207 | 6:17e3a28338dc | 45 | |
| tetsu_0207 | 6:17e3a28338dc | 46 | void BufferSerial::serial_rx(void) |
| tetsu_0207 | 6:17e3a28338dc | 47 | { |
| tetsu_0207 | 6:17e3a28338dc | 48 | while(RawSerial::readable()) { |
| tetsu_0207 | 6:17e3a28338dc | 49 | unsigned char c = RawSerial::getc(); |
| tetsu_0207 | 6:17e3a28338dc | 50 | rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE; |
| tetsu_0207 | 6:17e3a28338dc | 51 | if (i != _rx_buffer_tail) { |
| tetsu_0207 | 6:17e3a28338dc | 52 | _rx_buffer[_rx_buffer_head] = c; |
| tetsu_0207 | 6:17e3a28338dc | 53 | _rx_buffer_head = i; |
| tetsu_0207 | 6:17e3a28338dc | 54 | } |
| tetsu_0207 | 6:17e3a28338dc | 55 | } |
| tetsu_0207 | 6:17e3a28338dc | 56 | } |
| tetsu_0207 | 6:17e3a28338dc | 57 | |
| tetsu_0207 | 6:17e3a28338dc | 58 | int BufferSerial::timedRead() |
| tetsu_0207 | 6:17e3a28338dc | 59 | { |
| tetsu_0207 | 6:17e3a28338dc | 60 | int c; |
| tetsu_0207 | 6:17e3a28338dc | 61 | timer.reset(); |
| tetsu_0207 | 6:17e3a28338dc | 62 | _startMillis = timer.read_ms(); |
| tetsu_0207 | 6:17e3a28338dc | 63 | do { |
| tetsu_0207 | 6:17e3a28338dc | 64 | c = read(); |
| tetsu_0207 | 6:17e3a28338dc | 65 | if (c >= 0) return c; |
| tetsu_0207 | 6:17e3a28338dc | 66 | } while(timer.read_ms() - _startMillis < _timeout); |
| tetsu_0207 | 6:17e3a28338dc | 67 | return -1; // -1 indicates timeout |
| tetsu_0207 | 6:17e3a28338dc | 68 | } |