赞
踩
要在C#中读取USB扫描枪的信息,你可以使用HID设备的API。以下是一个示例代码,演示如何在C#中读取USB扫描枪的信息:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.InteropServices;
class Program
{
private const int WM_INPUT = 0x00FF;
private const int RIDEV_INPUTSINK = 0x00000100;
private const int RIDEV_REMOVE = 0x00000001;
[StructLayout(LayoutKind.Sequential)]
public struct RAWINPUTDEVICE
{
public ushort usUsagePage;
public ushort usUsage;
public uint dwFlags;
public IntPtr hwndTarget;
}
[DllImport("user32.dll")]
private static extern bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint uiNumDevices, uint cbSize);
[DllImport("user32.dll")]
private static extern uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetConsoleWindow();
static void Main(string[] args)
{
// 注册原始输入设备
RAWINPUTDEVICE[] rawInputDevices = new RAWINPUTDEVICE[1];
rawInputDevices[0].usUsagePage = 0x01;
rawInputDevices[0].usUsage = 0x06;
rawInputDevices[0].dwFlags = RIDEV_INPUTSINK;
rawInputDevices[0].hwndTarget = GetConsoleWindow();
RegisterRawInputDevices(rawInputDevices, (uint)rawInputDevices.Length, (uint)Marshal.SizeOf(typeof(RAWINPUTDEVICE)));
// 获取输入数据
while (true)
{
uint size = 0;
GetRawInputData(IntPtr.Zero, 0x10000003, IntPtr.Zero, ref size, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
IntPtr rawInput = Marshal.AllocHGlobal((int)size);
try
{
GetRawInputData(rawInput, 0x10000003, rawInput, ref size, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
// 解析输入数据
RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(rawInput, typeof(RAWINPUT));
if (raw.header.dwType == 0x0100 && raw.hDevice != IntPtr.Zero)
{
uint dataSize = 0;
GetRawInputData(raw.hDevice, 0x10000003, IntPtr.Zero, ref dataSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
IntPtr data = Marshal.AllocHGlobal((int)dataSize);
try
{
GetRawInputData(raw.hDevice, 0x10000003, data, ref dataSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
RAWINPUTDATA input = (RAWINPUTDATA)Marshal.PtrToStructure(data, typeof(RAWINPUTDATA));
Console.WriteLine($"扫描枪数据:{input.keyboard.VKey}");
}
finally
{
Marshal.FreeHGlobal(data);
}
}
}
finally
{
Marshal.FreeHGlobal(rawInput);
}
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct RAWINPUTHEADER
{
public uint dwType;
public uint dwSize;
public IntPtr hDevice;
public IntPtr wParam;
}
[StructLayout(LayoutKind.Explicit)]
public struct RAWINPUTDATA
{
[FieldOffset(0)]
public RAWKEYBOARD keyboard;
}
[StructLayout(LayoutKind.Sequential)]
public struct RAWKEYBOARD
{
public ushort MakeCode;
public ushort Flags;
public ushort Reserved;
public ushort VKey;
public uint Message;
public uint ExtraInformation;
}
在上述代码中,我们使用了注册原始输入设备的API函数RegisterRawInputDevices来注册HID设备。然后,使用GetRawInputData函数来获取原始输入数据。通过解析原始输入数据,可以获取到扫描枪的键盘码(VKey)。在示例代码中,我们简单地将扫描枪的键盘码输出到控制台。
请注意,由于扫描枪作为HID设备,它的输入数据可能与键盘输入数据混合在一起。因此,在实际应用中,你可能需要根据扫描枪的特定HID设备信息来过滤和处理输入数据。
希望以上信息对你有所帮助!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。