Reputation: 1375
I am new to using USB and am trying to figure out how to run a background timer and then when it fires, read from the USB device. Here is what I am using to start my timer and the method that fires when it expires:
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();
}
}
Now I want to call ReadUSB(), but I am throwing an EntryPointNotFound exception. I am using the USB code from Jan Axelson's USB complete. I know the delay is too long for USB, I just put it in there for testing purposes and will reduce it once I verify everything is working.
The exception in 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;
}
}
Upvotes: 1
Views: 1050
Reputation: 91875
The method's called CancelIo
(with an upper-case I
), not Cancello
(with a lower-case l
).
Upvotes: 1