You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中拦截/发送PC与USB设备的信号?含摇杆联动需求

嘿,我来帮你解决这个问题——不管是让Logitech Attack3摇杆和VB.NET控件联动,还是搞懂PC与USB设备的双向通信、拦截/发送信号,我都给你梳理清楚:

实现Logitech Attack3摇杆与VB.NET控件联动及USB双向通信指南

一、摇杆与TrackBar/ProgressBar同步联动实现

既然你的PC已经装好了驱动,最便捷的方式是用SharpDX.DirectInput库——它对游戏外设的支持非常友好,在VB.NET里集成也很简单。

步骤1:安装SharpDX.DirectInput

打开项目的NuGet包管理器,搜索SharpDX.DirectInput并安装到你的VB.NET项目中。

步骤2:编写核心联动代码

我们通过定时器轮询摇杆状态,将轴的位移值转换为控件的进度值,实现实时联动:

Imports SharpDX.DirectInput

Public Class MainForm
    Private di As DirectInput
    Private joystick As Joystick
    Private WithEvents updateTimer As New Timer()

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 初始化DirectInput
        di = New DirectInput()

        ' 匹配Logitech Attack3摇杆设备
        Dim targetJoystick = di.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices) _
                               .FirstOrDefault(Function(d) d.InstanceName.Contains("Attack3"))
        If targetJoystick Is Nothing Then
            MessageBox.Show("未检测到Logitech Attack3摇杆!")
            Return
        End If

        ' 初始化摇杆设备
        joystick = New Joystick(di, targetJoystick.InstanceGuid)
        joystick.Properties.BufferSize = 128
        joystick.Acquire()

        ' 设置10ms间隔的定时器,实时更新控件
        updateTimer.Interval = 10
        updateTimer.Start()
    End Sub

    Private Sub updateTimer_Tick(sender As Object, e As EventArgs) Handles updateTimer.Tick
        If joystick Is Nothing Then Return

        Try
            ' 获取当前摇杆状态
            joystick.Poll()
            Dim currentState = joystick.GetCurrentState()

            ' Attack3的轴范围通常是-10000到10000,转换为TrackBar的0-100范围
            Dim xAxisPercent = CInt((currentState.X + 10000) / 200)
            Dim yAxisPercent = CInt((currentState.Y + 10000) / 200)

            ' 同步更新控件
            TrackBar_X.Value = xAxisPercent
            ProgressBar_Y.Value = yAxisPercent
        Catch ex As Exception
            updateTimer.Stop()
            MessageBox.Show("摇杆连接异常:" & ex.Message)
        End Try
    End Sub

    Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' 释放资源,避免内存泄漏
        joystick?.Unacquire()
        joystick?.Dispose()
        di?.Dispose()
        updateTimer.Dispose()
    End Sub
End Class

小提示:

  • 不同摇杆的轴范围可能有差异,你可以通过joystick.Properties.GetRange(0)获取X轴的实际最小值和最大值,再调整转换公式。
  • 如果需要支持摇杆按钮,直接读取currentState.Buttons数组即可判断按钮是否按下。

二、PC与USB设备双向通信机制及信号拦截/发送

1. 基本通信逻辑

PC和USB设备的通信是基于USB协议栈的分层架构:

  • 硬件层:USB物理接口传输数据
  • 驱动层:设备驱动(比如Logitech的摇杆驱动)负责和硬件交互,向上暴露API接口
  • 应用层:你的VB.NET程序通过驱动提供的API(比如DirectInput、HID API)和设备通信

像Attack3这类游戏外设属于HID(人机接口设备),系统会通过HID类驱动来处理它的输入输出。

2. VB.NET中拦截/发送USB信号

拦截设备输入信号

如果要拦截设备发送给PC的原始数据,有两种实用方式:

  • Hook HID系统消息:用Windows API的SetWindowsHookEx挂钩HID输入消息,但需要处理底层Windows消息,复杂度较高。
  • 使用HIDSharp库:通过NuGet安装HIDSharp,它可以直接枚举HID设备并读取原始输入报告,相当于在驱动层之上拦截数据:
Imports HidSharp

Public Class HidSignalInterceptor
    Private targetDevice As HidDevice
    Private deviceStream As HidStream

    Public Sub StartIntercept()
        ' 通过VID和PID定位Attack3(VID=0x046D,PID=0xC215,可在设备管理器中查看)
        targetDevice = HidDevices.Enumerate(0x046D, 0xC215).FirstOrDefault()
        If targetDevice Is Nothing Then
            MessageBox.Show("未找到目标设备!")
            Return
        End If

        ' 打开设备数据流
        deviceStream = targetDevice.Open()
        ' 异步读取原始报告
        Dim reportBuffer As Byte() = New Byte(targetDevice.GetMaxInputReportLength() - 1) {}
        deviceStream.ReadAsync(reportBuffer, 0, reportBuffer.Length, AddressOf OnReportReceived)
    End Sub

    Private Sub OnReportReceived(result As IAsyncResult)
        Try
            Dim bytesRead = deviceStream.EndRead(result)
            ' 打印原始报告数据,你可以在这里做自定义处理
            Debug.WriteLine("收到HID原始报告:" & BitConverter.ToString(reportBuffer))

            ' 继续监听下一份报告
            Dim newBuffer As Byte() = New Byte(targetDevice.GetMaxInputReportLength() - 1) {}
            deviceStream.ReadAsync(newBuffer, 0, newBuffer.Length, AddressOf OnReportReceived)
        Catch ex As Exception
            Debug.WriteLine("读取报告异常:" & ex.Message)
        End Try
    End Sub
End Class

发送信号到USB设备

部分HID设备支持接收PC发送的输出报告(比如带振动的手柄),Attack3本身可能不支持输出,但如果你的设备支持,用HIDSharp可以这样发送:

' 构造输出报告(格式需参考设备的HID描述符)
Dim outputReport As Byte() = New Byte(targetDevice.GetMaxOutputReportLength() - 1) {}
outputReport(0) = 0x01 ' 报告ID(根据设备规范设置)
' 填充其他需要发送的数据...

' 发送报告到设备
deviceStream.Write(outputReport)

3. 学习方向参考

因为不能外链,给你推荐几个靠谱的学习渠道:

  • Microsoft Docs:搜索「HID Class Driver」「Windows Joystick API」「DirectInput for .NET」,里面有完整的协议和API说明。
  • SharpDX官方文档:查看SharpDX.DirectInput命名空间的类和方法说明,掌握外设交互细节。
  • HID官方规范:USB-IF组织的《HID Class Specification》,详细定义了HID设备的通信格式和报告结构。

内容的提问来源于stack exchange,提问作者Massimo D. N.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:29:00