private void pollThreadStart() { while (pollThreadRunning) { Common.Logger.Warn("Polling USB"); UsbRegDeviceList usbDeviceList = UsbDevice.AllDevices; var r = usbDeviceList.Where(x => VID == x.Vid && PIDs.Contains(x.Pid)); foreach (var inti in interfaces) { if (inti.Value.Destroyed) { Common.Logger.Debug("Found a interface (" + inti.Key + ") which was destroyed"); RemoveDevice(inti.Key); } } C.Logger.Warn("Filtered list conatins " + r.Count() + " devs - got " + interfaces.Count + " ifs so far"); if (r.Count() > 0) { C.Logger.Warn("Removing all devices"); RemoveAllDevices(); C.Logger.Warn("Calling poll"); PollDevice(); C.Logger.Warn("Done"); } Thread.Sleep(POLL_INTERVAL); } }
//called at init, and each time a system event occurs private void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e) { switch (e.EventType) { case EventType.DeviceArrival: C.Logger.Debug("LibUSB device arrival"); if (e.Device == null || e.Device.IdVendor != VID || !PIDs.Contains(e.Device.IdProduct)) { C.Logger.Debug("Not taking this device, PID/VID not a smartscope"); return; } UsbDeviceFinder usbFinder = new UsbDeviceFinder(e.Device.IdVendor, e.Device.IdProduct, e.Device.SerialNumber); UsbDevice usbDevice = UsbDevice.OpenUsbDevice(usbFinder); if (usbDevice != null) { DeviceFound(usbDevice); } break; case EventType.DeviceRemoveComplete: C.Logger.Debug(String.Format("LibUSB device removal [VID:{0},PID:{1}]", e.Device.IdVendor, e.Device.IdProduct)); if (e.Device != null && e.Device.IdVendor == VID && PIDs.Contains(e.Device.IdProduct)) { RemoveDevice(e.Device.SerialNumber); } break; default: C.Logger.Debug("LibUSB unhandled device event [" + Enum.GetName(typeof(EventType), e.EventType) + "]"); break; } }
private async Task SetPIDs() { try { if (!_driver.Started) { MessagingCenter.Send("Device not connected", BaseViewModel.MSG_ToastMessage); return; } var pids = new List <long>(); foreach (var PIDAsString in PIDs.Split(',')) { pids.Add(Convert.ToInt64(PIDAsString)); } var pidRes = await _driver.SetPIDs(pids); if (!pidRes.SuccessFlag) { throw new Exception("Response not success"); } MessagingCenter.Send("PID Filter applied", BaseViewModel.MSG_ToastMessage); } catch (Exception ex) { _loggingService.Error(ex, "Error while setting PID filter"); MessagingCenter.Send(ex.Message, BaseViewModel.MSG_ToastMessage); } }
/// <summary> /// Gets a packages return /// </summary> /// <returns>The data package with the base decoded</returns> RecievePackage GetReturn() { byte[] Data = new byte[9]; for (byte i = 0; i < 9; i++) { Data[i] = (byte)SerialPort.ReadByte(); } if (!(Data[0] == 0xEF && Data[1] == 0x01)) { return(null); } uint Address; PIDs PID = (PIDs)Data[6]; ushort Length; ushort CheckSum = 0; if (BitConverter.IsLittleEndian) { byte[] Number = new byte[4]; Array.Copy(Data, 2, Number, 0, 4); Array.Reverse(Number); Address = BitConverter.ToUInt32(Number, 0); } else { Address = BitConverter.ToUInt32(Data, 2); } if (BitConverter.IsLittleEndian) { byte[] Number = new byte[2]; Array.Copy(Data, 7, Number, 0, 2); Array.Reverse(Number); Length = BitConverter.ToUInt16(Number, 0); } else { Length = BitConverter.ToUInt16(Data, 7); } for (byte i = 6; i < 9; i++) { CheckSum += Data[i]; } Data = new byte[Length]; for (byte i = 0; i < Length; i++) { Data[i] = (byte)SerialPort.ReadByte(); } return(new RecievePackage(PID, Address, Data, CheckSum)); }
/// <summary> /// Sends a package /// </summary> /// <param name="Type">internal enum to mark package type</param> /// <param name="Data">The data to send in pakage</param> void SendPacket(PIDs Type, byte[] Data) { byte[] Packet = new byte[Data.Length + 11]; byte[] Length = BitConverter.GetBytes((ushort)(Data.Length + 2)); if (BitConverter.IsLittleEndian) { Array.Reverse(Length); } //mark packet start with header Array.Copy(Header, Packet, Header.Length); //Mark dest with address Array.Copy(AddressBytes, 0, Packet, 2, AddressBytes.Length); //Add identifier for pakage type Packet[6] = (byte)Type; //Set the length of the data. Array.Copy(Length, 0, Packet, 7, Length.Length); //load message Array.Copy(Data, 0, Packet, 9, Data.Length); byte[] CheckSum; { ushort CheckSumCalc = 0; int ReadTo = Packet.Length - 2; for (int i = 6; i < ReadTo; i++) { CheckSumCalc += Packet[i]; } CheckSum = BitConverter.GetBytes(CheckSumCalc); } if (BitConverter.IsLittleEndian) { Array.Reverse(CheckSum); } Array.Copy(CheckSum, 0, Packet, Packet.Length - 2, CheckSum.Length); SerialPort.Write(Packet, 0, Packet.Length); }
private void FilterDevice(UsbDevice usbDevice) { Logger.Debug(string.Format("Vid:0x{0:X4} Pid:0x{1:X4} - {2}", usbDevice.VendorId, usbDevice.ProductId, usbDevice.DeviceName)); if ((usbDevice.VendorId == VID) && (PIDs.Contains(usbDevice.ProductId))) { if (usbManager.HasPermission(usbDevice)) { AddDevice(usbDevice); } else { PendingIntent pi = PendingIntent.GetBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); usbManager.RequestPermission(usbDevice, pi); } } }
internal RecievePackage(PIDs PakageType, uint DeviceAddress, byte[] Data, ushort CheckSum) { this.PakageType = PakageType; this.DeviceAddress = DeviceAddress; this.Data = Data; ushort Check; if (BitConverter.IsLittleEndian) { byte[] Number = new byte[2]; Array.Copy(Data, Data.Length - 2, Number, 0, 2); Array.Reverse(Number); Check = BitConverter.ToUInt16(Number, 0); } else { Check = BitConverter.ToUInt16(Data, 7); } for (byte i = 0; i < Data.Length - 2; i++) { CheckSum += Data[i]; } Valid = CheckSum == Check; }
public override void PollDevice() { lock (interfaceLock) { if (interfaces.Count > 0 && onConnect != null) { onConnect(interfaces.First().Value, true); return; } foreach (int PID in PIDs) { USBDeviceInfo[] devs = USBDevice.GetDevices(guid); foreach (var dev in devs) { if (PIDs.Contains(dev.PID) && VID == dev.VID) { Thread.Sleep(10); try { USBDevice d = new USBDevice(dev); if (DeviceFound(d)) { return; } } catch (USBException e) { Logger.Warn("Though a device was found, we failed to capture it: " + e.Message); return; } } } } } }