/// <summary> /// Find all installed J2534 DLLs /// </summary> public static List <J2534.J2534Device> FindInstalledJ2534DLLs(ILogger logger) { List <J2534.J2534Device> installedDLLs = new List <J2534.J2534Device>(); try { RegistryKey myKey = Registry.LocalMachine.OpenSubKey(PASSTHRU_REGISTRY_PATH, false); if ((myKey == null)) { myKey = Registry.LocalMachine.OpenSubKey(PASSTHRU_REGISTRY_PATH_6432, false); if ((myKey == null)) { return(installedDLLs); } } string[] devices = myKey.GetSubKeyNames(); foreach (string device in devices) { J2534.J2534Device tempDevice = new J2534.J2534Device(); RegistryKey deviceKey = myKey.OpenSubKey(device); if ((deviceKey == null)) { continue; //Skip device... its empty } tempDevice.Vendor = (string)deviceKey.GetValue("Vendor", ""); tempDevice.Name = (string)deviceKey.GetValue("Name", ""); tempDevice.ConfigApplication = (string)deviceKey.GetValue("ConfigApplication", ""); tempDevice.FunctionLibrary = (string)deviceKey.GetValue("FunctionLibrary", ""); tempDevice.CAN = (int)(deviceKey.GetValue("CAN", 0)); tempDevice.ISO14230 = (int)(deviceKey.GetValue("ISO14230", 0)); tempDevice.ISO15765 = (int)(deviceKey.GetValue("ISO15765", 0)); tempDevice.ISO9141 = (int)(deviceKey.GetValue("ISO9141", 0)); tempDevice.J1850PWM = (int)(deviceKey.GetValue("J1850PWM", 0)); tempDevice.J1850VPW = (int)(deviceKey.GetValue("J1850VPW", 0)); tempDevice.SCI_A_ENGINE = (int)(deviceKey.GetValue("SCI_A_ENGINE", 0)); tempDevice.SCI_A_TRANS = (int)(deviceKey.GetValue("SCI_A_TRANS", 0)); tempDevice.SCI_B_ENGINE = (int)(deviceKey.GetValue("SCI_B_ENGINE", 0)); tempDevice.SCI_B_TRANS = (int)(deviceKey.GetValue("SCI_B_TRANS", 0)); installedDLLs.Add(tempDevice); } return(installedDLLs); } catch (Exception exception) { logger.AddDebugMessage("Error occured while finding installed J2534 devices"); logger.AddDebugMessage(exception.ToString()); return(installedDLLs); } }
public static Device CreateSerialDevice(string serialPortName, string serialPortDeviceType, ILogger logger) { try { IPort port; if (string.Equals(MockPort.PortName, serialPortName)) { port = new MockPort(logger); } else if (string.Equals(HttpPort.PortName, serialPortName)) { port = new HttpPort(logger); } else { port = new StandardPort(serialPortName); } Device device; switch (serialPortDeviceType) { case OBDXProDevice.DeviceType: device = new OBDXProDevice(port, logger); break; case AvtDevice.DeviceType: device = new AvtDevice(port, logger); break; case AvtDevice2.DeviceType: device = new AvtDevice2(port, logger); break; case MockDevice.DeviceType: device = new MockDevice(port, logger); break; case ElmDevice.DeviceType: device = new ElmDevice(port, logger); break; default: device = null; break; } if (device == null) { return(null); } return(device); } catch (Exception exception) { logger.AddUserMessage($"Unable to create {serialPortDeviceType} on {serialPortName}."); logger.AddDebugMessage(exception.ToString()); return(null); } }
public SerialPortInfo(ManagementObject property, ILogger logger) { /* These are not needed but are retained here in case they might be useful for diagnosing surprises. * this.Availability = property.GetPropertyValue("Availability") as int? ?? 0; * this.Caption = property.GetPropertyValue("Caption") as string ?? string.Empty; * this.ClassGuid = property.GetPropertyValue("ClassGuid") as string ?? string.Empty; * this.CompatibleID = property.GetPropertyValue("CompatibleID") as string[] ?? new string[] { }; * this.ConfigManagerErrorCode = property.GetPropertyValue("ConfigManagerErrorCode") as int? ?? 0; * this.ConfigManagerUserConfig = property.GetPropertyValue("ConfigManagerUserConfig") as bool? ?? false; * this.CreationClassName = property.GetPropertyValue("CreationClassName") as string ?? string.Empty; * this.Description = property.GetPropertyValue("Description") as string ?? string.Empty; * this.ErrorCleared = property.GetPropertyValue("ErrorCleared") as bool? ?? false; * this.ErrorDescription = property.GetPropertyValue("ErrorDescription") as string ?? string.Empty; * this.HardwareID = property.GetPropertyValue("HardwareID") as string[] ?? new string[] { }; * this.InstallDate = property.GetPropertyValue("InstallDate") as DateTime? ?? DateTime.MinValue; * this.LastErrorCode = property.GetPropertyValue("LastErrorCode") as int? ?? 0; * this.Manufacturer = property.GetPropertyValue("Manufacturer") as string ?? string.Empty; * this.PNPClass = property.GetPropertyValue("PNPClass") as string ?? string.Empty; * this.PNPDeviceID = property.GetPropertyValue("PNPDeviceID") as string ?? string.Empty; * this.PowerManagementCapabilities = property.GetPropertyValue("PowerManagementCapabilities") as int[] ?? new int[] { }; * this.PowerManagementSupported = property.GetPropertyValue("PowerManagementSupported") as bool? ?? false; * this.Present = property.GetPropertyValue("Present") as bool? ?? false; * this.Service = property.GetPropertyValue("Service") as string ?? string.Empty; * this.Status = property.GetPropertyValue("Status") as string ?? string.Empty; * this.StatusInfo = property.GetPropertyValue("StatusInfo") as int? ?? 0; * this.SystemCreationClassName = property.GetPropertyValue("SystemCreationClassName") as string ?? string.Empty; * this.SystemName = property.GetPropertyValue("SystemName") as string ?? string.Empty; */ this.Name = property.GetPropertyValue("Name") as string ?? string.Empty; this.DeviceID = property.GetPropertyValue("DeviceID") as string ?? string.Empty; this.PortName = GetPortName(this.DeviceID, logger); if (!string.IsNullOrEmpty(this.PortName) && !this.Name.Contains("(LPT") && this.PortName.StartsWith("COM") && this.PortName.Length > 3) { int number; int.TryParse(this.PortName.Substring(3), out number); this.PortNumber = number; } else { if (!this.PortName.StartsWith("LPT")) { logger.AddDebugMessage( string.Format( "Unable to get port number for '{0}' / '{1}' with port name '{2}'", this.Name, this.DeviceID, this.PortName)); } } }
private static string GetPortName(string deviceId, ILogger logger) { try { string key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" + deviceId + @"\Device Parameters"; return(Registry.GetValue(key, "PortName", null) as string); } catch (Exception exception) { logger.AddDebugMessage( string.Format( "GetPortName failed for '{0}': {1}: {2}", deviceId, exception.GetType().Name, exception.Message)); return(null); } }
/// <summary> /// Copy a single memory range to the PCM. /// </summary> private async Task <Response <bool> > WriteMemoryRange( MemoryRange range, byte[] image, bool justTestWrite, DateTime startTime, UInt32 totalSize, UInt32 bytesRemaining, CancellationToken cancellationToken) { int retryCount = 0; int devicePayloadSize = vehicle.DeviceMaxFlashWriteSendSize - 12; // Headers use 10 bytes, sum uses 2 bytes. for (int index = 0; index < range.Size; index += devicePayloadSize) { if (cancellationToken.IsCancellationRequested) { return(Response.Create(ResponseStatus.Cancelled, false, retryCount)); } await this.vehicle.SendToolPresentNotification(); int startAddress = (int)(range.Address + index); UInt32 thisPayloadSize = (UInt32)Math.Min(devicePayloadSize, (int)range.Size - index); logger.AddDebugMessage( string.Format( "Sending payload with offset 0x{0:X4}, start address 0x{1:X6}, length 0x{2:X4}.", index, startAddress, thisPayloadSize)); Message payloadMessage = protocol.CreateBlockMessage( image, startAddress, (int)thisPayloadSize, startAddress, justTestWrite ? BlockCopyType.TestWrite : BlockCopyType.Copy); string timeRemaining; TimeSpan elapsed = DateTime.Now - startTime; UInt32 totalWritten = totalSize - bytesRemaining; // Wait 10 seconds before showing estimates. if (elapsed.TotalSeconds < 10) { timeRemaining = "Measuring write speed..."; } else { UInt32 bytesPerSecond = (UInt32)(totalWritten / elapsed.TotalSeconds); // Don't divide by zero. if (bytesPerSecond > 0) { UInt32 secondsRemaining = (UInt32)(bytesRemaining / bytesPerSecond); timeRemaining = TimeSpan.FromSeconds(secondsRemaining).ToString("mm\\:ss"); } else { timeRemaining = "??:??"; } } logger.AddUserMessage( string.Format( "0x{0:X6}\t{1}%\t{2}", startAddress, totalWritten * 100 / totalSize, timeRemaining)); await this.vehicle.SetDeviceTimeout(TimeoutScenario.WriteMemoryBlock); // WritePayload contains a retry loop, so if it fails, we don't need to retry at this layer. Response <bool> response = await this.vehicle.WritePayload(payloadMessage, cancellationToken); if (response.Status != ResponseStatus.Success) { return(Response.Create(ResponseStatus.Error, false, response.RetryCount)); } bytesRemaining -= thisPayloadSize; retryCount += response.RetryCount; } return(Response.Create(ResponseStatus.Success, true, retryCount)); }
/// <summary> /// Writes a block of data to the PCM /// Requires an unlocked PCM /// </summary> private async Task <Response <bool> > WriteBlock(byte block, byte[] data) { Message m; Message ok = new Message(new byte[] { 0x6C, DeviceId.Tool, DeviceId.Pcm, 0x7B, block }); switch (data.Length) { case 6: m = new Message(new byte[] { 0x6C, DeviceId.Pcm, DeviceId.Tool, 0x3B, block, data[0], data[1], data[2], data[3], data[4], data[5] }); break; default: logger.AddDebugMessage("Cant write block size " + data.Length); return(Response.Create(ResponseStatus.Error, false)); } if (!await this.device.SendMessage(m)) { logger.AddUserMessage("Failed to write block " + block + ", communications failure"); return(Response.Create(ResponseStatus.Error, false)); } logger.AddDebugMessage("Successful write to block " + block); return(Response.Create(ResponseStatus.Success, true)); }
/// <summary> /// Writes a block of data to the PCM /// Requires an unlocked PCM /// </summary> public async Task <Response <bool> > WriteBlock(byte block, byte[] data) { Message tx; Message ok = new Message(new byte[] { 0x6C, DeviceId.Tool, DeviceId.Pcm, 0x7B, block }); switch (data.Length) { case 6: tx = new Message(new byte[] { 0x6C, DeviceId.Pcm, DeviceId.Tool, 0x3B, block, data[0], data[1], data[2], data[3], data[4], data[5] }); break; default: logger.AddDebugMessage("Cant write block size " + data.Length); return(Response.Create(ResponseStatus.Error, false)); } Response <Message> rx = await this.device.SendRequest(tx); if (rx.Status != ResponseStatus.Success) { logger.AddUserMessage("Failed to write block " + block + ", communications failure"); return(Response.Create(ResponseStatus.Error, false)); } if (!Utility.CompareArrays(rx.Value.GetBytes(), ok.GetBytes())) { logger.AddUserMessage("Failed to write block " + block + ", PCM rejected attempt"); return(Response.Create(ResponseStatus.Error, false)); } logger.AddDebugMessage("Successful write to block " + block); return(Response.Create(ResponseStatus.Success, true)); }