Preface: Arduino allow use a TX & a RX pin conduct serial communication with Android or Windows,But at many times,If you want to communication with more device,For instance,Use ESP8226 module and HC05 moudule at the same times, You must use software(virtual) serial conduct communication.
For Arduino,software serial is the same as Hardware serial in syntax,Hardware serial use <Serial.h> library,software use <SoftwareSerial.h>.Specific use method reference Arduino formal(At the bottom of this article),Let's get to comprehend how to it work process.
First,We should create an object,This class has a constructor functuon,Initialize two parameters,softwareSerial(RX,TX),Here we initialize 2pin and 3pin.
SoftwareSerial VirtualSerial(2,3);
Then setting serial buad rate.
VirtualSerial.begin(9600);
When serial buffer has data,Imediately output data to your device.In this process.VirtualSerial.available() return read byte count,VirtualSerial.read()return first byte read.VirtualSerial.println output received data sending to your device.
int ReceivedData=0;
void loop()
{
if(VirtualSerial.available())
{
ReveivedData=VirtualSerial.read();
VirtualSoftware.print(ReceivedData);
}
}
本文使用MarkDown语法编写,如有任何编码问题请私信我。
网友评论