nanoFramework.System.IO.Ports 1.1.86
前缀已保留
dotnet add package nanoFramework.System.IO.Ports --version 1.1.86
NuGet\Install-Package nanoFramework.System.IO.Ports -Version 1.1.86
<PackageReference Include="nanoFramework.System.IO.Ports" Version="1.1.86" />
paket add nanoFramework.System.IO.Ports --version 1.1.86
#r "nuget: nanoFramework.System.IO.Ports, 1.1.86"
// Install nanoFramework.System.IO.Ports as a Cake Addin #addin nuget:?package=nanoFramework.System.IO.Ports&version=1.1.86 // Install nanoFramework.System.IO.Ports as a Cake Tool #tool nuget:?package=nanoFramework.System.IO.Ports&version=1.1.86
欢迎使用.NET nanoFramework System.IO.Ports库存储库
构建状态
组件 | 构建状态 | NuGet包 |
---|---|---|
System.IO.Ports |
使用方法
您可以在测试中找到详细的示例。
创建SerialPort
您可以通过以下方式创建SerialPort
var port = new SerialPort("COM2");
请注意,端口号必须为COMx
,其中x是一个数字。
GetPortNames
方法将为您提供可用端口的列表
var ports = SerialPort.GetPortNames();
您还可以在构造函数中直接指定波特率和其他元素
var port = new SerialPort("COM2", 115200);
每个属性都可以调整,包括端口打开时。请注意,这可能会产生危险的行为。建议在端口关闭时更改属性。
重要:您应该为读写操作设置超时。如果没有设置,读或写周期可能导致线程无限期锁定。
port.WriteTimeout = 1000;
port.ReadTimeout = 1000;
注意:即使您可以在构造函数中设置它们,某些MCU也不支持握手或特定位奇偶校验。
打开和关闭端口
《SerialPort》只允许在开启时运行,关闭时结束所有操作。在释放时,不管是否有正在进行的接收或发送操作,《SerialPort》都会执行关闭操作。
var port = new SerialPort("COM2");
port.Open();
// Do a lot of things here, write, read
port.Close();
读写操作
您有多种读写函数,一些与字节相关,一些与字符串相关。请注意,字符串函数将使用UTF8 编码
字符集。
发送和接收字节
发送和读取字节数组的示例
byte[] toSend = new byte[] { 0x42, 0xAA, 0x11, 0x00 };
byte[] toReceive = new byte[50];
// this will send the 4 bytes:
port.Write(toSend, 0, toSend.Length);
// This will only send the bytes AA and 11:
port.Write(toSend, 1, 2);
// This will check then number of available bytes to read
var numBytesToRead = port.BytesToRead;
// This will read 50 characters:
port.Read(toReceive, 0, toReceive.Length);
// this will read 10 characters and place them at the offset position 3:
port.Read(toReceive, 3, 10);
// Note: in case of time out while reading or writing, you will receive a TimeoutException
// And you can as well read a single byte:
byte oneByte = port.ReadByte();
发送和接收字符串
示例
string toSend = "I ❤ nanoFramework";
port.WriteLine(toSend);
// this will send the string encoded finishing by a new line, by default `\n`
// You can change the new line to be anything:
port.NewLine = "❤❤";
// Now it will send 2 hearts as the line ending `WriteLine` and will use 2 hearts as the terminator for `ReadLine`.
// You can change it back to the `\n` default at anytime:
port.NewLine = SerialPort.DefaultNewLine; // default is "\n"
// This will read the existing buffer:
string existingString = port.ReadExisting();
// Note that if it can't properly convert the bytes to a string, you'll get an exception
// This will read a full line, it has to be terminated by the NewLine string.
// If nothing is found ending by the NewLine in the ReadTimeout time frame, a TimeoutException will be raised.
string aFullLine = port.ReadLine();
事件
字符
当接收到字符时,《SerialPort》支持事件。
// Subscribe to the event
port.DataReceived += DataReceivedNormalEvent;
// When you're done, you can as well unsubscribe
port.DataReceived -= DataReceivedNormalEvent;
private void DataReceivedNormalEvent(object sender, SerialDataReceivedEventArgs e)
{
var ser = (SerialPort)sender;
// Now you can check how many characters are available, read a line for example
var numBytesToRead = port.BytesToRead;
string aFullLine = ser.ReadLine();
}
WatchChar
.NET nanoFramework有一个监视传输期间是否存在特定字符的自定义API事件。
port.WatchChar = '\r';
// Subscribe to the event
port.DataReceived += DataReceivedNormalEvent;
private void DataReceivedNormalEvent(object sender, SerialDataReceivedEventArgs e)
{
if (e.EventType == SerialData.WatchChar)
{
// The specified character was detected when reading from the serialport.
}
}
反馈和文档
有关文档、提供反馈、问题以及了解如何贡献,请参阅首页仓库。
加入我们的Discord社区这里。
致谢
该项目贡献者的列表可在CONTRIBUTORS中找到。
许可协议
nanoFramework 类库采用MIT 许可协议。
行为准则
本项目采用了贡献者公约中定义的行为准则,以阐明我们社区中预期的行为。更多信息请参阅.NET Foundation 行为准则。
.NET Foundation
本项目得到了.NET Foundation的支持。
产品 | 版本 兼容和额外的计算目标框架版本。 |
---|---|
.NET 框架 | net 兼容。 |
-
- nanoFramework.CoreLibrary (>= 1.15.5)
- nanoFramework.Runtime.Events (>= 1.11.18)
- nanoFramework.System.IO.Streams (>= 1.1.59)
NuGet 包 (21)
显示依赖 nanoFramework.System.IO.Ports 的前 5 个 NuGet 包
包 | 下载 |
---|---|
nanoFramework.M5Core2 此包包含适用于 .NET nanoFramework C# 项目的 nanoFramework.M5Core2 程序集。 |
|
nanoFramework.M5Core 此包包含适用于 .NET nanoFramework C# 项目的 nanoFramework.M5Core 程序集。 |
|
nanoFramework.Fire 此包包含用于.NET nanoFramework C#项目的nanoFramework.Fire组件。 |
|
nanoFramework.Logging.Serial 此包包含nanoFramework.Logging.Serial组件(仅串行日志),用于.NET nanoFramework C#项目。还有一个只包含流日志的包和一个包含基本调试日志的包。 |
|
nanoFramework.Tough 此包包含用于.NET nanoFramework C#项目的nanoFramework.Tough组件。 |
GitHub仓库 (2)
显示依赖nanoFramework.System.IO.Ports的最受欢迎的2个GitHub仓库
仓库 | 星标 |
---|---|
nanoframework/Samples
🍬 nanoFramework团队在测试、概念验证和其他探索性工作中使用的技术示例代码
|
|
nanoframework/nanoFramework.IoT.Device
📦 此仓库包含各种传感器、芯片、显示屏、帽子和驱动程序的.NET nanoFramework实现
|
版本 | 下载 | 最后更新 |
---|---|---|
1.1.86 | 3,102 | 5/13/2024 |
1.1.84 | 1,366 | 4/8/2024 |
1.1.82 | 384 | 4/3/2024 |
1.1.78 | 4,658 | 11/9/2023 |
1.1.75 | 131 | 11/9/2023 |
1.1.60 | 25,711 | 12/28/2022 |
1.1.57 | 312 | 12/28/2022 |
1.1.54 | 305 | 12/27/2022 |
1.1.46 | 12,466 | 10/26/2022 |
1.1.44 | 3,068 | 10/25/2022 |
1.1.42 | 684 | 10/25/2022 |
1.1.40 | 920 | 10/24/2022 |
1.1.38 | 725 | 10/24/2022 |
1.1.36 | 980 | 10/23/2022 |
1.1.34 | 2,981 | 10/22/2022 |
1.1.31 | 6,254 | 10/9/2022 |
1.1.29 | 2,780 | 10/8/2022 |
1.1.25 | 12,129 | 9/15/2022 |
1.1.22 | 748 | 9/15/2022 |
1.1.18 | 426 | 9/15/2022 |
1.1.16 | 747 | 9/15/2022 |
1.1.6 | 17,424 | 8/4/2022 |
1.1.4 | 406 | 8/3/2022 |
1.1.2 | 416 | 8/3/2022 |
1.0.7.12 | 389 | 8/3/2022 |
1.0.7.10 | 33,304 | 6/16/2022 |
1.0.7.8 | 6,547 | 6/13/2022 |
1.0.7.6 | 1,065 | 6/8/2022 |
1.0.7.1 | 11,394 | 5/26/2022 |
1.0.6.1 | 413 | 5/26/2022 |
1.0.4 | 32,256 | 3/29/2022 |
1.0.4-preview.3 | 139 | 3/29/2022 |
1.0.3 | 780 | 3/28/2022 |
1.0.3-preview.46 | 122 | 3/28/2022 |
1.0.3-preview.44 | 133 | 3/28/2022 |
1.0.3-preview.42 | 125 | 3/28/2022 |
1.0.3-preview.39 | 292 | 3/17/2022 |
1.0.3-preview.37 | 221 | 3/14/2022 |
1.0.3-preview.35 | 135 | 3/14/2022 |
1.0.3-preview.33 | 381 | 2/24/2022 |
1.0.3-preview.29 | 214 | 2/17/2022 |
1.0.3-preview.25 | 246 | 2/8/2022 |
1.0.3-preview.23 | 333 | 2/5/2022 |
1.0.3-preview.21 | 140 | 2/4/2022 |
1.0.3-preview.18 | 318 | 1/28/2022 |
1.0.3-preview.16 | 178 | 1/28/2022 |
1.0.3-preview.14 | 146 | 1/28/2022 |
1.0.3-preview.12 | 311 | 1/21/2022 |
1.0.3-preview.10 | 160 | 1/21/2022 |
1.0.3-preview.8 | 148 | 1/21/2022 |
1.0.3-preview.6 | 271 | 1/12/2022 |
1.0.3-preview.4 | 263 | 12/29/2021 |
1.0.3-preview.3 | 144 | 12/28/2021 |
1.0.2 | 1,865 | 12/3/2021 |
1.0.2-preview.3 | 153 | 12/3/2021 |
1.0.1 | 297 | 12/2/2021 |
1.0.1-preview.31 | 154 | 12/2/2021 |
1.0.1-preview.29 | 150 | 12/2/2021 |
1.0.1-preview.27 | 144 | 12/2/2021 |
1.0.1-preview.25 | 155 | 12/2/2021 |
1.0.1-preview.21 | 218 | 11/4/2021 |
1.0.1-preview.19 | 257 | 11/2/2021 |
1.0.1-preview.17 | 195 | 10/23/2021 |
1.0.1-preview.15 | 158 | 10/22/2021 |
1.0.1-preview.13 | 444 | 10/18/2021 |
1.0.1-preview.11 | 380 | 9/29/2021 |
1.0.1-preview.9 | 299 | 9/26/2021 |
1.0.1-preview.3 | 1,128 | 7/17/2021 |
1.0.0 | 8,676 | 7/16/2021 |
1.0.0-preview.54 | 166 | 7/16/2021 |
1.0.0-preview.52 | 175 | 7/15/2021 |
1.0.0-preview.50 | 196 | 7/14/2021 |
1.0.0-preview.44 | 623 | 6/26/2021 |
1.0.0-preview.42 | 215 | 6/20/2021 |
1.0.0-preview.40 | 191 | 6/18/2021 |
1.0.0-preview.35 | 216 | 6/8/2021 |
1.0.0-preview.33 | 212 | 6/7/2021 |
1.0.0-preview.31 | 291 | 6/5/2021 |
1.0.0-preview.29 | 176 | 6/4/2021 |
1.0.0-preview.27 | 412 | 6/2/2021 |
1.0.0-preview.25 | 199 | 6/1/2021 |
1.0.0-preview.23 | 214 | 5/31/2021 |
1.0.0-preview.21 | 212 | 5/30/2021 |
1.0.0-preview.19 | 172 | 5/29/2021 |
1.0.0-preview.17 | 173 | 5/27/2021 |
1.0.0-preview.15 | 163 | 5/26/2021 |
1.0.0-preview.12 | 172 | 5/24/2021 |