SoftwareSerial Library

86Duino 内建支援序列介面在脚位 0 和 1。内建的序列介面由包含在 86Duino CPU 裡的晶片产生,叫做 UART。因为有这个 UART 晶片提供序列介面,让 86Duino 的 CPU 在处理其他运算时,也可以接收序列介面的输入。

SofrwareSerial 函式库提供把一般的数位脚位作为序列介面的功能,用软体的方式提供和硬体序列介面相同的功能。如果需要产生反向的序列讯号,在初始化时可以利用参数来设定,使得序列介面的输出为反向。

SoftwareSerial 函式库从 86Duino Coding 103 开始支援。

限制

SofrwareSerial 函式库有以下已知的限制:

  • 如果使用多个软体序列介面,同时间只有一个可以接收资料。
  • 86Duino 并不是每一个数位脚位都支援中断,所以只有以下的数位脚位可以作为 RX 信号脚位: 18, 19, 20, 33, 34, 35, 36, 37, 38, 42, 43, 44.
  • 鲍率最高支援到 19200 bps

范例

/*
  Software serial multple serial test
 
  从硬体序列介面接收到的资料透过软体序列介面传送出去。
  从软体序列介面接收到的资料透过硬体序列介面传送出去。
 
 连接方式: 
 * RX 是脚位 42 (与其他装置的 TX 连接)
 * TX 是脚位 11 (与其他装置的 RX 连接)
 */

#include <SoftwareSerial.h>

SoftwareSerial mySerial(42, 11); // RX, TX

void setup()  
{
  // 开启硬体序列介面并等待启动完成
  Serial.begin(57600);
  while (!Serial) {
    ;
  }


  Serial.println("Goodnight moon!");

  // 设定软体序列介面的鲍率
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // 重复的做以下的动作
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

函式

SoftwareSerial()
available()
begin()
isListening()
overflow()
peek()
read()
print()
println()
listen()
write()


函式库参考主页面

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.