Arduino 软串口
2022-07-11 本文已影响0人
Charles2018
软串口
软串口就是通过将开发板的串口,映射到普通IO上作为串口使用
接线图
RX | TX |
---|---|
D10 | D11 |
代码
#include<SoftwareSerial.h>
SoftwareSerial softSerial(6, 5);
void setup() {
softSerial.begin(115200); //初始化虚拟串口
Serial.begin(9600); //初始化Arduino默认串口
softSerial.listen();
Serial.println(softSerial.isListening());
}
String device_A_String = "";
String device_B_String = "";
void loop() { // run over and over
if (Serial.available() > 0)
{
device_A_String = Serial.readString();
softSerial.println(device_A_String);
Serial.println("You say: ");
Serial.println(device_A_String);
}
if(softSerial.available() > 0)
{
device_B_String = softSerial.readString();
Serial.println("received from b: ");
Serial.println(device_B_String);
}
/*if (softSerial.available() > 0)
{
if (softSerial.peek() != '\n')
{
device_A_String += (char)softSerial.read();
}
else
{
softSerial.read();
Serial.print("device A said:");
Serial.println(device_A_String);
device_A_String = "";
}
}*/
}