Better Serial C++ class library with interrupt

Dependencies:   RingBuffer

Dependents:   Dumb_box_rev2 mbed_GPSproject

iSerial class library can easy be used just by swapping Serial declarations.

API

iSerial

Examples

#include "mbed.h"
#include "iSerial.h"

char s[] = "12345678901234567890123456789012345678901234567890";

int main() {

    iSerial pc(USBTX,USBRX);
    char* c = s;

    while(1) {
        pc.puts(s);
        
        while(pc.readable()){
            int ret = pc.getc();
            if(ret)
                *c++ = ret;
            if(c >= s+sizeof(s)-1)
                c = s;
        }
        wait(1);
    }
}

Example 2

Comparing speed of ordinary Serial functions

#include "mbed.h"
#include "iSerial.h"

char s[] = "12345678901234567890123456789012345678901234567890";    // 50 bytes

int main() {

    Timer t;

    Serial pc(USBTX,USBRX);
    pc.baud(921600);
    
    t.reset();
    t.start();
    for(int i=0; i<30; i++) {
        pc.printf(s);
        wait(0.001);
    }
    t.stop();
    
    int t1 = t.read_us();

    pc.puts("\n\n\n");


    iSerial ipc(USBTX,USBRX,NULL,1000,100); // try NOT to set the buffer over flowed
//    iSerial ipc(USBTX,USBRX);   // set as default = 100, 100
    ipc.baud(921600);

    t.reset();
    t.start();
    for(int i=0; i<30; i++) {
        ipc.printf(s);
        wait(0.001);
    }
    t.stop();
    
    int t2 = t.read_us();

    ipc.printf("\n\n\nThe time %d, %d [us] was taken.\n", t1, t2);
    ipc.puts("End.");
    ipc.flush();
}
Revision:
2:3fc74f4d685a
Parent:
0:1d193cb1a933
Child:
3:d5353b68105f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iSerial.h	Fri Aug 31 17:32:00 2012 +0000
@@ -0,0 +1,80 @@
+//
+//  iSerial.h ... Serial Driver with Interrupt Rec/Send
+//
+//  Copyright 2012  Yoji KURODA
+//
+//  2009.11.13 ... Originally written by Y.Kuroda for Renesas H83664
+//  2012.08.31 ... Code convert for mbed in C++
+//
+#ifndef _ISERIAL_H
+#define _ISERIAL_H
+
+#include <string.h>
+#include "RingBuffer.h"
+
+
+class ISerial : public Serial {
+  protected:
+
+    RingBuffer txbuf;
+    RingBuffer rxbuf;
+
+    void tx_handler(void);
+    void rx_handler(void);
+
+
+  public:
+
+    enum TERMINL_CODES { CR=0x0D, LF=0x0A };
+
+    /*
+     *  void init_sci(void)
+     *
+     * ポート初期化関数....すべてのI/Oの設定を行っている.
+     *
+     *  引数:なし
+     *  戻り値:なし
+     *
+     */
+    ISerial(PinName _tx, PinName _rx, const char *_name=NULL, int _txbufsize=100, int _rxbufsize=100);
+
+    /*
+     *  シリアル入力に文字があるかどうかチェック
+     *  返値:    0  :文字がない
+     *            0以外:文字が来ている
+     */
+    int readable(void);
+    
+    /*
+     *  シリアルから一文字入力
+     */
+    int getc(void);
+    
+    /*
+     * シリアルへ一文字出力
+     */
+    void putc(short ch);
+    
+
+    /*
+     * シリアルへ文字列を出力
+     *  注:一回の最大文字数はLINESIZE
+     *  注:文字列の終わりにはヌル文字が必要.
+     *
+     *  引数:文字列へのポインタ
+     *  返値:出力した文字数
+     */
+    short int putstr(const char* s);
+
+    /* void outs(char* s)
+     * シリアルへ文字列を一行出力
+     *  注:最後に改行コードを送る他はoutstrと同じ
+     *
+     *  引数:文字列へのポインタ
+     *  返値:出力した文字数
+     */
+    short int puts(const char* s);
+};
+
+
+#endif    /* _SCI_H */