LoginSignup
6
4

More than 5 years have passed since last update.

mbed シリアル通信クラス

Last updated at Posted at 2016-06-12

mbedオフィシャルのシリアル通信クラス(Serial, RawSerial)は、ソフトウェアによるバッファリングが実装されていません。
なので、通信手順によってデータ欠落が発生します。

ユーザーが、いくつかバッファリング機能を実装した公開しているので、インポート数の多いものを試しました。

折り返し通信テスト

9600,8,N,1で、折り返し通信。

mbed

  • mbed LPC1768を使用。
  • 受信バッファは8192。
  • 受信データを配列に貯める。
  • 改行コードが来たら配列を送信。
#include "mbed.h"
#include "BufferedSerial.h"

int main()
{
    BufferedSerial pc(USBTX, USBRX, 8192);
    pc.baud(9600);

    char data[1100];
    int data_index = 0;

    while(1) {
        if (!pc.readable()) continue;

        char c = pc.getc();
        data[data_index++] = c;
        if (c == '\n')
        {
            data[data_index] = '\0';
            pc.puts(data);
            data_index = 0;
        }

    }
}

母艦PC

  • 100~1000バイトの文字列(長さはランダム)を送信。
  • 改行コードまでを受信。
  • 送信と受信が一致するか確認。

MODSERIAL by Andy

  • ライブラリページ
  • インポート数 5133回
  • 最終変更 2014/1/8(←更新止まってる?)
  • 親クラス Serial

親クラスがSerialなので、LPC1114などuLibだとアレな感じ。
GPDMAによるバルク送信が実装されているみたい。なかなかパワフルですね。

後述のMODSERIAL by Erikが継承しているようなので、そちらで試すことにします。

BufferedSerial by Sam

折り返し通信テストは、、、折り返しデータが送られてこないorz

MODSERIAL by Erik

親クラスがSerialなので、LPC1114などuLibだとアレな感じ。

折り返し通信テストは良好!
115200bpsにすると、時々、データが化ける。1204705バイトのうち、7バイト。

結論

  • MODSERIAL by Erikがオススメ。
  • 115200bpsの場合は、通信エラーの考慮が必要。

mbedライブラリをrev.121より新しくすると、MODSERIAL by Erikはコンパイルエラーになりますorz

6
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4