-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
689 lines (595 loc) · 23.5 KB
/
MainWindow.xaml.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
using System;
using System.IO.Ports;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using Microsoft.Win32;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using stm32_custom_flas_loader;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Linq;
namespace IMAGOPrinterProgrammerTool
{
public partial class MainWindow : Window
{
private SerialPort serialPort;
private TcpClient tcpClient;
private NetworkStream networkStream;
private StringBuilder receivedDataASCII = new StringBuilder();
private StringBuilder receivedDataHex = new StringBuilder();
private byte[] fileBytes;
private BackgroundWorker uploadWorker;
private bool ackFlag = false;
ManualResetEvent oSignalEvent;
public class HexViewerRow
{
public string Address { get; set; }
public string Hex00 { get; set; }
public string Hex01 { get; set; }
public string Hex02 { get; set; }
public string Hex03 { get; set; }
public string Hex04 { get; set; }
public string Hex05 { get; set; }
public string Hex06 { get; set; }
public string Hex07 { get; set; }
public string Hex08 { get; set; }
public string Hex09 { get; set; }
public string Hex0A { get; set; }
public string Hex0B { get; set; }
public string Hex0C { get; set; }
public string Hex0D { get; set; }
public string Hex0E { get; set; }
public string Hex0F { get; set; }
public string AsciiDump { get; set; }
}
public MainWindow()
{
InitializeComponent();
InitializeDefaults();
buttonScan.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
// Initialize BackgroundWorker for uploading
uploadWorker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
uploadWorker.DoWork += UploadWorker_DoWork;
uploadWorker.ProgressChanged += UploadWorker_ProgressChanged;
uploadWorker.RunWorkerCompleted += UploadWorker_RunWorkerCompleted;
oSignalEvent = new ManualResetEvent(false);
}
private void ButtonUpload_Click(object sender, RoutedEventArgs e)
{
if (serialPort != null && serialPort.IsOpen && fileBytes != null)
{
progressBarUpload.Value = 0;
textBlockStatus.Text = "Uploading...";
uploadWorker.RunWorkerAsync();
}
else
{
MessageBox.Show("Please connect to a COM port and load a binary file first.");
}
}
private void SendAsciiString(string command)
{
// Convert the ASCII string to a byte array
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(command);
// Send the ASCII string to the COM port
if (serialPort != null && serialPort.IsOpen)
{
serialPort.Write(commandBytes, 0, commandBytes.Length);
}
else if (tcpClient != null && tcpClient.Connected) // Send the ASCII string to the TCP server
{
if (networkStream != null && networkStream.CanWrite)
{
networkStream.Write(commandBytes, 0, commandBytes.Length);
}
}
}
private void UploadWorker_DoWork(object sender, DoWorkEventArgs e)
{
const byte COMMAND_EXTENDED_ERASE = 0x44;
const byte COMMAND_GO = 0x21;
const uint START_ADDRESS = 0x08000000; // flash starting addres stm32f4
const int CHUNK_SIZE = 256; // max size for write command
// Send command to activate bootloader
SendAsciiString("DO_PROGRAMMING");
// Delay for configuration
System.Threading.Thread.Sleep(200);
// Send 0x7F as the next step in the protocol
SendHex(new byte[] { 0x7F });
// Wait for 0x79 ACK
WaitForACK();
//ERASE MEMORY
SendCommand(COMMAND_EXTENDED_ERASE);
// Step 2: Wait for ACK
WaitForACK();
// Step 3: Send the target address and its checksum
SendHex(new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00 });
// Step 4: Wait for ACK
WaitForACK();
// Send command to start upload
// Assuming the protocol is handled in helper methods like SendCommand and ReceiveData
for (int i = 0; i < fileBytes.Length; i += CHUNK_SIZE)
{
if (uploadWorker.CancellationPending)
{
e.Cancel = true;
return;
}
int chunkLength = Math.Min(CHUNK_SIZE, fileBytes.Length - i);
byte[] chunk = new byte[chunkLength];
Array.Copy(fileBytes, i, chunk, 0, chunkLength);
// Send the chunk to the target device
bool success = UploadChunk(START_ADDRESS + (uint)i, chunk);
if (!success)
{
e.Result = false;
return;
}
// Report progress
uploadWorker.ReportProgress((i + chunkLength) * 100 / fileBytes.Length);
}
// Verify upload
bool verificationResult = VerifyUpload(fileBytes, START_ADDRESS);
e.Result = verificationResult;
SendCommand(COMMAND_GO);
WaitForACK();
//flash start address
SendHex(new byte[] { 0x08, 0x00, 0x00, 0x00, 0x08 });
// Step 4: Wait for ACK
WaitForACK();
}
private void UploadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (serialPort != null && serialPort.IsOpen)
{
progressBarUpload.Value = e.ProgressPercentage;
}
else if (tcpClient != null && tcpClient.Connected)
{
progressBarUploadEth.Value = e.ProgressPercentage;
}
}
private void UploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
if (serialPort != null && serialPort.IsOpen)
{
textBlockStatus.Text = "Upload cancelled.";
}
else if (tcpClient != null && tcpClient.Connected)
{
textBlockStatusEth.Text = "Upload cancelled.";
}
}
else if (e.Error != null)
{
if (serialPort != null && serialPort.IsOpen)
{
textBlockStatus.Text = "Error during upload: " + e.Error.Message;
}
else if (tcpClient != null && tcpClient.Connected)
{
textBlockStatusEth.Text = "Error during upload: " + e.Error.Message;
}
}
else
{
bool success = (bool)e.Result;
if (serialPort != null && serialPort.IsOpen)
{
textBlockStatus.Text = success ? "Upload and verification successful!" : "Verification failed!";
}
else if (tcpClient != null && tcpClient.Connected)
{
textBlockStatusEth.Text = success ? "Upload and verification successful!" : "Verification failed!";
}
}
}
private bool UploadChunk(uint address, byte[] chunk)
{
const byte COMMAND_WRITE_MEMORY = 0x31;
try
{
// Step 1: Send the Write Memory command (0x31)
SendCommand(COMMAND_WRITE_MEMORY);
// Step 2: Wait for ACK
WaitForACK();
// Step 3: Send the target address and its checksum
SendAddressWithChecksum(address);
// Step 4: Wait for ACK
WaitForACK();
// Step 5: Send the length (N - 1) and the data, followed by the checksum
SendDataWithChecksum(chunk);
// Step 6: Wait for final ACK
WaitForACK();
return true;
}
catch (Exception ex)
{
// Handle any exceptions, such as communication errors
MessageBox.Show($"Error during upload: {ex.Message}");
return false;
}
}
private void SendCommand(byte command)
{
byte[] commandBuffer = new byte[2];
commandBuffer[0] = command;
commandBuffer[1] = (byte)(command ^ 0xFF); // XOR with 0xFF for checksum
if (serialPort != null && serialPort.IsOpen)
{
serialPort.Write(commandBuffer, 0, commandBuffer.Length);
}
else if (tcpClient != null && tcpClient.Connected)
{
networkStream.Write(commandBuffer, 0, commandBuffer.Length);
}
}
private void SendHex(byte[] hex)
{
if (serialPort != null && serialPort.IsOpen)
{
serialPort.Write(hex, 0, hex.Length);
}
else if (tcpClient != null && tcpClient.Connected)
{
networkStream.Write(hex, 0, hex.Length);
}
}
private void SendAddressWithChecksum(uint address)
{
byte[] addressBuffer = new byte[5];
addressBuffer[0] = (byte)((address >> 24) & 0xFF); // MSB
addressBuffer[1] = (byte)((address >> 16) & 0xFF);
addressBuffer[2] = (byte)((address >> 8) & 0xFF);
addressBuffer[3] = (byte)(address & 0xFF); // LSB
// Checksum calculation: XOR all bytes of the address
addressBuffer[4] = (byte)(addressBuffer[0] ^ addressBuffer[1] ^ addressBuffer[2] ^ addressBuffer[3]);
if (serialPort != null && serialPort.IsOpen)
{
serialPort.Write(addressBuffer, 0, addressBuffer.Length);
}
else if (tcpClient != null && tcpClient.Connected)
{
networkStream.Write(addressBuffer, 0, addressBuffer.Length);
}
}
private void SendDataWithChecksum(byte[] data)
{
int length = data.Length;
byte[] dataBuffer = new byte[length + 2];
// First byte is the length (N - 1)
dataBuffer[0] = (byte)(length - 1);
// Copy the data
Array.Copy(data, 0, dataBuffer, 1, length);
// Calculate checksum: XOR all bytes including length
byte checksum = dataBuffer[0];
for (int i = 1; i <= length; i++)
{
checksum ^= dataBuffer[i];
}
// Last byte is the checksum
dataBuffer[length + 1] = checksum;
// Send the data buffer
if (serialPort != null && serialPort.IsOpen)
{
serialPort.Write(dataBuffer, 0, dataBuffer.Length);
}
else if (tcpClient != null && tcpClient.Connected)
{
networkStream.Write(dataBuffer, 0, dataBuffer.Length);
}
}
private void WaitForACK(int timeout = 1000, int retryCount = 3)
{
oSignalEvent.WaitOne();
oSignalEvent.Reset();
}
private bool GetAckFlag()
{
while (ackFlag != true) { };
return ackFlag;
}
private bool VerifyUpload(byte[] originalData, uint startAddress)
{
// Placeholder: Implement the protocol to read back data from the device and verify.
// Compare the received data with originalData.
// Return true if the verification is successful, false otherwise.
return true;
}
private void InitializeDefaults()
{
comboBoxBaudRate.SelectedIndex = 1;
comboBoxParity.SelectedIndex = 1;
comboBoxDataBits.SelectedIndex = 0;
}
private void ButtonScan_Click(object sender, RoutedEventArgs e)
{
comboBoxCOMPorts.Items.Clear();
string[] ports = SerialPort.GetPortNames();
foreach (var port in ports)
{
comboBoxCOMPorts.Items.Add(port);
}
if (comboBoxCOMPorts.Items.Count > 0)
comboBoxCOMPorts.SelectedIndex = 0;
else
MessageBox.Show("No COM ports found!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
private void ButtonConnect_Click(object sender, RoutedEventArgs e)
{
if(tcpClient != null && tcpClient.Connected)
{
MessageBox.Show("Ethernet already connected!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
try
{
if (comboBoxCOMPorts.SelectedItem == null)
{
MessageBox.Show("Please select a COM port", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
serialPort = new SerialPort(comboBoxCOMPorts.SelectedItem.ToString())
{
BaudRate = 115200,
Parity = Parity.Even,
DataBits = 8,
StopBits = StopBits.One,
// Encoding = Encoding.ASCII
};
serialPort.DataReceived += SerialPort_DataReceived;
serialPort.Open();
MessageBox.Show("Connected to " + serialPort.PortName, "Success", MessageBoxButton.OK, MessageBoxImage.Information);
buttonDisconnect.Visibility = Visibility.Visible;
buttonConnect.Visibility = Visibility.Collapsed;
}
catch (Exception ex)
{
MessageBox.Show("Error connecting to COM port: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = serialPort.ReadExisting();
Dispatcher.Invoke(() =>
{
// Update ASCII view
receivedDataASCII.Append(data);
textBoxReceivedDataASCII.Text = receivedDataASCII.ToString();
textBoxReceivedDataASCII.ScrollToEnd();
// Update Hex view
byte[] bytes = Encoding.ASCII.GetBytes(data);
foreach (byte b in bytes)
{
receivedDataHex.AppendFormat("{0:X2} ", b);
}
textBoxReceivedDataHex.Text = receivedDataHex.ToString();
textBoxReceivedDataHex.ScrollToEnd();
if (data == "y")
{
ackFlag = true; // ACK received
}
});
}
private void ButtonDisconnect_Click(object sender, RoutedEventArgs e)
{
if (serialPort != null && serialPort.IsOpen)
{
serialPort.Close();
MessageBox.Show("Disconnected", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
buttonDisconnect.Visibility = Visibility.Collapsed;
buttonConnect.Visibility = Visibility.Visible;
}
else if(tcpClient != null && tcpClient.Connected)
{
if (networkStream != null)
{
networkStream.Close();
}
if (tcpClient != null)
{
tcpClient.Close();
}
MessageBox.Show("Disconnected", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
buttonDisconnect.Visibility = Visibility.Collapsed;
buttonConnectEth.Visibility = Visibility.Visible;
}
}
private void ButtonLoadFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
fileBytes = File.ReadAllBytes(openFileDialog.FileName);
DisplayHexData(fileBytes);
}
}
private void DisplayHexData(byte[] data)
{
var hexViewerRows = new List<HexViewerRow>();
for (int i = 0; i < data.Length; i += 16)
{
var row = new HexViewerRow();
row.Address = i.ToString("X8");
var hexValues = new StringBuilder();
var asciiValues = new StringBuilder();
for (int j = 0; j < 16; j++)
{
if (i + j < data.Length)
{
byte b = data[i + j];
string hex = b.ToString("X2");
string ascii = b >= 32 && b <= 126 ? ((char)b).ToString() : ".";
hexValues.Append(hex + " ");
asciiValues.Append(ascii);
row.GetType().GetProperty($"Hex{j:X2}").SetValue(row, hex);
}
else
{
hexValues.Append(" "); // Placeholder for missing data in the last row
}
}
row.AsciiDump = asciiValues.ToString();
hexViewerRows.Add(row);
}
hexViewerDataGrid.ItemsSource = hexViewerRows;
}
private void ButtonSendData_Click(object sender, RoutedEventArgs e)
{
if (serialPort != null && serialPort.IsOpen)
{
string dataToSend = textBoxSendData.Text;
if (!string.IsNullOrEmpty(dataToSend))
{
byte[] bytesToSend;
if (IsHex(dataToSend))
{
bytesToSend = StringToByteArray(dataToSend);
}
else
{
bytesToSend = Encoding.ASCII.GetBytes(dataToSend);
}
serialPort.Write(bytesToSend, 0, bytesToSend.Length);
}
}
else if(tcpClient != null && tcpClient.Connected)
{
string dataToSend = textBoxSendData.Text;
if (!string.IsNullOrEmpty(dataToSend))
{
byte[] bytesToSend;
if (IsHex(dataToSend))
{
bytesToSend = StringToByteArray(dataToSend);
}
else
{
bytesToSend = Encoding.ASCII.GetBytes(dataToSend);
}
networkStream.Write(bytesToSend, 0, bytesToSend.Length);
}
}
else
{
MessageBox.Show("Please connect to a COM port or TCP Client first.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void ButtonClearData_Click(object sender, RoutedEventArgs e)
{
receivedDataASCII.Clear();
receivedDataHex.Clear();
textBoxReceivedDataASCII.Clear();
textBoxReceivedDataHex.Clear();
}
private bool IsHex(string input)
{
foreach (char c in input)
{
if (!Uri.IsHexDigit(c) && !char.IsWhiteSpace(c))
{
return false;
}
}
return true;
}
private byte[] StringToByteArray(string hex)
{
hex = hex.Replace(" ", "");
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
}
private async Task ConnectToServer(string ipAddress, int port)
{
try
{
tcpClient = new TcpClient();
await tcpClient.ConnectAsync(ipAddress, port);
networkStream = tcpClient.GetStream();
// MessageBox.Show("Connected to the server!");
buttonDisconnect.Visibility = Visibility.Visible;
buttonConnectEth.Visibility = Visibility.Collapsed;
// Start listening for incoming data
StartReceiving();
}
catch (Exception ex)
{
MessageBox.Show($"Failed to connect: {ex.Message}");
}
}
private async void StartReceiving()
{
byte[] buffer = new byte[1024];
try
{
while (tcpClient != null && tcpClient.Connected)
{
int bytesRead = await networkStream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
string receivedData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
// Update the UI with received data
Dispatcher.Invoke(() =>
{
textBoxReceivedDataASCII.AppendText(receivedData);
textBoxReceivedDataASCII.ScrollToEnd();
foreach (byte b in buffer.Take(bytesRead))
{
receivedDataHex.AppendFormat("{0:X2} ", b);
}
textBoxReceivedDataHex.Text = receivedDataHex.ToString();
textBoxReceivedDataHex.ScrollToEnd();
});
if (receivedData == "y")
{
ackFlag = true; // ACK received
oSignalEvent.Set();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error while receiving data: {ex.Message}");
}
}
private async void ButtonConnect_ClickEth(object sender, RoutedEventArgs e)
{
string ipAddr = textBoxIpAddr.Text;
int tcpIpPort = int.Parse(textBoxTcpIpPort.Text);
await ConnectToServer(ipAddr, tcpIpPort);
}
private void ButtonUpload_ClickEth(object sender, RoutedEventArgs e)
{
if (tcpClient != null && tcpClient.Connected && fileBytes != null)
{
progressBarUploadEth.Value = 0;
textBlockStatusEth.Text = "Uploading...";
uploadWorker.RunWorkerAsync();
}
else
{
MessageBox.Show("Please connect to a TCP Client and load a binary file first.");
}
}
}
}