c# - SerialPort.Write(byte[], int, int) 的文档是否具有误导性?

标签 c# .net serial-port

<分区>

我问这个是为了防止我错误地使用了 SerialPort.Write()。我很确定我没有,但你永远不知道...

SerialPort.Write(byte[], int, int) 的文档状态:

By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater than 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.

这与 SerialPort.Write(string) 文档中的注释相同和 SerialPort.Write(char[], int, int) .

多年来一直如此,但我以前从未真正注意到它。我一直很高兴地使用 SerialPort.Write(byte[], int, int) 来发送值大于 0x7f 的二进制数据。我发现发送字节数组的文档在谈论字符很奇怪。

是我的问题,还是那里的文档有问题?

最佳答案

文档绝对是错误的。如果你反编译 Write(char[], int, int) 你会看到

byte[] bytes = this.Encoding.GetBytes(buffer, offset, count);
this.Write(bytes, 0, bytes.Length); // This is the Write(byte[], int, int) method

Write(byte[], int, int) 方法仅包含:

this.internalSerialStream.Write(buffer, offset, count, this.writeTimeout);

所以很明显在这个方法中没有出现ASCII编码。

关于c# - SerialPort.Write(byte[], int, int) 的文档是否具有误导性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15131883/

相关文章:

c# - 语音/语音转文本

.NET:是否可以在安装 .NET 4.0 后安装 .NET frameworks 2.0 和 3.5?

c# - CV.Invoke on emgu

c# - 如何从目录字符串中仅获取最后一个子目录名称和文件名称?

c# - 引用 Entity Framework 中表的架构名称

c# - 在 SerialPort 上设置 DTR、RTS、CTS、DSR 和 Xonn/Xoff

c# - 在 C# 中以编程方式更改串行端口配置

c# - 如何关闭在集合初始值设定项中消除大括号间距的自动套用格式?

.net - 将字符串转换为流

c++ - Qt Creator - 将串行接收的数据写入 UI