linkgeek 发表于 2016-2-19 10:47:46

关于softwareserial中的例程TwoPortReceive

#include <SoftwareSerial.h>
SoftwareSerial portOne(10, 11);
SoftwareSerial portTwo(8, 9);

void setup()
{
Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
}

portOne.begin(9600);
portTwo.begin(9600);
}

void loop()
{
portOne.listen();
Serial.println("Data from port one:");
while (portOne.available() > 0) {
    char inByte = portOne.read();
    Serial.write(inByte);
}
Serial.println();

portTwo.listen();
Serial.println("Data from port two:");
while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
}
Serial.println();
}
------------------------------------------------------------------------
为了帮助我理解TwoPortReceive这个程序,我又写了TwoPortSend程序,代码如下:
#include <SoftwareSerial.h>
SoftwareSerial portOne(10, 11);
SoftwareSerial portTwo(8, 9);

void setup()
{
Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
}

portOne.begin(9600);
portTwo.begin(9600);
}

void loop()
{
portOne.println("1");
portTwo.println("2");
}
-----------------------------------------------------------------------------
将这两段程序分别烧录到两块arduino uno主板上,烧录有TwoPortReceive程序的arduino主板连接至电脑,烧录有TwoPortSend程序的arduino主板连接至烧录有TwoPortReceive程序的arduino主板,结果发现串口监视器显示一片乱码。将TwoPortReceive程序去掉有关portOne或portTwo的代码,串口监视器正常显示portTwo或portOne发来的信息。
请问我如何既能收portone又能收porttwo的信息?
页: [1]
查看完整版本: 关于softwareserial中的例程TwoPortReceive