简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย

导读切换到宽版
SPACE discord

SPACE Forum

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 247|回复: 8

请问Mirabox支持使用c#直接访问设备吗?

[复制链接]

1

主题

2

回帖

15

积分

新手上路

积分
15
发表于 2025-11-9 21:18:50 | 显示全部楼层 |阅读模式
Stream Deck好像可以通过StreamDeckSharp实现,Mirabox是否兼容?以下是AI的回答,但不确定是否兼容。

我们使用StreamDeckSharp库来控制Stream Deck设备,但是请注意,Mirabox设备可能不是标准的Stream Deck设备,所以需要确保StreamDeckSharp支持该设备。
由于StreamDeckSharp主要支持Elgato Stream Deck系列,如果Mirabox设备与Elgato Stream Deck兼容,那么可以使用。如果不兼容,可能需要使用其他SDK。
这里假设Mirabox设备与Elgato Stream Deck兼容,并且可以使用StreamDeckSharp。
以下是使用StreamDeckSharp控制设备的示例代码:
步骤:
  • 安装StreamDeckSharp NuGet包。
  • 使用StreamDeck.OpenDevice()打开设备。
  • 处理按键事件。
  • 设置按键图像等。

注意:Mirabox设备可能有不同的按键布局,请根据实际情况调整。
由于StreamDeckSharp支持多种Stream Deck型号,如果Mirabox是其中一种,那么它会被自动检测到。

1

主题

2

回帖

15

积分

新手上路

积分
15
 楼主| 发表于 2025-11-9 22:19:11 | 显示全部楼层
希望以下代码能够工作:

using HidSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading;

public class MiraboxHidSharpController : IDisposable
{
    private HidDevice _device;
    private HidStream _stream;
    private bool _isConnected = false;
   
    // Mirabox 设备信息(需要根据实际设备修改)
    private const int VENDOR_ID = 0xffff;  // 替换为 Mirabox 的实际 VID
    private const int PRODUCT_ID = 0xffff; // 替换为 Mirabox 的实际 PID
   
    // Mirabox 规格
    public const int KEY_COUNT = 15;
    public const int IMAGE_SIZE = 72;
    public const int REPORT_LENGTH = 1024; // HID 报告长度

    public event EventHandler<MiraboxKeyEventArgs> KeyStateChanged;

    public class MiraboxKeyEventArgs : EventArgs
    {
        public int KeyIndex { get; set; }
        public bool IsPressed { get; set; }
    }

    // 设备检测
    public static List<HidDevice> FindMiraboxDevices()
    {
        var deviceList = DeviceList.Local;
        var allHidDevices = deviceList.GetHidDevices().ToList();
        
        var miraboxDevices = new List<HidDevice>();
        
        Console.WriteLine("扫描 HID 设备...");
        foreach (var device in allHidDevices)
        {
            Console.WriteLine($"找到 HID 设备: {device.GetProductName()} " +
                            $"(VID: {device.VendorID:X4}, PID: {device.ProductID:X4})");
            
            // 根据设备名称或 VID/PID 过滤 Mirabox 设备
            if (device.GetProductName()?.ToLower().Contains("mirabox") == true ||
                (device.VendorID == VENDOR_ID && device.ProductID == PRODUCT_ID))
            {
                miraboxDevices.Add(device);
                Console.WriteLine($"*** 识别为 Mirabox 设备 ***");
            }
        }
        
        return miraboxDevices;
    }

    public bool Connect()
    {
        try
        {
            var devices = FindMiraboxDevices();
            
            if (devices.Count == 0)
            {
                Console.WriteLine("未找到 Mirabox 设备");
                return false;
            }

            _device = devices.First();
            
            // 尝试打开设备
            if (!_device.TryOpen(out _stream))
            {
                Console.WriteLine("无法打开 Mirabox 设备");
                return false;
            }

            // 设置读取数据回调
            _stream.ReadTimeout = Timeout.Infinite;
            BeginRead();
            
            _isConnected = true;
            Console.WriteLine("Mirabox 连接成功");
            
            // 初始化设备
            InitializeDevice();
            
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"连接 Mirabox 失败: {ex.Message}");
            return false;
        }
    }

    private void BeginRead()
    {
        var buffer = new byte[REPORT_LENGTH];
        _stream.BeginRead(buffer, 0, buffer.Length, OnRead, buffer);
    }

    private void OnRead(IAsyncResult result)
    {
        try
        {
            if (!_isConnected) return;
            
            int bytesRead = _stream.EndRead(result);
            var buffer = (byte[])result.AsyncState;
            
            if (bytesRead > 0)
            {
                ProcessInputReport(buffer, bytesRead);
            }
            
            // 继续读取下一个报告
            BeginRead();
        }
        catch (ObjectDisposedException)
        {
            // 流已关闭,正常退出
        }
        catch (Exception ex)
        {
            Console.WriteLine($"读取数据错误: {ex.Message}");
            if (_isConnected)
            {
                // 尝试重新开始读取
                Thread.Sleep(100);
                BeginRead();
            }
        }
    }

    private void ProcessInputReport(byte[] data, int length)
    {
        // 解析按键状态报告
        // 这里需要根据 Mirabox 的实际协议来解析
        
        // 示例:假设数据格式为 [报告ID, 按键状态...]
        if (length >= 2)
        {
            for (int i = 0; i < KEY_COUNT && i < (length - 1); i++)
            {
                byte keyState = data[i + 1];
                bool isPressed = (keyState & 0x01) != 0;
               
                if (isPressed)
                {
                    Console.WriteLine($"按键 {i} 状态变化: {isPressed}");
                    KeyStateChanged?.Invoke(this, new MiraboxKeyEventArgs
                    {
                        KeyIndex = i,
                        IsPressed = isPressed
                    });
                }
            }
        }
    }

    private void InitializeDevice()
    {
        // 重置设备
        ResetDevice();
        
        // 设置初始亮度
        SetBrightness(50);
        
        // 清空所有按键
        ClearAllKeys();
        
        Console.WriteLine("Mirabox 初始化完成");
    }
}

3

主题

50

回帖

171

积分

注册会员

积分
171
发表于 2025-11-11 14:14:48 | 显示全部楼层
This is incompatible; the StreamDeckSharp library cannot be used to control MiraBox devices.

MiraBox device HID commands are not the same as StreamDeck.

Currently, the official SDK provides direct device control, but there is no C# version.

However, you can use the C++ version or the WebSocket version directly.

https://github.com/MiraboxSpace/StreamDock-Device-SDK

3

主题

50

回帖

171

积分

注册会员

积分
171
发表于 2025-11-11 14:29:11 | 显示全部楼层
Please note that the WebSocket SDK may lack some device controls.

Finally, pay special attention when setting the device background; the size of the background image must be strictly controlled (this size can be viewed in the StreamDock software settings).

Incorrect size may cause device firmware corruption.

3

主题

50

回帖

171

积分

注册会员

积分
171
发表于 2025-11-12 17:27:12 | 显示全部楼层
zoomify 发表于 2025-11-9 22:19
希望以下代码能够工作:

using HidSharp;

You can refer to my reply above to handle this.

1

主题

2

回帖

15

积分

新手上路

积分
15
 楼主| 发表于 2025-11-14 08:46:38 | 显示全部楼层
Heart 发表于 2025-11-12 17:27
You can refer to my reply above to handle this.

感谢回复

155

主题

1700

回帖

5277

积分

管理员

积分
5277
发表于 2025-11-17 09:17:01 | 显示全部楼层

0

主题

1

回帖

4

积分

新手上路

积分
4
发表于 2025-12-6 20:17:46 | 显示全部楼层
敲碗希望有C#

155

主题

1700

回帖

5277

积分

管理员

积分
5277
发表于 2025-12-16 09:02:17 | 显示全部楼层

没有明白你想问的问题是什么
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|SPACE ( 粤ICP备16086630号 )

GMT+8, 2026-4-30 14:46 , Processed in 0.051272 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表