我是使用USB的新手,我正在试图弄清楚如何运行后台计时器,然后当它触发时,从USB设备读取。这是我用来启动我的计时器以及它到期时触发的方法:
void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//ReadUSB();
}
private void wndwMain_ContentRendered(object sender, EventArgs e)
{
USBInit();
if (deviceFound)
{
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
myTimer.Start();
}
}
现在我想调用ReadUSB(),但我抛出一个EntryPointNotFound异常。我正在使用Jan Axelson的USB完整USB代码。我知道USB的延迟太长了,我只是把它放在那里用于测试目的,一旦我验证一切正常,它会减少它。
ReadUSB()中的例外:
private void ReadUSB()
{
IntPtr eventObject = IntPtr.Zero;
NativeOverlapped HidOverlapped = new NativeOverlapped();
Byte[] inputReportBuffer = null;
Int32 numberOfBytesRead = 0;
Int32 result = 0;
Boolean success = false;
IntPtr unManagedBuffer = IntPtr.Zero;
IntPtr unManagedOverlapped = IntPtr.Zero;
Array.Resize(ref inputReportBuffer, Capabilities.InputReportByteLength);
eventObject = CreateEvent
(IntPtr.Zero,
false,
false,
String.Empty);
HidOverlapped.OffsetLow = 0;
HidOverlapped.OffsetHigh = 0;
HidOverlapped.EventHandle = eventObject;
unManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length);
unManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(HidOverlapped));
Marshal.StructureToPtr(HidOverlapped, unManagedOverlapped, false);
readHandle = CreateFile
(devicePathName,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
success = ReadFile
(readHandle,
unManagedBuffer,
inputReportBuffer.Length,
ref numberOfBytesRead,
unManagedOverlapped);
// If ReadFile returned true, report is available. Otherwise, check for completion
if (!success)
{
result = WaitForSingleObject
(eventObject, 3000);
switch (result)
{
case WAIT_OBJECT_0:
success = true;
GetOverlappedResult
(readHandle,
unManagedOverlapped,
ref numberOfBytesRead,
false);
break;
case WAIT_TIMEOUT:
Cancello(readHandle); <-- Exception thrown here.
break;
default:
Cancello(readHandle);
break;
}
}
答案 0 :(得分:1)
该方法名为CancelIo
(大写I
),而不是Cancello
(小写l
)。