SoftwareSerial

SoftwareSerialライブラリを使えば、Arduinoボードの他のデジタルピンを使ってシリアル通信を行うことができる。


AUTHOR: Arduino、LAST REVISION: 2024/01/04 19:18


SoftwareSerialライブラリは、ソフトウェア実装により(なのでSoftwareSerialと名づけられた)、Arduinoの他のデジタルピンを使ってシリアル通信を利用できるようにするために開発された。115200bpsまでのスピードで、複数のソフトウェアシリアルポートを利用することができる。パラメータを設定することで、負論理(inverted signaling )のデバイスにも対応することができる。

1.0以降のSoftwareSerialのバージョンはMikal Hartが作成したNewSoftSerialライブラリを基にしている。

このライブラリを使うためには、以下を宣言する。

#include <SoftwareSerial.h> 

制約

このライブラリには以下の制約がある。

  • 送信と受信を同時には行えない。
  • 複数のソフトウェアシリアルポートを利用しても、同時には一つのデータしか受信できない。
  • MegaとMega 2560では、全てのピンが入力の変化に対する割り込みをサポートしているわけではないので、以下のピンだけが受信(RX)に対応している:10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8(62), A9(63), A10(64), A11(65), A12(66), A13(67), A14(68), A15(69)。
  • LeonardとMicroでは、全てのピンが入力の変化に対する割り込みをサポートしているわけではないので、以下のピンだけが受信(RX)に対応している:8, 9, 10, 11, 14(MISO), 15(SCK), 16(MOSI)。
  • Arduino/Genuino 101では、現在の最大受信(RX)スピードは57600bpsである。
  • Arduino/Genuino 101では、13番ピンでは受信できない。

同時にシリアル通信を利用する必要がある場合は、Paul Stoffregen作成のAltSoftSerialライブラリを参照すること。

SoftwareSerial

プログラム例

プログラム例 説明
Software Serial Example シリアルポートが一つだと足らないときはこのライブラリを使おう。
Two Port Receive 複数のソフトウェアシリアルポートを使う。

関数

SoftwareSerial()

SoftwareSerialはSoftwareSerialオブジェクトのインスタンスを作成する。複数のSoftwareSerialオブジェクトを作成することができるが、同時に利用できるのは1つだけである。

書式

1
SoftwareSerial(rxPin, txPin, inverse_logic)

引数

  • rxPin: シリアルデータを受信するピン番号。
  • txPin: シリアルデータを送信するピン番号。
  • inverse_logic: 入力ビットを反転させる。デフォルトは通常のロジック。このパラメータを設定したときは、RXピンのLOW(通常は0V)を1(アイドル状態)、HIGH(通常は5V)を0として取り扱う。Txピンへの書き込みにも影響を与える。デフォルトはfalse。

戻り値

なし。

使用例

1
2
3
4
5
6
7
#include <SoftwareSerial.h>

const byte rxPin = 2;
const byte txPin = 3;

// set up a new serial object
SoftwareSerial mySerial (rxPin, txPin);

available()

ソフトウェアシリアルポートから読み込み可能なバイト数(文字数)を取得する。これは、すでに到着しシリアル受信バッファに格納されているデータである。

書式

1
mySerial.available()

引数

なし。

戻り値

読み込み可能なバイト数。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
    // Define pin modes for TX and RX
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    
    // Set the baud rate for the SoftwareSerial object
    mySerial.begin(9600);
}

void loop() {
    if (mySerial.available() > 0) {
        mySerial.read();
    }
}

SoftwareSerial.begin()

begin()

シリアル通信の速度(ボーレート)を設定する。利用可能な速度は、300、600、1200、2400、4800、9600、14400、19200、28800、31250、38400、57600、115200である。

書式

1
mySerial.begin(speed)

引数

  • speed: 通信速度(long)。利用可能な速度は、300、600、1200、2400、4800、9600、14400、19200、28800、31250、38400、57600、115200である。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
    // Define pin modes for TX and RX
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);

    // Set the baud rate for the SoftwareSerial object
    mySerial.begin(9600);
}

void loop() {
    // ...
}

isListening()

指定したソフトウェアシリアルポートが受信中かを調べる。

書式

1
mySerial.isListening()

引数

なし。

戻り値

受信中ならtrue、そうでなければfalse。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial portOne(10, 11);

void setup() {
    // Set the baud rate for the Serial port
    Serial.begin(9600);

    // Set the baud rate for the SerialSoftware object
    portOne.begin(9600);
}

void loop() {
    if (portOne.isListening()) { 
        Serial.println("portOne is listening!");
    }

    // ...

overflow()

SoftwareSerialのバッファがオーバーフローしているかどうかを調べる。この関数を呼ぶとオーバーフローフラグをクリアするので、さらにデータを受信してオーバーフローを起こしていない限りは、次の呼び出しではfalseを返却する。SoftwareSerialのバッファは64バイトである。

書式

1
mySerial.overflow()

引数

なし。

戻り値

オーバーフローしていればtrue、そうでなければfalse。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial portOne(10, 11);

void setup() {
    // Set the baud rate for the Serial port
    Serial.begin(9600);

    // Set the baud rate for the SerialSoftware object
    portOne.begin(9600);
}

void loop() {
    if (portOne.overflow()) {
        Serial.println("portOne overflow!");
    }

    // ...

peek()

ソフトウェアシリアルポートのRXピンで受信した文字を返却する。read()とは異なり、次にpeek()を呼んでも同じ文字を返却する。同時には、一つのSoftwareSerialインスタンスだけがデータを受信できる。どのインスタンスで受信するかは、listen()関数を用いて選択する。

書式

1
mySerial.peek()

引数

なし。

戻り値

受信した文字。利用可能な文字がない場合は-1。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial(10, 11);

void setup() {
    // Set the baud rate for the SerialSoftware object
    mySerial.begin(9600);
}

void loop() {
    char c = mySerial.peek();
}

read()

ソフトウェアシリアルポートのRXピンで受信した文字を返却する。同時には、一つのSoftwareSerialインスタンスだけがデータを受信できる。どのインスタンスで受信するかは、listen()関数を用いて選択する。

書式

1
mySerial.read()

引数

なし。

戻り値

受信した文字。利用可能な文字がない場合は-1。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial(10, 11);

void setup() {
    // Set the baud rate for the SerialSoftware object
    mySerial.begin(9600);
}

void loop() {
    char c = mySerial.read();
}

print()

ソフトウェアシリアルポートの送信ピンにデータを書き込む。Serial.print()関数と同様に動作する。

書式

1
mySerial.print(val)

引数

  • val: 書き込む値。

戻り値

書き込んだバイト数。戻り値の利用はオプション。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial(10, 11);

int analogValue;

void setup() {
    // Set the baud rate for the SerialSoftware object
    mySerial.begin(9600);
}

void loop() {
    // Read the analog value on pin A0
    analogValue = analogRead(A0);

    // Print analogValue in the Serial Monitor in many formats:
    mySerial.print(analogValue);         // Print as an ASCII-encoded decimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, DEC);    // Print as an ASCII-encoded decimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, HEX);    // Print as an ASCII-encoded hexadecimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, OCT);    // Print as an ASCII-encoded octal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, BIN);    // Print as an ASCII-encoded binary
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue/4, BYTE); // Print as a raw byte value (divide the
                                         // value in 4 because analogRead() function returns numbers
                                         // from 0 to 1023, but a byte can only hold values up to 255)

    mySerial.print("\t");                // Print a tab character    
    mySerial.println();                  // Print a line feed character

    // Pause for 10 milliseconds before the next reading
    delay(10);
}
}

println()

ソフトウェアシリアルポートの送信ピンにデータを書き込み、その後に、CR(キャリッジリターン)とLF(ラインフィード)を書き込む。Serial.println()関数と同様に動作する。

書式

1
mySerial.println(val)

引数

  • val: 書き込む値。

戻り値

書き込んだバイト数。戻り値の利用はオプション。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial(10, 11);

int analogValue;

void setup() {
    // Set the baud rate for the SerialSoftware object
    mySerial.begin(9600);
}

void loop() {
    // Read the analog value on pin A0
    analogValue = analogRead(A0);

    // Print analogValue in the Serial Monitor in many formats:
    mySerial.print(analogValue);         // Print as an ASCII-encoded decimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, DEC);    // Print as an ASCII-encoded decimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, HEX);    // Print as an ASCII-encoded hexadecimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, OCT);    // Print as an ASCII-encoded octal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, BIN);    // Print as an ASCII-encoded binary
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue/4, BYTE); // Print as a raw byte value (divide the
                                         // value in 4 because analogRead() function returns numbers
                                         // from 0 to 1023, but a byte can only hold values up to 255)

    mySerial.print("\t");                // Print a tab character    
    mySerial.println();                  // Print a line feed character

    // Pause for 10 milliseconds before the next reading
    delay(10);
}

listen()

選択したソフトウェアシリアルポートを受信可能状態にする。同時に受信できるソフトウェアシリアルポートは一つである。他のポートに到着したデータは捨てられる。listen()の呼び出し中に受信したデータは、そのインスタンスが既に受信可能状態でない限り、捨てられる。

書式

1
mySerial.listen()

引数

なし。

戻り値

他のものを置き換えた場合はtrue。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial portOne(10, 11);

// Set up a new SoftwareSerial object with RX in digital pin 8 and TX in digital pin 9
SoftwareSerial portTwo(8, 9);

void setup() {
    // Set the baud rate for the Serial object
    Serial.begin(9600);

    // Set the baud rate for the SerialSoftware objects
    portOne.begin(9600);
    portTwo.begin(9600);
}

void loop() {
    // Enable SoftwareSerial object to listen
    portOne.listen();
    
    if (portOne.isListening()) {
        Serial.println("portOne is listening!");
    } else {
        Serial.println("portOne is not listening!");
    }

    if (portTwo.isListening()) {
        Serial.println("portTwo is listening!");
    } else {
        Serial.println("portTwo is not listening!");
    }
}

write()

ソフトウェアシリアルポートの送信ピンにデータを書き込む。Serial.write()関数と同様に動作する。

書式

1
mySerial.write(val)

引数

  • val: 書き込むバイナリ値。

戻り値

書き込んだバイト数。戻り値の利用はオプション。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial(10, 11);

void setup() {
    // Set the baud rate for the SerialSoftware object
    mySerial.begin(9600);
}

void loop() {
    // Send a byte with the value 45
    mySerial.write(45);

    //Send the string “hello” and return the length of the string.
    int bytesSent = mySerial.write(hello);
}

オリジナルのページ

https://docs.arduino.cc/learn/built-in-libraries/software-serial

最終更新日

January 7, 2024

inserted by FC2 system