此掃描槍使用的是無線,接受端是一個像無線網卡的小東西,在設備管理看到是hid輸入。
現在想接在軟體上,沒有輸入框焦點,從后臺獲取掃描的內容,網上下載了很多例子,都不行,都是只能連接,掃描后觸發不了接受事件。
是不是掃描槍在開發前需要什么設定才能接受到內容呢(正常焦點在文本里是可以得到的)?現在沒一點頭緒,霍的官方技術電話一通就掛,無耐
以下是關鍵代碼
public HID_RETURN OpenDevice(UInt16 vID, UInt16 pID, string serial)
{
if (deviceOpened == false)
{
//獲取連接的HID串列
List<string> deviceList = new List<string>();
GetHidDeviceList(ref deviceList);
if (deviceList.Count == 0)
return HID_RETURN.NO_DEVICE_CONECTED;
for (int i = 0; i < deviceList.Count; i++)
{
IntPtr device = CreateFile(deviceList[i],
DESIREDACCESS.GENERIC_READ | DESIREDACCESS.GENERIC_WRITE,
0,
0,
CREATIONDISPOSITION.OPEN_EXISTING,
FLAGSANDATTRIBUTES.FILE_FLAG_OVERLAPPED,
0);
if (device != INVALID_HANDLE_VALUE)
{
HIDD_ATTRIBUTES attributes;
IntPtr serialBuff = Marshal.AllocHGlobal(512);
HidD_GetAttributes(device, out attributes);
HidD_GetSerialNumberString(device, serialBuff, 512);
string deviceStr = Marshal.PtrToStringAuto(serialBuff);
Marshal.FreeHGlobal(serialBuff);
if (attributes.VendorID == vID && attributes.ProductID == pID && deviceStr.Contains(serial))
{
IntPtr preparseData;
HIDP_CAPS caps;
HidD_GetPreparsedData(device, out preparseData);
HidP_GetCaps(preparseData, out caps);
HidD_FreePreparsedData(preparseData);
outputReportLength = caps.OutputReportByteLength;
inputReportLength = caps.InputReportByteLength;
hidDevice = new FileStream(new SafeFileHandle(device, false), FileAccess.ReadWrite, inputReportLength, true);
deviceOpened = true;
BeginAsyncRead();
hHubDevice = device;
return HID_RETURN.SUCCESS;
}
}
}
return HID_RETURN.DEVICE_NOT_FIND;
}
else
return HID_RETURN.DEVICE_OPENED;
}
/// <summary>
/// 開始一次異步讀
/// </summary>
private void BeginAsyncRead()
{
byte[] inputBuff = new byte[InputReportLength];
hidDevice.BeginRead(inputBuff, 0, InputReportLength, new AsyncCallback(ReadCompleted), inputBuff);
}
第一次進入讀后,掃描沒有再次觸發,請大家指點
uj5u.com熱心網友回復:
以前做了很多霍尼韋爾的產品,官網都有代碼示例,還有各種槍使用的型號。下個示例看看就學會了uj5u.com熱心網友回復:
AsyncCallback 這種寫法,你需要寫成類似“尾遞回”的方式,才能永續讀下去。當然我個人建議直接使用別人寫好的把
https://github.com/mikeobrien/HidLibrary/tree/master/examples/Honeywell4000Series
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/247520.html
標籤:C#
