一個 Flutter 插件,可在 Windows 上使用原生 win32 API 實現序列埠通訊。它允許透過序列埠直接連接實體裝置,例如微控制器、感測器或工業設備。專為 Windows 設計,提供底層存取且抽象程度極低,適合需要在桌面應用程式中實現可靠且高效序列通訊的開發者。該套件可與 Flutter 生態系統無縫整合,非常適合硬體導向的專案。
適用情境
連接到Arduino或類似的微控制器
從工業傳感器讀取數據
透過串列協定控制硬體裝置
為嵌入式系統開發桌面應用程式
與串行外設建立即時通訊
主要特色
原生 win32 API 結合
僅限 Windows 的平台支援
低延遲串列通訊
用於開啟/關閉埠的簡單 API
支援傳輸速率與奇偶校驗設定
即時資料流
適合對象
Windows 桌面應用程式開發者
硬體介面專案
物聯網設備控制應用
嵌入式系統整合
即時數據採集工具
使用建議
僅限 Windows 作業系統
某些操作需要管理員權限
不支援 Linux 或 macOS
需要手動處理錯誤
依賴於特定於 Windows 的系統呼叫
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
serial_port_win32
A SerialPort library using win32 API.
Breaking Changes from 2.x.x to 3.x.x
Due to many major changes in win32 package, this package need to break some APIs.
Some main APIs change below:
set Parity(int parity) -> set Parity(DCB_PARITY parity)
set StopBits(int stopBits) -> set StopBits(DCB_STOP_BITS stopBits)
final LPWSTR _portNameUtf16; -> final PCWSTR _portNameUtf16;
Some Exceptions change to WindowsExceptions
Deprecated
@Deprecated("Use win32 constant CLRDTR instead of this, because it is more clear to understand")
staticconstint CLRDTR = 6;
/// Clears the RTS (request-to-send) signal.@Deprecated("Use win32 constant CLRRTS instead of this, because it is more clear to understand")
staticconstint CLRRTS = 4;
/// Sends the DTR (data-terminal-ready) signal.@Deprecated("Use win32 constant SETDTR instead of this, because it is more clear to understand")
staticconstint SETDTR = 5;
/// Sends the RTS (request-to-send) signal.@Deprecated("Use win32 constant SETRTS instead of this, because it is more clear to understand")
staticconstint SETRTS = 3;
Getting Started
Get Ports
final ports = SerialPort.getAvailablePorts();
print(ports);
/// result like [COM3, COM4]
The port instance is Singleton Pattern. Don't re-create port for same Com name.
final port = SerialPort("COM5", openNow: true, ByteSize: 8, BaudRate: 9600);
// orfinal port = SerialPort("COM5", openNow: false, ByteSize: 8, BaudRate: 9600);
port.open();
// or
port.openWithSettings(BaudRate: CBR_115200);
// final port = SerialPort("COM5"); /// auto open with default settings
Read
There are several methods to read bytes from serial port.
readBytesUntil
[readBytesUntil] will read until an [expected] sequence is found
readBytes
General Read with the timeout parameter.
readFixedSizeBytes
[readFixedSizeBytes] will always read until readData.length == bytesSize.
print(await port.readBytesUntil(Uint8List.fromList("T".codeUnits))); /// '\0' is not included/// orvar read = port.readBytes(18, timeout: Duration(milliseconds: 10))..then((onValue) => print(onValue));
var result = await read;
print(result);
/// orvar fixedBytesRead = port.readFixedSizeBytes(2)..then((onValue) => print(onValue));
await fixedBytesRead;
/// see more in small example