-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObdDriver.cs
186 lines (148 loc) · 5.32 KB
/
ObdDriver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using TCC.ODBDriver.Commands;
namespace TCC.ODBDriver
{
public class ObdDriver
{
private DataReader _reader;
private DataWriter _writer;
private StreamSocket _streamSocket;
private RfcommDeviceService _deviceService;
public async Task<bool> InitializeConnection()
{
DeviceInformationCollection _deviceCollection;
DeviceInformation _selectedDevice;
var index = 0;
var result = false;
var device = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
_deviceCollection = await DeviceInformation.FindAllAsync(device);
if (_deviceCollection.Count > 0)
{
_selectedDevice = _deviceCollection[index];
if (_selectedDevice != null)
_deviceService = await RfcommDeviceService.FromIdAsync(_selectedDevice.Id);
if (_deviceService != null)
{
try
{
_streamSocket = new StreamSocket();
await _streamSocket.ConnectAsync(_deviceService.ConnectionHostName,
_deviceService.ConnectionServiceName);
SetupStreams();
await SendInitializationCommands();
result = true;
}
catch (Exception e)
{
var xx = e;
}
}
}
return result;
}
private void SetupStreams()
{
_reader = new DataReader(_streamSocket.InputStream)
{
InputStreamOptions = InputStreamOptions.Partial
};
_writer = new DataWriter(_streamSocket.OutputStream);
}
/// <summary>
/// Send the initilization commands to the ELM327
/// </summary>
private async Task SendInitializationCommands()
{
await SendCommand("ATZ\r");
await SendCommand("ATSP6\r");
await SendCommand("ATH0\r");
await SendCommand("ATCAF1\r");
}
private async Task<string> SendCommand(string command)
{
var result = "Failure sending command!";
try
{
_writer.WriteString(command);
await _writer.StoreAsync();
await _writer.FlushAsync();
IAsyncOperation<uint> count = _reader.LoadAsync(512);
count.AsTask().Wait();
result = _reader.ReadString(count.GetResults());
}
catch (Exception e)
{
var xx = e;
}
return result;
}
public async Task<double> GetSpeed()
{
var result = await new Speed(_reader, _writer).GetValue();
return result;
}
public async Task<double> GetRpm()
{
var result = await new RPM(_reader, _writer).GetValue();
return result;
}
public async Task<double> GetEngineTemperature()
{
var result = await new EngineTemperature(_reader, _writer).GetValue();
return result;
}
public async Task<double> GetFuelPressure()
{
var result = await new FuelPressure(_reader, _writer).GetValue();
return result;
}
public async Task<double> GetThrottlePosition()
{
var result = await new ThrottlePosition(_reader, _writer).GetValue();
return result;
}
public async Task<double> GetIntakeAirTemperature()
{
var result = await new IntakeAirTemperature(_reader, _writer).GetValue();
return result;
}
public async Task<double> GetFuelTankLevelInput()
{
var result = await new FuelTankLevelInput(_reader, _writer).GetValue();
return result;
}
public async Task<AllData> GetAllData()
{
var result = new AllData();
//await Task.Delay(50);
result.Speed = await new Speed(_reader, _writer).GetValue();
//await Task.Delay(50);
result.RPM = await new RPM(_reader, _writer).GetValue();
//await Task.Delay(50);
result.FuelTankLevelInput = await new FuelTankLevelInput(_reader, _writer).GetValue();
//await Task.Delay(50);
result.IntakeAirTemperature = await new IntakeAirTemperature(_reader, _writer).GetValue();
//await Task.Delay(50);
result.EngineTemperature = await new EngineTemperature(_reader, _writer).GetValue();
//await Task.Delay(50);
return result;
}
public async Task Close()
{
if (_streamSocket != null)
{
await _streamSocket.CancelIOAsync();
_streamSocket.Dispose();
_streamSocket = null;
}
_deviceService.Dispose();
_deviceService = null;
}
}
}