來源:官網頁面
Stream 串流
[Communication]
Description
Stream is the base class for character and binary based streams. It is not called directly, but invoked whenever you use a function that relies on it.
Stream是基於字元(character)及二位元(binary)的基本類別。
Stream不會被直接呼叫,但當你使用依靠它的函數時,Stream會被呼叫。
Stream defines the reading functions in Arduino. When using any core functionality that uses a read()
or similar method, you can safely assume it calls on the Stream class. For functions like print()
, Stream inherits from the Print class.
在Arduino中,Stream 定義了讀取函數。當使用了任何使用read()或類似方法的核心功能時,你可以安全地假設他呼叫了 Stream 類別。
對像是 prinit() 的函數,Stream 自 Print 類別 繼承而來。
Some of the libraries that rely on Stream include : 依靠 Stream 的一些函式庫包含:
============
Serial 來源:官網頁面
[Communication]
Description
Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART), and some have several.
用來進行Arduino與電腦或其他裝置間的溝通。所有Arduino版子至少有一個串列埠(習知的UART或USART),有的有好幾個。
Board | USB CDC name | Serial pins | Serial1 pins | Serial2 pins | Serial3 pins |
---|---|---|---|---|---|
Uno, Nano, Mini |
0(RX), 1(TX) |
||||
Mega |
0(RX), 1(TX) |
19(RX), 18(TX) |
16(RX), 17(TX) |
15(RX), 14(TX) |
|
Leonardo, Micro, Yún |
Serial |
0(RX), 1(TX) |
|||
Uno WiFi Rev.2 |
Connected to USB |
0(RX), 1(TX) |
Connected to NINA |
||
MKR boards |
Serial |
13(RX), 14(TX) |
|||
Zero |
SerialUSB (Native USB Port only) |
Connected to Programming Port |
0(RX), 1(TX) |
||
Due |
SerialUSB (Native USB Port only) |
0(RX), 1(TX) |
19(RX), 18(TX) |
16(RX), 17(TX) |
15(RX), 14(TX) |
101 |
Serial |
0(RX), 1(TX) |
On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.
在Uno、Nano、Mini以及Mega,腳位0及1是用來進行跟電腦的通訊。連接任何東西到這些腳位會干擾通訊,包括導致上傳到板子失敗。
You can use the Arduino environment’s built-in serial monitor to communicate with an Arduino board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to begin()
.
你可以使用 Arduino 環境內建的串列監視器來與Arduino開發版通訊。按下在工具列上串列監視器的鈕,並選擇與在呼叫 begin()中 相同的鲍率。
Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board). Don’t connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino board.
在TX/RX腳位的串列通訊使用TTL邏輯位準(依開發板,可能是5V或3.3V)。不要把這些腳位直接連到RS232串列埠;RS232運作在+/-12V,這樣可能會傷害你的開發板。
To use these extra serial ports to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor. To use them to communicate with an external TTL serial device, connect the TX pin to your device’s RX pin, the RX to your device’s TX pin, and the ground of your Mega to your device’s ground.
當這些額外的埠沒有連到Mega的USB轉串列轉接器,要使用這些額外的埠來與你的個人電腦通訊,你會需要額外的USB轉串列轉接器。要使用這些額外的埠來與外部的TTL串列裝置通訊,把TX連到你的裝置的RX 腳位,把RX連到你的裝置的TX 腳位,把你的Mega板的地(ground)連到你的裝置的地(ground)。
===========================
===========================
Stream.available() 來源:官網頁面
Description 描述
available()
gets the number of bytes available in the stream. This is only for bytes that have already arrived.
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
available()取得在串流中可取得的位元組的數目。這僅是對已經抵達的位元組。
此函數是Stream類別的一部份,且可以被任何從Stream繼承的類別呼叫。參看Stream class頁面,以取得更多資訊。
Syntax 語法
stream.available()
Parameters 參數
stream
: an instance of a class that inherits from Stream.
stream:從Stream繼承的類別的一個實例(instance)。
Returns 回傳值
int
: the number of bytes available to read
int:可取得來讀取的位元組的數目
=========
Stream.read() 來源:官網頁面
Description 描述
read()
reads characters from an incoming stream to the buffer.
read() 從正在進來的串流中讀取字元,存入緩衝器。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the stream class main page for more information.
此參數是Stream類別的一部分,可以被從它繼承的任何類別(Wire, Serial, etc)呼叫。參看Stream class頁面,以取得更多資訊。
Syntax 語法
stream.read()
Parameters 參數
stream
: an instance of a class that inherits from Stream.
stream:從Stream繼承的類別的一個實例(instance)。
Returns 回傳值
The first byte of incoming data available (or -1 if no data is available).
正來到的可取得的資料的第一個位元組(或是-1,如果沒有資料可取得的話)。
========
Stream.flush() 來源:官網頁面
Description 描述
flush()
clears the buffer once all outgoing characters have been sent.
flush()
清空緩衝器,一旦所有要向外送的(outgoing)字元已經被送出完成。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the stream class main page for more information.
Syntax 語法
stream.flush()
Parameters 參數
stream
: an instance of a class that inherits from Stream.
Returns 回傳值
Nothing 無
======
Stream.find() 來源:官網頁面
Description
find()
reads data from the stream until the target is found. The function returns true if target is found, false if timed out (see ../streamsettimeout[Stream.setTimeout()]).
find()從串流中讀取資料,直到發現目標為止。
如果發現目標,此函數回傳true,如果超過時間都未發現目標,回傳false。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the stream class main page for more information.
Syntax
stream.find(target)
stream.find(target, length)
Parameters
stream
: an instance of a class that inherits from Stream.
target
: the string to search for (char) 要搜尋(char)的字串。
length
: length of the target (size_t) 目標的長度(size_t)
Returns
bool
======
Stream.findUntil() 來源:官網頁面
Description
findUntil()
reads data from the stream until the target string of given length or terminator string is found, or it times out (see ../streamsettimeout[Stream.setTimeout()]).
The function returns true if target string is found, false if timed out
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax
stream.findUntil(target, terminal)
Parameters
stream
: an instance of a class that inherits from Stream
target
: the string to search for (char)
terminal
: the terminal string in the search (char)
Returns
bool
======
Stream.peek() 來源:官網頁面
Description
Read a byte from the file without advancing to the next one. That is, successive calls to peek()
will return the same value, as will the next call to read()
.
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax
stream.peek()
Parameters
stream
: an instance of a class that inherits from Stream.
Returns
The next byte (or character), or -1 if none is available.
======
Stream.readBytes() 來源:官網頁面
Description 描述
readBytes()
read characters from a stream into a buffer. The function terminates if the determined length has been read, or it times out (see setTimeout()).
readBytes()
returns the number of bytes placed in the buffer. A 0 means no valid data was found.
readBytes()從串流中,讀取字元,存入緩衝器。如果預先決定的長度被讀取完,或時間超時
(參閱 setTimeout()),此函數中止。
readBytes()回傳放置在緩衝器中的位元組的數量。0 代表沒有找到任何有效的資料。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
此參數是Stream類別的一部分,可以被從它繼承的任何類別(Wire, Serial, etc)呼叫。參看Stream class頁面,以取得更多資訊。
Syntax 語法
stream.readBytes(buffer, length)
Parameters 參數
stream
: an instance of a class that inherits from Stream.
buffer
: the buffer to store the bytes in (char[]
or byte[]
) 用來儲存char[]
或 byte[]形式的位元組的緩衝區
length
: the number of bytes to read(int) 要進行
read(int)的位元組的數量
Returns 回傳值
The number of bytes placed in the buffer (size_t
)
放置在buffer (size_t
)中的位元組的數量
=====
Stream.readBytesUntil() 來源:官網頁面
Description 描述
readBytesUntil()
reads characters from a stream into a buffer. The function terminates if the terminator character is detected, the determined length has been read, or it times out (see setTimeout()). The function returns the characters up to the last character before the supplied terminator. The terminator itself is not returned in the buffer.
readBytesUntil()
從串流中讀取字元,存入緩衝器。如果偵測到終止字元、讀取到預設的長度、或時間超過(see setTimeout()),則函數停止運作。
此函數回傳最多到在提供的終止運算子之前的最後一個字元為止。終止運算子本身不會被回傳。
readBytesUntil()
returns the number of bytes placed in the buffer. A 0 means no valid data was found.
readBytesUntil()回傳被放置在緩衝器中的位元組的數目。0 代表沒有找到任何有效的資料。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax 語法
stream.readBytesUntil(character, buffer, length)
Parameters 參數
stream
: an instance of a class that inherits from Stream.
character
: the character to search for (char
) 要尋找的字元
buffer
: the buffer to store the bytes in (char[]
or byte[]
)
用來儲存位元組(以char[]
或 byte[]的形式
)的緩衝器
length
: the number of bytes to read(int) 要進行read(int)的位元組的數目
Returns 回傳值
The number of bytes placed in the buffer.
放置在buffer (size_t
)中的位元組的數量
Notes and Warnings
The terminator character is discarded from the stream.
終止運算的字元會被捨棄。
==========
Stream.readString() 來源:官網頁面
Description 描述
readString()
reads characters from a stream into a String. The function terminates if it times out (see setTimeout()).
readString()從串流中,讀取字元,存成字串。
如果時間超過則函數終止。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax 語法
stream.readString()
Parameters 參數
stream
: an instance of a class that inherits from Stream
Returns 回傳值
A String read from a stream.
從船流中讀取到的字串。
========
Stream.readStringUntil() 來源:官網頁面
Description
readStringUntil()
reads characters from a stream into a String. The function terminates if the terminator character is detected or it times out (see setTimeout()).
readStringUntil()從串流中讀取字元,存成一個字串。
如果終止字元被偵測到,或時間超時,則本函數終止。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax
stream.readString(terminator)
Parameters
stream
: an instance of a class that inherits from Stream.
terminator
: the character to search for (char
)
Returns
The entire String read from a stream, up to the terminator character
從串流中讀取到的整個字串,直到終止字元。
Notes and Warnings
The terminator character is discarded from the stream.
終止字元會被從串流中捨棄。
=====
Stream.parseInt() 來源:官網頁面
Description
parseInt()
returns the first valid (long) integer number from the current position. Initial characters that are not integers (or the minus sign) are skipped.
parseInt()回傳從目前位置第一個有效的(長)整數數字。不是整數的初始字元(或負號)會被忽略。
In particular: 尤其是
1. Initial characters that are not digits or a minus sign, are skipped;
不是數字或負號的初始字元,會被忽略
2. Parsing stops when no characters have been read for a configurable time-out value, or a non-digit is read;
當超時時間到達前,沒有字元被讀取到,或是非數字字元被讀取到時,Parsing動作停止。
3. If no valid digits were read when the time-out (see Stream.setTimeout()) occurs, 0 is returned;
當時間超時,如果沒有有效數字被偵測到,則回傳0。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax
stream.parseInt(list)
stream.parseInt(''list', char skipchar')
Parameters
stream
: an instance of a class that inherits from Stream.
list
: the stream to check for ints (char
)
skipChar
: used to skip the indicated char in the search. Used for example to skip thousands divider.
Returns
long
======
Stream.parseFloat() 來源:官網頁面
Description
parseFloat()
returns the first valid floating point number from the current position. Initial characters that are not digits (or the minus sign) are skipped. parseFloat()
is terminated by the first character that is not a floating point number. The function terminates if it times out (see ../streamsettimeout[Stream.setTimeout()]).
parseFloat()回傳從目前位置第一個有效的浮點數目。不是數字(或負號)的初始字元會被忽略。
當碰到不是浮點數目的地一個字元,parseFloat()終止。
如果超過時間,此函數終止。(參閱
../streamsettimeout[Stream.setTimeout()])
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more informatio
Syntax
stream.parseFloat(list)
Parameters
stream
: an instance of a class that inherits from Stream.
list
: the stream to check for floats (char
)
Returns
float
==========
Stream.setTimeout() 來源:官網頁面
Description
setTimeout()
sets the maximum milliseconds to wait for stream data, it defaults to 1000 milliseconds.
setTimeout() 設定等待串流資料的最大值(毫秒)。預設值為1000毫秒(1秒)。
This function is part of the Stream class, and can be called by any class that inherits from it (Wire, Serial, etc). See the Stream class main page for more information.
Syntax
stream.setTimeout(time)
Parameters
stream
: an instance of a class that inherits from Stream.
time
: timeout duration in milliseconds (long
). 超時時間長度,單位:毫秒(long,長整數)
Returns
Nothing
Notes and Warnings
Stream functions that use the timeout value set via setTimeout()
:
-
find()
-
findUntil()
-
parseInt()
-
parseFloat()
-
readBytes()
-
readBytesUntil()
-
readString()
-
readStringUntil()
留言列表