#include<SoftwareSerial.h>#define rxPin 10
#define txPin 11
// Set up a new SoftwareSerial object
SoftwareSerialmySerial=SoftwareSerial(rxPin,txPin);voidsetup(){// Define pin modes for TX and RX
pinMode(rxPin,INPUT);pinMode(txPin,OUTPUT);// Set the baud rate for the SoftwareSerial object
mySerial.begin(9600);}voidloop(){if(mySerial.available()>0){mySerial.read();}}
#include<SoftwareSerial.h>#define rxPin 10
#define txPin 11
// Set up a new SoftwareSerial object
SoftwareSerialmySerial=SoftwareSerial(rxPin,txPin);voidsetup(){// Define pin modes for TX and RX
pinMode(rxPin,INPUT);pinMode(txPin,OUTPUT);// Set the baud rate for the SoftwareSerial object
mySerial.begin(9600);}voidloop(){// ...
}
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
SoftwareSerialportOne(10,11);voidsetup(){// Set the baud rate for the Serial port
Serial.begin(9600);// Set the baud rate for the SerialSoftware object
portOne.begin(9600);}voidloop(){if(portOne.isListening()){Serial.println("portOne is listening!");}// ...
#include<SoftwareSerial.h>// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerialportOne(10,11);voidsetup(){// Set the baud rate for the Serial port
Serial.begin(9600);// Set the baud rate for the SerialSoftware object
portOne.begin(9600);}voidloop(){if(portOne.overflow()){Serial.println("portOne overflow!");}// ...
#include<SoftwareSerial.h>// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerialmySerial(10,11);voidsetup(){// Set the baud rate for the SerialSoftware object
mySerial.begin(9600);}voidloop(){charc=mySerial.peek();}
#include<SoftwareSerial.h>// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerialmySerial(10,11);voidsetup(){// Set the baud rate for the SerialSoftware object
mySerial.begin(9600);}voidloop(){charc=mySerial.read();}
#include<SoftwareSerial.h>// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerialmySerial(10,11);intanalogValue;voidsetup(){// Set the baud rate for the SerialSoftware object
mySerial.begin(9600);}voidloop(){// 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);}}
#include<SoftwareSerial.h>// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerialmySerial(10,11);intanalogValue;voidsetup(){// Set the baud rate for the SerialSoftware object
mySerial.begin(9600);}voidloop(){// 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);}
#include<SoftwareSerial.h>// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerialportOne(10,11);// Set up a new SoftwareSerial object with RX in digital pin 8 and TX in digital pin 9
SoftwareSerialportTwo(8,9);voidsetup(){// 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);}voidloop(){// 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!");}}
#include<SoftwareSerial.h>// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerialmySerial(10,11);voidsetup(){// Set the baud rate for the SerialSoftware object
mySerial.begin(9600);}voidloop(){// Send a byte with the value 45
mySerial.write(45);//Send the string “hello” and return the length of the string.
intbytesSent=mySerial.write(“hello”);}