MultiSerial

はじめに

シリアルポートにMultiSerialデータを送信します。

ArduinoとPCとをUSBケーブルで接続します。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
  Multiple Serial test

  Receives from the main serial port, sends to the others.
  Receives from serial port 1, sends to the main serial (Serial 0).

  This example works only with boards with more than one serial like Arduino Mega, Due, Zero etc.

  The circuit:
  - any serial device attached to Serial port 1
  - Serial Monitor open on Serial port 0

  created 30 Dec 2008
  modified 20 May 2012
  by Tom Igoe & Jed Roach
  modified 27 Nov 2015
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/MultiSerialMega
*/
 
  

このプログラムでは特に何もしていません。

setup()

25
26
27
28
29
30
void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}
 

Serial.begin()を使って、シリアルポートを初期化します。

loop()

31
32
33
34
35
36
37
38
39
40
41
42
43
void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

Serial1.available()でSerial1のデータが利用できるかを調べ、利用可能であればSerial1.read()でデータを読みます。Serial.write()で、もう一方のシリアルポートに読んだデータを送信します。

また、Serialから読んだ値をSerial1に送信しています。

バージョン

Hardware:Arduino Mega/Arduino Due/Arduino Zero
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system