Я пишу функцию C#, которая будет включать мой контроллер серии Xbox. На данный момент я смог открыть контроллер и прочитать данные из него, но я не могу понять, как написать данные. Я прочитал документы и понимаю, что мне нужно отправить байтовый массив с полезной нагрузкой, но я не понимаю, как объединить байты заголовка и полезной нагрузки. Я хочу установить контроллер в состояние 0x04, которое выключено. Результат тот же. Это одно мгновение. Сообщение:
Received 24 bytes: 7E ED 82 C6 8B DA 00 00 03 20 00 00 04 00 00 00 00 00 00 00 8B 00 00 58< /code>
я получаю привет и сообщения о прибытии. < /p>
и вот код: < /p>
using AudioSwitcher.AudioApi;
using AudioSwitcher.AudioApi.CoreAudio;
using BigPictureManager.Properties;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Automation;
using System.Windows.Forms;
using Windows.Devices.Radios;
namespace BigPictureManager
{
internal static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new BigPictureTray());
}
}
public class BigPictureTray : ApplicationContext
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFileW(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadFile(
SafeFileHandle hFile,
byte[] lpBuffer,
uint nNumberOfBytesToRead,
out uint lpNumberOfBytesRead,
IntPtr lpOverlapped
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteFile(
SafeFileHandle hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
IntPtr lpOverlapped
);
// Constants for CreateFile
const uint GENERIC_READ = 0x80000000;
const uint GENERIC_WRITE = 0x40000000;
const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint OPEN_EXISTING = 3;
const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
const int GIP_REPORT_SIZE = 64;
private static void ProcessGipData(byte[] data, int length)
{
Console.WriteLine($"Received {length} bytes:");
for (int i = 0; i < length; i++)
{
Console.Write($"{data:X2} ");
if ((i + 1) % 16 == 0)
Console.WriteLine();
}
Console.WriteLine();
}
public SafeFileHandle OpenXboxGipDevice()
{
string devicePath = @"\\.\XboxGIP";
SafeFileHandle hFile = CreateFileW(
devicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero
);
if (hFile.IsInvalid)
{
int error = Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(error, "Failed to open GIP device");
}
return hFile;
}
public BigPictureTray()
{
trayIcon = new NotifyIcon()
{
Icon = Resources.TrayIcon,
Visible = true,
Text = AppName,
ContextMenuStrip = CreateMainMenu(),
};
using (var handle = OpenXboxGipDevice())
{
Console.WriteLine("GIP device opened successfully");
byte[] buffer = new byte[1000];
uint bytesRead;
while (ReadFile(handle, buffer, (uint)buffer.Length, out bytesRead, IntPtr.Zero))
{
if (bytesRead > 0)
{
ProcessGipData(buffer, (int)bytesRead);
byte[] command = new byte[64];
command[0] = 0x05;
command[1] = 0x20;
//command[2] = 0x00;
command[3] = 0x01;
command[4] = 0x04;
uint bytesRead2;
var success = WriteFile(
handle,
command,
(uint)command.Length,
out bytesRead2,
IntPtr.Zero
);
}
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... controller
Как записать данные на GIP -устройство (контроллер серии Xbox)? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение