#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);//RX=2,TX=3
void setup()
{
//硬件串口波特率
Serial.begin(4800);
//软件串口波特率
mySerial.begin(9600);
}
void loop()
{
//如果硬件串口有数据
if(Serial.available())
{
//从硬件串口读出一字节,写入软件串口
mySerial.write(Serial.read());
}
//如果软件串口有数据
if(mySerial.available())
{
//从软件串口读出一字节,写入硬件串口
Serial.write(mySerial.read());
}
}

#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10, 11);
// software serial #2: TX = digital pin 8, RX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
SoftwareSerial portTwo(8, 9);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Start each software serial port
portOne.begin(9600);
portTwo.begin(9600);
}
void loop() {
// By default, the last intialized port is listening.
// when you want to listen on a port, explicitly select it:
portOne.listen();
Serial.println("Data from port one:");
// while there is data coming in, read it
// and send to the hardware serial port:
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
// Now listen on the second port
portTwo.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
Serial.println("Data from port two:");
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
}
-
- 如果有多少软串口,一次只有一个软串口能接收数据;
- 不是所有Arduino产品的每个引脚都支持Change中断,要看每个Arduino产品具体有哪些引脚支持中断;