0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Host PCからmbedボードを経由してシリアルデバイスを操作する(シリアルコンソール)

Posted at

モデム等を用いた開発をしていると、PCからマイコンボードを経由して直接ATコマンド等をシリアルデバイスに流し込みたい時があります。
Arduino系の場合はサンプルがあるのですが、mbedでは見当たらなかったのでメモ代わりに置いておきます。

a.gif

論よりコード

main.cpp
# include "mbed.h"

Serial HostPC(USBTX, USBRX);
Serial UARTdevice(PA_0, PA_1); /* Using serial4 on F446RE */

int main() {
    HostPC.baud(115200);
    HostPC.printf("\r\n-- Start: SerialBridge\r\n");
    UARTdevice.baud(9600); /* Check: Baudrate of UARTdevice */
    wait(0.2);             /* IMPORTANT: waiting for warm-up of UARTdevice */
    HostPC.printf("-- Start interactive session (Recommmend: Local echo ON and CRLF)\r\n");
    HostPC.printf("-- Example: AT[enter]\r\n");
    while(1) {
        while (UARTdevice.readable()) HostPC.putc(UARTdevice.getc());
        while (HostPC.readable()) UARTdevice.putc(HostPC.getc());
    }
}

確認環境

解説

mbed.h のみで実現したので、大体どのボードでも使えるとは思います。
ローカルエコーや改行コードはHost PCのターミナルで設定してください。

オフィシャルのシリアル通信クラスはバッファリングが無いので取りこぼしもありそうです。そのためBufferedSerialを使うのも良さそうです。(出典: mbed シリアル通信クラス)

テンプレートを使わずに「スッピンで」mbedの環境をそろえる

mbed オンラインコンパイラで新しく開発を始めようとすると色々テンプレートがあります。便利ですが、ちょっとしたことを行うにはデカすぎることが多いです。
ゼロから開発する方法を オンラインIDEで、新規から開発する で解説してあります。

参考資料

あとがき

久々に書いたよ。

EoT

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?