Serial Communication with a PC(翻訳)

元のぺーじはこちら

The mbed Microcontroller can communicate with a host PC through a "USB Virtual Serial Port" over the same USB cable that is used for programming.
mbed マイクロコントローラーは、プログラミングに使用しているUSBを "USB Virtual Serial Port" としてホストPCと通信をすることができます。

This enables you to:
できること:

  • Print out messages to a host PC terminal (useful for debugging!)
    ホストPCのターミナルにメッセージを表示させます (デバッグに便利!)
  • Read input from the host PC keyboard
    ホストPCのキーボード入力を読み取れます。
  • Communicate with applications and programming languages running on the host PC that can communicate with a serial port, e.g. perl, python, java and so on.
    通信ソフトもしくは、ホストPC上で動作しているプログラミング言語 (例として perl, python, java 等) とシリアルポートでの通信が可能です。

Hello World!

#include "mbed.h"              

Serial pc(USBTX, USBRX); // tx, rx

int main() {
    pc.printf("Hello World!\n");
}

 

Host Interface and Terminal Applications

Your mbed Microcontroller can appear on your computer as a serial port. On Mac and Linux, this will happen by default. For Windows, you need to install a driver:
mbedマイクロコントローラーはホストPCのシリアルポートとして表示されます。Mac と Linuxではデフォルトで表示されます、Windows の場合のみ、以下のドライバをインストールする必要があります。

  • See Windows Serial Driver for full details about setting up Windows for Serial Communication with your mbed Microcontroller
    mbedマイクロコントローラーでシリアル通信をWindowsで行うためのセットアップ方法の詳細は、この Windows Sirial Driver のページを参照して下さい。

It is common to use a Terminal Application on the host PC to communicate with the mbed Microcontroller. This allows the mbed Microcontroller to print to your PC screen, and for you to send characters back.
mbedマイクロコントローラーとホストPCを通信させる場合には、ターミナルソフトを使用するのが一般的です。またPC画面にmbedマイクロコントローラーから表示させたり、文字の入力を可能にします。

  • Terminal Applications - Using Terminal applications to communicate between the Host PC and the mbed Micrcontroller
    Terminal Applicationa - ターミナルソフトからホストPCを介して、mbedマイクロコントローラーと通信します

Some terminal programs (e.g. TeraTerm) list the available serial ports by name. However, if you do need to know the identity of the serial port so that you can attach a terminal or an application to it:
いくつかのターミナルソフト ( 例えば TeraTerm ) では使用可能なシリアルポートがリストアップされます。しかし他のターミナルソフトを使用していたり、アプリケーションに組み込む場合等、シリアルポート番号を知りたい場合は以下の作業で知ることが可能です。

  • Windows - Look under the "Ports" section in "Device Manager" (Start -> Control Panel -> System -> Hardware -> Device Manager). The name will be mbed Serial Port (COMx), where x is the number of the COM port allocated.
    Windows - デバイスマネージャーのポート (COM と LPT)に表示されています。(スタートメニュー -> マイコンピューターを右クリック -> プロパティクリックでシステムのプロパティを表示 -> ハードウェアタブ内のデバイスマネージャーをクリック)。 名称は "mbed Serial Port (COMx)、xは割り当てられた番号になります。 (例ではCOM7)
  • Mac OS X - Use the command ls /dev/tty.usbmodem*
    Mac OS X - $ ls /dev/tty.usbmodem* で確認
  • Linux - Use the command ls /dev/ttyACM*
    Linux - $ ls /dev/ttyACM* で確認

Terminal Application

Details

Communication over the USB Serial port simply uses the standard Serial Interface, specifying the internal (USBTX, USBRX) pins to connect to the Serial Port routed over USB.
USBシリアルポートの通信は単純に標準シリアルインターフェイスである、内部(USBTX, USBRX)ピンにUSBを通して接続されます。

The Serial Interface defaults to a 9600 baud standard serial connection (8 bits, 1 stop bit, no parity), so your host program should be set to the same settings. If you want to communicate at a different standard baud rate, ensure you modify the settings of both the Serial Interface and the Host PC application!
シリアルインターフェイスのデフォルト設定は、通信速度:9600 Baud、データ長:8 ビット、ストップビット:1、パリティ:なしです。ホスト上で動作するソフトもデフォルト設定を推奨します。もしデフォルトのBaud Rate以外の通信を行いたい場合、シリアルインターフェイスとホストPCソフト両方の設定を変更する必要があります。

Examples

// Echo back characters you type.

#include "mbed.h"              

Serial pc(USBTX, USBRX);

int main() {
    pc.printf("Echoes back to the screen anything you type\n");
    while(1) {
        pc.putc(pc.getc());
    }
}

// Connect to your mbed Microcontroller with a Terminal program and uses the 'u' and 'd' keys to make LED1 brighter or dimmer.

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
PwmOut led(LED1);

float brightness = 0.0;

int main() {
    pc.printf("Press 'u' to turn LED1 brightness up, 'd' to turn it down\n");

    while(1) {
        char c = pc.getc();
        if((c == 'u') && (brightness < 0.5)) {
            brightness += 0.01;
            led = brightness;
        }
        if((c == 'd') && (brightness > 0.0)) {
            brightness -= 0.01;
            led = brightness;
        } 

    }
}
// Pass through characters in both directions between the PC and Serial Port

#include "mbed.h"

Serial pc(USBTX, USBRX);
Serial uart(p28, p27);

DigitalOut pc_activity(LED1);
DigitalOut uart_activity(LED2);

int main() {
    while(1) {
        if(pc.readable()) {
            uart.putc(pc.getc());
            pc_activity = !pc_activity;
        }
        if(uart.readable()) {
            pc.putc(uart.getc());
            uart_activity = !uart_activity;
        }
    }
}

// The C stdin, stdout and stderr file handles are also defaulted to the PC serial connection

#include "mbed.h"

int main() {
    printf("Hello World!\n");
}


0 comments

You need to log in to post a comment