From 2435abc2bd4e6d2617f7ec24ad2166fd3cf0ace7 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 12 Feb 2018 19:47:55 +0100 Subject: [PATCH] Changed the code to be valid with C#6 to fix #7 --- OBD.NET/OBD.NET.Common/Devices/ELM327.cs | 21 +++++++++++------ OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj | 4 ++++ .../OBD.NET.Common/OBDData/AbstractOBDData.cs | 4 ++-- .../OBD.NET.Common/OBDData/DTC/TroubleCode.cs | 21 ++++++++--------- OBD.NET/OBD.NET.sln | 23 ++++--------------- .../ODB.NET.ConsoleClient.csproj | 1 + .../Communication/SerialConnection.cs | 10 ++++++-- .../ODB.NET.Desktop/ODB.NET.Desktop.csproj | 1 + 8 files changed, 44 insertions(+), 41 deletions(-) diff --git a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs index 3e1d635..d421f1f 100644 --- a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs +++ b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs @@ -158,7 +158,8 @@ namespace OBD.NET.Common.Devices CommandResult result = SendCommand(((int)Mode.ShowStoredDiagnosticTroubleCodes).ToString()); await result.WaitHandle.WaitAsync(); - if (!(result.Result is List resultCodes)) return new TroubleCode[0]; + List resultCodes = result.Result as List; + if (resultCodes == null) return new TroubleCode[0]; int count = Math.Min(dtcStatus.DTCCount, resultCodes.Count); return resultCodes.Take(count).ToArray(); @@ -179,15 +180,18 @@ namespace OBD.NET.Common.Devices { case Mode.ShowCurrentData: byte pid = (byte)message.Substring(2, 2).GetHexVal(); - if (DataTypeCache.TryGetValue(pid, out Type dataType)) + Type dataType; + if (DataTypeCache.TryGetValue(pid, out dataType)) { IOBDData obdData = (IOBDData)Activator.CreateInstance(dataType); obdData.Load(message.Substring(4)); - if (DataReceivedEventHandlers.TryGetValue(dataType, out IDataEventManager dataEventManager)) + IDataEventManager dataEventManager; + if (DataReceivedEventHandlers.TryGetValue(dataType, out dataEventManager)) dataEventManager.RaiseEvent(this, obdData, timestamp); - if (DataReceivedEventHandlers.TryGetValue(typeof(IOBDData), out IDataEventManager genericDataEventManager)) + IDataEventManager genericDataEventManager; + if (DataReceivedEventHandlers.TryGetValue(typeof(IOBDData), out genericDataEventManager)) genericDataEventManager.RaiseEvent(this, obdData, timestamp); return obdData; @@ -214,7 +218,8 @@ namespace OBD.NET.Common.Devices protected virtual byte ResolvePid() where T : class, IOBDData, new() { - if (!PidCache.TryGetValue(typeof(T), out byte pid)) + byte pid; + if (!PidCache.TryGetValue(typeof(T), out pid)) pid = AddToPidCache(); return pid; @@ -269,7 +274,8 @@ namespace OBD.NET.Common.Devices public void SubscribeDataReceived(DataReceivedEventHandler eventHandler) where T : IOBDData { - if (!DataReceivedEventHandlers.TryGetValue(typeof(T), out IDataEventManager eventManager)) + IDataEventManager eventManager; + if (!DataReceivedEventHandlers.TryGetValue(typeof(T), out eventManager)) DataReceivedEventHandlers.Add(typeof(T), (eventManager = new GenericDataEventManager())); ((GenericDataEventManager)eventManager).DataReceived += eventHandler; @@ -277,7 +283,8 @@ namespace OBD.NET.Common.Devices public void UnsubscribeDataReceived(DataReceivedEventHandler eventHandler) where T : IOBDData { - if (DataReceivedEventHandlers.TryGetValue(typeof(T), out IDataEventManager eventManager)) + IDataEventManager eventManager; + if (DataReceivedEventHandlers.TryGetValue(typeof(T), out eventManager)) ((GenericDataEventManager)eventManager).DataReceived -= eventHandler; } diff --git a/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj b/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj index f82f963..3a8780b 100644 --- a/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj +++ b/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj @@ -10,6 +10,10 @@ 1.1.0 + + 6 + + diff --git a/OBD.NET/OBD.NET.Common/OBDData/AbstractOBDData.cs b/OBD.NET/OBD.NET.Common/OBDData/AbstractOBDData.cs index 944f300..a9a7e26 100644 --- a/OBD.NET/OBD.NET.Common/OBDData/AbstractOBDData.cs +++ b/OBD.NET/OBD.NET.Common/OBDData/AbstractOBDData.cs @@ -13,7 +13,7 @@ namespace OBD.NET.Common.OBDData private byte[] _rawData; public byte[] RawData { - get => _rawData; + get { return _rawData; } set { if (value.Length != _length) @@ -22,7 +22,7 @@ namespace OBD.NET.Common.OBDData _rawData = value; } } - + public bool IsValid => RawData.Length == _length; protected byte A => RawData.Length > 0 ? RawData[0] : default(byte); diff --git a/OBD.NET/OBD.NET.Common/OBDData/DTC/TroubleCode.cs b/OBD.NET/OBD.NET.Common/OBDData/DTC/TroubleCode.cs index 83ccfbe..6f0f750 100644 --- a/OBD.NET/OBD.NET.Common/OBDData/DTC/TroubleCode.cs +++ b/OBD.NET/OBD.NET.Common/OBDData/DTC/TroubleCode.cs @@ -10,7 +10,6 @@ namespace OBD.NET.Common.OBDData.DTC private static readonly Dictionary PART_DESCRIPTION = new Dictionary { - { TroublePart.Powertrain, "P" }, { TroublePart.Chassis, "C" }, { TroublePart.Body, "B" }, @@ -21,16 +20,16 @@ namespace OBD.NET.Common.OBDData.DTC { get { - byte data = (byte)(A & 0b11000000); + byte data = (byte)(A & 0xC0); switch (data) { case 0: return TroublePart.Powertrain; - case 0b01000000: + case 0x40: return TroublePart.Chassis; - case 0b10000000: + case 0x80: return TroublePart.Body; - case 0b11000000: + case 0xC0: return TroublePart.Network; default: throw new IndexOutOfRangeException($"Can't parse TroublePart with data {data}"); @@ -42,16 +41,16 @@ namespace OBD.NET.Common.OBDData.DTC { get { - byte data = (byte)(A & 0b00110000); + byte data = (byte)(A & 0x30); switch (data) { - case 0: + case 0x0: return Definition.SAEJ2012; - case 0b01000000: + case 0x40: return Definition.Custom; - case 0b10000000: + case 0x80: return Definition.Undefined; - case 0b11000000: + case 0xC0: return Definition.Undefined2; default: throw new IndexOutOfRangeException($"Can't parse Definition with data {data}"); @@ -59,7 +58,7 @@ namespace OBD.NET.Common.OBDData.DTC } } - public SpecificPart SpecificPart => (SpecificPart)(A & 0b00001111); + public SpecificPart SpecificPart => (SpecificPart)(A & 0xF); public string FaultCode => B.ToHexString(); diff --git a/OBD.NET/OBD.NET.sln b/OBD.NET/OBD.NET.sln index c8614b3..deee0d6 100644 --- a/OBD.NET/OBD.NET.sln +++ b/OBD.NET/OBD.NET.sln @@ -1,14 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26403.7 +VisualStudioVersion = 15.0.27130.2027 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OBD.NET.Common", "OBD.NET.Common\OBD.NET.Common.csproj", "{D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ODB.NET.Desktop", "ODB.NET.Desktop\ODB.NET.Desktop.csproj", "{14CB98E1-95DE-4923-8896-FDF5171AA49E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OBD.NET.Universal", "OBD.NET.Universal\OBD.NET.Universal.csproj", "{E0EAFF82-C514-4827-8F49-F1928EBA8E73}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ODB.NET.ConsoleClient", "ODB.NET.ConsoleClient\ODB.NET.ConsoleClient.csproj", "{8F8EC5D5-94BD-47CF-9714-CA8C0198BDDC}" EndProject Global @@ -55,22 +53,6 @@ Global {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|x64.Build.0 = Release|Any CPU {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|x86.ActiveCfg = Release|Any CPU {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|x86.Build.0 = Release|Any CPU - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM.ActiveCfg = Debug|ARM - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM.Build.0 = Debug|ARM - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x64.ActiveCfg = Debug|x64 - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x64.Build.0 = Debug|x64 - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x86.ActiveCfg = Debug|x86 - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x86.Build.0 = Debug|x86 - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|Any CPU.Build.0 = Release|Any CPU - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM.ActiveCfg = Release|ARM - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM.Build.0 = Release|ARM - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x64.ActiveCfg = Release|x64 - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x64.Build.0 = Release|x64 - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x86.ActiveCfg = Release|x86 - {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x86.Build.0 = Release|x86 {8F8EC5D5-94BD-47CF-9714-CA8C0198BDDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8F8EC5D5-94BD-47CF-9714-CA8C0198BDDC}.Debug|Any CPU.Build.0 = Debug|Any CPU {8F8EC5D5-94BD-47CF-9714-CA8C0198BDDC}.Debug|ARM.ActiveCfg = Debug|Any CPU @@ -91,4 +73,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3B1EAB3A-F27B-451F-B23C-48D12E10AD80} + EndGlobalSection EndGlobal diff --git a/OBD.NET/ODB.NET.ConsoleClient/ODB.NET.ConsoleClient.csproj b/OBD.NET/ODB.NET.ConsoleClient/ODB.NET.ConsoleClient.csproj index a8ae99f..81224ea 100644 --- a/OBD.NET/ODB.NET.ConsoleClient/ODB.NET.ConsoleClient.csproj +++ b/OBD.NET/ODB.NET.ConsoleClient/ODB.NET.ConsoleClient.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + 6 AnyCPU diff --git a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs index b20bdfc..e4d2fd1 100644 --- a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs +++ b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs @@ -63,9 +63,15 @@ namespace ODB.NET.Desktop.Communication public void Dispose() => _serialPort?.Dispose(); - public Task ConnectAsync() => throw new NotSupportedException("Asynchronous operations not supported"); + public Task ConnectAsync() + { + throw new NotSupportedException("Asynchronous operations not supported"); + } - public Task WriteAsync(byte[] data) => throw new NotSupportedException("Asynchronous operations not supported"); + public Task WriteAsync(byte[] data) + { + throw new NotSupportedException("Asynchronous operations not supported"); + } public void Write(byte[] data) => _serialPort.Write(data, 0, data.Length); diff --git a/OBD.NET/ODB.NET.Desktop/ODB.NET.Desktop.csproj b/OBD.NET/ODB.NET.Desktop/ODB.NET.Desktop.csproj index 09a6f13..16a146f 100644 --- a/OBD.NET/ODB.NET.Desktop/ODB.NET.Desktop.csproj +++ b/OBD.NET/ODB.NET.Desktop/ODB.NET.Desktop.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + 6 pdbonly