當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Arduino Serial.begin()用法及代碼示例

說明

設置串行數據傳輸的數據速率(以比特/秒(波特)為單位)。為了與串行監視器通信,請確保使用其屏幕右下角菜單中列出的波特率之一。但是,您可以指定其他速率 - 例如,通過引腳 0 和 1 與需要特定波特率的組件進行通信。

可選的第二個參數配置數據、奇偶校驗和停止位。默認為 8 個數據位,無奇偶校驗,1 個停止位。

用法

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

參數

Serial: 串口對象。請參閱每個板上的可用串行端口列表序列主頁麵.
speed:以每秒位數(波特)為單位。允許的數據類型:long.
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

返回

示例代碼

void setup() {
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {}

Arduino 超級示例:

// Arduino Mega using all four of its Serial ports
// (Serial, Serial1, Serial2, Serial3),
// with different baud rates:

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

  Serial.println("Hello Computer");
  Serial1.println("Hello Serial 1");
  Serial2.println("Hello Serial 2");
  Serial3.println("Hello Serial 3");
}
void loop() {}

感謝 Jeff Gray 提供的大型示例

注意事項和警告

對於 USB CDC 串行端口(例如 Leonardo 上的 Serial),Serial.begin() 無關緊要。您可以使用任何波特率和配置與這些端口進行串行通信。請參閱 Serial main page 上每個板的可用串行端口列表。

Arduino Nano 33 BLE 和 Nano 33 BLE Sense 板上的 Serial1 支持的唯一 config 值是 SERIAL_8N1

相關用法


注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 Serial.begin()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。