简体   繁体   English

从C#中的USB端口读取字节

[英]ReadByte from USB port in C#

I would like to ask why does my program ReadByte() function returns a different value. 我想问为什么我的程序ReadByte()函数返回不同的值。 My USB port will be sending a byte value of 8, however, when the c# program reads it, it receives a value of 56. Why is that happening? 我的USB端口将发送一个字节值8,但是,当c#程序读取它时,它会收到一个值56。为什么会这样? The c# code is as below: C#代码如下:

private void serial_Click(object sender, EventArgs e)
{
            SerialPort port = new SerialPort("COM4", 9600);
            port.Open();

            int num1 = 0;
            num1 = port.ReadByte();
            String a = Convert.ToString(num1);
            MessageBox.Show(a);
            port.Close();
}

Your help is much appreciated. 非常感谢您的帮助。

You may be sending ascii instead of binary data. 您可能正在发送ascii而不是二进制数据。 56 is ascii for "8". 56是“ 8”的ASCII。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM