Buffered Serial Port Driver for RTOS

Dependents:   nucleo_cannonball PiballNeoController

Buffered Serial Port Driver for RTOS

  • ISR driven, ring buffered IO operation
  • IO operations are idle waiting, don't waste time in RTOS :D
  • Can use external buffers
  • Based on mbed RawSerial

Example

SerialDriver Example

#include "SerialDriver.h"

SerialDriver pc(USBTX, USBRX);

int main()
{
    // setup serial port
    pc.baud(9600);
    
    // print some text
    pc.puts("This is just a string.\r\n");
    pc.printf("But this is a %s with integer %i and float %f.\r\n", "formatted text", 123, 0.456f);
    
    // now lets behave like a null modem 
    while(1)
       pc.putc(pc.getc());
}

Look at the API Documentation for more Examples.

Dependencies

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Import librarymbed-rtos

Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

If you find a bug, please help me to fix it. Send me a message. You can help me a lot: Write a demo program that causes the bug reproducible.

Examples/Example_Blocking.cpp

Committer:
BlazeX
Date:
2015-01-14
Revision:
0:cd0d79be0c1a
Child:
1:1464146bd7fb

File content as of revision 0:cd0d79be0c1a:

/// @file Example_Blocking.cpp
/// @brief Test blocking write / read
/// 
/// - Uses SerialDriver with USBTX and USBRX.
/// - Has much too small buffers, to how far can it get?
/// 
/// - Testing how much bytes were transmitted, with too small TX buffer and forced non blocking
/// - Waiting till 10 bytes are received
/// - LED4 indicates parallel thread working
/// 

#if 0

#include "mbed.h"
#include "SerialDriver.h"

SerialDriver pc(USBTX, USBRX, 4, 32);

// This thread is running in parallel
DigitalOut led4(LED4);
void parallel(void const * argument)
{
    while(1)
    {
        Thread::wait(20);
        led4= !led4;
    }
}

int main()
{
    // Start the other thread
    Thread parallelTask(&parallel);
    
    
    const char * completeText= "This is a complete text. How much will you receive?";
    const int completeTextLength= strlen(completeText);
    int writtenBytes;
    
    
    const int readBufferLength= 10;
    unsigned char readBuffer[readBufferLength];
    int receivedBytes= 0;
    
    while(1)
    {
        // write non blocking, how much get transmitted?
        writtenBytes= pc.write((const unsigned char*)completeText, completeTextLength, false);
        
        // now print the result
        pc.printf("\r\nOnly %i of %i bytes were transmitted using non blocking write.\r\n", writtenBytes, completeTextLength);
        
        
        // wait for 10 bytes
        pc.printf("I wait for my 10 bytes. Send them!\r\n", writtenBytes, completeTextLength);
        receivedBytes+= pc.read(readBuffer, readBufferLength);
        
        // now print result
        pc.printf("Received %i bytes since start.\r\n\r\n", receivedBytes);
        
        
        // wait a bit
        Thread::wait(1000);
    }
}

#endif