极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12843|回复: 0

关于softwareserial中的例程TwoPortReceive

[复制链接]
发表于 2016-2-19 10:47:46 | 显示全部楼层 |阅读模式
#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的信息?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 03:36 , Processed in 0.033907 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表