public DeviceMappedRegion(uint start, uint end, DataWrittenCallback writeCallback, DataReadCallback readCallback) { this.mbase = start; this.mlimit = end; this.writeCallback = writeCallback; this.readCallback = readCallback; }
public HidReader(string deviceName, string devicePath, DataReadCallback callback) { if (callback == null) { throw new ArgumentNullException("callback"); } // Create an instance of HidThread that will manage async reading from the device hThread = new HidThread(deviceName, devicePath, callback, OnThreadException); }
public HidThread(string deviceName, string devicePath, DataReadCallback callback, ThreadExceptionCallback exceptionCallback) { devicename = deviceName; devicepath = devicePath; wdcallback = new WeakDelegate(callback); wdexcallback = new WeakDelegate(exceptionCallback); // Initialize ResourceManager for loading localizable strings rm = new ResourceManager("Strings", Assembly.GetExecutingAssembly()); // Create an event to signal that the thread is up and running ThreadStarted = new ManualResetEvent(false); // Create a new event to signal the thread to terminate ThreadTerminating = new ManualResetEvent(false); }
public void ThreadMethod() { threadId = Thread.CurrentThread.ManagedThreadId; try { // Signal the openning thread that we're up and running. if (ThreadStarted != null) { ThreadStarted.Set(); } using (FileStream fs = new FileStream(HidHandle, FileAccess.Read, InputReportByteLength, true)) { byte[] buffer = new byte[InputReportByteLength]; while (true) { IAsyncResult res = fs.BeginRead(buffer, 0, InputReportByteLength, AsyncCallback, null); // We need to wait for either IO to complete or the read to be signaled WaitHandle[] IOCompleteOrThreadTerminating = { res.AsyncWaitHandle, ThreadTerminating }; // Wait for data or thread termination if (1 == WaitHandle.WaitAny(IOCompleteOrThreadTerminating)) { break; } // Call endRead to get the # of bytes actually read. This will throw an exception // if the read was not successfull (i.e. the device was removed, etc). int bytesRead = fs.EndRead(res); Logger.Info(devicename, "read " + bytesRead.ToString(CultureInfo.InvariantCulture) + " bytes from device."); // Make sure we got the correct # of bytes if (bytesRead == InputReportByteLength) { // Get strong ref to callback delegate DataReadCallback callback = wdcallback.Target as DataReadCallback; if (callback == null) { break; } // report data to caller callback(buffer); callback = null; } else { // Unexpected data size - we'll thrown an exception for now. // This may need to change for devices that report variable length data. throw new PosControlException(rm.GetString("IDS_UNEXPECTED_NUMBER_BYTES"), ErrorCode.Failure); } } } } catch (Exception e) { // We must eat this exception and marshal it back to the calling thread or else the // CLR will terminate the process Logger.Error(devicename, "Exception occurred in HID read thread.", e); //if (e.Message.CompareTo("The device is not connected.\r\n") == 0) if (e is IOException) { lock (syncRoot) { if (null != HidHandle) { if (!HidHandle.IsClosed) { HidHandle.Close(); } HidHandle.Dispose(); HidHandle = null; } } } ThreadExceptionCallback excallback = wdexcallback.Target as ThreadExceptionCallback; if (excallback != null) { excallback(e); // report exception to caller } excallback = null; } }
public HidReader(string deviceName, string devicePath, DataReadCallback callback) { if (callback == null) throw new ArgumentNullException("callback"); // Create an instance of HidThread that will manage async reading from the device hThread = new HidThread(deviceName, devicePath, callback, OnThreadException); }