forked from qugan09/rp1210.Net
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrp1210Control.cs
383 lines (334 loc) · 16.5 KB
/
rp1210Control.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
using log4net;
using zonar_dotnet;
namespace rp1210
{
public partial class rp1210Control : UserControl
{
private static readonly ILog log = LogManager.GetLogger(typeof(rp1210Control));
private RP121032 J1939inst;
private RP121032 J1587inst;
private RP1210BDriverInfo driverInfo;
private Stream RxLogFileStream;
private Stream TxLogFileStream;
private StreamWriter RxLogger;
private StreamReader TxLogger;
private bool bConnected = false;
private J1939Message nextJ1939Message;
private J1587Message nextJ1587Message;
private long nextMessageTimeMs;
private long TxLogTimeOffsetMs;
private bool TimeOffsetsCalculated;
private Stopwatch timeKeeper;
public event DataRecievedHandler DataRecieved;
protected virtual void OnDataRecieved(DataRecievedArgs e)
{
DataRecieved(this, e);
}
public rp1210Control()
{
log.Debug("New Control Instance.");
InitializeComponent();
}
private void rp1210Control_Load(object sender, EventArgs e)
{
List<string> devicelist = RP121032.ScanForDrivers();
cmbDriverList.DataSource = devicelist;
}
private void J1939AddressClaim()
{
// J1939 "NAME" for this sample source code application (see J1939/81)
// Self Configurable = 0 = NO
// Industry Group = 0 = GLOBAL
// Vehicle System = 0 = Non-Specific
// Vehicle System Instance = 0 = First Diagnostic PC
// Reserved = 0 = Must be zero
// Function = 129 = Offboard Service Tool
// Function Instance = 0 = First Offboard Service Tool
// Manufacturer Code = 11 = Dearborn Group Technology
// Manufacturer Identity = 0 = Dearborn Group Sample Source Code
byte[] J1939Name = { 0, 0, 0x60, 1, 0, 0x81, 0, 0 };
byte[] TxBuffer = new byte[J1939Name.Length + 2];
//TxBuffer[0] = 129; // Source Address of the Service Tool\
TxBuffer[0] = 0; // Source Address of the Service Tool
TxBuffer[1] = J1939Name[0];
TxBuffer[2] = J1939Name[1];
TxBuffer[3] = J1939Name[2];
TxBuffer[4] = J1939Name[3];
TxBuffer[5] = J1939Name[4];
TxBuffer[6] = J1939Name[5];
TxBuffer[7] = J1939Name[6];
TxBuffer[8] = J1939Name[7];
TxBuffer[9] = 0; //block until done
J1939inst.RP1210_SendCommand(RP1210_Commands.RP1210_Protect_J1939_Address, new StringBuilder(Encoding.UTF7.GetString(TxBuffer)), (short)TxBuffer.Length);
}
private void cmdConnect_Click(object sender, EventArgs e)
{
bool failed = false;
if (bConnected)
{
if (J1587inst != null) J1587inst = null;
if (J1939inst != null) J1939inst = null;
if (RxLogger != null) RxLogger.Close();
if (RxLogFileStream != null) RxLogFileStream.Close();
chkJ1587Enable.Enabled = true;
chkJ1939Enable.Enabled = true;
chkLogToFile.Enabled = true;
cmdConnect.Text = "Connect";
bConnected = false;
tmrJ1939.Enabled = false;
}
else
{
if (chkLogToFile.Checked)
{
SaveFileDialog newLogFile = new SaveFileDialog();
newLogFile.Filter = "dgd files (*.dgd)|*.dgd|All files (*.*)|*.*";
newLogFile.FilterIndex = 2;
newLogFile.RestoreDirectory = true;
if (newLogFile.ShowDialog() == DialogResult.OK)
{
if ((RxLogFileStream = newLogFile.OpenFile()) != null)
{
RxLogger = new StreamWriter(RxLogFileStream);
}
}
}
if (chkJ1939Enable.Checked)
{
J1939inst = new RP121032(cmbDriverList.SelectedItem.ToString());
try
{
//J1939inst.RP1210_ClientConnect((short)150, new StringBuilder("J1939"), 0, 0, 0);
DeviceInfo selectedDevice = (DeviceInfo)cmbDeviceList.SelectedValue;
J1939inst.RP1210_ClientConnect(selectedDevice.DeviceId, new StringBuilder("J1939"), 0, 0, 0);
txtStatus.Text = "SUCCESS - UserDevice= " + J1939inst.nClientID;
try
{
J1939inst.RP1210_SendCommand(RP1210_Commands.RP1210_Set_All_Filters_States_to_Pass, new StringBuilder(""), 0);
try
{
J1939AddressClaim();
}
catch (Exception err)
{
failed = true;
throw new Exception(err.Message);
}
}
catch (Exception err)
{
failed = true;
throw new Exception(err.Message);
}
}
catch (Exception err)
{
failed = true;
txtStatus.Text = "FAILURE - " + err.Message;
}
}
if (chkJ1587Enable.Checked)
{
J1587inst = new RP121032(cmbDriverList.SelectedItem.ToString());
try
{
J1587inst.RP1210_ClientConnect((short)cmbDeviceList.SelectedItem, new StringBuilder("J1708"), 0, 0, 0);
txtStatus.Text = "SUCCESS - UserDevice= " + J1587inst.nClientID;
}
catch (Exception err)
{
failed = true;
txtStatus.Text = "FAILURE - " + err.Message;
}
try
{
J1587inst.RP1210_SendCommand(RP1210_Commands.RP1210_Set_All_Filters_States_to_Pass, new StringBuilder(""), 0);
}
catch (Exception err)
{
failed = true;
txtStatus.Text = "FAILURE - " + err.Message;
}
}
if (!failed)
{
chkJ1587Enable.Enabled = false;
chkJ1939Enable.Enabled = false;
chkLogToFile.Enabled = false;
cmdConnect.Text = "Disconnect";
bConnected = true;
tmrJ1939.Enabled = true;
}
}
}
private void cmbDriverList_SelectedIndexChanged(object sender, EventArgs e)
{
if (!bConnected)
{
driverInfo = RP121032.LoadDeviceParameters(Environment.GetEnvironmentVariable("SystemRoot") + "\\" + cmbDriverList.SelectedItem.ToString() + ".ini");
cmbDeviceList.DataSource = driverInfo.RP1210Devices;
cmbDeviceList.DisplayMember = "DeviceId";
}
}
private void tmrJ1939_Tick(object sender, EventArgs e)
{
if (J1939inst != null)
{
try
{
while (true)
{
byte[] response = J1939inst.RP1210_ReadMessage(0);
DataRecievedArgs EventArgs = new DataRecievedArgs();
EventArgs.J1939 = true;
rp1210.J1939Message message = RP121032.DecodeJ1939Message(response);
EventArgs.RecievedJ1939Message = message;
string datastring = zcrc.ByteArrayToHexString(message.data);
string displayString = "RX J1939 - " + message.TimeStamp + " PGN: " + message.PGN + " SA: " + message.SourceAddress;
displayString += " DA: " + message.DestinationAddress + " Pri: " + message.Priority;
displayString += " Data: " + datastring + Environment.NewLine;
txtRX.AppendText(displayString);
datastring = datastring.Remove(datastring.Length - 1, 1);
datastring = datastring.Replace(" ", ", ");
if (chkLogToFile.Checked)
{
UInt32 canID = (UInt32)((message.Priority << 26) + (message.PGN << 8) + message.SourceAddress);
RxLogger.WriteLine("H RXJ1939, {0:d}, {1:x}, {2}", message.TimeStamp, canID, datastring);
}
OnDataRecieved(EventArgs);
}
}
catch (Exception err)
{
txtStatus.Text = err.Message;
}
if (TxLogger != null)
{
while (((timeKeeper ==null ) || (timeKeeper.ElapsedMilliseconds > nextMessageTimeMs)) && !TxLogger.EndOfStream)
{
string txline = TxLogger.ReadLine();
txtTX.AppendText(txline + Environment.NewLine);
txline = txline.Replace(" ", "");
string[] rawdata = txline.Split(new char[] { ',' });
if (!TimeOffsetsCalculated)
{
TxLogTimeOffsetMs = Convert.ToUInt32(rawdata[1]);
timeKeeper = Stopwatch.StartNew();
TimeOffsetsCalculated = true;
}
else
{
nextMessageTimeMs = Convert.ToUInt32(rawdata[1]) - TxLogTimeOffsetMs;
}
if ((rawdata[0] == "HRXJ1939") && (J1939inst != null))
{
nextJ1939Message = new J1939Message();
nextJ1939Message.TimeStamp = Convert.ToUInt32(rawdata[1]);
nextJ1939Message.SourceAddress = (short)(Convert.ToInt32(rawdata[2], 16) & 0x00FF);
nextJ1939Message.Priority = (byte)(Convert.ToInt32(rawdata[2], 16) >> 26);
nextJ1939Message.PGN = (UInt16)((Convert.ToInt32(rawdata[2], 16) >> 8) & 0xFFFF);
nextJ1939Message.DestinationAddress = 0xFF;
string[] strArrayTemp = new string[rawdata.Length - 3];
Array.Copy(rawdata, 3, strArrayTemp, 0, rawdata.Length - 3);
nextJ1939Message.data = Array.ConvertAll(strArrayTemp, x => Convert.ToByte(x, 16));
nextJ1939Message.dataLength = (UInt16)(rawdata.Length - 3);
if (timeKeeper.ElapsedMilliseconds > nextMessageTimeMs)
{
byte[] txArray = RP121032.EncodeJ1939Message(nextJ1939Message);
RP1210_Returns returnTemp = J1939inst.RP1210_SendMessage(txArray, (short)txArray.Length, 0, 0);
txtStatus.Text = returnTemp.ToString();
}
}
else if ((rawdata[0] == "HRXJ1708") && (J1587inst != null))
{
nextJ1587Message = new J1587Message();
nextJ1587Message.TimeStamp = Convert.ToUInt32(rawdata[1]);
nextJ1587Message.Priority = 8;
nextJ1587Message.MID = Convert.ToByte(rawdata[2],16);
nextJ1587Message.PID = Convert.ToByte(rawdata[3],16);
string[] strArrayTemp = new string[rawdata.Length - 4];
Array.Copy(rawdata, 3, strArrayTemp, 0, rawdata.Length - 4);
nextJ1587Message.data = Array.ConvertAll(strArrayTemp, x => Convert.ToByte(x, 16));
nextJ1587Message.dataLength = (UInt16)(rawdata.Length - 4);
if (timeKeeper.ElapsedMilliseconds > nextMessageTimeMs)
{
byte[] txArray = nextJ1587Message.ToArray();
RP1210_Returns returnTemp = J1587inst.RP1210_SendMessage(txArray, (short)(txArray.Length - 1), 0, 0);
txtStatus.Text = returnTemp.ToString();
}
}
}
}
}
if (J1587inst != null)
{
byte[] response = J1587inst.RP1210_ReadMessage(0);
rp1210.J1587Message message = RP121032.DecodeJ1587Message(response);
string datastring = zcrc.ByteArrayToHexString(message.data);
string displayString = "RX J1587 - " + message.TimeStamp + " MID: " + message.MID + " PID: " + message.PID;
displayString += " Data: " + zcrc.ByteArrayToHexString(message.data) + Environment.NewLine;
txtRX.AppendText(displayString);
datastring = datastring.Remove(datastring.Length - 1, 1);
datastring = datastring.Replace(" ", ", ");
if (chkLogToFile.Checked)
{
RxLogger.WriteLine("H RXJ1708, {0:d}, {1:x2}, {2:x2}, {3}", message.TimeStamp, message.MID, message.PID, datastring);
}
}
}
private void frmJBus_FormClosing(object sender, FormClosingEventArgs e)
{
if (RxLogger != null) RxLogger.Close();
if (RxLogFileStream != null) RxLogFileStream.Close();
}
private void cmdSend_Click(object sender, EventArgs e)
{
OpenFileDialog txlogfile = new OpenFileDialog();
txlogfile.Filter = "dgd files (*.dgd)|*.dgd|All files (*.*)|*.*";
txlogfile.FilterIndex = 1;
txlogfile.RestoreDirectory = true;
if (txlogfile.ShowDialog() == DialogResult.OK)
{
if ((TxLogFileStream = txlogfile.OpenFile()) != null)
{
TxLogger = new StreamReader(TxLogFileStream);
}
}
}
public void SendData(J1939Message msgToSend)
{
if (J1939inst != null)
{
try
{
byte[] txArray = RP121032.EncodeJ1939Message(msgToSend);
UInt32 canID = (UInt32)((msgToSend.Priority << 26) + (msgToSend.PGN << 8) + msgToSend.SourceAddress);
string txline = "H TXJ1939, " + msgToSend.TimeStamp + ", " + canID.ToString("X") + ", " + zcrc.ByteArrayToHexString(msgToSend.data);
txtTX.AppendText(txline + Environment.NewLine);
RP1210_Returns returnTemp = J1939inst.RP1210_SendMessage(txArray, (short)txArray.Length, 0, 0);
txtStatus.Text = returnTemp.ToString();
}
catch(Exception err)
{
txtStatus.Text = err.Message.ToString();
}
}
}
}
public class DataRecievedArgs : EventArgs
{
public bool J1939 { get; set; }
public bool J1587 { get; set; }
public J1939Message RecievedJ1939Message { get; set; }
public J1587Message RecievedJ1587Message { get; set; }
}
public delegate void DataRecievedHandler(object sender, DataRecievedArgs e);
}