< >
Home » Arduino库教程 » Arduino库教程-Software Serial-Two Port Receive

Arduino库教程-Software Serial-Two Port Receive

Two Port Receive

  • Arduino和genuino板内置的引脚0和1支持串口通信,但是如果你需要更多的串行端口怎么办?SoftwareSerial 库已经发展到允许串行通信发生在你开发板的其他数字引脚上,用软件复制硬件RX和TX线的功能。这可能是非常有用的,尤其是需要与两个串行功能的设备进行通信,或只和一个设备,但要让主要的串行端口开放来调试。

  • 在下面的例子中,你的Arduino或genuino板的数字引脚pin2和4作为虚拟的RX和TX串行线使用。引脚3和5是虚拟的TX线。开发板倾听一个虚拟端口(portOne),知道它接受到一个"?"字符。之后,它会倾听第二虚拟端口(portTwo)。

硬件要求

  • Arduino or Genuino Board

电路

  • 这个例子没有额外电路。确保你的Arduino或genuino开发板通过USB是能串口通讯连接到电脑。

请输入图片描述

图由 Fritzing 软件绘制

原理图

请输入图片描述
图由 Fritzing 软件绘制

样例代码

/*
  Software serial multple serial test

 Receives from the two software serial ports,
 sends to the hardware serial port.

 In order to listen on a software port, you call port.listen().
 When using two software serial ports, you have to switch ports
 by listen()ing on each one in turn. Pick a logical time to switch
 ports, like the end of an expected transmission, or when the
 buffer is empty. This example switches ports when there is nothing
 more to read from a port

 The circuit:
 Two devices which communicate serially are needed.
 * First serial device's TX attached to digital pin 10(RX), RX to pin 11(TX)
 * Second serial device's TX attached to digital pin 8(RX), RX to pin 9(TX)

 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

 Not all pins on the Leonardo support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

 created 18 Apr. 2011
 modified 19 March 2016
 by Tom Igoe
 based on Mikal Hart's twoPortRXExample

 This example code is in the public domain.

 */

#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 10, TX = digital pin 11
SoftwareSerial portOne(10, 11);

// software serial #2: RX = digital pin 8, TX = 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();
}

[Get Code]
更多

  • The Software Serial Library
  • SoftwareSerialExample - 两个串行端口,在镜像的TX和RX之间发送数据。
  • MultiSerialMega - 使用在Arduino和Genuino Mega上的两个有效串行端口。
  • Serial Call Response - 通过呼叫-响应(握手)方法来发送多个变量。
  • Serial Call Response ASCII - 通过呼叫-响应(握手)方法来发送多个变量,并且在发送前ASCII编码这些值。

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: arduino库教程, arduino software serial, arduino two port receive