c# – 我可以使用SerialPort.Write发送字节数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 我可以使用SerialPort.Write发送字节数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1296字,纯文字阅读大概需要2分钟。
内容图文
SerialPort Write的文档说明了这一点
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.
另见here.这是否意味着我不能使用write发送字节数组?
解决方法:
By default, SerialPort uses ASCIIEncoding to encode the characters
您使用读取/写入字节的方法来混淆读取/写入字符串或字符的方法.
例如,当你打电话给这个时:
port.Write( “АБВ”)
你会得到 ”???” (默认情况下,端口缓冲区中为0x3F 0x3F 0x3F).另一方面,这个电话:
// this is equivalent of sending "абв" in Windows-1251 encoding
port.Write(new byte[] { 0xE0, 0xE1, 0xE2 }, 0, 3)
将直接写入序列0xE0 0xE1 0xE2,而不将字节替换为0x3F值.
UPD.
我们来看看源代码:
public void Write(string text)
{
// preconditions checks are omitted
byte[] bytes = this.encoding.GetBytes(text);
this.internalSerialStream.Write(bytes, 0, bytes.Length, this.writeTimeout);
}
public void Write(byte[] buffer, int offset, int count)
{
// preconditions checks are omitted
this.internalSerialStream.Write(buffer, offset, count, this.writeTimeout);
}
你看得到差别吗?
方法,接受字符串,使用当前编码的端口将字符串转换为字节数组.接受字节数组的方法将其直接写入流,该流是本机API的包装.
是的,文档欺骗了你.
内容总结
以上是互联网集市为您收集整理的c# – 我可以使用SerialPort.Write发送字节数组全部内容,希望文章能够帮你解决c# – 我可以使用SerialPort.Write发送字节数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。