VB.NET SerialPort 发送字符串和字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30866071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:12:37  来源:igfitidea点击:

VB.NET SerialPort send strings and bytes

vb.net

提问by GoingForGold

I'm stuck at the moment because it seems like I'm really mixing up things and I am not able to test it because there are so many conversions and I'm not sure what happens where. I'm sure you can resolve that :)

我现在被卡住了,因为看起来我真的把事情搞混了,我无法测试它,因为转换太多了,我不确定在哪里发生了什么。我相信你可以解决这个问题:)

I need to send strings and bytes over serial.

我需要通过串行发送字符串和字节。

Sending a string is easy: SerialPort.Write(text)

发送字符串很简单:SerialPort.Write(text)

But I also need to send bytes. How do I send the byte-value 50 to the SerialPort? NOT the integer 50 converted to byte! How do I go about this exactly?

但我也需要发送字节。如何将字节值 50 发送到 SerialPort?不是整数 50 转换为字节!我该怎么做呢?

SerialPort.Write(50, 0, 1) says 50 is an integer. How do I make this a byte?

SerialPort.Write(50, 0, 1) 表示 50 是一个整数。我如何使它成为一个字节?

Here's the reference: https://msdn.microsoft.com/en-us/library/ms143551%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

这是参考:https: //msdn.microsoft.com/en-us/library/ms143551%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Cheers and thanks!

干杯和感谢!

回答by dbasnett

Something like this

像这样的东西

    Dim b() As Byte = {50}
    yourSerailPort.Write(b, 0, 1)