begin()

描述

设定以每秒多少 bit 的速度 (又称为鲍率) 来传输串列资料。电脑透过串列埠在传输资料时,通常是使用 300、600、1200、2400、4800、9600、14400、19200、28800、38400、57600 或 115200 鲍率。除此之外,你也可以指定其它的鲍率,然后透过板子上的 0 和 1 脚,与一个需要特殊鲍率的元件做沟通。在 86Duino 上最高可以支援到 6000000 (6Mbps) 的鲍率。

除了鲍率外还有第二个可选的输入参数,其值包含了资料位元、同位元检查和停止位元。预设是 8 个资料位元,非同位元检查,以及一个停止位元。

第三个可选参数,是用来选择全双工或半双工模式。预设是全双工模式。

语法

适用所有板子:

Serial.begin(speed)
Serial1.begin(speed)
Serial.begin(speed, config)
Serial1.begin(speed, config)

Serial1.begin(speed, mode)
Serial1.begin(speed, config, mode)

适用 86Duino ONE:

Serial2.begin(speed)
Serial3.begin(speed)
Serial485.begin(speed)
Serial2.begin(speed, config)
Serial3.begin(speed, config)
Serial485.begin(speed, config)

Serial2.begin(speed, mode)
Serial3.begin(speed, mode)
Serial2.begin(speed, config, mode)
Serial3.begin(speed, config, mode)

适用 86Duino EduCake:

Serial2.begin(speed)
Serial3.begin(speed)
Serial232.begin(speed)
Serial2.begin(speed, config)
Serial3.begin(speed, config)
Serial232.begin(speed, config)

Serial2.begin(speed, mode)
Serial3.begin(speed, mode)
Serial2.begin(speed, config, mode)
Serial3.begin(speed, config, mode)

参数

speed: 每秒多少 bit (鲍率) long 资料型别,可设定的值有:

-50
-300
-600
-1200
-2400
-4800
-9600
-14400
-19200
-28800
-38400
-57600
-115200
-125000
-150000
-200000
-250000
-300000
-500000
-750000
-1000000
-1500000
-2000000
-3000000
-6000000

config: 设定资料位元、同位元检查和停止位元。可设定的值有:

-SERIAL_5N1
-SERIAL_6N1
-SERIAL_7N1
-SERIAL_8N1 (预设值)
-SERIAL_5N2
-SERIAL_6N2
-SERIAL_7N2
-SERIAL_8N2
-SERIAL_5E1
-SERIAL_6E1
-SERIAL_7E1
-SERIAL_8E1
-SERIAL_5E2
-SERIAL_6E2
-SERIAL_7E2
-SERIAL_8E2
-SERIAL_5O1
-SERIAL_6O1
-SERIAL_7O1
-SERIAL_8O1
-SERIAL_5O2
-SERIAL_6O2
-SERIAL_7O2
-SERIAL_8O2

mode: 设定全双工或半双工模式。可设定的值有:

-COM_FullDuplex (预设值)
-COM_HalfDuplex

回传

无回传值

范例

void setup() {
    Serial1.begin(9600); // 打开序列埠,设定鲍率为 9600 bps
}

void loop() {}

86Duino ONE/EduCake 范例:

// Serial1, Serial2, Serial3 使用不同的鲍率

void setup(){
  Serial1.begin(38400);
  Serial2.begin(19200);
  Serial3.begin(4800);

  Serial1.println("Hello Serial 1");
  Serial2.println("Hello Serial 2");
  Serial3.println("Hello Serial 3");
}

void loop() {}

See also

- Serial
- available()
- end()
- find()
- findUntil()
- flush()
- parseFloat()
- parseInt()
- peek()
- print()
- println()
- read()
- readBytes()
- readBytesUntil()
- setTimeout()
- write()
- serialEvent()


语法参考主页面

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.