public bool Send(byte[] bytes)
 {
     Thread.Sleep(SendInterval);
     if (Status == DeviceStatus.Online && bytes?.Length > 0 && bytes.Length == SendBlockSize)
     {
         return(AtUsbHid.WriteData(bytes));
     }
     return(false);
 }
        public HidDeviceViewModel()
        {
            var atUsbHidFilepath = $"{Environment.CurrentDirectory}\\AtUsbHid.dll";

            if (!File.Exists(atUsbHidFilepath))
            {
                File.WriteAllBytes(atUsbHidFilepath, Properties.Resources.AtUsbHid);
            }
            AtUsbHid.LoadUnmanagedDll(atUsbHidFilepath);

            Task.Factory.StartNew(deviceStatusMonitor);
            Task.Factory.StartNew(recieveDataMonitor);
        }
        public void UpdateStatus()
        {
            var isOnline            = AtUsbHid.FindHidDevice(_vendorId, _productIds[SelectedProductId]);
            var currentDeviceStatus = isOnline ? DeviceStatus.Online : DeviceStatus.Offline;

            if (currentDeviceStatus == Status)
            {
                return;
            }

            if (currentDeviceStatus == DeviceStatus.Online)
            {
                SendBlockSize = AtUsbHid.GetInputReportLength();
            }

            Status = currentDeviceStatus;
            DeviceChangeStatus?.Invoke(this);
        }
        private void recieveDataMonitor()
        {
            while (true)
            {
                Thread.Sleep(ReadBufferInterval);

                if (Status != DeviceStatus.Online)
                {
                    continue;
                }

                var buff = AtUsbHid.ReadBuffer();
                if (buff != null && buff.Length != 0)
                {
                    Task.Factory.StartNew(() => ReceiveBytes?.Invoke(buff));
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
 public void Dispose()
 {
     AtUsbHid.FreeLibrary();
 }