Ejemplo n.º 1
0
    /// <summary>
    /// Device read thread method.
    /// </summary>
    private void ReadThread()
    {
      IntPtr receiveParamsPtr = IntPtr.Zero;

      try
      {
        IReceiveParams receiveParams;
        if (_isSystem64Bit)
        {
          receiveParams = new ReceiveParams64();
        }
        else
        {
          receiveParams = new ReceiveParams32();
        }

        int receiveParamsSize = Marshal.SizeOf(receiveParams) + DeviceBufferSize + 8;
        receiveParamsPtr = Marshal.AllocHGlobal(receiveParamsSize);

        receiveParams.ByteCount = DeviceBufferSize;
        Marshal.StructureToPtr(receiveParams, receiveParamsPtr, false);

        _readThreadMode = _readThreadModeNext;
        while (_readThreadMode != ReadThreadMode.Stop)
        {
          // Cycle thread if device stopped reading.
          if (!_deviceReceiveStarted)
          {
            if (_readThreadModeNext == ReadThreadMode.Receiving)
              StartReceive(_receivePort, PacketTimeout);
            else
            {
              StartReceive(_learnPort, PacketTimeout);
            }
            _readThreadMode = _readThreadModeNext;
            _deviceReceiveStarted = true;
          }

          int bytesRead;
          IoControl(IoCtrl.Receive, IntPtr.Zero, 0, receiveParamsPtr, receiveParamsSize, out bytesRead);

          if (bytesRead > Marshal.SizeOf(receiveParams))
          {
            int dataSize = bytesRead;

            bytesRead -= Marshal.SizeOf(receiveParams);

            byte[] packetBytes = new byte[bytesRead];
            byte[] dataBytes = new byte[dataSize];

            Marshal.Copy(receiveParamsPtr, dataBytes, 0, dataSize);
            Array.Copy(dataBytes, dataSize - bytesRead, packetBytes, 0, bytesRead);

            int[] timingData = GetTimingDataFromPacket(packetBytes);

            DebugWrite("{0:yyyy-MM-dd HH:mm:ss.ffffff} - ", DateTime.Now);
            DebugWrite("Received timing:    ");
            DebugDump(timingData);

            if (_readThreadMode == ReadThreadMode.Learning)
              _learningCode.AddTimingData(timingData);
            else
              IrDecoder.DecodeIR(timingData, _remoteCallback, _keyboardCallback, _mouseCallback);
          }

          // Determine carrier frequency when learning ...
          if (_readThreadMode == ReadThreadMode.Learning && bytesRead >= Marshal.SizeOf(receiveParams))
          {
            IReceiveParams receiveParams2;
            if (_isSystem64Bit)
            {
              receiveParams2 = (ReceiveParams64)Marshal.PtrToStructure(receiveParamsPtr, receiveParams.GetType());
            }
            else
            {
              receiveParams2 = (ReceiveParams32)Marshal.PtrToStructure(receiveParamsPtr, receiveParams.GetType());
            }
            if (System.Convert.ToInt64(receiveParams2.DataEnd) != 0)
            {
              _learningCode.Carrier = System.Convert.ToInt32(receiveParams2.CarrierFrequency);
              _readThreadMode = ReadThreadMode.LearningDone;
            }
          }
        }
      }
      catch (Exception ex)
      {
        DebugWriteLine(ex.ToString());

        if (_eHomeHandle != null)
          CancelIo(_eHomeHandle);
      }
      finally
      {
        if (receiveParamsPtr != IntPtr.Zero)
          Marshal.FreeHGlobal(receiveParamsPtr);

        try
        {
          if (_eHomeHandle != null)
            StopReceive();
        }
        catch (Exception ex)
        {
          DebugWriteLine(ex.ToString());
        }
      }

      DebugWriteLine("Read Thread Ended");
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Device read thread method.
        /// </summary>
        private void ReadThread()
        {
            var receiveParamsPtr = IntPtr.Zero;

            try
            {
                IReceiveParams receiveParams;
                if (_isSystem64Bit)
                {
                    receiveParams = new ReceiveParams64();
                }
                else
                {
                    receiveParams = new ReceiveParams32();
                }

                var receiveParamsSize = Marshal.SizeOf(receiveParams) + DeviceBufferSize + 8;
                receiveParamsPtr = Marshal.AllocHGlobal(receiveParamsSize);

                receiveParams.ByteCount = DeviceBufferSize;
                Marshal.StructureToPtr(receiveParams, receiveParamsPtr, false);

                while (_readThreadMode != ReadThreadMode.Stop)
                {
                    // Cycle thread if device stopped reading.
                    if (!_deviceReceiveStarted)
                    {
                        StartReceive(_readThreadModeNext == ReadThreadMode.Receiving ? _receivePort : _learnPort,
                                     PacketTimeout);
                        _readThreadMode = _readThreadModeNext;
                        _deviceReceiveStarted = true;

                        switch (_readThreadMode)
                        {
                            case ReadThreadMode.Receiving:
                                FireStateChanged(new StateChangedEventArgs(RunningState.Started, ReceivingState.Receiving));
                                break;
                            case ReadThreadMode.Learning:
                                FireStateChanged(new StateChangedEventArgs(RunningState.Started, ReceivingState.Learning));
                                break;
                        }
                    }

                    int bytesRead;
                    IoControl(IoCtrl.Receive, IntPtr.Zero, 0, receiveParamsPtr, receiveParamsSize, out bytesRead);

                    if (bytesRead > Marshal.SizeOf(receiveParams))
                    {
                        var dataSize = bytesRead;

                        bytesRead -= Marshal.SizeOf(receiveParams);

                        var packetBytes = new byte[bytesRead];
                        var dataBytes = new byte[dataSize];

                        Marshal.Copy(receiveParamsPtr, dataBytes, 0, dataSize);
                        Array.Copy(dataBytes, dataSize - bytesRead, packetBytes, 0, bytesRead);

                        var timingData = GetTimingDataFromPacket(packetBytes);

                        Log.Trace("{0:yyyy-MM-dd HH:mm:ss.ffffff} - ", DateTime.Now);
                        Log.Trace("Received timing:    ");
                        Log.WriteArray(LogLevel.Trace, timingData);

                        if (_readThreadMode == ReadThreadMode.Learning)
                            _learningCode.AddTimingData(timingData);
                        else
                        {
                            var code = new IRCode(timingData);
                            code.FinalizeData();
                            FireCodeReceived(new CodeReceivedEventArgs(code));
                        }
                    }

                    // Determine carrier frequency when learning ...
                    Log.Trace("bytesRead: {0}, receiveParams Size: {1}", bytesRead, Marshal.SizeOf(receiveParams));
                    if (_readThreadMode == ReadThreadMode.Learning && bytesRead >= Marshal.SizeOf(receiveParams))
                    {
                        IReceiveParams receiveParams2;
                        if (_isSystem64Bit)
                        {
                            receiveParams2 =
                                (ReceiveParams64) Marshal.PtrToStructure(receiveParamsPtr, receiveParams.GetType());
                        }
                        else
                        {
                            receiveParams2 =
                                (ReceiveParams32) Marshal.PtrToStructure(receiveParamsPtr, receiveParams.GetType());
                        }
                        Log.Trace("DataEnd {0}", Convert.ToInt64(receiveParams2.DataEnd));
                        if (Convert.ToInt64(receiveParams2.DataEnd) != 0)
                        {
                            _learningCode.Carrier = Convert.ToInt32(receiveParams2.CarrierFrequency);
                            _readThreadMode = ReadThreadMode.LearningDone;
                        }
                    }
                }
                FireStateChanged(new StateChangedEventArgs(RunningState.Stopping));
            }
            catch (Exception ex)
            {
                Log.Warn(ex);

                if (_eHomeHandle != null)
                    CancelIo(_eHomeHandle);
            }
            finally
            {
                if (receiveParamsPtr != IntPtr.Zero)
                    Marshal.FreeHGlobal(receiveParamsPtr);

                try
                {
                    if (_eHomeHandle != null)
                        StopReceive();
                }
                catch (Exception ex)
                {
                    Log.Warn(ex);
                }
            }

            Log.Debug("Read Thread Ended");
            FireStateChanged(new StateChangedEventArgs(RunningState.Stopped));
        }