You are viewing an older revision! See the latest version
MODSERIAL
Import libraryMODSERIAL
Bug fix release
MODSERIAL is an easy to use library that extends Serial to add fully buffered input and output.
The features of MODSERIAL include:-
- Directly compatible with Serial
- Fully customisable buffer settings
- Current buffer information via the API
- Circular buffers that allow for dynamic resizing
Connecting up the MODSERIAL module¶
The starting point for using MODSERIAL is the Mbed's own handbook for Serial library object. MODSERIAL inherits Serial and adds extensions for buffering. So getting started is easy. Follow the Mbed instructions for Serial to get setup. Here's a reproduction of Serial's simple code starter:-
1 #include "mbed.h"
2
3 Serial pc(USBTX, USBRX); // tx, rx
4
5 int main() {
6 pc.printf("Hello World!");
7 while(1) {
8 pc.putc(pc.getc() + 1);
9 }
10 }
All we need to do to use MODSERIAL is to alter one line thus:-
1 #include "mbed.h"
2 #include "MODSERIAL.h"
3 MODSERIAL pc(USBTX, USBRX); // tx, rx
4
5 int main() {
6 pc.printf("Hello World!");
7 while(1) {
8 pc.putc(pc.getc() + 1);
9 }
10 }
As we can see, all we have done is change line 3 to specify the use of MODSERIAL in replacement for Serial. The default settings for MODSERIAL are that both the TX and RX buffers are assigned 256 bytes each of storage space. This storage space is acquired from the heap using malloc.
The default buffer assignment can be manipulated in two ways. First is the compile time setting which alters the default parameters used when creating a MODSERIAL object. This is done thus:-
<<code>>
1 #include "mbed.h"
2
3 #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
4 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 1024
5 #include "MODSERIAL.h"
6
7 MODSERIAL pc(USBTX, USBRX); tx, rx
8 ...
<</code>
By defining the two #defines before the #include "MODSERIAL.h" alters the defaults MODSERIAL uses to create it's buffers. The second method is the run-time version. To get TX at 1024 and RX buffer at 512 as above during run-time initialisation, alter the constructor thus:-
<<code>>
1 #include "mbed.h"
2 #include "MODSERIAL.h"
3
4 MODSERIAL pc(USBTX, USBRX, 1024, 512); tx, rx
5 ...
<</code>
If you supply only one numeric value, as shown below, both TX and RX will have the same buffer sizes assigned to them:-
<<code>>
1 #include "mbed.h"
2 #include "MODSERIAL.h"
3
4 Make both TX and RX use a 512byte buffer.
5 MODSERIAL pc(USBTX, USBRX, 512); tx, rx
6 ...
<</code>
