forked from EosBandi/MultiWiiWinGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_downloader.cs
308 lines (229 loc) · 8.19 KB
/
log_downloader.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Text.RegularExpressions;
namespace MultiWiiWinGUI
{
public partial class log_downloader : Form
{
public string sSerialPortName;
public int iSerialPortBaudrate;
public string sLogDirectory;
enum serialstatus {
Unknown,
Connecting,
Connected,
Erasing,
GetLogs,
Downloading,
Testing,
Fucking,
Exiting
}
private bool isConnected;
private serialstatus status = serialstatus.Unknown;
private string line = "";
private int cross = 0;
private int logcount = 0;
private int receivedbytes = 0;
private int currentlog;
private StreamWriter file;
private string logfilename;
public log_downloader()
{
InitializeComponent();
}
private void UpdateDisplay()
{
if (status != serialstatus.Downloading)
{
textBox1.AppendText(line);
}
label1.Text = Convert.ToString(cross);
if (isConnected) lStatus.Text = status.ToString() + " " + receivedbytes + " " + serialPort1.BytesToRead;
else lStatus.Text = status.ToString() + " " + receivedbytes + " 0";
this.Refresh();
}
private void DisplayText(object sender, EventArgs e)
{
UpdateDisplay();
}
private void bConnect_Click(object sender, EventArgs e)
{
if (!isConnected)
{
if (sSerialPortName == "") { return; } //if no port selected then do nothin' at connect
//Assume that the selection in the combobox for port is still valid
serialPort1.PortName = sSerialPortName;
serialPort1.BaudRate = iSerialPortBaudrate;
try
{
serialPort1.Open();
}
catch
{
//WRONG, it seems that the combobox selection pointed to a port which is no longer available
MessageBoxEx.Show(this, "Please check that your USB cable is still connected.\r\n", "Error opening COM port", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; //Exit without connecting;
}
//Set button text and status
isConnected = true;
System.Threading.Thread.Sleep(3000);
status = serialstatus.Connecting;
serialPort1.Write("+++"); //Try to enter command line mode
bConnect.Text = "Disconnect";
UpdateDisplay();
}
else
{
serialPort1.Write("exit\r");
bConnect.Text = "Connect";
//System.Threading.Thread.Sleep(500);
serialPort1.Close();
isConnected = false;
UpdateDisplay();
}
}
void genchkcombo(int logcount)
{
MethodInvoker m = delegate()
{
if (!chkList.Items.Contains(logcount))
{
chkList.Items.Add(logcount);
}
};
try
{
BeginInvoke(m);
}
catch
{
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
serialPort1.ReadTimeout = 500;
while (serialPort1.BytesToRead > 0 && isConnected)
{
//updatedisplay
line = "";
try
{
line = serialPort1.ReadLine(); //readline(comPort);
if (!line.Contains("\n"))
line = line + "\n";
}
catch
{
line = serialPort1.ReadExisting();
//byte[] data = readline(comPort);
//line = Encoding.ASCII.GetString(data, 0, data.Length);
}
receivedbytes += line.Length;
if (line.Contains(">>>"))
{
// if (status == serialstatus.GetLogs)
// {
// if (logcount >= 1) genchkcombo(-1);
// }
status = serialstatus.Connected;
}
switch (status)
{
case serialstatus.Connecting:
if (line.Contains(">>>"))
{
status = serialstatus.Connected;
}
break;
case serialstatus.Erasing:
cross++;
break;
case serialstatus.GetLogs:
if (line.Contains("start") && line.Contains("end"))
{
Regex regex2 = new Regex(@"^Log ([0-9]+),", RegexOptions.IgnoreCase);
if (regex2.IsMatch(line))
{
MatchCollection matchs = regex2.Matches(line);
logcount = int.Parse(matchs[0].Groups[1].Value);
genchkcombo(logcount);
//status = serialstatus.Done;
}
}
break;
case serialstatus.Downloading:
file.Write(line);
break;
}
this.Invoke(new EventHandler(DisplayText));
}
}
private void bEraseLogs_Click(object sender, EventArgs e)
{
if (status == serialstatus.Connected)
{
cross = 0;
serialPort1.Write("erase\r");
status = serialstatus.Erasing;
}
}
private void bListLogs_Click(object sender, EventArgs e)
{
if (status == serialstatus.Connected)
{
serialPort1.Write("logs\r");
status = serialstatus.GetLogs;
}
}
private void downloadsinglethread()
{
for (int i = 0; i < chkList.CheckedItems.Count; i++)
{
int a = (int)chkList.CheckedItems[i];
{
currentlog = a;
//open file
logfilename = sLogDirectory + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd HH-mm") + " " + currentlog + ".log";
file = new StreamWriter(logfilename);
System.Threading.Thread.Sleep(500);
serialPort1.Write("dump " + a.ToString() + "\r");
status = serialstatus.Downloading;
System.Threading.Thread.Sleep(100);
while (status != serialstatus.Connected)
{
System.Threading.Thread.Sleep(100);
}
file.Close();
file.Dispose();
}
}
}
private void bDownload_Click(object sender, EventArgs e)
{
if (status == serialstatus.Connected)
{
System.Threading.Thread t11 = new System.Threading.Thread(delegate() { downloadsinglethread(); });
t11.Name = "Log download single thread";
t11.Start();
}
}
private void processLine(string line)
{
line = line.Replace(", ", ",");
line = line.Replace(": ", ":");
string[] items = line.Split(',', ':');
if (items[0].Contains("GPS"))
{
}
}
}
}