Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing to .NET Standard 2.0 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/.vs/obd2NET/v15/sqlite3/storage.ide
Binary file not shown.
17 changes: 10 additions & 7 deletions src/obd2NET.sln
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30723.0
# Visual Studio 15
VisualStudioVersion = 15.0.26730.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "obd2NET", "obd2NET\obd2NET.csproj", "{388801C6-5CC7-43AE-B82F-C13FE2DBC131}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Obd2NET", "Obd2NET\Obd2NET.csproj", "{4C873365-DFFC-4166-9BB2-2AC43E036BFD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{388801C6-5CC7-43AE-B82F-C13FE2DBC131}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{388801C6-5CC7-43AE-B82F-C13FE2DBC131}.Debug|Any CPU.Build.0 = Debug|Any CPU
{388801C6-5CC7-43AE-B82F-C13FE2DBC131}.Release|Any CPU.ActiveCfg = Release|Any CPU
{388801C6-5CC7-43AE-B82F-C13FE2DBC131}.Release|Any CPU.Build.0 = Release|Any CPU
{4C873365-DFFC-4166-9BB2-2AC43E036BFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C873365-DFFC-4166-9BB2-2AC43E036BFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C873365-DFFC-4166-9BB2-2AC43E036BFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C873365-DFFC-4166-9BB2-2AC43E036BFD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A7CBB526-91B8-46AF-BFCE-6C372C5E60CC}
EndGlobalSection
EndGlobal
8 changes: 2 additions & 6 deletions src/obd2NET/ControllerResponse.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace obd2NET
namespace Obd2NET
{
/// <summary>
/// Represents the response received from the controller unit.
Expand All @@ -24,7 +20,7 @@ public Byte[] Value
{
get
{
if(RequestedPID != Vehicle.PID.Unknown && RequestedMode != Vehicle.Mode.Unknown)
if (RequestedPID != Vehicle.PID.Unknown && RequestedMode != Vehicle.Mode.Unknown)
{
Match matchedPattern = Regex.Match(Raw, @"\n([0-9a-fA-F ]{5})([0-9a-fA-F ]+)\r\n>");
return (matchedPattern.Groups.Count > 2) ? matchedPattern.Groups[2].Value.Replace(" ", "").ToByteArray() : Raw.ToByteArray();
Expand Down
40 changes: 18 additions & 22 deletions src/obd2NET/DiagnosticTroubleCode.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace obd2NET
namespace Obd2NET
{
/// <summary>
/// Represents a Diagnostic trouble code which is used for reporting errors within the vehicle.
Expand All @@ -27,7 +24,7 @@ public enum Location
/// <summary>
/// Raw and complete trouble code
/// </summary>
public Byte[] Code { get; set; }
public byte[] Code { get; set; }

/// <summary>
/// Human readable text representation
Expand All @@ -38,24 +35,24 @@ public string TextRepresentation
{
get
{
string representation = "";
switch(ErrorLocation)
var sb = new StringBuilder();
switch (ErrorLocation)
{
case Location.Powertrain: { representation = "P"; break; }
case Location.Chassis: { representation = "C"; break; }
case Location.Body: { representation = "B"; break; }
case Location.Network: { representation = "U"; break; }
case Location.Powertrain: { sb.Append("P"); break; }
case Location.Chassis: { sb.Append("C"); break; }
case Location.Body: { sb.Append("B"); break; }
case Location.Network: { sb.Append("U"); break; }
}

Byte firstByte = Code.First();
representation += (firstByte >> 4) & 3;
representation += Convert.ToInt32(((firstByte >> 0) & 15).ToString(), 16);
byte firstByte = Code.First();
sb.Append((firstByte >> 4) & 3);
sb.Append(Convert.ToInt32(((firstByte >> 0) & 15).ToString(), 16));

Byte secondByte = Code.ElementAt(1);
representation += Convert.ToInt32(((secondByte >> 4) & 15).ToString(), 16);
representation += Convert.ToInt32(((secondByte >> 0) & 15).ToString(), 16);
byte secondByte = Code.ElementAt(1);
sb.Append(Convert.ToInt32(((secondByte >> 4) & 15).ToString(), 16));
sb.Append(Convert.ToInt32(((secondByte >> 0) & 15).ToString(), 16));

return representation;
return sb.ToString();
}
}

Expand All @@ -66,17 +63,16 @@ public Location ErrorLocation
{
get
{
Byte firstByte = Code.First();
return (Location) ((firstByte >> 6) & 3);
return (Location)((Code.First() >> 6) & 3);
}
}

public DiagnosticTroubleCode(string code)
{
Code = System.Text.Encoding.Default.GetBytes(code);
Code = Encoding.Default.GetBytes(code);
}

public DiagnosticTroubleCode(Byte[] code)
public DiagnosticTroubleCode(byte[] code)
{
Code = code;
}
Expand Down
8 changes: 2 additions & 6 deletions src/obd2NET/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace obd2NET
namespace Obd2NET
{
public static class Extensions
{
Expand All @@ -22,4 +18,4 @@ public static byte[] ToByteArray(this string hex)
return bytes;
}
}
}
}
10 changes: 4 additions & 6 deletions src/obd2NET/IOBDConnection.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;

namespace obd2NET
namespace Obd2NET
{
/// <summary>
/// Interface to be implemented by all connections that are interacting with the OBD interface of the vehicle
Expand All @@ -15,6 +11,8 @@ public interface IOBDConnection
void Open();
void Close();
ControllerResponse Query(Vehicle.Mode parameterMode, Vehicle.PID parameterID);
ValueTask<ControllerResponse> QueryAsync(Vehicle.Mode parameterMode, Vehicle.PID parameterID);
ControllerResponse Query(Vehicle.Mode parameterMode);
ValueTask<ControllerResponse> QueryAsync(Vehicle.Mode parameterMode);
}
}
36 changes: 0 additions & 36 deletions src/obd2NET/Properties/AssemblyInfo.cs

This file was deleted.

12 changes: 4 additions & 8 deletions src/obd2NET/QueryException.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace obd2NET
namespace Obd2NET
{
public class QueryException : Exception
{
public QueryException(string msg):
base(msg)
{ }
public QueryException(string message) : base(message)
{
}
}
}
107 changes: 81 additions & 26 deletions src/obd2NET/SerialConnection.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace obd2NET
namespace Obd2NET
{
/// <summary>
/// Represents the serial connection to the vehicle's interface
/// </summary>
public class SerialConnection : IOBDConnection
public class SerialConnection : IOBDConnection, IDisposable
{
/// <summary>
/// Port used for communicating with the vehicle's interface
/// </summary>
public SerialPort Port { get; set; }
public SerialConnection()
: this(new SerialPort())
{

public SerialConnection():
this(new SerialPort())
{ }
}

public SerialConnection(string comPort)
: this(new SerialPort(comPort))
{

public SerialConnection(string comPort) :
this(new SerialPort(comPort))
{ }
}

public SerialConnection(SerialPort port)
{
Port = port;
Open();
}

~SerialConnection()
/// <summary>
/// Port used for communicating with the vehicle's interface
/// </summary>
public SerialPort Port { get; set; }

private bool disposed;

/// <summary>
/// Closes the connection to the interface if the connection is established
/// </summary>
public void Close()
{
Close();
if (Port.IsOpen)
Port.Close();
}

/// <summary>
Expand All @@ -46,13 +53,9 @@ public void Open()
Port.Open();
}

/// <summary>
/// Closes the connection to the interface if the connection is established
/// </summary>
public void Close()
~SerialConnection()
{
if (Port.IsOpen)
Port.Close();
Close();
}

/// <summary>
Expand All @@ -64,20 +67,58 @@ public void Close()
/// <remarks> Blocking until a complete answer has been received </remarks>
public ControllerResponse Query(Vehicle.Mode parameterMode, Vehicle.PID parameterID)
{
Port.Write(Convert.ToUInt32(parameterMode).ToString("X2") + Convert.ToUInt32(parameterID).ToString("X2") + "\r");
Port.Write($"{Convert.ToUInt32(parameterMode).ToString("X2")}{Convert.ToUInt32(parameterID).ToString("X2")}{"\r"}");
Thread.Sleep(100);

string fullResponse = "";
while(!fullResponse.Contains(">"))
while (!fullResponse.Contains(">"))
{
byte[] readBuffer = new byte[1024];
Port.Read(readBuffer, 0, 1024);
fullResponse = System.Text.Encoding.Default.GetString(readBuffer);
fullResponse = Encoding.Default.GetString(readBuffer);
}

return new ControllerResponse(fullResponse, parameterMode, parameterID);
}

public async ValueTask<ControllerResponse> QueryAsync(Vehicle.Mode parameterMode, Vehicle.PID parameterID)
{
Port.Write($"{Convert.ToUInt32(parameterMode).ToString("X2")}{Convert.ToUInt32(parameterID).ToString("X2")}{"\r"}");
await Task.Delay(100);

string fullResponse = "";
while (!fullResponse.Contains(">"))
{
byte[] readBuffer = new byte[1024];
Port.Read(readBuffer, 0, 1024);
fullResponse = Encoding.Default.GetString(readBuffer);
}

return new ControllerResponse(fullResponse, parameterMode, parameterID);
}

/// <summary>
/// Queries data from the vehicle by sending a specific mode
/// </summary>
/// <param name="parameterMode"> <c>Vehicle.Mode</c> used </param>
/// <returns> <c>ControllerResponse</c> object holding the returned data from the controller unit </returns>
/// <remarks> Blocking until a complete answer has been received </remarks>
public async ValueTask<ControllerResponse> QueryAsync(Vehicle.Mode parameterMode)
{
Port.Write(Convert.ToUInt32(parameterMode).ToString("X2") + "\r");
await Task.Delay(100);

string fullResponse = "";
while (!fullResponse.Contains(">"))
{
byte[] readBuffer = new byte[1024];
Port.Read(readBuffer, 0, 1024);
fullResponse = Encoding.Default.GetString(readBuffer);
}

return new ControllerResponse(fullResponse, parameterMode);
}

/// <summary>
/// Queries data from the vehicle by sending a specific mode
/// </summary>
Expand All @@ -99,5 +140,19 @@ public ControllerResponse Query(Vehicle.Mode parameterMode)

return new ControllerResponse(fullResponse, parameterMode);
}

private void Dispose(bool disposed)
{
if (!disposed)
{
Close();
}
}

public void Dispose()
{
Dispose(disposed);
disposed = true;
}
}
}
Loading