Subversion Repositories Projects

Rev

Rev 2259 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2233 - 1
///============================================================================
2
/// MKLiveView 
3
/// Copyright © 2016 Steph
4
/// 
5
///This file is part of MKLiveView.
6
///
7
///MKLiveView is free software: you can redistribute it and/or modify
8
///it under the terms of the GNU General Public License as published by
9
///the Free Software Foundation, either version 3 of the License, or
10
///(at your option) any later version.
11
///
12
///MKLiveView is distributed in the hope that it will be useful,
13
///but WITHOUT ANY WARRANTY; without even the implied warranty of
14
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
///GNU General Public License for more details.
16
///
17
///You should have received a copy of the GNU General Public License
18
///along with cssRcon.  If not, see <http://www.gnu.org/licenses/>.
19
///
20
///============================================================================
21
///Credits:
22
///Chootair (http://www.codeproject.com/script/Membership/View.aspx?mid=3941737)
23
///for his "C# Avionic Instrument Controls" (http://www.codeproject.com/Articles/27411/C-Avionic-Instrument-Controls)
24
///I used some of his code for displaying the compass 
25
///
26
///Tom Pyke (http://tom.pycke.be)
27
///for his "Artifical horizon" (http://tom.pycke.be/mav/100/artificial-horizon)
28
///Great job!
29
///
30
/// and last but most of all to JOHN C. MACDONALD at Ira A. Fulton College of Engineering and Technology
31
/// for his MIKROKOPTER SERIAL CONTROL TUTORIAL (http://hdl.lib.byu.edu/1877/2747)
2265 - 32
/// and the sourcecode (http://hdl.lib.byu.edu/1877/2748)
2233 - 33
/// By his work I finally managed to get the communication with the Mikrokopter controllers to run
34
/// Some of his code was used in this programm like the SimpelSerialPort class (with some changes)
35
/// and the FilghtControllerMessage class
36
/// 
37
///============================================================================
2259 - 38
/// DISCLAIMER
39
/// ===========
40
/// 
41
/// I created this software with my best knowledge and belief.
42
/// 
43
/// IN NO EVENT, UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, 
44
/// SHALL I, OR ANY PERSON BE LIABLE FOR ANY LOSS, EXPENSE OR DAMAGE, 
45
/// OF ANY TYPE OR NATURE ARISING OUT OF THE USE OF, 
46
/// OR INABILITY TO USE THIS SOFTWARE OR PROGRAM, 
47
/// INCLUDING, BUT NOT LIMITED TO, CLAIMS, SUITS OR CAUSES OF ACTION 
48
/// INVOLVING ALLEGED INFRINGEMENT OF COPYRIGHTS, 
49
/// PATENTS, TRADEMARKS, TRADE SECRETS, OR UNFAIR COMPETITION.
50
/// 
51
/// This means: use it & have fun (but @ Your own risk...)
52
/// ===========================================================================
2233 - 53
 
54
using System;
55
using System.Data;
56
using System.Drawing;
57
using System.Text;
58
using System.Windows.Forms;
59
using System.IO;
60
using System.Threading;
61
using System.Diagnostics;
62
using System.Runtime.InteropServices;
63
 
64
namespace MKLiveView
65
{
66
    public partial class MainForm : Form
67
    {
2250 - 68
        String[] NC_Error = new string[44]
69
        {
70
            "No Error",
71
            "FC not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A1_.22FC_not_compatible_.22",
72
            "MK3Mag not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A2_.22MK3Mag_not_compatible_.22",
73
            "no FC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A3_.22no_FC_communication_.22",
74
            "no compass communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A4_.22no_compass_communication_.22",
75
            "no GPS communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A5_.22no_GPS_communication_.22",
76
            "bad compass value" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A6_.22bad_compass_value.22",
77
            "RC Signal lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A7_.22RC_Signal_lost_.22",
78
            "FC spi rx error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A8_.22FC_spi_rx_error_.22",
79
            "ERR: no NC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A9:_.22ERR:_no_NC_communication.22",
80
            "ERR: FC Nick Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A10_.22ERR:_FC_Nick_Gyro.22",
81
            "ERR: FC Roll Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A11_.22ERR:_FC_Roll_Gyro.22",
82
            "ERR: FC Yaw Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A12_.22ERR:_FC_Yaw_Gyro.22",
83
            "ERR: FC Nick ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A13_.22ERR:_FC_Nick_ACC.22",
84
            "ERR: FC Roll ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A14_.22ERR:_FC_Roll_ACC.22",
85
            "ERR: FC Z-ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A15_.22ERR:_FC_Z-ACC.22",
86
            "ERR: Pressure sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A16_.22ERR:_Pressure_sensor.22",
87
            "ERR: FC I2C" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A17_.22ERR:_FC_I2C.22",
88
            "ERR: Bl Missing" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A18_.22ERR:_Bl_Missing.22",
89
            "Mixer Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A19_.22Mixer_Error.22",
90
            "FC: Carefree Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A20_.22FC:_Carefree_Error.22",
91
            "ERR: GPS lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A21_.22ERR:_GPS_lost.22",
92
            "ERR: Magnet Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A22_.22ERR:_Magnet_Error.22",
93
            "Motor restart" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A23_.22Motor_restart.22",
94
            "BL Limitation" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A24_.22BL_Limitation.22",
95
            "Waypoint range" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A25_.22Waypoint_range.22",
96
            "ERR:No SD-Card" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A26_.22ERR:No_SD-Card.22",
97
            "ERR:SD Logging aborted" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A27_.22ERR:SD_Logging_aborted.22",
98
            "ERR:Flying range!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A28_.22ERR:Flying_range.21.22",
99
            "ERR:Max Altitude" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A29_.22ERR:Max_Altitude.22",
100
            "No GPS Fix" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A30_.22No_GPS_Fix.22",
101
            "compass not calibrated" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A31_.22compass_not_calibrated.22",
102
            "ERR:BL selftest" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A32_.22ERR:BL_selftest.22",
103
            "no ext. compass" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A33_.22no_ext._compass.22",
104
            "compass sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A34_.22compass_sensor.22",
105
            "FAILSAFE pos.!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A35_.22FAILSAFE_pos..21__.22",
106
            "ERR:Redundancy" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A36_.22ERR:Redundancy__.22",
107
            "Redundancy test" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A37_.22Redundancy_test_.22",
108
            "GPS Update rate" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A38_.22GPS_Update_rate.22",
109
            "ERR:Canbus" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A39_.22ERR:Canbus.22",
110
            "ERR: 5V RC-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A40_.22ERR:_5V_RC-Supply.22",
111
            "ERR:Power-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A41_.22ERR:Power-Supply.22",
112
            "ACC not calibr." + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A42_.22ACC_not_calibr..22",
113
            "ERR:Parachute!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A43_.22ERR:Parachute.21.22"
114
        };
2233 - 115
 
116
        [FlagsAttribute]
117
        enum NC_HWError0 : short
118
        {
119
            None = 0,
120
            SPI_RX = 1,
121
            COMPASS_RX = 2,
122
            FC_INCOMPATIBLE = 4,
123
            COMPASS_INCOMPATIBLE = 8,
124
            GPS_RX = 16,
125
            COMPASS_VALUE = 32
126
        };
127
        [FlagsAttribute]
128
        enum FC_HWError0 : short
129
        {
130
            None = 0,
131
            GYRO_NICK = 1,
132
            GYRO_ROLL = 2,
133
            GYRO_YAW = 4,
134
            ACC_NICK = 8,
135
            ACC_ROLL = 16,
136
            ACC_TOP = 32,
137
            PRESSURE = 64,
138
            CAREFREE = 128
139
        };
140
        [FlagsAttribute]
141
        enum FC_HWError1 : short
142
        {
143
            None = 0,
144
            I2C = 1,
145
            BL_MISSING = 2,
146
            SPI_RX = 4,
147
            PPM = 8,
148
            MIXER = 16,
149
            RC_VOLTAGE = 32,
150
            ACC_NOT_CAL = 64,
151
            RES3 = 128
152
        };
153
        public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
154
        // Various colors for logging info
155
        private Color[] LogMsgTypeColor = { Color.FromArgb(43, 145, 175), Color.Green, Color.Black, Color.Orange, Color.Red };
156
 
157
        string[] sAnalogLabel = new string[32];
158
        string[] sAnalogData = new string[32];
159
        bool bReadContinously = false;
160
        bool check_HWError = false;
161
        bool _bCBInit = true;
162
        bool _init = true;
163
        bool _debugDataAutorefresh = true;
164
        bool _navCtrlDataAutorefresh = true;
165
        bool _blctrlDataAutorefresh = true;
166
        bool _OSDAutorefresh = true;
2254 - 167
        bool _bErrorLog = false;
2233 - 168
        int crcError = 0;
169
        int iLableIndex = 0;
170
        string filePath = Directory.GetCurrentDirectory();
171
        string fileName = "NCLabelTexts.txt";
172
        int _iCtrlAct = 0;
173
        int _iLifeCounter = 0;
174
        int iOSDPage = 0;
175
        int iOSDMax = 0;
2257 - 176
        int[] serChan = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
177
        string[] serChanTitle = new string[12];
2233 - 178
        /// <summary>
179
        /// interval for sending debugdata (multiplied by 10ms)
180
        /// </summary>
2265 - 181
        byte debugInterval = 10; //(=> 100ms)
2233 - 182
        /// <summary>
183
        /// interval for sending BL-CTRL status (multiplied by 10ms)
184
        /// </summary>
2265 - 185
        byte blctrlInterval = 75;
2233 - 186
        /// <summary>
187
        /// interval for sending NAV-CTRL status (multiplied by 10ms)
188
        /// </summary>
189
        byte navctrlInterval = 80;
190
        /// <summary>
191
        /// interval for sending OSD page update (multiplied by 10ms)
192
        /// </summary>
193
        byte OSDInterval = 85;
194
        /// <summary>
195
        /// datatable for the debug data array - displayed on settings tabpage in datagridview
196
        /// </summary>
197
        DataTable dtAnalog = new DataTable();
2254 - 198
        /// <summary>
199
        /// datatable for motordata (current,temp)
200
        /// </summary>
2250 - 201
        DataTable dtMotors1 = new DataTable();
202
        DataTable dtMotors2 = new DataTable();
203
 
2257 - 204
        DataTable dtWaypoints = new DataTable();
205
 
2233 - 206
        public MainForm()
207
        {
208
            InitializeComponent();
2257 - 209
            serChanTitle.Initialize();
210
 
2233 - 211
            _readIni();
2257 - 212
 
2259 - 213
            _dataTablesInit();
2257 - 214
 
2233 - 215
            simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed;
216
            simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened;
217
            simpleSerialPort.DataReceived += processMessage;
2265 - 218
 
2233 - 219
            chkbAutoBL.Checked = _blctrlDataAutorefresh;
220
            chkbAutoDbg.Checked = _debugDataAutorefresh;
221
            chkbAutoNav.Checked = _navCtrlDataAutorefresh;
222
            chkbAutoOSD.Checked = _OSDAutorefresh;
2265 - 223
 
2233 - 224
            labelTimingDebug.Text = (debugInterval * 10).ToString();
225
            labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString();
226
            labelTimingNAV.Text = (navctrlInterval * 10).ToString();
227
            labelTimingOSD.Text = (OSDInterval * 10).ToString();
2265 - 228
 
229
            TabControl1.TabPages.Remove(tabPageTesting); //a testing page
2233 - 230
        }
2257 - 231
 
2233 - 232
        #region events
233
        private void MainForm_Shown(object sender, EventArgs e)
234
        {
235
            _loadLabelNames();
2257 - 236
            _initSerialCtrl();
2233 - 237
            _init = false;
2265 - 238
            splitContainer1.SplitterDistance = 510;
2233 - 239
        }
240
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
241
        {
242
            _writeIni();
243
        }
244
        private void SimpleSerialPort_PortOpened()
245
        {
246
            btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(192, 255, 192)));
247
            btnConn.Invoke((Action)(() => btnConn.Text = "close" + Environment.NewLine + "serial port"));
248
            _getVersion();
249
            Thread.Sleep(100);
250
            _OSDMenue(0);
2257 - 251
            Thread.Sleep(200);
252
            _sendSerialData();
2233 - 253
           // _readCont(true);
254
        }
255
        private void SimpleSerialPort_PortClosed()
256
        {
257
            btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(224, 224, 224)));
258
            btnConn.Invoke((Action)(() => btnConn.Text = "open" + Environment.NewLine + "serial port"));
259
            _readCont(false);
260
        }
261
        /// <summary>
262
        /// timer for refreshing subscription of subscribed data
263
        /// query lifecounter for connection failure
264
        /// </summary>
265
        private void timer1_Tick(object sender, EventArgs e)
266
        {
267
            if(bReadContinously)
268
            {
269
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
270
 
2250 - 271
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
2233 - 272
 
273
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
274
                check_HWError = true;
275
                _getVersion();
276
                Thread.Sleep(10);
277
                if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); }
278
                if (_iLifeCounter > 0)
279
                {
280
                    lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0);
281
                    _iLifeCounter = 0;
282
                }
283
                else
284
                {
285
                    Log(LogMsgType.Error, "No communication to NC/FC!");
286
                    lblLifeCounter.BackColor = Color.FromArgb(224, 0, 0);
287
                }
288
            }
289
        }
290
        private void cbOSD_SelectedIndexChanged(object sender, EventArgs e)
291
        {
292
            if (!_bCBInit && cbOSD.SelectedIndex > -1)
293
                _OSDMenue(cbOSD.SelectedIndex);
294
        }
295
        private void chkbAutoDbg_CheckedChanged(object sender, EventArgs e)
296
        {
297
            if(!_init) _debugDataAutorefresh = chkbAutoDbg.Checked;
298
        }
299
        private void chkbAutoNav_CheckedChanged(object sender, EventArgs e)
300
        {
301
            if (!_init) _navCtrlDataAutorefresh = chkbAutoNav.Checked;
302
        }
303
        private void chkbAutoBL_CheckedChanged(object sender, EventArgs e)
304
        {
305
            if (!_init) _blctrlDataAutorefresh = chkbAutoBL.Checked;
306
        }
307
        private void chkbAutoOSD_CheckedChanged(object sender, EventArgs e)
308
        {
309
            if (!_init) _OSDAutorefresh = chkbAutoOSD.Checked;
310
        }
311
        private void cbTimingDebug_SelectedIndexChanged(object sender, EventArgs e)
312
        {
313
            if (cbTimingDebug.SelectedIndex > -1)
314
            {
315
                debugInterval = (byte)(Convert.ToInt16(cbTimingDebug.SelectedItem) / 10);
316
                labelTimingDebug.Text = (debugInterval * 10).ToString();
317
            }
318
        }
319
        private void cbTimingNAV_SelectedIndexChanged(object sender, EventArgs e)
320
        {
321
            if (cbTimingNAV.SelectedIndex > -1)
322
            {
323
                navctrlInterval = (byte)(Convert.ToInt16(cbTimingNAV.SelectedItem) / 10);
324
                labelTimingNAV.Text = (navctrlInterval * 10).ToString();
325
            }
326
        }
327
        private void cbTimingBLCTRL_SelectedIndexChanged(object sender, EventArgs e)
328
        {
329
            if (cbTimingBLCTRL.SelectedIndex > -1)
330
            {
331
                blctrlInterval = (byte)(Convert.ToInt16(cbTimingBLCTRL.SelectedItem) / 10);
332
                labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString();
333
            }
334
        }
335
        private void cbTimingOSD_SelectedIndexChanged(object sender, EventArgs e)
336
        {
337
            if (cbTimingOSD.SelectedIndex > -1)
338
            {
339
                OSDInterval = (byte)(Convert.ToInt16(cbTimingOSD.SelectedItem) / 10);
340
                labelTimingOSD.Text = (OSDInterval * 10).ToString();
341
            }
342
        }
2254 - 343
        private void rtfError_LinkClicked(object sender, LinkClickedEventArgs e)
344
        {
345
            System.Diagnostics.Process.Start(e.LinkText);
346
        }
2257 - 347
 
2233 - 348
        #endregion events
349
 
350
        /// <summary> Log data to the terminal window. </summary>
351
        /// <param name="msgtype"> The type of message to be written. </param>
352
        /// <param name="msg"> The string containing the message to be shown. </param>
353
        private void Log(LogMsgType msgtype, string msg)
354
        {
355
            rtfTerminal.Invoke(new EventHandler(delegate
356
            {
357
                if (rtfTerminal.Lines.Length >= 1000)   //Wenn Terminal mehr als 1000 Zeilen hat
358
                    rtfTerminal.Select(42, (500 * 129));     //500 löschen
359
                rtfTerminal.Select(rtfTerminal.Text.Length, 0);
360
                rtfTerminal.SelectedText = string.Empty;
361
                rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Regular);
362
                rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype];
363
                rtfTerminal.AppendText(msg + Environment.NewLine);
364
                rtfTerminal.ScrollToCaret();
365
            }));
366
        }
367
        /// <summary> display the OSD text in 4 lines à 20 chars </summary>
368
        /// <param name="msgtype"> The type of message to be written. </param>
369
        /// <param name="msg"> The string containing the message to be shown. </param>
370
        private void OSD(LogMsgType msgtype, string msg)
371
        {
372
            rtfOSD.Invoke(new EventHandler(delegate
373
            {
374
                if (rtfOSD.Lines.Length > 4)
375
                    rtfOSD.Clear();
376
                rtfOSD.Select(rtfOSD.Text.Length,0);
377
                rtfOSD.SelectedText = string.Empty;
378
                rtfOSD.SelectionFont = new Font(rtfOSD.SelectionFont, FontStyle.Regular);
379
                rtfOSD.SelectionColor = LogMsgTypeColor[(int)msgtype];
380
                rtfOSD.AppendText(msg + Environment.NewLine);
381
                if (rtfOSD.Text.IndexOf("ERR") > 0)
382
                {
383
                    rtfOSD.Select(rtfOSD.Text.IndexOf("ERR"), 40);
384
                    rtfOSD.SelectionColor = LogMsgTypeColor[(int)LogMsgType.Error];
385
                }
386
            }));
387
        }
388
        private void ErrorLog(LogMsgType msgtype, string msg)
389
        {
390
            rtfError.Invoke(new EventHandler(delegate
391
            {
392
                if (rtfError.Lines.Length > 4)
393
                    rtfError.Clear();
394
                rtfError.Focus();
395
                rtfError.Select(rtfError.Text.Length, 0);
396
                rtfError.SelectedText = string.Empty;
397
                rtfError.SelectionFont = new Font(rtfError.SelectionFont, FontStyle.Regular);
398
                rtfError.SelectionColor = LogMsgTypeColor[(int)msgtype];
399
                rtfError.AppendText(msg + Environment.NewLine);
400
 
401
            }));
2254 - 402
            _bErrorLog = true;
2233 - 403
        }
404
 
405
        #region functions        
406
 
2250 - 407
        #region processing received data
408
        /// <summary> Processing the messages and displaying them in the according form controls 
409
        /// function called by simpleSerialPort.DataReceived event
410
        /// </summary>
2233 - 411
        /// <param name="message"> message bytearray recieved by SimpleSerialPort class </param>
412
        private void processMessage(byte[] message)
413
        {
414
            if (message.Length > 0)
415
            {
416
                _iLifeCounter++;
417
                //Log(LogMsgType.Incoming, BitConverter.ToString(message));
418
                //Log(LogMsgType.Incoming, message.Length.ToString());
419
                string s = new string(ASCIIEncoding.ASCII.GetChars(message, 0, message.Length));
420
                char cmdID;
421
                byte adr;
422
                byte[] data;
2257 - 423
                byte[] tmp = null;
2233 - 424
                if (message[0] != '#')
2257 - 425
                {
426
                    int iFound = -1;
427
                    for(int i=0;i<message.Length;i++)   //Sometimes the FC/NC sends strings without termination (like WP messages)
428
                    {                                   //so this is a workaround to not spam the log box
429
                        if (message[i] == 35)
430
                        {
431
                            iFound = i;
432
                            break;
433
                        }                          
434
                    }
435
                    if(iFound>0)
436
                    {
437
                        s = new string(ASCIIEncoding.ASCII.GetChars(message, 0,iFound));
438
                        tmp = new byte[message.Length - iFound];
439
                        Buffer.BlockCopy(message, iFound, tmp, 0, message.Length - iFound);
440
                    }
441
                    s = s.Trim('\0', '\n', '\r');
442
                    if(s.Length > 0)
443
                        Log(LogMsgType.Normal, s);
444
                    if (tmp != null)
445
                    {
446
                        s = new string(ASCIIEncoding.ASCII.GetChars(tmp, 0, tmp.Length));
447
                        processMessage(tmp);
448
                    }
449
                }
2233 - 450
                //Debug.Print(s);
451
                else
452
                {
453
                    FlightControllerMessage.ParseMessage(message, out cmdID, out adr, out data);
454
 
455
                    if (adr == 255) { crcError++; }
456
                    else crcError = 0;
457
                    lblCRCErr.Invoke((Action)(() => lblCRCErr.Text = crcError.ToString()));
2250 - 458
                    //display the active controller (FC / NC) 
2233 - 459
                    if (adr > 0 && adr < 3 && adr != _iCtrlAct) //adr < 3: temporary workaround cause when I've connected the FC alone it always switches between mk3mag & FC every second...???
460
                    {
461
                        _iCtrlAct = adr;
462
                        switch (adr)
463
                        {
464
                            case 1:
465
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "FC"));
466
                                lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "FC"));
467
                                _setFieldsNA(); //display fields NA for FC 
468
                                break;
469
                            case 2:
470
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "NC"));
471
                                lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "NC"));
472
                                break;
473
                            case 3:
474
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "MK3MAG"));
475
                                break;
476
                            case 4:
477
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "BL-CTRL"));
478
                                break;
479
                            default:
480
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "...."));
481
                                break;
482
                        }
2257 - 483
                        _loadLabelNames();
2233 - 484
                    }
2257 - 485
                    // else
486
                    //     Debug.Print("Address == 0?");
2233 - 487
 
488
                    if (data != null && data.Length > 0)
489
                    {
490
                        s = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1));
491
                        s = s.Trim('\0', '\n');
492
 
493
                        switch (cmdID)
494
                        {
2250 - 495
                            case 'A': //Label names
496
                                _processLabelNames(s);
2233 - 497
                                break;
498
 
2250 - 499
                            case 'D': //Debug data
2257 - 500
                                _processDebugVals(adr, data);
2233 - 501
                                break;
502
 
2250 - 503
                            case 'V': //Version
504
                                _processVersion(adr, data);
2233 - 505
                                break;
506
 
2250 - 507
                            case 'K'://BL-CTRL data
508
                                _processBLCtrl(data);
2233 - 509
                                break;
510
 
511
                            case 'O': //NC Data
2250 - 512
                                _processNCData(data);
2233 - 513
                                break;
514
 
515
                            case 'E': //NC error-string
516
                                ErrorLog(LogMsgType.Error, "NC Error: " + s);
517
                                break;
518
 
2250 - 519
                            case 'L': //OSD Menue (called by pagenumber)
520
                                _processOSDSingle(data);
2233 - 521
                                break;
522
 
2250 - 523
                            case 'H': //OSD Menue (with autoupdate - called by Key)
524
                                _processOSDAuto(data);
2233 - 525
                                break;
526
 
2257 - 527
                            case 'X': //Waypoint data
528
                                _processWPData(data);
529
                                break;
530
 
2233 - 531
                            //default:
532
                            //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
533
                            //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
534
                            //    break;
535
                        }
536
                    }
537
                    //else
538
                    //{
539
                    //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
540
                    //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
541
                    //}
542
                }
543
            }
544
        }
2250 - 545
        /// <summary>
546
        /// Analog label names 'A'
547
        /// each label name is returned as a single string 
548
        /// and added to string array sAnalogLabel[]
549
        /// and the datatable dtAnalog
550
        /// </summary>
551
        /// <param name="s">the label name</param>
552
        void _processLabelNames(string s)
553
        {
554
            if (iLableIndex < 32)
555
            {
556
                sAnalogLabel[iLableIndex] = s;
557
                if (dtAnalog.Rows.Count < 32)
558
                    dtAnalog.Rows.Add(s, "");
559
                else
560
                    dtAnalog.Rows[iLableIndex].SetField(0, s);
2233 - 561
 
2250 - 562
                _getAnalogLabels(iLableIndex + 1);
563
            }
564
            Debug.Print(s);
565
        }
566
        /// <summary>
567
        /// Debug values 'D'
568
        /// </summary>
569
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
570
        /// <param name="data">the received byte array to process</param>
571
        void _processDebugVals(byte adr,byte[] data)
572
        {
573
            if (data.Length == 66)
574
            {
575
                int[] iAnalogData = new int[32];
576
 
577
                int index = 0;
578
                Int16 i16 = 0;
579
                double dTemp = 0;
580
                for (int i = 2; i < 66; i += 2)
581
                {
582
                    i16 = data[i + 1];
583
                    i16 = (Int16)(i16 << 8);
584
                    iAnalogData[index] = data[i] + i16;
585
                    sAnalogData[index] = (data[i] + i16).ToString();
586
                    dtAnalog.Rows[index].SetField(1, sAnalogData[index]);
587
 
588
                    if (adr == 2) //NC
589
                    {
590
                        switch (index)
591
                        {
592
                            case 0: //pitch (German: nick)
593
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10)));
594
                                lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
595
                                break;
596
                            case 1: //roll
597
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10)));
598
                                lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
599
                                break;
600
                            case 4: //altitude
601
                                lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m")));
602
                                break;
603
                            case 7: //Voltage
604
                                lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V")));
605
                                break;
606
                            case 8: // Current
607
                                lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A")));
608
                                break;
609
                            case 10: //heading
610
                                lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°"));
611
                                headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index])));
612
                                break;
613
                            case 12: // SPI error
614
                                lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index]));
615
                                break;
616
                            case 14: //i2c error
617
                                lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index]));
618
                                break;
619
                            case 20: //Earthmagnet field
620
                                lblNCMF.Invoke((Action)(() => lblNCMF.Text = sAnalogData[index] + "%"));
621
                                break;
622
                            case 21: //GroundSpeed
623
                                lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s")));
624
                                break;
625
                            case 28: //Distance East from saved home position -> calculate distance with distance N + height
626
                                dTemp = Math.Pow((double)iAnalogData[index], 2) + Math.Pow((double)iAnalogData[index - 1], 2);
627
                                dTemp = Math.Sqrt(dTemp) / (double)10; //'flat' distance from HP with N/E
628
                                                                       //  lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00")));
629
                                dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation
2254 - 630
                                dTemp = Math.Sqrt(dTemp) / (double)10;
631
                                lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = dTemp.ToString("0.0 m")));
2250 - 632
                                break;
633
                            case 31: //Sats used
634
                                lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index]));
635
                                break;
636
                        }
637
                    }
638
                    if (adr == 1) //FC
639
                    {
640
                        switch (index)
641
                        {
642
                            case 0: //pitch (German: nick)
643
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10)));
644
                                lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
645
                                break;
646
                            case 1: //roll
647
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10)));
648
                                lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
649
                                break;
650
                            case 5: //altitude
651
                                lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m")));
652
                                break;
653
                            case 8: //heading
654
                                lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°"));
655
                                headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index])));
656
                                break;
657
                            case 9: //Voltage
658
                                lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V")));
659
                                break;
660
                            case 10: //Receiver quality
661
                                lblNCRC.Invoke((Action)(() => lblNCRC.Text = sAnalogData[index]));
662
                                break;
663
                            case 22: // Current
664
                                lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A")));
665
                                break;
666
                            case 23: //capacity used
667
                                lblNCCap.Invoke((Action)(() => lblNCCap.Text = (iAnalogData[index]).ToString("0 mAh")));
668
                                break;
669
                            case 27: // SPI error
670
                                lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index]));
671
                                break;
672
                            case 28: //i2c error
673
                                lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index]));
674
                                break;
675
                        }
676
                    }
677
                    index++;
678
                }
679
            }
680
            else
681
                Debug.Print("wrong data-length (66): " + data.Length.ToString());
682
        }
683
        /// <summary>
684
        /// Version string 'V'
685
        /// </summary>
686
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
687
        /// <param name="data">the received byte array to process</param>
688
        void _processVersion(byte adr,byte[] data)
689
        {
690
            if (data.Length == 12)
691
            {
692
                if (!check_HWError)
693
                {
694
                    string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " };
695
                    string sVersion = "";
696
                    //sbyte[] signed = Array.ConvertAll(data, b => unchecked((sbyte)b));
697
                    Log(LogMsgType.Warning, (adr == 1 ? "FC-" : "NC-") + "Version: ");
698
                    sVersion = "HW V" + (data[7] / 10).ToString() + "." + (data[7] % 10).ToString();
699
                    Log(LogMsgType.Incoming, sVersion);
700
                    sVersion = "SW V" + (data[0]).ToString() + "." + (data[1]).ToString() + ((char)(data[4] + 'a')).ToString();
701
                    Log(LogMsgType.Incoming, sVersion);
702
                    Log(LogMsgType.Incoming, "BL-Firmware: V" + (data[8] / 100).ToString() + "." + (data[8] % 100).ToString());
703
                }
704
                if (data[5] > 0) //error0 
705
                {
706
                    if (adr == 1)
707
                        ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[5].ToString() + ": " + ((FC_HWError0)data[5]).ToString());
708
                    if (adr == 2)
709
                        ErrorLog(LogMsgType.Error, "NC - HW-Error " + data[5].ToString() + ": " + ((NC_HWError0)data[5]).ToString());
710
                }
711
                if (data[6] > 0) //error1 
712
                {
713
                    if (adr == 1)
714
                        ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[6].ToString() + ": " + ((FC_HWError1)data[6]).ToString());
715
                    if (adr == 2)
716
                        ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field
717
                }
2254 - 718
                if((data[5] + data[6] == 0) && _bErrorLog)
719
                    _clearErrorLog(adr==1 ? "FC - HW-Error" : "FC - HW-Error");
2250 - 720
 
721
            }
722
            check_HWError = false;
723
        }
724
        /// <summary>
725
        /// BL-Ctrl data 'K'
726
        /// for FC you have to use a customized firmware
727
        /// </summary>
728
        /// <param name="data">the received byte array to process</param>
729
        void _processBLCtrl(byte[] data)
730
        {
731
            if (data.Length % 6 == 0) //data.Length up to 96 (16 motors x 6 byte data) --> new datastruct in FC -> not standard!
732
            {
733
                bool bAvailable = false;
734
                for (int i = 0; i < data.Length && data[i] < 8; i += 6) // data[i] < 8 --> at moment there are 8 display fields for motors
735
                {
736
 
737
                    if ((data[i + 4] & 128) == 128) //Status bit at pos 7 = 128 dec -- if true, motor is available
738
                        bAvailable = true;
739
                    else
740
                        bAvailable = false;
741
 
742
                    if (data[i] < 4)
743
                    {
744
                        if (bAvailable)
745
                        {
746
                            dtMotors1.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
747
                            dtMotors1.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C"));
748
                        }
749
                        else
750
                        {
751
                            dtMotors1.Rows[data[i]].SetField(1, "NA");
752
                            dtMotors1.Rows[data[i]].SetField(2, "NA");
753
                        }
754
                    }
755
                    if (data[i] > 3 && data[i] < 8)
756
                    {
757
                        if (bAvailable)
758
                        {
759
                            dtMotors2.Rows[data[i] - 4].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
760
                            dtMotors2.Rows[data[i] - 4].SetField(2, data[i + 2].ToString("0 °C"));
761
                        }
762
                        else
763
                        {
764
                            dtMotors2.Rows[data[i] - 4].SetField(1, "NA");
765
                            dtMotors2.Rows[data[i] - 4].SetField(2, "NA");
766
                        }
767
                    }
768
                }
769
            }
770
 
771
        }
772
        /// <summary>
773
        /// Navi-Ctrl data 'O'
774
        /// GPS-Position, capacatiy, flying time...
775
        /// </summary>
776
        /// <param name="data">the received byte array to process</param>
777
        void _processNCData(byte[] data)
778
        {
779
            int i_32, i_16, iVal;
780
            double d;
781
            i_32 = data[4];
782
            iVal = i_32 << 24;
783
            i_32 = data[3];
784
            iVal += i_32 << 16;
785
            i_32 = data[2];
786
            iVal += i_32 << 8;
787
            iVal += data[1];
788
            d = (double)iVal / Math.Pow(10, 7);
789
            lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = d.ToString("0.######°"))); //GPS-Position: Longitude in decimal degree
790
            //lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = _convertDegree(d))); //GPS-Position: Longitude in minutes, seconds
791
 
792
            i_32 = data[8];
793
            iVal = i_32 << 24;
794
            i_32 = data[7];
795
            iVal += i_32 << 16;
796
            i_32 = data[6];
797
            iVal += i_32 << 8;
798
            iVal += data[5];
799
            d = (double)iVal / Math.Pow(10, 7);
800
            lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = d.ToString("0.######°"))); //GPS-Position: Latitude in decimal degree
2254 - 801
                                                                                            //lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = _convertDegree(d))); //GPS-Position: Latitude in minutes, seconds
2250 - 802
 
2254 - 803
            i_16 = data[28];
804
            i_16 = (Int16)(i_16 << 8);
805
            iVal = data[27] + i_16;
806
            lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP
807
 
808
            i_16 = data[45];
809
            i_16 = (Int16)(i_16 << 8);
810
            iVal = data[44] + i_16;
811
            lblNCDistHP1.Invoke((Action)(() => lblNCDistHP1.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP
812
 
813
            lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = data[48].ToString())); //Waypoint index
814
            lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = data[49].ToString())); //Waypoints count
815
 
2250 - 816
            i_16 = data[81];
817
            i_16 = (Int16)(i_16 << 8);
818
            iVal = data[80] + i_16;
819
            lblNCCap.Invoke((Action)(() => lblNCCap.Text = iVal.ToString() + " mAh")); //Capacity used
820
 
821
            i_16 = data[56];
822
            i_16 = (Int16)(i_16 << 8);
823
            iVal = data[55] + i_16;
824
            TimeSpan t = TimeSpan.FromSeconds(iVal);
825
            string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2");
826
            lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time
827
 
828
            lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality
829
            lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString()));   //NC Errornumber
830
            //if (data[69] > 0)
831
            //    _readNCError();
832
            //break;
833
            if (data[69] > 0 & data[69] < 44)
834
                ErrorLog(LogMsgType.Error, "NC Error [" + data[69].ToString() + "]: " + NC_Error[data[69]]);
2254 - 835
            else
836
                if(_bErrorLog) _clearErrorLog("NC Error");
2250 - 837
 
838
        }
839
        /// <summary>
2257 - 840
        /// Navi-Ctrl WP data struct 'X'
841
        /// called by index
842
        /// </summary>
843
        /// <param name="data">the received byte array to process</param>
844
        void _processWPData(byte[] data)
845
        {
846
            if (data.Length >= 28)
847
            {
848
                int count = data[0];
849
                int index = data[1];
850
                cbWPIndex.Invoke((Action)(() => cbWPIndex.Items.Clear()));
851
                for (int i = 0; i < count; i++)
852
                    cbWPIndex.Invoke((Action)(() => cbWPIndex.Items.Add(i + 1)));
853
                cbWPIndex.Invoke((Action)(() => cbWPIndex.SelectedItem = index));
854
                DataRow dr = dtWaypoints.NewRow();
855
                dr = Waypoints.toDataRow(data, dr);
856
                dtWaypoints.Rows.Add(dr);
857
                dgvWP.Invoke((Action)(() => dgvWP.Update()));
858
            }
859
            else
860
                Debug.Print(new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length)));
861
        }
862
        /// <summary>
2250 - 863
        /// OSD Menue 'L'
864
        /// single page called by pagenumber
865
        /// no autoupdate
866
        /// </summary>
867
        /// <param name="data">the received byte array to process</param>
868
        void _processOSDSingle(byte[] data)
869
        {
870
            if (data.Length == 84)
871
            {
872
                string sMessage = "";
873
                iOSDPage = data[0];
874
                iOSDMax = data[1];
875
                if (cbOSD.Items.Count != iOSDMax) _initOSDCB();
876
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4));
877
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20));
878
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20));
879
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20));
880
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20));
881
                lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]")));
882
 
883
            }
884
            else
885
                OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)");
886
 
887
        }
888
        /// <summary>
889
        /// OSD Menue 'H'
890
        /// called by keys (0x01,0x02,0x03,0x04)
891
        /// autoupdate
892
        /// </summary>
893
        /// <param name="data">the received byte array to process</param>
894
        void _processOSDAuto(byte[] data)
895
        {
896
            if (data.Length == 81)
897
            {
898
                string sMessage = "";
899
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1));
900
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20));
901
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20));
902
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20));
903
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20));
904
 
905
            }
906
            else
907
                OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)");
908
        }
909
        #endregion processing received data
910
 
2233 - 911
        /// <summary> send message to controller to request data
912
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
913
        /// </summary>
914
        /// <param name="CMDID"> the command ID </param>
915
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
916
        private void _sendControllerMessage(char CMDID, byte address)
917
        {
918
            if (simpleSerialPort.Port.IsOpen)
919
            {
920
                Stream serialStream = simpleSerialPort.Port.BaseStream;
921
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address);
922
                serialStream.Write(bytes, 0, bytes.Length);
923
 
924
            }
925
            else
926
                Log(LogMsgType.Error, "NOT CONNECTED!");
927
        }
928
        /// <summary> send message to controller to request data
929
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
930
        /// </summary>
931
        /// <param name="CMDID"> the command ID </param>
932
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
933
        /// <param name="data"> additional data for the request</param>
934
        private void _sendControllerMessage(char CMDID, byte address, byte[]data)
935
        {
936
            if (simpleSerialPort.Port.IsOpen)
937
            {
938
                Stream serialStream = simpleSerialPort.Port.BaseStream;
939
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address,data);
940
                serialStream.Write(bytes, 0, bytes.Length);
941
 
942
            }
943
            else
944
                Log(LogMsgType.Error, "NOT CONNECTED!");
945
        }
946
 
947
        /// <summary>
948
        /// read the analog-label names for the actual controller
949
        /// and load it into listbox
950
        /// </summary>
951
        void _loadLabelNames()
952
        {
953
            if (_iCtrlAct > 0 && _iCtrlAct < 3)
954
            {
955
                switch (_iCtrlAct)
956
                {
957
                    case 1:
958
                        sAnalogLabel = Properties.Resources.FCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
959
                        break;
960
                    case 2:
961
                        sAnalogLabel = Properties.Resources.NCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
962
                        break;
963
                }
964
                for (int i = 0; i < 32; i++)
965
                {
966
                    if (dtAnalog.Rows.Count < 32)
967
                        dtAnalog.Rows.Add(sAnalogLabel[i], "");
968
                    else
969
                        dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]);
970
                }
971
                dataGridView1.Invoke((Action)(()=>dataGridView1.Refresh()));
972
            }
973
        }
974
        /// <summary>
975
        /// no longer used...
976
        /// read the analog-label textfile for the actual controller
977
        /// </summary>
978
         private void _loadLabelFile()
979
        {
980
            switch (_iCtrlAct)
981
            {
982
                case 1:
983
                    fileName = "FCLabelTexts.txt";
984
                    break;
985
                case 2:
986
                    fileName = "NCLabelTexts.txt";
987
                    break;
988
                //default:
989
                //    fileName = "NCLabelTexts.txt";
990
                //    break;
991
            }
992
 
993
            if (File.Exists(filePath + "\\" + fileName))
994
            {
995
                sAnalogLabel.Initialize();
996
                sAnalogLabel = File.ReadAllLines(filePath + "\\" + fileName);
997
                lbLabels.Invoke((Action)(() => lbLabels.Items.Clear()));
998
                lbLabels.Invoke((Action)(() => lbLabels.Update()));
999
                lbLabels.Invoke((Action)(() => lbLabels.Items.AddRange(sAnalogLabel)));
1000
                Console.WriteLine("Names loaded from file");
1001
                lblFileName.Invoke((Action)(() => lblFileName.Text = fileName));
1002
            }
1003
            else
1004
            {
1005
                _readCont(false);
1006
                Log(LogMsgType.Error, "Label-file not found!");
1007
                Log(LogMsgType.Error, "Please go to settings-tab and load the label texts from the copter control (FC & NC)");
1008
                Log(LogMsgType.Error, "When done, you have to save the label texts with the 'save' button!");
1009
            }
1010
        }
1011
        /// <summary>
1012
        /// no longer used...
1013
        /// assign the analog-label names from the textfile to the datatable
1014
        /// 
1015
        /// </summary>
1016
        private void _assignLabelNames()
1017
        {
1018
            if (lbLabels.Items.Count == 32)
1019
            {
1020
                lbLabels.Items.CopyTo(sAnalogLabel, 0);
1021
                for (int i = 0; i < 32; i++)
1022
                {
1023
                    if (dtAnalog.Rows.Count < 32)
1024
                        dtAnalog.Rows.Add(sAnalogLabel[i], "");
1025
                    else
1026
                        dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]);
1027
 
1028
                }
1029
            }
1030
        }
1031
        /// <summary>
1032
        /// get the version struct of actual controller
1033
        /// </summary>
1034
        /// <summary>
1035
        /// get the labeltexts for the analog values
1036
        /// </summary>
1037
        private void _getAnalogLabels()
1038
        {
1039
            if (simpleSerialPort.Port.IsOpen)
1040
            {
1041
                iLableIndex = 0;
1042
                for (int i = 0; i < 32; i++)
1043
                {
1044
                    Stream serialStream = simpleSerialPort.Port.BaseStream;
1045
                    byte[] bytes = FlightControllerMessage.CreateMessage('a', 0, new byte[1] { (byte)i });
1046
                    serialStream.Write(bytes, 0, bytes.Length);
1047
                    Thread.Sleep(10);
1048
                }
1049
            }
1050
            else
1051
                Log(LogMsgType.Error, "NOT CONNECTED!");
1052
        }
1053
        /// <summary>
1054
        /// get the labeltext for a single label
1055
        /// </summary>
1056
        /// <param name="iIndex">index of the label</param>
1057
        private void _getAnalogLabels(int iIndex)
1058
        {
1059
            if (simpleSerialPort.Port.IsOpen)
1060
            {
1061
                if (iIndex < 32)
1062
                {
1063
                    iLableIndex = iIndex;
1064
                    _sendControllerMessage('a', 0, new byte[1] { (byte)iLableIndex });
1065
                }
1066
            }
1067
            else
1068
                Log(LogMsgType.Error, "NOT CONNECTED!");
1069
        }
1070
        private void _getVersion()
1071
        {
1072
            _sendControllerMessage('v', 0);
1073
        }
1074
        /// <summary>
1075
        /// get FC version struct via NC
1076
        /// by sending '1' as data (not documented in wiki...)
1077
        /// returns HW error 255 (comment in uart1.c : tells the KopterTool that it is the FC-version)
1078
        /// </summary>
1079
        /// <param name="ctrl">controller number 1=FC</param> 
1080
        private void _getVersion(byte ctrl)
1081
        {
1082
            _sendControllerMessage('v', 0, new byte[1] {ctrl});
1083
        }
1084
        /// <summary>
1085
        /// Switch back to NC by sending the 'Magic Packet' 0x1B,0x1B,0x55,0xAA,0x00
1086
        /// </summary>
1087
        private void _switchToNC()
1088
        {
1089
            if (simpleSerialPort.Port.IsOpen)
1090
            {
1091
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1092
                byte[] bytes = new byte[5] { 0x1B,0x1B,0x55,0xAA,0x00 };
1093
                serialStream.Write(bytes, 0, bytes.Length);
1094
 
1095
                Thread.Sleep(100);
1096
                _getVersion();
1097
                Thread.Sleep(100);
1098
                _OSDMenue(0);
1099
            }
1100
            else
1101
                Log(LogMsgType.Error, "NOT CONNECTED!");
1102
        }
1103
        /// <summary>
1104
        /// switch to FC
1105
        /// </summary>
1106
        private void _switchToFC()
1107
        {
1108
            _sendControllerMessage('u', 2, new byte[1] { (byte)0 });
1109
            Thread.Sleep(100);
1110
            _getVersion();
1111
            Thread.Sleep(100);
1112
            _OSDMenue(0);
1113
        }
1114
        /// <summary>
1115
        /// send RESET signal to FC
1116
        /// </summary>
1117
        private void _resetCtrl()
1118
        {
1119
            _sendControllerMessage('R', 1);
1120
        }
1121
        /// <summary>
1122
        /// poll the debug data (4sec subscription)
1123
        /// </summary>
1124
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1125
        private void _readDebugData(bool auto)
1126
        {
1127
            byte interval = auto ? debugInterval : (byte)0;
1128
            _sendControllerMessage('d', 0, new byte[1] { debugInterval });
1129
        }
1130
        /// <summary>
1131
        /// poll the BL-CTRL status via NC (4sec subscription)
1132
        /// </summary>
1133
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1134
        private void _readBLCtrl(bool auto)
1135
        {
1136
            byte interval = auto ? blctrlInterval : (byte)0;
1137
            _sendControllerMessage('k', 0, new byte[1] { interval });
1138
        }
1139
        /// <summary>
1140
        /// poll the NC data struct (4sec subscription)
1141
        /// </summary>
1142
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1143
        private void _readNavData(bool auto)
1144
        {
1145
            byte interval = auto ? navctrlInterval : (byte)0;
1146
            _sendControllerMessage('o', 2, new byte[1] { interval });
1147
        }
1148
        /// <summary>
1149
        /// get the errortext for pending NC error
1150
        /// </summary>
1151
        private void _readNCError()
1152
        {
1153
            _sendControllerMessage('e', 2);
1154
        }
1155
        /// <summary>
1156
        /// start/stop continous polling of controller values
1157
        /// </summary>
1158
        /// <param name="b">start/stop switch</param>
1159
        void _readCont(bool b)
1160
        {
1161
            bReadContinously = b;
1162
            btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.Text = bReadContinously ? "stop automatic" + Environment.NewLine + "data refresh" : "start automatic" + Environment.NewLine + "data refresh"));
1163
            btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.BackColor = bReadContinously ? Color.FromArgb(192, 255, 192) : Color.FromArgb(224, 224, 224)));
1164
            if (bReadContinously)
1165
            {
2250 - 1166
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
1167
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
1168
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
1169
                if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10);}
2233 - 1170
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0)));
1171
            }
1172
            else
1173
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(224, 224, 224)));
1174
            _iLifeCounter = 0;
1175
        }
1176
        /// <summary>
2250 - 1177
        /// set values to "NA" when not available with FC
2233 - 1178
        /// </summary>
1179
        void _setFieldsNA()
1180
        {
1181
            Thread.Sleep(100);
2250 - 1182
            _initDTMotors();
2233 - 1183
            lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = "NA"));    //FlightTime
2250 - 1184
            lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA"));  //NC ErrorNr
1185
            lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA"));            //earth magnet field
1186
            lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA"));    //GroundSpeed
1187
            lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = "NA"));    //Distance to HP
1188
            lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA"));          //Sats used
1189
            lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = "NA"));  //GPS position - longitude
1190
            lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = "NA"));    //GPS position - latitude
2254 - 1191
            lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = "NA"));    //next WP distance
1192
            lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = "NA"));  //index of actual WP
1193
            lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = "NA"));  //count of items in WP list
2233 - 1194
        }
1195
        /// <summary>
1196
        /// one time query of the OSD Menue with pagenumber
1197
        /// </summary>
1198
        /// <param name="iMenue">Menue page</param>
1199
        void _OSDMenue(int iMenue)
1200
        {
1201
            if (simpleSerialPort.Port.IsOpen)
1202
            {
1203
                if (iMenue > iOSDMax)
1204
                    iMenue = 0;
1205
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1206
                byte[] bytes = FlightControllerMessage.CreateMessage('l', 0, new byte[1] { (byte)iMenue });
1207
                serialStream.Write(bytes, 0, bytes.Length);
1208
            }
1209
            else
1210
                Log(LogMsgType.Error, "NOT CONNECTED!");
1211
 
1212
        }
1213
        /// <summary>
1214
        /// call the OSDMenue and start autorefresh
1215
        ///  usually by sending a menuekey
1216
        /// a bit tricky - but by sending inverted value of 32 (32 = 0010 0000) you can start the OSD menue with autoupdate (abo) without switching the page with the keyvalues (0x1, 0x2)
1217
        /// therefore the value has to be negative (inverted) in order to distinguish from old (2 line) menuestyle
1218
        /// and must not have any bits of the menue keys 0x1 0x2 0x4 0x8 (0x10?) --> 0x20 = -33
1219
        /// </summary>
1220
        void _OSDMenueAutoRefresh()
1221
        {
1222
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)(-33)),OSDInterval });
1223
        }
1224
        void _OSDMenueAutoRefresh(byte key)
1225
        {
1226
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)~key), OSDInterval });
1227
        }
1228
        /// <summary>
1229
        /// initialize the OSD menue combobox
1230
        /// combox is filled by numbers from 0 to max pagenumber
1231
        /// </summary>
1232
        void _initOSDCB()
1233
        {
1234
            _bCBInit = true;
1235
            if(iOSDMax == 0)
1236
            {
1237
                _OSDMenue(0);
1238
                Thread.Sleep(10);
1239
            }
1240
            cbOSD.Invoke((Action)(()=>cbOSD.Items.Clear()));
1241
            for(int i = 0; i <= iOSDMax;i++)
1242
            {
1243
                cbOSD.Invoke((Action)(() => cbOSD.Items.Add(i)));
1244
            }
1245
            cbOSD.Invoke((Action)(() => cbOSD.SelectedItem = iOSDPage));
1246
            _bCBInit = false;
1247
        }
2259 - 1248
        /// <summary>
1249
        /// initialize the datatables
1250
        /// with columnnames etc
1251
        /// </summary>
1252
        void _dataTablesInit()
1253
        {
1254
            dtAnalog.Columns.Add("ID");
1255
            dtAnalog.Columns.Add("Value");
1256
            dataGridView1.DataSource = dtAnalog;
1257
 
1258
            dtMotors1.Columns.Add("#");
1259
            dtMotors1.Columns.Add("Current");
1260
            dtMotors1.Columns.Add("Temp");
1261
            dtMotors2.Columns.Add("#");
1262
            dtMotors2.Columns.Add("Current");
1263
            dtMotors2.Columns.Add("Temp");
1264
            dgvMotors1.DataSource = dtMotors1;
1265
            dgvMotors2.DataSource = dtMotors2;
1266
            _initDTMotors();
1267
            dgvMotors1.Columns[0].Width = 24;
1268
            dgvMotors1.Columns[1].Width = 74;
1269
            dgvMotors1.Columns[2].Width = 74;
1270
            dgvMotors2.Columns[0].Width = 24;
1271
            dgvMotors2.Columns[1].Width = 74;
1272
            dgvMotors2.Columns[2].Width = 74;
1273
 
1274
            dtWaypoints.Columns.Add("Index");
1275
            dtWaypoints.Columns.Add("Type");
1276
            dtWaypoints.Columns.Add("Name");
1277
            dtWaypoints.Columns.Add("Latitude");
1278
            dtWaypoints.Columns.Add("Longitude");
1279
            dtWaypoints.Columns.Add("Altitude");
1280
            dtWaypoints.Columns.Add("Heading");
1281
            dtWaypoints.Columns.Add("Speed");
1282
            dtWaypoints.Columns.Add("Altitude rate");
1283
            dtWaypoints.Columns.Add("Tol.radius");
1284
            dtWaypoints.Columns.Add("Hold time");
1285
            dtWaypoints.Columns.Add("AutoTrigger");
1286
            dtWaypoints.Columns.Add("Cam angle");
1287
            dtWaypoints.Columns.Add("Event");
1288
            dtWaypoints.Columns.Add("Eventchan. Val.");
1289
            dtWaypoints.Columns.Add("Status");
1290
            dgvWP.DataSource = dtWaypoints;
1291
        }
1292
        /// <summary>
1293
        /// read settings from ini-file
1294
        /// </summary>
2233 - 1295
        void _readIni()
1296
        {
1297
            if (!File.Exists(filePath + "\\MKLiveViewSettings.ini"))
1298
                _writeIni();
1299
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
1300
            ini.path = filePath + "\\MKLiveViewSettings.ini";
1301
 
1302
            string sVal = ini.IniReadValue("default", "AutorefreshDebugData");
1303
            _debugDataAutorefresh = Convert.ToBoolean(sVal);
1304
            sVal = ini.IniReadValue("default", "AutorefreshNavCtrlData");
1305
            _navCtrlDataAutorefresh = Convert.ToBoolean(sVal);
1306
            sVal = ini.IniReadValue("default", "AutorefreshBLCtrlData");
1307
            _blctrlDataAutorefresh = Convert.ToBoolean(sVal);
1308
            sVal = ini.IniReadValue("default", "AutorefreshOSDData");
1309
            _OSDAutorefresh = Convert.ToBoolean(sVal);
1310
 
1311
            sVal = ini.IniReadValue("default", "IntervalDebugData");
1312
            debugInterval = (byte)Convert.ToInt16(sVal);
1313
            sVal = ini.IniReadValue("default", "IntervalNavCtrlData");
1314
            navctrlInterval = (byte)Convert.ToInt16(sVal);
1315
            sVal = ini.IniReadValue("default", "IntervalBLCtrlData");
1316
            blctrlInterval = (byte)Convert.ToInt16(sVal);
1317
            sVal = ini.IniReadValue("default", "IntervalOSDData");
1318
            OSDInterval = (byte)Convert.ToInt16(sVal);
2257 - 1319
            for(int i = 0; i < 12; i++)
1320
            {
1321
                sVal = ini.IniReadValue("serial", "ch" + i.ToString() + "Val");
1322
                if(sVal != "")
1323
                    serChan[i] = Convert.ToInt16(sVal);
1324
                sVal = ini.IniReadValue("serial", "ch" + i.ToString() + "Title");
1325
                if(sVal != "")
1326
                    serChanTitle[i] = sVal;
1327
            }
2233 - 1328
        }
2259 - 1329
        /// <summary>
1330
        /// save settings to ini-file
1331
        /// </summary>
2233 - 1332
        void _writeIni()
1333
        {
1334
 
1335
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
1336
            ini.path = filePath + "\\MKLiveViewSettings.ini";
1337
 
1338
            ini.IniWriteValue("default", "AutorefreshDebugData", _debugDataAutorefresh ? "true":"false");
1339
            ini.IniWriteValue("default", "AutorefreshNavCtrlData", _navCtrlDataAutorefresh ? "true":"false");
1340
            ini.IniWriteValue("default", "AutorefreshBLCtrlData", _blctrlDataAutorefresh ? "true":"false");
1341
            ini.IniWriteValue("default", "AutorefreshOSDData", _OSDAutorefresh ? "true":"false");
1342
 
1343
            ini.IniWriteValue("default", "IntervalDebugData", debugInterval.ToString());
1344
            ini.IniWriteValue("default", "IntervalNavCtrlData", navctrlInterval.ToString());
1345
            ini.IniWriteValue("default", "IntervalBLCtrlData", blctrlInterval.ToString());
1346
            ini.IniWriteValue("default", "IntervalOSDData", OSDInterval.ToString());
2257 - 1347
 
1348
            for (int i = 0; i < 12; i++)
1349
            {
1350
                ini.IniWriteValue("serial", "ch" + i.ToString() + "Val", serChan[i].ToString());
1351
                ini.IniWriteValue("serial", "ch" + i.ToString() + "Title", serChanTitle[i]);
1352
            }
1353
 
2233 - 1354
        }
1355
 
2250 - 1356
        /// <summary>
1357
        /// initialize the 2 datatables for motor values
1358
        /// dtMotors1 - motor 1 - 4
1359
        /// dtMotors2 - motor 5 - 8
1360
        /// DataGridView dgvMotors1/2 are bound to dtMotors1/2 
1361
        /// </summary>
1362
        void _initDTMotors()
1363
        {
1364
            for(int i = 0; i < 4; i++)
1365
            {
1366
                if (dtMotors1.Rows.Count < 4)
1367
                    dtMotors1.Rows.Add((i + 1).ToString(), "NA", "NA");
1368
                else
1369
                {
1370
                    dtMotors1.Rows[i].SetField(1, "NA");
1371
                    dtMotors1.Rows[i].SetField(2, "NA");
1372
                }
1373
                if (dtMotors2.Rows.Count < 4)
1374
                    dtMotors2.Rows.Add((i + 5).ToString(), "NA", "NA");
1375
                else
1376
                {
1377
                    dtMotors2.Rows[i].SetField(1, "NA");
1378
                    dtMotors2.Rows[i].SetField(2, "NA");
1379
                }
1380
            }
1381
            dgvMotors1.Invoke((Action)(() => dgvMotors1.Refresh()));
1382
            dgvMotors2.Invoke((Action)(() => dgvMotors2.Refresh()));
1383
        }
1384
 
1385
        /// <summary>
1386
        /// Convert decimal degrees to degrees, minutes, seconds, milliseconds 
1387
        /// </summary>
1388
        /// <param name="coord">the degree value as double</param>
1389
        /// <returns>0° 0' 0,0"</returns>
1390
        string _convertDegree(double coord)
1391
        {
1392
            //double minutes = (degree - Math.Floor(degree)) * 60.0;
1393
            //double seconds = (minutes - Math.Floor(minutes)) * 60.0;
1394
            //double tenths = (seconds - Math.Floor(seconds)) * 10.0;
1395
            //// get rid of fractional part
1396
            //minutes = Math.Floor(minutes);
1397
            //seconds = Math.Floor(seconds);
1398
            //tenths = Math.Floor(tenths);
1399
 
1400
 
1401
            //int sec = (int)Math.Round(coord * 3600);
1402
            //int deg = sec / 3600;
1403
            //sec = Math.Abs(sec % 3600);
1404
            //int min = sec / 60;
1405
            //sec %= 60;
1406
 
1407
            var ts = TimeSpan.FromHours(Math.Abs(coord));
1408
            double deg = Math.Sign(coord) * Math.Floor(ts.TotalHours);
1409
            int min = ts.Minutes;
1410
            int sec = ts.Seconds;
1411
            int milli = ts.Milliseconds;
1412
 
1413
            return deg.ToString("0° ") + min.ToString("0") + "' " + sec.ToString("0") + "," + milli.ToString() + "\"";
1414
        }
2257 - 1415
        /// <summary>
1416
        /// Clear the line in the  errorlog window 
1417
        /// containing the error string when error has ceased
1418
        /// </summary>
1419
        /// <param name="s">substring of errrormessage</param>
2254 - 1420
        void _clearErrorLog(string s)
1421
        {
1422
            rtfError.Invoke((Action)(() =>
1423
            {
1424
                if (rtfError.Text.Contains(s))
1425
                {
1426
                    int iLength = 0;
1427
                    int iStart = rtfError.Text.IndexOf(s);
1428
                    int iEnd = rtfError.Text.IndexOf('\n', iStart);
1429
                    if (iEnd > 0)
1430
                    {
1431
                        iLength = iEnd + 1;
1432
                        int iHttp = rtfError.Text.IndexOf("http", iEnd);
1433
                        if (iHttp == iLength)
1434
                        {
1435
                            int iEnd2 = rtfError.Text.IndexOf('\n', iLength);
1436
                            if (iEnd2 > iLength)
1437
                            {
1438
                                iLength = iEnd2 + 1;
1439
                                rtfError.Select(iStart, iLength);
1440
                                rtfError.SelectedText = string.Empty;
1441
                                if(rtfError.Text.Length < 2) _bErrorLog = false;
1442
                            }
1443
 
1444
                        }
1445
                        else
1446
                        {
1447
                            rtfError.Select(iStart, iLength);
1448
                            rtfError.SelectedText = string.Empty;
1449
                            if(rtfError.Text.Length < 2) _bErrorLog = false;
1450
                        }
1451
                    }
1452
                }
1453
            }));
1454
 
1455
        }
2257 - 1456
        /// <summary>
1457
        /// request the Waypoint at index
1458
        /// </summary>
1459
        /// <param name="index"></param>
1460
        void _getpWP(int index)
1461
        {
1462
            if (simpleSerialPort.Port.IsOpen)
1463
            {
1464
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1465
                byte[] bytes = FlightControllerMessage.CreateMessage('x', 2, new byte[1] { (byte)index });
1466
                serialStream.Write(bytes, 0, bytes.Length);
1467
            }
1468
            else
1469
                Log(LogMsgType.Error, "NOT CONNECTED!");
1470
 
1471
        }
1472
        /// <summary>
1473
        /// Sending the serial channel values
1474
        /// </summary>
1475
        void _sendSerialData()
1476
        {
1477
            byte[] serData = new byte[12];
1478
            for(int i = 0; i < 12; i++)
1479
            {
1480
                serData[i] = unchecked((byte)(serChan[i] - 127));
1481
            }
1482
            _sendControllerMessage('y', 1, serData);
1483
        }
2259 - 1484
        /// <summary>
1485
        /// init the controls for displaying
1486
        /// and setting serial control channels
1487
        /// </summary>
2257 - 1488
        void _initSerialCtrl()
1489
        {
1490
            trckbarSerial1.Value = serChan[0];
1491
            textBoxSerial1.Text = serChanTitle[0];
1492
            lblTbSerial1.Text = serChan[0].ToString();
1493
            trckbarSerial2.Value = serChan[1];
1494
            textBoxSerial2.Text = serChanTitle[1];
1495
            lblTbSerial2.Text = serChan[1].ToString();
2259 - 1496
            trckbarSerial3.Value = serChan[2];
1497
            textBoxSerial3.Text = serChanTitle[2];
1498
            lblTbSerial3.Text = serChan[2].ToString();
1499
            trckbarSerial4.Value = serChan[3];
1500
            textBoxSerial4.Text = serChanTitle[3];
1501
            lblTbSerial4.Text = serChan[3].ToString();
2265 - 1502
            trckbarSerial5.Value = serChan[4];
1503
            textBoxSerial5.Text = serChanTitle[4];
1504
            lblTbSerial5.Text = serChan[4].ToString();
1505
            trckbarSerial6.Value = serChan[5];
1506
            textBoxSerial6.Text = serChanTitle[5];
1507
            lblTbSerial6.Text = serChan[5].ToString();
1508
            trckbarSerial7.Value = serChan[6];
1509
            textBoxSerial7.Text = serChanTitle[6];
1510
            lblTbSerial7.Text = serChan[6].ToString();
1511
            trckbarSerial8.Value = serChan[7];
1512
            textBoxSerial8.Text = serChanTitle[7];
1513
            lblTbSerial8.Text = serChan[7].ToString();
2257 - 1514
        }
2233 - 1515
        #endregion functions
1516
 
1517
        #region buttons
1518
        private void buttonReset_Click(object sender, EventArgs e)
1519
        {
1520
            _resetCtrl();
1521
        }
1522
        private void btnVersion_Click(object sender, EventArgs e)
1523
        {
1524
            _getVersion();
1525
        }
1526
        private void btnAnalogLabels_Click(object sender, EventArgs e)
1527
        {
1528
            _getAnalogLabels(0);
1529
        }
1530
        private void btnDbgData_Click(object sender, EventArgs e)
1531
        {
1532
            _readDebugData(false); //onetime reading of debug data --> subscription lasts 4sec - this means you will receive data for 4 seconds
1533
        }
1534
        private void btnSaveLabels_Click(object sender, EventArgs e)
1535
        {
1536
            switch (_iCtrlAct)
1537
            {
1538
                case 1:
1539
                    fileName = "FCLabelTexts.txt";
1540
                    break;
1541
                case 2:
1542
                    fileName = "NCLabelTexts.txt";
1543
                    break;
1544
                default:
1545
                    fileName = "NCLabelTexts.txt";
1546
                    break;
1547
            }
1548
            if (sAnalogLabel[0] != null)
1549
            {
1550
                File.WriteAllLines(filePath + "\\" + fileName, sAnalogLabel);
1551
                Console.WriteLine("Names saved to file");
1552
                _loadLabelFile();
1553
            }
1554
            else
1555
                Log(LogMsgType.Warning, "there's no data -> read first from fc/nc!");
1556
        }
1557
        private void btnLoadLabels_Click(object sender, EventArgs e)
1558
        {
1559
            _assignLabelNames();
1560
        }
1561
        private void btnReadLabelFile_Click(object sender, EventArgs e)
1562
        {
1563
            _loadLabelFile();
1564
        }
1565
        private void btnSwitchFC_Click(object sender, EventArgs e)
1566
        {
1567
            _switchToFC();
1568
        }
1569
        private void btnSwitchNC_Click(object sender, EventArgs e)
1570
        {
1571
            _switchToNC();
1572
        }
1573
        private void btnReadDbgCont_Click(object sender, EventArgs e)
1574
        {
1575
            _readCont(!bReadContinously);
1576
        }
1577
        private void btnReadBLCtrl_Click(object sender, EventArgs e)
1578
        {
1579
 
1580
            if (_iCtrlAct == 2) _readBLCtrl(false);
1581
            else Log(LogMsgType.Warning, "only available when connected to NC");
1582
        }
1583
        private void btnGetNaviData_Click(object sender, EventArgs e)
1584
        {
1585
            if (_iCtrlAct == 2) _readNavData(false);
1586
            else Log(LogMsgType.Warning, "only available when connected to NC");
1587
        }
1588
        private void btnConn_Click(object sender, EventArgs e)
1589
        {
1590
            simpleSerialPort.Connect(!simpleSerialPort.Port.IsOpen);
1591
        }
1592
        private void button3_Click(object sender, EventArgs e)
1593
        {
1594
            _getVersion(1);
1595
        }
1596
        private void button4_Click(object sender, EventArgs e)
1597
        {
1598
            _getVersion(2);
1599
        }
1600
        private void btnOSD_Click(object sender, EventArgs e)
1601
        {
1602
            if (iOSDPage > iOSDMax)
1603
                iOSDPage = 0;
1604
            _OSDMenue(iOSDPage);
1605
        }
1606
        private void btnOSDForward_Click(object sender, EventArgs e)
1607
        {
1608
            iOSDPage++;
1609
            if (iOSDPage > iOSDMax)
1610
                iOSDPage = 0;
1611
 
1612
            _OSDMenue(iOSDPage);
1613
        }
1614
        private void btnOSDBackward_Click(object sender, EventArgs e)
1615
        {
1616
            iOSDPage--;
1617
            if (iOSDPage < 0)
1618
                iOSDPage = iOSDMax;
1619
 
1620
            _OSDMenue(iOSDPage);
1621
        }
1622
        private void btnOSDAuto_Click(object sender, EventArgs e)
1623
        {
1624
            _OSDMenueAutoRefresh();
1625
        }
1626
        /// call the OSDMenue with Key 0x8
1627
        private void btnOSDLeave_Click(object sender, EventArgs e)
1628
        {
1629
            _OSDMenueAutoRefresh(8);
1630
        }
1631
        /// call the OSDMenue with Key 0x4
1632
        private void btnOSDEnter_Click(object sender, EventArgs e)
1633
        {
1634
            _OSDMenueAutoRefresh(4);
1635
        }
2257 - 1636
        private void btnGetWP_Click(object sender, EventArgs e)
1637
        {
1638
            if(cbWPIndex.Items.Count >0)
1639
            _getpWP((int)cbWPIndex.SelectedItem);
1640
            else
1641
                _getpWP(1);
1642
        }
2233 - 1643
        #endregion buttons
2259 - 1644
        #region serial control channels - buttons & events
2257 - 1645
        private void tbSerial1_Scroll(object sender, EventArgs e)
1646
        {
1647
            lblTbSerial1.Text = trckbarSerial1.Value.ToString();
1648
            serChan[0] = trckbarSerial1.Value;
1649
            if (!_init) _sendSerialData();
1650
        }
1651
        private void textBoxSerial1_TextChanged(object sender, EventArgs e)
1652
        {
1653
            serChanTitle[0] = textBoxSerial1.Text;
1654
        }
1655
        private void btnSer1_0_Click(object sender, EventArgs e)
1656
        {
1657
            trckbarSerial1.Value = 0;
1658
        }
1659
        private void btnSer1_127_Click(object sender, EventArgs e)
1660
        {
1661
            trckbarSerial1.Value = 127;
1662
        }
1663
        private void btnSer1_254_Click(object sender, EventArgs e)
1664
        {
1665
            trckbarSerial1.Value = 254;
1666
        }
1667
        private void trckbarSerial1_ValueChanged(object sender, EventArgs e)
1668
        {
1669
            lblTbSerial1.Text = trckbarSerial1.Value.ToString();
1670
            serChan[0] = trckbarSerial1.Value;
1671
            if (!_init) _sendSerialData();
1672
        }
1673
        private void textBoxSerial2_TextChanged(object sender, EventArgs e)
1674
        {
2259 - 1675
            serChanTitle[1] = textBoxSerial2.Text;
2257 - 1676
        }
1677
        private void trckbarSerial2_ValueChanged(object sender, EventArgs e)
1678
        {
1679
            lblTbSerial2.Text = trckbarSerial2.Value.ToString();
1680
            serChan[1] = trckbarSerial2.Value;
1681
            if (!_init) _sendSerialData();
1682
        }
1683
        private void btnSer2_0_Click(object sender, EventArgs e)
1684
        {
1685
            trckbarSerial2.Value = 0;
1686
        }
1687
        private void btnSer2_127_Click(object sender, EventArgs e)
1688
        {
1689
            trckbarSerial2.Value = 127;
1690
        }
1691
        private void btnSer2_254_Click(object sender, EventArgs e)
1692
        {
1693
            trckbarSerial2.Value = 254;
1694
        }
2259 - 1695
        private void textBoxSerial3_TextChanged(object sender, EventArgs e)
1696
        {
1697
            serChanTitle[2] = textBoxSerial3.Text;
1698
        }
1699
        private void trckbarSerial3_ValueChanged(object sender, EventArgs e)
1700
        {
1701
            lblTbSerial3.Text = trckbarSerial3.Value.ToString();
1702
            serChan[2] = trckbarSerial3.Value;
1703
            if (!_init) _sendSerialData();
1704
        }
1705
        private void btnSer3_0_Click(object sender, EventArgs e)
1706
        {
1707
            trckbarSerial3.Value = 0;
1708
        }
1709
        private void btnSer3_127_Click(object sender, EventArgs e)
1710
        {
1711
            trckbarSerial3.Value = 127;
1712
        }
1713
        private void btnSer3_254_Click(object sender, EventArgs e)
1714
        {
1715
            trckbarSerial3.Value = 254;
1716
        }
1717
        private void textBoxSerial4_TextChanged(object sender, EventArgs e)
1718
        {
1719
            serChanTitle[3] = textBoxSerial4.Text;
1720
        }
1721
        private void trckbarSerial4_ValueChanged(object sender, EventArgs e)
1722
        {
1723
            lblTbSerial4.Text = trckbarSerial4.Value.ToString();
1724
            serChan[3] = trckbarSerial4.Value;
1725
            if (!_init) _sendSerialData();
1726
        }
1727
        private void btnSer4_0_Click(object sender, EventArgs e)
1728
        {
1729
            trckbarSerial4.Value = 0;
1730
        }
1731
        private void btnSer4_127_Click(object sender, EventArgs e)
1732
        {
1733
            trckbarSerial4.Value = 127;
1734
        }
1735
        private void btnSer4_254_Click(object sender, EventArgs e)
1736
        {
1737
            trckbarSerial4.Value = 254;
1738
        }
2265 - 1739
        private void tbSerial5_Scroll(object sender, EventArgs e)
1740
        {
1741
            lblTbSerial5.Text = trckbarSerial5.Value.ToString();
1742
            serChan[4] = trckbarSerial5.Value;
1743
            if (!_init) _sendSerialData();
1744
        }
1745
        private void textBoxSerial5_TextChanged(object sender, EventArgs e)
1746
        {
1747
            serChanTitle[4] = textBoxSerial5.Text;
1748
        }
1749
        private void btnSer5_0_Click(object sender, EventArgs e)
1750
        {
1751
            trckbarSerial5.Value = 0;
1752
        }
1753
        private void btnSer5_127_Click(object sender, EventArgs e)
1754
        {
1755
            trckbarSerial5.Value = 127;
1756
        }
1757
        private void btnSer5_254_Click(object sender, EventArgs e)
1758
        {
1759
            trckbarSerial5.Value = 254;
1760
        }
1761
        private void trckbarSerial5_ValueChanged(object sender, EventArgs e)
1762
        {
1763
            lblTbSerial5.Text = trckbarSerial5.Value.ToString();
1764
            serChan[4] = trckbarSerial5.Value;
1765
            if (!_init) _sendSerialData();
1766
        }
1767
        private void tbSerial6_Scroll(object sender, EventArgs e)
1768
        {
1769
            lblTbSerial6.Text = trckbarSerial6.Value.ToString();
1770
            serChan[5] = trckbarSerial6.Value;
1771
            if (!_init) _sendSerialData();
1772
        }
1773
        private void textBoxSerial6_TextChanged(object sender, EventArgs e)
1774
        {
1775
            serChanTitle[5] = textBoxSerial6.Text;
1776
        }
1777
        private void btnSer6_0_Click(object sender, EventArgs e)
1778
        {
1779
            trckbarSerial6.Value = 0;
1780
        }
1781
        private void btnSer6_127_Click(object sender, EventArgs e)
1782
        {
1783
            trckbarSerial6.Value = 127;
1784
        }
1785
        private void btnSer6_254_Click(object sender, EventArgs e)
1786
        {
1787
            trckbarSerial6.Value = 254;
1788
        }
1789
        private void trckbarSerial6_ValueChanged(object sender, EventArgs e)
1790
        {
1791
            lblTbSerial6.Text = trckbarSerial6.Value.ToString();
1792
            serChan[5] = trckbarSerial6.Value;
1793
            if (!_init) _sendSerialData();
1794
        }
1795
        private void tbSerial7_Scroll(object sender, EventArgs e)
1796
        {
1797
            lblTbSerial7.Text = trckbarSerial7.Value.ToString();
1798
            serChan[6] = trckbarSerial7.Value;
1799
            if (!_init) _sendSerialData();
1800
        }
1801
        private void textBoxSerial7_TextChanged(object sender, EventArgs e)
1802
        {
1803
            serChanTitle[6] = textBoxSerial7.Text;
1804
        }
1805
        private void btnSer7_0_Click(object sender, EventArgs e)
1806
        {
1807
            trckbarSerial7.Value = 0;
1808
        }
1809
        private void btnSer7_127_Click(object sender, EventArgs e)
1810
        {
1811
            trckbarSerial7.Value = 127;
1812
        }
1813
        private void btnSer7_254_Click(object sender, EventArgs e)
1814
        {
1815
            trckbarSerial7.Value = 254;
1816
        }
1817
        private void trckbarSerial7_ValueChanged(object sender, EventArgs e)
1818
        {
1819
            lblTbSerial7.Text = trckbarSerial7.Value.ToString();
1820
            serChan[6] = trckbarSerial7.Value;
1821
            if (!_init) _sendSerialData();
1822
        }
1823
        private void tbSerial8_Scroll(object sender, EventArgs e)
1824
        {
1825
            lblTbSerial8.Text = trckbarSerial8.Value.ToString();
1826
            serChan[7] = trckbarSerial8.Value;
1827
            if (!_init) _sendSerialData();
1828
        }
1829
        private void textBoxSerial8_TextChanged(object sender, EventArgs e)
1830
        {
1831
            serChanTitle[7] = textBoxSerial8.Text;
1832
        }
1833
        private void btnSer8_0_Click(object sender, EventArgs e)
1834
        {
1835
            trckbarSerial8.Value = 0;
1836
        }
1837
        private void btnSer8_127_Click(object sender, EventArgs e)
1838
        {
1839
            trckbarSerial8.Value = 127;
1840
        }
1841
        private void btnSer8_254_Click(object sender, EventArgs e)
1842
        {
1843
            trckbarSerial8.Value = 254;
1844
        }
1845
        private void trckbarSerial8_ValueChanged(object sender, EventArgs e)
1846
        {
1847
            lblTbSerial8.Text = trckbarSerial8.Value.ToString();
1848
            serChan[7] = trckbarSerial8.Value;
1849
            if (!_init) _sendSerialData();
1850
        }
2259 - 1851
        #endregion serial control channels
1852
 
2233 - 1853
    }
1854
    public class IniFile
1855
    {
1856
        public string path;
1857
 
1858
        [DllImport("kernel32")]
1859
        private static extern long WritePrivateProfileString(string section,
1860
          string key, string val, string filePath);
1861
 
1862
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
1863
        static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer,
1864
               uint nSize, string lpFileName);
1865
 
1866
        [DllImport("kernel32")]
1867
        private static extern int GetPrivateProfileString(string section,
1868
          string key, string def, StringBuilder retVal,
1869
          int size, string filePath);
1870
 
1871
        public IniFile(string INIPath)
1872
        {
1873
            path = INIPath;
1874
        }
1875
 
1876
        public void IniWriteValue(string Section, string Key, string Value)
1877
        {
1878
            WritePrivateProfileString(Section, Key, Value, this.path);
1879
        }
1880
 
1881
        public string IniReadValue(string Section, string Key)
1882
        {
1883
            StringBuilder temp = new StringBuilder(255);
1884
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
1885
            return temp.ToString();
1886
        }
1887
        //Ini_sections auslesen in String-Array
1888
        public string[] IniSectionNames()
1889
        {
1890
 
1891
            //  uint MAX_BUFFER = 32767;
1892
            uint MAX_BUFFER = 8388608;
1893
            IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
1894
            uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
1895
            if (bytesReturned == 0)
1896
            {
1897
                Marshal.FreeCoTaskMem(pReturnedString);
1898
                return null;
1899
            }
1900
            string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
1901
            Marshal.FreeCoTaskMem(pReturnedString);
1902
            //use of Substring below removes terminating null for split
1903
            return local.Substring(0, local.Length - 1).Split('\0');
1904
 
1905
 
1906
        }
1907
    }
1908
    public static class ControlExtensions
1909
    {
1910
        /// <summary> 
1911
        /// Execute a threadsafe operation, when accessing a control via another thread 
1912
        /// action is a lamdaexpression
1913
        /// e.g. comboBox1.ExecuteThreadSafe(() => comboBox1.Enabled = true);
1914
        /// </summary>
1915
        /// <param name="control"> The control </param>
1916
        /// <param name="action"> The 'action' to perform </param>
1917
        public static void ExecuteThreadSafe(this Control control, Action action)
1918
        {
1919
            if (control.InvokeRequired)
1920
            {
1921
                control.BeginInvoke(action); //"BeginInvoke" is an async call -> threadsafety error when called to many times successively -> then take "Invoke"
1922
            }
1923
            else
1924
            {
1925
                action.Invoke();
1926
            }
1927
        }
1928
    }
1929
 
1930
}