【问题标题】:ReadByte from USB port in C#从 C# 中的 USB 端口读取字节
【发布时间】:2016-05-27 09:23:28
【问题描述】:

我想问一下为什么我的程序 ReadByte() 函数返回不同的值。我的 USB 端口将发送一个字节值 8,但是,当 c# 程序读取它时,它收到一个值 56。为什么会发生这种情况? 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();
}

非常感谢您的帮助。

【问题讨论】:

  • 我认为您应该尝试使用数据包嗅探器来调试设备实际发送的内容。试试 Microsoft 的消息分析器:msdn.microsoft.com/en-us/library/windows/hardware/… 我可以看到 56 和 8 都以二进制 1000 结尾,但如果不实际调试从 USB 驱动器发送的消息,我就不能说太多。

标签: c# usb port


【解决方案1】:

您可能发送的是 ascii 而不是二进制数据。 56 是“8”的ASCII码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多