Subversion Repositories Projects

Rev

Rev 2274 | 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 };
2280 - 177
        int[] serChan_sub = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2257 - 178
        string[] serChanTitle = new string[12];
2233 - 179
        /// <summary>
180
        /// interval for sending debugdata (multiplied by 10ms)
181
        /// </summary>
2265 - 182
        byte debugInterval = 10; //(=> 100ms)
2233 - 183
        /// <summary>
184
        /// interval for sending BL-CTRL status (multiplied by 10ms)
185
        /// </summary>
2265 - 186
        byte blctrlInterval = 75;
2233 - 187
        /// <summary>
188
        /// interval for sending NAV-CTRL status (multiplied by 10ms)
189
        /// </summary>
190
        byte navctrlInterval = 80;
191
        /// <summary>
192
        /// interval for sending OSD page update (multiplied by 10ms)
193
        /// </summary>
194
        byte OSDInterval = 85;
195
        /// <summary>
196
        /// datatable for the debug data array - displayed on settings tabpage in datagridview
197
        /// </summary>
198
        DataTable dtAnalog = new DataTable();
2254 - 199
        /// <summary>
200
        /// datatable for motordata (current,temp)
201
        /// </summary>
2250 - 202
        DataTable dtMotors1 = new DataTable();
203
        DataTable dtMotors2 = new DataTable();
204
 
2257 - 205
        DataTable dtWaypoints = new DataTable();
206
 
2233 - 207
        public MainForm()
208
        {
209
            InitializeComponent();
2274 - 210
            _initForm();
211
        }
212
        void _initForm()
213
        {
2257 - 214
            serChanTitle.Initialize();
215
 
2233 - 216
            _readIni();
2257 - 217
 
2259 - 218
            _dataTablesInit();
2257 - 219
 
2233 - 220
            simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed;
221
            simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened;
222
            simpleSerialPort.DataReceived += processMessage;
2265 - 223
 
2233 - 224
            chkbAutoBL.Checked = _blctrlDataAutorefresh;
225
            chkbAutoDbg.Checked = _debugDataAutorefresh;
226
            chkbAutoNav.Checked = _navCtrlDataAutorefresh;
227
            chkbAutoOSD.Checked = _OSDAutorefresh;
2265 - 228
 
2233 - 229
            labelTimingDebug.Text = (debugInterval * 10).ToString();
230
            labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString();
231
            labelTimingNAV.Text = (navctrlInterval * 10).ToString();
232
            labelTimingOSD.Text = (OSDInterval * 10).ToString();
2265 - 233
 
234
            TabControl1.TabPages.Remove(tabPageTesting); //a testing page
2233 - 235
        }
236
        #region events
237
        private void MainForm_Shown(object sender, EventArgs e)
238
        {
239
            _loadLabelNames();
2257 - 240
            _initSerialCtrl();
2233 - 241
            _init = false;
2265 - 242
            splitContainer1.SplitterDistance = 510;
2233 - 243
        }
244
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
245
        {
246
            _writeIni();
2274 - 247
            // after adding a second language the programm didn't close properly anymore
248
            // so this is kinda workaround...
249
            Dispose(true);
250
            Environment.Exit(0);
2233 - 251
        }
252
        private void SimpleSerialPort_PortOpened()
253
        {
254
            btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(192, 255, 192)));
2274 - 255
            if(Thread.CurrentThread.CurrentUICulture.Name== "")
256
                btnConn.Invoke((Action)(() => btnConn.Text = "close" + Environment.NewLine + "serial port"));
257
            else
258
                btnConn.Invoke((Action)(() => btnConn.Text = "COM-Port" + Environment.NewLine + "schliessen"));
2233 - 259
            _getVersion();
260
            Thread.Sleep(100);
261
            _OSDMenue(0);
2257 - 262
            Thread.Sleep(200);
263
            _sendSerialData();
2233 - 264
           // _readCont(true);
265
        }
266
        private void SimpleSerialPort_PortClosed()
267
        {
268
            btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(224, 224, 224)));
2274 - 269
            if (Thread.CurrentThread.CurrentUICulture.Name == "")
270
                btnConn.Invoke((Action)(() => btnConn.Text = "open" + Environment.NewLine + "serial port"));
271
            else
272
                btnConn.Invoke((Action)(() => btnConn.Text = "COM-Port" + Environment.NewLine + "öffnen"));
2233 - 273
            _readCont(false);
274
        }
275
        /// <summary>
276
        /// timer for refreshing subscription of subscribed data
277
        /// query lifecounter for connection failure
278
        /// </summary>
279
        private void timer1_Tick(object sender, EventArgs e)
280
        {
281
            if(bReadContinously)
282
            {
283
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
284
 
2250 - 285
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
2233 - 286
 
287
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
288
                check_HWError = true;
289
                _getVersion();
290
                Thread.Sleep(10);
291
                if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); }
292
                if (_iLifeCounter > 0)
293
                {
294
                    lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0);
295
                    _iLifeCounter = 0;
296
                }
297
                else
298
                {
299
                    Log(LogMsgType.Error, "No communication to NC/FC!");
300
                    lblLifeCounter.BackColor = Color.FromArgb(224, 0, 0);
301
                }
302
            }
303
        }
304
        private void cbOSD_SelectedIndexChanged(object sender, EventArgs e)
305
        {
306
            if (!_bCBInit && cbOSD.SelectedIndex > -1)
307
                _OSDMenue(cbOSD.SelectedIndex);
308
        }
309
        private void chkbAutoDbg_CheckedChanged(object sender, EventArgs e)
310
        {
311
            if(!_init) _debugDataAutorefresh = chkbAutoDbg.Checked;
312
        }
313
        private void chkbAutoNav_CheckedChanged(object sender, EventArgs e)
314
        {
315
            if (!_init) _navCtrlDataAutorefresh = chkbAutoNav.Checked;
316
        }
317
        private void chkbAutoBL_CheckedChanged(object sender, EventArgs e)
318
        {
319
            if (!_init) _blctrlDataAutorefresh = chkbAutoBL.Checked;
320
        }
321
        private void chkbAutoOSD_CheckedChanged(object sender, EventArgs e)
322
        {
323
            if (!_init) _OSDAutorefresh = chkbAutoOSD.Checked;
324
        }
325
        private void cbTimingDebug_SelectedIndexChanged(object sender, EventArgs e)
326
        {
327
            if (cbTimingDebug.SelectedIndex > -1)
328
            {
329
                debugInterval = (byte)(Convert.ToInt16(cbTimingDebug.SelectedItem) / 10);
330
                labelTimingDebug.Text = (debugInterval * 10).ToString();
331
            }
332
        }
333
        private void cbTimingNAV_SelectedIndexChanged(object sender, EventArgs e)
334
        {
335
            if (cbTimingNAV.SelectedIndex > -1)
336
            {
337
                navctrlInterval = (byte)(Convert.ToInt16(cbTimingNAV.SelectedItem) / 10);
338
                labelTimingNAV.Text = (navctrlInterval * 10).ToString();
339
            }
340
        }
341
        private void cbTimingBLCTRL_SelectedIndexChanged(object sender, EventArgs e)
342
        {
343
            if (cbTimingBLCTRL.SelectedIndex > -1)
344
            {
345
                blctrlInterval = (byte)(Convert.ToInt16(cbTimingBLCTRL.SelectedItem) / 10);
346
                labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString();
347
            }
348
        }
349
        private void cbTimingOSD_SelectedIndexChanged(object sender, EventArgs e)
350
        {
351
            if (cbTimingOSD.SelectedIndex > -1)
352
            {
353
                OSDInterval = (byte)(Convert.ToInt16(cbTimingOSD.SelectedItem) / 10);
354
                labelTimingOSD.Text = (OSDInterval * 10).ToString();
355
            }
356
        }
2254 - 357
        private void rtfError_LinkClicked(object sender, LinkClickedEventArgs e)
358
        {
359
            System.Diagnostics.Process.Start(e.LinkText);
360
        }
2274 - 361
        /// <summary>
362
        /// Combobox for selecting UI language
363
        /// </summary>
364
        /// <param name="sender"></param>
365
        /// <param name="e"></param>
366
        private void cbLanguage_SelectedIndexChanged(object sender, EventArgs e)
367
        {
368
            if (cbLanguage.SelectedIndex == 1)
369
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
370
            else
371
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("");
372
            updateStringRessource();
373
        }
374
        /// <summary>
375
        /// the form has to be recreated when changing the language of the UI
376
        /// </summary>
377
        private void updateStringRessource()
378
        {
379
            try
380
            {
381
                Point pt = this.Location;// memorize location of form
382
                Size sz = this.Size;// memorize size of form
383
 
384
                if (simpleSerialPort.Port.IsOpen)
385
                {
386
                    simpleSerialPort.Port.Close();
387
                    Thread.Sleep(200);
388
                    _readCont(false);
389
                    Thread.Sleep(100);
390
                    while (simpleSerialPort.Port.IsOpen | bReadContinously)
391
                        Thread.Sleep(100);
392
                }
393
 
394
                this.Controls.Clear();// clear all controls of form
395
                components.Dispose();// dispose all components and release resources
396
                // delete all events to avoid double instances when calling  InitializeComponent(); 
397
                this.Events.Dispose();
398
                if (timer1 != null)
399
                {
400
                    timer1.Dispose();
401
                    timer1 = null;
402
                }
403
                _init = true;
404
                timer1 = new System.Windows.Forms.Timer();
405
 
406
                dtAnalog = new DataTable();
407
                dtMotors1 = new DataTable();
408
                dtMotors2 = new DataTable();
409
                dtWaypoints = new DataTable();
410
 
411
                InitializeComponent(); // reload UI
412
 
413
                _dataTablesInit();
414
 
415
                simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed;
416
                simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened;
417
                simpleSerialPort.DataReceived += processMessage;
418
 
419
                chkbAutoBL.Checked = _blctrlDataAutorefresh;
420
                chkbAutoDbg.Checked = _debugDataAutorefresh;
421
                chkbAutoNav.Checked = _navCtrlDataAutorefresh;
422
                chkbAutoOSD.Checked = _OSDAutorefresh;
423
 
424
                labelTimingDebug.Text = (debugInterval * 10).ToString();
425
                labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString();
426
                labelTimingNAV.Text = (navctrlInterval * 10).ToString();
427
                labelTimingOSD.Text = (OSDInterval * 10).ToString();
428
 
429
                TabControl1.TabPages.Remove(tabPageTesting); //a testing page
430
 
431
                this.Size = sz;// set size
432
                this.Location = pt;// position form
433
                _loadLabelNames();
434
                _initSerialCtrl();
435
                _init = false;
436
 
437
            }
438
            catch (Exception ex)
439
            {
440
                MessageBox.Show(ex.Message);
441
            }
442
        }
443
 
2233 - 444
        #endregion events
445
 
446
        /// <summary> Log data to the terminal window. </summary>
447
        /// <param name="msgtype"> The type of message to be written. </param>
448
        /// <param name="msg"> The string containing the message to be shown. </param>
449
        private void Log(LogMsgType msgtype, string msg)
450
        {
451
            rtfTerminal.Invoke(new EventHandler(delegate
452
            {
453
                if (rtfTerminal.Lines.Length >= 1000)   //Wenn Terminal mehr als 1000 Zeilen hat
454
                    rtfTerminal.Select(42, (500 * 129));     //500 löschen
455
                rtfTerminal.Select(rtfTerminal.Text.Length, 0);
456
                rtfTerminal.SelectedText = string.Empty;
457
                rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Regular);
458
                rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype];
459
                rtfTerminal.AppendText(msg + Environment.NewLine);
460
                rtfTerminal.ScrollToCaret();
461
            }));
462
        }
463
        /// <summary> display the OSD text in 4 lines à 20 chars </summary>
464
        /// <param name="msgtype"> The type of message to be written. </param>
465
        /// <param name="msg"> The string containing the message to be shown. </param>
466
        private void OSD(LogMsgType msgtype, string msg)
467
        {
468
            rtfOSD.Invoke(new EventHandler(delegate
469
            {
470
                if (rtfOSD.Lines.Length > 4)
471
                    rtfOSD.Clear();
472
                rtfOSD.Select(rtfOSD.Text.Length,0);
473
                rtfOSD.SelectedText = string.Empty;
474
                rtfOSD.SelectionFont = new Font(rtfOSD.SelectionFont, FontStyle.Regular);
475
                rtfOSD.SelectionColor = LogMsgTypeColor[(int)msgtype];
476
                rtfOSD.AppendText(msg + Environment.NewLine);
477
                if (rtfOSD.Text.IndexOf("ERR") > 0)
478
                {
479
                    rtfOSD.Select(rtfOSD.Text.IndexOf("ERR"), 40);
480
                    rtfOSD.SelectionColor = LogMsgTypeColor[(int)LogMsgType.Error];
481
                }
482
            }));
483
        }
484
        private void ErrorLog(LogMsgType msgtype, string msg)
485
        {
486
            rtfError.Invoke(new EventHandler(delegate
487
            {
488
                if (rtfError.Lines.Length > 4)
489
                    rtfError.Clear();
490
                rtfError.Focus();
491
                rtfError.Select(rtfError.Text.Length, 0);
492
                rtfError.SelectedText = string.Empty;
493
                rtfError.SelectionFont = new Font(rtfError.SelectionFont, FontStyle.Regular);
494
                rtfError.SelectionColor = LogMsgTypeColor[(int)msgtype];
495
                rtfError.AppendText(msg + Environment.NewLine);
496
 
497
            }));
2254 - 498
            _bErrorLog = true;
2233 - 499
        }
500
 
501
        #region functions        
502
 
2250 - 503
        #region processing received data
504
        /// <summary> Processing the messages and displaying them in the according form controls 
505
        /// function called by simpleSerialPort.DataReceived event
506
        /// </summary>
2233 - 507
        /// <param name="message"> message bytearray recieved by SimpleSerialPort class </param>
508
        private void processMessage(byte[] message)
509
        {
510
            if (message.Length > 0)
511
            {
512
                _iLifeCounter++;
513
                //Log(LogMsgType.Incoming, BitConverter.ToString(message));
514
                //Log(LogMsgType.Incoming, message.Length.ToString());
515
                string s = new string(ASCIIEncoding.ASCII.GetChars(message, 0, message.Length));
516
                char cmdID;
517
                byte adr;
518
                byte[] data;
2257 - 519
                byte[] tmp = null;
2233 - 520
                if (message[0] != '#')
2257 - 521
                {
522
                    int iFound = -1;
523
                    for(int i=0;i<message.Length;i++)   //Sometimes the FC/NC sends strings without termination (like WP messages)
524
                    {                                   //so this is a workaround to not spam the log box
525
                        if (message[i] == 35)
526
                        {
527
                            iFound = i;
528
                            break;
529
                        }                          
530
                    }
531
                    if(iFound>0)
532
                    {
533
                        s = new string(ASCIIEncoding.ASCII.GetChars(message, 0,iFound));
534
                        tmp = new byte[message.Length - iFound];
535
                        Buffer.BlockCopy(message, iFound, tmp, 0, message.Length - iFound);
536
                    }
537
                    s = s.Trim('\0', '\n', '\r');
538
                    if(s.Length > 0)
539
                        Log(LogMsgType.Normal, s);
540
                    if (tmp != null)
541
                    {
542
                        s = new string(ASCIIEncoding.ASCII.GetChars(tmp, 0, tmp.Length));
543
                        processMessage(tmp);
544
                    }
545
                }
2233 - 546
                //Debug.Print(s);
547
                else
548
                {
549
                    FlightControllerMessage.ParseMessage(message, out cmdID, out adr, out data);
550
 
551
                    if (adr == 255) { crcError++; }
552
                    else crcError = 0;
553
                    lblCRCErr.Invoke((Action)(() => lblCRCErr.Text = crcError.ToString()));
2250 - 554
                    //display the active controller (FC / NC) 
2233 - 555
                    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...???
556
                    {
557
                        _iCtrlAct = adr;
558
                        switch (adr)
559
                        {
560
                            case 1:
561
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "FC"));
562
                                lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "FC"));
563
                                _setFieldsNA(); //display fields NA for FC 
564
                                break;
565
                            case 2:
566
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "NC"));
567
                                lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "NC"));
568
                                break;
569
                            case 3:
570
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "MK3MAG"));
571
                                break;
572
                            case 4:
573
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "BL-CTRL"));
574
                                break;
575
                            default:
576
                                lblCtrl.Invoke((Action)(() => lblCtrl.Text = "...."));
577
                                break;
578
                        }
2257 - 579
                        _loadLabelNames();
2233 - 580
                    }
2257 - 581
                    // else
582
                    //     Debug.Print("Address == 0?");
2233 - 583
 
584
                    if (data != null && data.Length > 0)
585
                    {
586
                        s = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1));
587
                        s = s.Trim('\0', '\n');
588
 
589
                        switch (cmdID)
590
                        {
2250 - 591
                            case 'A': //Label names
592
                                _processLabelNames(s);
2233 - 593
                                break;
594
 
2250 - 595
                            case 'D': //Debug data
2257 - 596
                                _processDebugVals(adr, data);
2233 - 597
                                break;
598
 
2250 - 599
                            case 'V': //Version
600
                                _processVersion(adr, data);
2233 - 601
                                break;
602
 
2250 - 603
                            case 'K'://BL-CTRL data
604
                                _processBLCtrl(data);
2233 - 605
                                break;
606
 
607
                            case 'O': //NC Data
2250 - 608
                                _processNCData(data);
2233 - 609
                                break;
610
 
611
                            case 'E': //NC error-string
612
                                ErrorLog(LogMsgType.Error, "NC Error: " + s);
613
                                break;
614
 
2250 - 615
                            case 'L': //OSD Menue (called by pagenumber)
616
                                _processOSDSingle(data);
2233 - 617
                                break;
618
 
2250 - 619
                            case 'H': //OSD Menue (with autoupdate - called by Key)
620
                                _processOSDAuto(data);
2233 - 621
                                break;
622
 
2257 - 623
                            case 'X': //Waypoint data
624
                                _processWPData(data);
625
                                break;
626
 
2233 - 627
                            //default:
628
                            //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
629
                            //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
630
                            //    break;
631
                        }
632
                    }
633
                    //else
634
                    //{
635
                    //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
636
                    //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
637
                    //}
638
                }
639
            }
640
        }
2250 - 641
        /// <summary>
642
        /// Analog label names 'A'
643
        /// each label name is returned as a single string 
644
        /// and added to string array sAnalogLabel[]
645
        /// and the datatable dtAnalog
646
        /// </summary>
647
        /// <param name="s">the label name</param>
648
        void _processLabelNames(string s)
649
        {
650
            if (iLableIndex < 32)
651
            {
652
                sAnalogLabel[iLableIndex] = s;
653
                if (dtAnalog.Rows.Count < 32)
654
                    dtAnalog.Rows.Add(s, "");
655
                else
656
                    dtAnalog.Rows[iLableIndex].SetField(0, s);
2233 - 657
 
2250 - 658
                _getAnalogLabels(iLableIndex + 1);
659
            }
660
            Debug.Print(s);
661
        }
662
        /// <summary>
663
        /// Debug values 'D'
664
        /// </summary>
665
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
666
        /// <param name="data">the received byte array to process</param>
667
        void _processDebugVals(byte adr,byte[] data)
668
        {
669
            if (data.Length == 66)
670
            {
671
                int[] iAnalogData = new int[32];
672
 
673
                int index = 0;
674
                Int16 i16 = 0;
675
                double dTemp = 0;
676
                for (int i = 2; i < 66; i += 2)
677
                {
678
                    i16 = data[i + 1];
679
                    i16 = (Int16)(i16 << 8);
680
                    iAnalogData[index] = data[i] + i16;
681
                    sAnalogData[index] = (data[i] + i16).ToString();
682
                    dtAnalog.Rows[index].SetField(1, sAnalogData[index]);
683
 
684
                    if (adr == 2) //NC
685
                    {
686
                        switch (index)
687
                        {
688
                            case 0: //pitch (German: nick)
689
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10)));
690
                                lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
691
                                break;
692
                            case 1: //roll
693
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10)));
694
                                lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
695
                                break;
696
                            case 4: //altitude
697
                                lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m")));
698
                                break;
699
                            case 7: //Voltage
700
                                lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V")));
701
                                break;
702
                            case 8: // Current
703
                                lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A")));
704
                                break;
705
                            case 10: //heading
706
                                lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°"));
707
                                headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index])));
708
                                break;
709
                            case 12: // SPI error
710
                                lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index]));
711
                                break;
712
                            case 14: //i2c error
713
                                lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index]));
714
                                break;
715
                            case 20: //Earthmagnet field
716
                                lblNCMF.Invoke((Action)(() => lblNCMF.Text = sAnalogData[index] + "%"));
717
                                break;
718
                            case 21: //GroundSpeed
719
                                lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s")));
720
                                break;
721
                            case 28: //Distance East from saved home position -> calculate distance with distance N + height
722
                                dTemp = Math.Pow((double)iAnalogData[index], 2) + Math.Pow((double)iAnalogData[index - 1], 2);
723
                                dTemp = Math.Sqrt(dTemp) / (double)10; //'flat' distance from HP with N/E
724
                                                                       //  lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00")));
725
                                dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation
2254 - 726
                                dTemp = Math.Sqrt(dTemp) / (double)10;
727
                                lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = dTemp.ToString("0.0 m")));
2250 - 728
                                break;
729
                            case 31: //Sats used
730
                                lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index]));
731
                                break;
732
                        }
733
                    }
734
                    if (adr == 1) //FC
735
                    {
736
                        switch (index)
737
                        {
738
                            case 0: //pitch (German: nick)
739
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10)));
740
                                lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
741
                                break;
742
                            case 1: //roll
743
                                artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10)));
744
                                lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°")));
745
                                break;
746
                            case 5: //altitude
747
                                lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m")));
748
                                break;
749
                            case 8: //heading
750
                                lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°"));
751
                                headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index])));
752
                                break;
753
                            case 9: //Voltage
754
                                lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V")));
755
                                break;
756
                            case 10: //Receiver quality
757
                                lblNCRC.Invoke((Action)(() => lblNCRC.Text = sAnalogData[index]));
758
                                break;
759
                            case 22: // Current
760
                                lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A")));
761
                                break;
762
                            case 23: //capacity used
763
                                lblNCCap.Invoke((Action)(() => lblNCCap.Text = (iAnalogData[index]).ToString("0 mAh")));
764
                                break;
765
                            case 27: // SPI error
766
                                lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index]));
767
                                break;
768
                            case 28: //i2c error
769
                                lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index]));
770
                                break;
771
                        }
772
                    }
773
                    index++;
774
                }
775
            }
776
            else
777
                Debug.Print("wrong data-length (66): " + data.Length.ToString());
778
        }
779
        /// <summary>
780
        /// Version string 'V'
781
        /// </summary>
782
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
783
        /// <param name="data">the received byte array to process</param>
784
        void _processVersion(byte adr,byte[] data)
785
        {
786
            if (data.Length == 12)
787
            {
788
                if (!check_HWError)
789
                {
790
                    string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " };
791
                    string sVersion = "";
792
                    //sbyte[] signed = Array.ConvertAll(data, b => unchecked((sbyte)b));
793
                    Log(LogMsgType.Warning, (adr == 1 ? "FC-" : "NC-") + "Version: ");
794
                    sVersion = "HW V" + (data[7] / 10).ToString() + "." + (data[7] % 10).ToString();
795
                    Log(LogMsgType.Incoming, sVersion);
796
                    sVersion = "SW V" + (data[0]).ToString() + "." + (data[1]).ToString() + ((char)(data[4] + 'a')).ToString();
797
                    Log(LogMsgType.Incoming, sVersion);
798
                    Log(LogMsgType.Incoming, "BL-Firmware: V" + (data[8] / 100).ToString() + "." + (data[8] % 100).ToString());
799
                }
800
                if (data[5] > 0) //error0 
801
                {
802
                    if (adr == 1)
803
                        ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[5].ToString() + ": " + ((FC_HWError0)data[5]).ToString());
804
                    if (adr == 2)
805
                        ErrorLog(LogMsgType.Error, "NC - HW-Error " + data[5].ToString() + ": " + ((NC_HWError0)data[5]).ToString());
806
                }
807
                if (data[6] > 0) //error1 
808
                {
809
                    if (adr == 1)
810
                        ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[6].ToString() + ": " + ((FC_HWError1)data[6]).ToString());
811
                    if (adr == 2)
812
                        ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field
813
                }
2254 - 814
                if((data[5] + data[6] == 0) && _bErrorLog)
815
                    _clearErrorLog(adr==1 ? "FC - HW-Error" : "FC - HW-Error");
2250 - 816
 
817
            }
818
            check_HWError = false;
819
        }
820
        /// <summary>
821
        /// BL-Ctrl data 'K'
822
        /// for FC you have to use a customized firmware
823
        /// </summary>
824
        /// <param name="data">the received byte array to process</param>
825
        void _processBLCtrl(byte[] data)
826
        {
827
            if (data.Length % 6 == 0) //data.Length up to 96 (16 motors x 6 byte data) --> new datastruct in FC -> not standard!
828
            {
829
                bool bAvailable = false;
830
                for (int i = 0; i < data.Length && data[i] < 8; i += 6) // data[i] < 8 --> at moment there are 8 display fields for motors
831
                {
832
 
833
                    if ((data[i + 4] & 128) == 128) //Status bit at pos 7 = 128 dec -- if true, motor is available
834
                        bAvailable = true;
835
                    else
836
                        bAvailable = false;
837
 
838
                    if (data[i] < 4)
839
                    {
840
                        if (bAvailable)
841
                        {
842
                            dtMotors1.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
843
                            dtMotors1.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C"));
844
                        }
845
                        else
846
                        {
847
                            dtMotors1.Rows[data[i]].SetField(1, "NA");
848
                            dtMotors1.Rows[data[i]].SetField(2, "NA");
849
                        }
850
                    }
851
                    if (data[i] > 3 && data[i] < 8)
852
                    {
853
                        if (bAvailable)
854
                        {
855
                            dtMotors2.Rows[data[i] - 4].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
856
                            dtMotors2.Rows[data[i] - 4].SetField(2, data[i + 2].ToString("0 °C"));
857
                        }
858
                        else
859
                        {
860
                            dtMotors2.Rows[data[i] - 4].SetField(1, "NA");
861
                            dtMotors2.Rows[data[i] - 4].SetField(2, "NA");
862
                        }
863
                    }
864
                }
865
            }
866
 
867
        }
868
        /// <summary>
869
        /// Navi-Ctrl data 'O'
870
        /// GPS-Position, capacatiy, flying time...
871
        /// </summary>
872
        /// <param name="data">the received byte array to process</param>
873
        void _processNCData(byte[] data)
874
        {
875
            int i_32, i_16, iVal;
876
            double d;
877
            i_32 = data[4];
878
            iVal = i_32 << 24;
879
            i_32 = data[3];
880
            iVal += i_32 << 16;
881
            i_32 = data[2];
882
            iVal += i_32 << 8;
883
            iVal += data[1];
884
            d = (double)iVal / Math.Pow(10, 7);
885
            lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = d.ToString("0.######°"))); //GPS-Position: Longitude in decimal degree
886
            //lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = _convertDegree(d))); //GPS-Position: Longitude in minutes, seconds
887
 
888
            i_32 = data[8];
889
            iVal = i_32 << 24;
890
            i_32 = data[7];
891
            iVal += i_32 << 16;
892
            i_32 = data[6];
893
            iVal += i_32 << 8;
894
            iVal += data[5];
895
            d = (double)iVal / Math.Pow(10, 7);
896
            lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = d.ToString("0.######°"))); //GPS-Position: Latitude in decimal degree
2254 - 897
                                                                                            //lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = _convertDegree(d))); //GPS-Position: Latitude in minutes, seconds
2250 - 898
 
2254 - 899
            i_16 = data[28];
900
            i_16 = (Int16)(i_16 << 8);
901
            iVal = data[27] + i_16;
902
            lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP
903
 
904
            i_16 = data[45];
905
            i_16 = (Int16)(i_16 << 8);
906
            iVal = data[44] + i_16;
907
            lblNCDistHP1.Invoke((Action)(() => lblNCDistHP1.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP
908
 
909
            lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = data[48].ToString())); //Waypoint index
910
            lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = data[49].ToString())); //Waypoints count
911
 
2250 - 912
            i_16 = data[81];
913
            i_16 = (Int16)(i_16 << 8);
914
            iVal = data[80] + i_16;
915
            lblNCCap.Invoke((Action)(() => lblNCCap.Text = iVal.ToString() + " mAh")); //Capacity used
916
 
917
            i_16 = data[56];
918
            i_16 = (Int16)(i_16 << 8);
919
            iVal = data[55] + i_16;
920
            TimeSpan t = TimeSpan.FromSeconds(iVal);
921
            string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2");
922
            lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time
923
 
924
            lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality
925
            lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString()));   //NC Errornumber
926
            //if (data[69] > 0)
927
            //    _readNCError();
928
            //break;
929
            if (data[69] > 0 & data[69] < 44)
930
                ErrorLog(LogMsgType.Error, "NC Error [" + data[69].ToString() + "]: " + NC_Error[data[69]]);
2254 - 931
            else
932
                if(_bErrorLog) _clearErrorLog("NC Error");
2250 - 933
 
934
        }
935
        /// <summary>
2257 - 936
        /// Navi-Ctrl WP data struct 'X'
937
        /// called by index
938
        /// </summary>
939
        /// <param name="data">the received byte array to process</param>
940
        void _processWPData(byte[] data)
941
        {
942
            if (data.Length >= 28)
943
            {
944
                int count = data[0];
945
                int index = data[1];
946
                cbWPIndex.Invoke((Action)(() => cbWPIndex.Items.Clear()));
947
                for (int i = 0; i < count; i++)
948
                    cbWPIndex.Invoke((Action)(() => cbWPIndex.Items.Add(i + 1)));
949
                cbWPIndex.Invoke((Action)(() => cbWPIndex.SelectedItem = index));
950
                DataRow dr = dtWaypoints.NewRow();
951
                dr = Waypoints.toDataRow(data, dr);
952
                dtWaypoints.Rows.Add(dr);
953
                dgvWP.Invoke((Action)(() => dgvWP.Update()));
954
            }
955
            else
956
                Debug.Print(new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length)));
957
        }
958
        /// <summary>
2250 - 959
        /// OSD Menue 'L'
960
        /// single page called by pagenumber
961
        /// no autoupdate
962
        /// </summary>
963
        /// <param name="data">the received byte array to process</param>
964
        void _processOSDSingle(byte[] data)
965
        {
966
            if (data.Length == 84)
967
            {
968
                string sMessage = "";
969
                iOSDPage = data[0];
970
                iOSDMax = data[1];
971
                if (cbOSD.Items.Count != iOSDMax) _initOSDCB();
972
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4));
973
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20));
974
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20));
975
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20));
976
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20));
977
                lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]")));
978
 
979
            }
980
            else
981
                OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)");
982
 
983
        }
984
        /// <summary>
985
        /// OSD Menue 'H'
986
        /// called by keys (0x01,0x02,0x03,0x04)
987
        /// autoupdate
988
        /// </summary>
989
        /// <param name="data">the received byte array to process</param>
990
        void _processOSDAuto(byte[] data)
991
        {
992
            if (data.Length == 81)
993
            {
994
                string sMessage = "";
995
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1));
996
                OSD(LogMsgType.Incoming, sMessage.Substring(0, 20));
997
                OSD(LogMsgType.Incoming, sMessage.Substring(20, 20));
998
                OSD(LogMsgType.Incoming, sMessage.Substring(40, 20));
999
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20));
1000
 
1001
            }
1002
            else
1003
                OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)");
1004
        }
1005
        #endregion processing received data
1006
 
2233 - 1007
        /// <summary> send message to controller to request data
1008
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
1009
        /// </summary>
1010
        /// <param name="CMDID"> the command ID </param>
1011
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
1012
        private void _sendControllerMessage(char CMDID, byte address)
1013
        {
1014
            if (simpleSerialPort.Port.IsOpen)
1015
            {
1016
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1017
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address);
1018
                serialStream.Write(bytes, 0, bytes.Length);
1019
 
1020
            }
1021
            else
1022
                Log(LogMsgType.Error, "NOT CONNECTED!");
1023
        }
1024
        /// <summary> send message to controller to request data
1025
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
1026
        /// </summary>
1027
        /// <param name="CMDID"> the command ID </param>
1028
        /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param>
1029
        /// <param name="data"> additional data for the request</param>
1030
        private void _sendControllerMessage(char CMDID, byte address, byte[]data)
1031
        {
1032
            if (simpleSerialPort.Port.IsOpen)
1033
            {
1034
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1035
                byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address,data);
1036
                serialStream.Write(bytes, 0, bytes.Length);
1037
 
1038
            }
1039
            else
1040
                Log(LogMsgType.Error, "NOT CONNECTED!");
1041
        }
1042
 
1043
        /// <summary>
1044
        /// read the analog-label names for the actual controller
1045
        /// and load it into listbox
1046
        /// </summary>
1047
        void _loadLabelNames()
1048
        {
1049
            if (_iCtrlAct > 0 && _iCtrlAct < 3)
1050
            {
1051
                switch (_iCtrlAct)
1052
                {
1053
                    case 1:
1054
                        sAnalogLabel = Properties.Resources.FCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
1055
                        break;
1056
                    case 2:
1057
                        sAnalogLabel = Properties.Resources.NCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
1058
                        break;
1059
                }
1060
                for (int i = 0; i < 32; i++)
1061
                {
1062
                    if (dtAnalog.Rows.Count < 32)
1063
                        dtAnalog.Rows.Add(sAnalogLabel[i], "");
1064
                    else
1065
                        dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]);
1066
                }
1067
                dataGridView1.Invoke((Action)(()=>dataGridView1.Refresh()));
1068
            }
1069
        }
1070
        /// <summary>
1071
        /// no longer used...
1072
        /// read the analog-label textfile for the actual controller
1073
        /// </summary>
1074
         private void _loadLabelFile()
1075
        {
1076
            switch (_iCtrlAct)
1077
            {
1078
                case 1:
1079
                    fileName = "FCLabelTexts.txt";
1080
                    break;
1081
                case 2:
1082
                    fileName = "NCLabelTexts.txt";
1083
                    break;
1084
                //default:
1085
                //    fileName = "NCLabelTexts.txt";
1086
                //    break;
1087
            }
1088
 
1089
            if (File.Exists(filePath + "\\" + fileName))
1090
            {
1091
                sAnalogLabel.Initialize();
1092
                sAnalogLabel = File.ReadAllLines(filePath + "\\" + fileName);
1093
                lbLabels.Invoke((Action)(() => lbLabels.Items.Clear()));
1094
                lbLabels.Invoke((Action)(() => lbLabels.Update()));
1095
                lbLabels.Invoke((Action)(() => lbLabels.Items.AddRange(sAnalogLabel)));
1096
                Console.WriteLine("Names loaded from file");
1097
                lblFileName.Invoke((Action)(() => lblFileName.Text = fileName));
1098
            }
1099
            else
1100
            {
1101
                _readCont(false);
1102
                Log(LogMsgType.Error, "Label-file not found!");
1103
                Log(LogMsgType.Error, "Please go to settings-tab and load the label texts from the copter control (FC & NC)");
1104
                Log(LogMsgType.Error, "When done, you have to save the label texts with the 'save' button!");
1105
            }
1106
        }
1107
        /// <summary>
1108
        /// no longer used...
1109
        /// assign the analog-label names from the textfile to the datatable
1110
        /// 
1111
        /// </summary>
1112
        private void _assignLabelNames()
1113
        {
1114
            if (lbLabels.Items.Count == 32)
1115
            {
1116
                lbLabels.Items.CopyTo(sAnalogLabel, 0);
1117
                for (int i = 0; i < 32; i++)
1118
                {
1119
                    if (dtAnalog.Rows.Count < 32)
1120
                        dtAnalog.Rows.Add(sAnalogLabel[i], "");
1121
                    else
1122
                        dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]);
1123
 
1124
                }
1125
            }
1126
        }
1127
        /// <summary>
1128
        /// get the version struct of actual controller
1129
        /// </summary>
1130
        /// <summary>
1131
        /// get the labeltexts for the analog values
1132
        /// </summary>
1133
        private void _getAnalogLabels()
1134
        {
1135
            if (simpleSerialPort.Port.IsOpen)
1136
            {
1137
                iLableIndex = 0;
1138
                for (int i = 0; i < 32; i++)
1139
                {
1140
                    Stream serialStream = simpleSerialPort.Port.BaseStream;
1141
                    byte[] bytes = FlightControllerMessage.CreateMessage('a', 0, new byte[1] { (byte)i });
1142
                    serialStream.Write(bytes, 0, bytes.Length);
1143
                    Thread.Sleep(10);
1144
                }
1145
            }
1146
            else
1147
                Log(LogMsgType.Error, "NOT CONNECTED!");
1148
        }
1149
        /// <summary>
1150
        /// get the labeltext for a single label
1151
        /// </summary>
1152
        /// <param name="iIndex">index of the label</param>
1153
        private void _getAnalogLabels(int iIndex)
1154
        {
1155
            if (simpleSerialPort.Port.IsOpen)
1156
            {
1157
                if (iIndex < 32)
1158
                {
1159
                    iLableIndex = iIndex;
1160
                    _sendControllerMessage('a', 0, new byte[1] { (byte)iLableIndex });
1161
                }
1162
            }
1163
            else
1164
                Log(LogMsgType.Error, "NOT CONNECTED!");
1165
        }
1166
        private void _getVersion()
1167
        {
1168
            _sendControllerMessage('v', 0);
1169
        }
1170
        /// <summary>
1171
        /// get FC version struct via NC
1172
        /// by sending '1' as data (not documented in wiki...)
1173
        /// returns HW error 255 (comment in uart1.c : tells the KopterTool that it is the FC-version)
1174
        /// </summary>
1175
        /// <param name="ctrl">controller number 1=FC</param> 
1176
        private void _getVersion(byte ctrl)
1177
        {
1178
            _sendControllerMessage('v', 0, new byte[1] {ctrl});
1179
        }
1180
        /// <summary>
1181
        /// Switch back to NC by sending the 'Magic Packet' 0x1B,0x1B,0x55,0xAA,0x00
1182
        /// </summary>
1183
        private void _switchToNC()
1184
        {
1185
            if (simpleSerialPort.Port.IsOpen)
1186
            {
1187
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1188
                byte[] bytes = new byte[5] { 0x1B,0x1B,0x55,0xAA,0x00 };
1189
                serialStream.Write(bytes, 0, bytes.Length);
1190
 
1191
                Thread.Sleep(100);
1192
                _getVersion();
1193
                Thread.Sleep(100);
1194
                _OSDMenue(0);
1195
            }
1196
            else
1197
                Log(LogMsgType.Error, "NOT CONNECTED!");
1198
        }
1199
        /// <summary>
1200
        /// switch to FC
1201
        /// </summary>
1202
        private void _switchToFC()
1203
        {
1204
            _sendControllerMessage('u', 2, new byte[1] { (byte)0 });
1205
            Thread.Sleep(100);
1206
            _getVersion();
1207
            Thread.Sleep(100);
1208
            _OSDMenue(0);
1209
        }
1210
        /// <summary>
1211
        /// send RESET signal to FC
1212
        /// </summary>
1213
        private void _resetCtrl()
1214
        {
1215
            _sendControllerMessage('R', 1);
1216
        }
1217
        /// <summary>
1218
        /// poll the debug data (4sec subscription)
1219
        /// </summary>
1220
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1221
        private void _readDebugData(bool auto)
1222
        {
1223
            byte interval = auto ? debugInterval : (byte)0;
1224
            _sendControllerMessage('d', 0, new byte[1] { debugInterval });
1225
        }
1226
        /// <summary>
1227
        /// poll the BL-CTRL status via NC (4sec subscription)
1228
        /// </summary>
1229
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1230
        private void _readBLCtrl(bool auto)
1231
        {
1232
            byte interval = auto ? blctrlInterval : (byte)0;
1233
            _sendControllerMessage('k', 0, new byte[1] { interval });
1234
        }
1235
        /// <summary>
1236
        /// poll the NC data struct (4sec subscription)
1237
        /// </summary>
1238
        /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param>
1239
        private void _readNavData(bool auto)
1240
        {
1241
            byte interval = auto ? navctrlInterval : (byte)0;
1242
            _sendControllerMessage('o', 2, new byte[1] { interval });
1243
        }
1244
        /// <summary>
1245
        /// get the errortext for pending NC error
1246
        /// </summary>
1247
        private void _readNCError()
1248
        {
1249
            _sendControllerMessage('e', 2);
1250
        }
1251
        /// <summary>
1252
        /// start/stop continous polling of controller values
1253
        /// </summary>
1254
        /// <param name="b">start/stop switch</param>
1255
        void _readCont(bool b)
1256
        {
1257
            bReadContinously = b;
2274 - 1258
 
1259
            if (Thread.CurrentThread.CurrentUICulture.Name == "")
1260
                btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.Text = bReadContinously ? "stop automatic" + Environment.NewLine + "data refresh" : "start automatic" + Environment.NewLine + "data refresh"));
1261
            else
1262
                btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.Text = bReadContinously ? "Aktualisierung" + Environment.NewLine + "beenden" : "Aktualisierung" + Environment.NewLine + "starten"));
2233 - 1263
            btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.BackColor = bReadContinously ? Color.FromArgb(192, 255, 192) : Color.FromArgb(224, 224, 224)));
1264
            if (bReadContinously)
1265
            {
2250 - 1266
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
1267
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
1268
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
1269
                if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10);}
2233 - 1270
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0)));
1271
            }
1272
            else
1273
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(224, 224, 224)));
1274
            _iLifeCounter = 0;
1275
        }
1276
        /// <summary>
2250 - 1277
        /// set values to "NA" when not available with FC
2233 - 1278
        /// </summary>
1279
        void _setFieldsNA()
1280
        {
1281
            Thread.Sleep(100);
2250 - 1282
            _initDTMotors();
2233 - 1283
            lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = "NA"));    //FlightTime
2250 - 1284
            lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA"));  //NC ErrorNr
1285
            lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA"));            //earth magnet field
1286
            lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA"));    //GroundSpeed
1287
            lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = "NA"));    //Distance to HP
1288
            lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA"));          //Sats used
1289
            lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = "NA"));  //GPS position - longitude
1290
            lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = "NA"));    //GPS position - latitude
2254 - 1291
            lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = "NA"));    //next WP distance
1292
            lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = "NA"));  //index of actual WP
1293
            lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = "NA"));  //count of items in WP list
2233 - 1294
        }
1295
        /// <summary>
1296
        /// one time query of the OSD Menue with pagenumber
1297
        /// </summary>
1298
        /// <param name="iMenue">Menue page</param>
1299
        void _OSDMenue(int iMenue)
1300
        {
1301
            if (simpleSerialPort.Port.IsOpen)
1302
            {
1303
                if (iMenue > iOSDMax)
1304
                    iMenue = 0;
1305
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1306
                byte[] bytes = FlightControllerMessage.CreateMessage('l', 0, new byte[1] { (byte)iMenue });
1307
                serialStream.Write(bytes, 0, bytes.Length);
1308
            }
1309
            else
1310
                Log(LogMsgType.Error, "NOT CONNECTED!");
1311
 
1312
        }
1313
        /// <summary>
1314
        /// call the OSDMenue and start autorefresh
1315
        ///  usually by sending a menuekey
1316
        /// 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)
1317
        /// therefore the value has to be negative (inverted) in order to distinguish from old (2 line) menuestyle
1318
        /// and must not have any bits of the menue keys 0x1 0x2 0x4 0x8 (0x10?) --> 0x20 = -33
1319
        /// </summary>
1320
        void _OSDMenueAutoRefresh()
1321
        {
1322
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)(-33)),OSDInterval });
1323
        }
1324
        void _OSDMenueAutoRefresh(byte key)
1325
        {
1326
            _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)~key), OSDInterval });
1327
        }
1328
        /// <summary>
1329
        /// initialize the OSD menue combobox
1330
        /// combox is filled by numbers from 0 to max pagenumber
1331
        /// </summary>
1332
        void _initOSDCB()
1333
        {
1334
            _bCBInit = true;
1335
            if(iOSDMax == 0)
1336
            {
1337
                _OSDMenue(0);
1338
                Thread.Sleep(10);
1339
            }
1340
            cbOSD.Invoke((Action)(()=>cbOSD.Items.Clear()));
1341
            for(int i = 0; i <= iOSDMax;i++)
1342
            {
1343
                cbOSD.Invoke((Action)(() => cbOSD.Items.Add(i)));
1344
            }
1345
            cbOSD.Invoke((Action)(() => cbOSD.SelectedItem = iOSDPage));
1346
            _bCBInit = false;
1347
        }
2259 - 1348
        /// <summary>
1349
        /// initialize the datatables
1350
        /// with columnnames etc
1351
        /// </summary>
1352
        void _dataTablesInit()
1353
        {
1354
            dtAnalog.Columns.Add("ID");
1355
            dtAnalog.Columns.Add("Value");
1356
            dataGridView1.DataSource = dtAnalog;
1357
 
1358
            dtMotors1.Columns.Add("#");
2274 - 1359
            if (Thread.CurrentThread.CurrentUICulture.Name == "")
1360
                dtMotors1.Columns.Add("Current");
1361
            else
1362
                dtMotors1.Columns.Add("Strom");
2259 - 1363
            dtMotors1.Columns.Add("Temp");
1364
            dtMotors2.Columns.Add("#");
2274 - 1365
            if (Thread.CurrentThread.CurrentUICulture.Name == "")
1366
                dtMotors2.Columns.Add("Current");
1367
            else
1368
                dtMotors2.Columns.Add("Strom");
2259 - 1369
            dtMotors2.Columns.Add("Temp");
1370
            dgvMotors1.DataSource = dtMotors1;
1371
            dgvMotors2.DataSource = dtMotors2;
1372
            _initDTMotors();
1373
            dgvMotors1.Columns[0].Width = 24;
1374
            dgvMotors1.Columns[1].Width = 74;
1375
            dgvMotors1.Columns[2].Width = 74;
1376
            dgvMotors2.Columns[0].Width = 24;
1377
            dgvMotors2.Columns[1].Width = 74;
1378
            dgvMotors2.Columns[2].Width = 74;
1379
 
1380
            dtWaypoints.Columns.Add("Index");
1381
            dtWaypoints.Columns.Add("Type");
1382
            dtWaypoints.Columns.Add("Name");
1383
            dtWaypoints.Columns.Add("Latitude");
1384
            dtWaypoints.Columns.Add("Longitude");
1385
            dtWaypoints.Columns.Add("Altitude");
1386
            dtWaypoints.Columns.Add("Heading");
1387
            dtWaypoints.Columns.Add("Speed");
1388
            dtWaypoints.Columns.Add("Altitude rate");
1389
            dtWaypoints.Columns.Add("Tol.radius");
1390
            dtWaypoints.Columns.Add("Hold time");
1391
            dtWaypoints.Columns.Add("AutoTrigger");
1392
            dtWaypoints.Columns.Add("Cam angle");
1393
            dtWaypoints.Columns.Add("Event");
1394
            dtWaypoints.Columns.Add("Eventchan. Val.");
1395
            dtWaypoints.Columns.Add("Status");
1396
            dgvWP.DataSource = dtWaypoints;
1397
        }
1398
        /// <summary>
1399
        /// read settings from ini-file
1400
        /// </summary>
2233 - 1401
        void _readIni()
1402
        {
1403
            if (!File.Exists(filePath + "\\MKLiveViewSettings.ini"))
1404
                _writeIni();
1405
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
1406
            ini.path = filePath + "\\MKLiveViewSettings.ini";
1407
 
1408
            string sVal = ini.IniReadValue("default", "AutorefreshDebugData");
1409
            _debugDataAutorefresh = Convert.ToBoolean(sVal);
1410
            sVal = ini.IniReadValue("default", "AutorefreshNavCtrlData");
1411
            _navCtrlDataAutorefresh = Convert.ToBoolean(sVal);
1412
            sVal = ini.IniReadValue("default", "AutorefreshBLCtrlData");
1413
            _blctrlDataAutorefresh = Convert.ToBoolean(sVal);
1414
            sVal = ini.IniReadValue("default", "AutorefreshOSDData");
1415
            _OSDAutorefresh = Convert.ToBoolean(sVal);
1416
 
1417
            sVal = ini.IniReadValue("default", "IntervalDebugData");
1418
            debugInterval = (byte)Convert.ToInt16(sVal);
1419
            sVal = ini.IniReadValue("default", "IntervalNavCtrlData");
1420
            navctrlInterval = (byte)Convert.ToInt16(sVal);
1421
            sVal = ini.IniReadValue("default", "IntervalBLCtrlData");
1422
            blctrlInterval = (byte)Convert.ToInt16(sVal);
1423
            sVal = ini.IniReadValue("default", "IntervalOSDData");
1424
            OSDInterval = (byte)Convert.ToInt16(sVal);
2257 - 1425
            for(int i = 0; i < 12; i++)
1426
            {
1427
                sVal = ini.IniReadValue("serial", "ch" + i.ToString() + "Val");
1428
                if(sVal != "")
1429
                    serChan[i] = Convert.ToInt16(sVal);
1430
                sVal = ini.IniReadValue("serial", "ch" + i.ToString() + "Title");
1431
                if(sVal != "")
1432
                    serChanTitle[i] = sVal;
2280 - 1433
                for (int y = 0; i < 4 && y < 3; y++)
1434
                {
1435
                    sVal = ini.IniReadValue("serial", "ch" + i.ToString() + "ValSub" + y.ToString());
1436
                    if (sVal != "")
1437
                        serChan_sub[(i*3) + y] = Convert.ToInt16(sVal);
1438
                }
2257 - 1439
            }
2233 - 1440
        }
2259 - 1441
        /// <summary>
1442
        /// save settings to ini-file
1443
        /// </summary>
2233 - 1444
        void _writeIni()
1445
        {
1446
 
1447
            IniFile ini = new IniFile("MKLiveViewSettings.ini");
1448
            ini.path = filePath + "\\MKLiveViewSettings.ini";
1449
 
1450
            ini.IniWriteValue("default", "AutorefreshDebugData", _debugDataAutorefresh ? "true":"false");
1451
            ini.IniWriteValue("default", "AutorefreshNavCtrlData", _navCtrlDataAutorefresh ? "true":"false");
1452
            ini.IniWriteValue("default", "AutorefreshBLCtrlData", _blctrlDataAutorefresh ? "true":"false");
1453
            ini.IniWriteValue("default", "AutorefreshOSDData", _OSDAutorefresh ? "true":"false");
1454
 
1455
            ini.IniWriteValue("default", "IntervalDebugData", debugInterval.ToString());
1456
            ini.IniWriteValue("default", "IntervalNavCtrlData", navctrlInterval.ToString());
1457
            ini.IniWriteValue("default", "IntervalBLCtrlData", blctrlInterval.ToString());
1458
            ini.IniWriteValue("default", "IntervalOSDData", OSDInterval.ToString());
2257 - 1459
 
1460
            for (int i = 0; i < 12; i++)
1461
            {
1462
                ini.IniWriteValue("serial", "ch" + i.ToString() + "Val", serChan[i].ToString());
1463
                ini.IniWriteValue("serial", "ch" + i.ToString() + "Title", serChanTitle[i]);
2280 - 1464
                for (int y = 0; i < 4 && y < 3; y++)
1465
                {
1466
                    ini.IniWriteValue("serial", "ch" + i.ToString() + "ValSub" + y.ToString(), serChan_sub[(i * 3) + y].ToString());
1467
                }
2257 - 1468
            }
1469
 
2233 - 1470
        }
1471
 
2250 - 1472
        /// <summary>
1473
        /// initialize the 2 datatables for motor values
1474
        /// dtMotors1 - motor 1 - 4
1475
        /// dtMotors2 - motor 5 - 8
1476
        /// DataGridView dgvMotors1/2 are bound to dtMotors1/2 
1477
        /// </summary>
1478
        void _initDTMotors()
1479
        {
1480
            for(int i = 0; i < 4; i++)
1481
            {
1482
                if (dtMotors1.Rows.Count < 4)
1483
                    dtMotors1.Rows.Add((i + 1).ToString(), "NA", "NA");
1484
                else
1485
                {
1486
                    dtMotors1.Rows[i].SetField(1, "NA");
1487
                    dtMotors1.Rows[i].SetField(2, "NA");
1488
                }
1489
                if (dtMotors2.Rows.Count < 4)
1490
                    dtMotors2.Rows.Add((i + 5).ToString(), "NA", "NA");
1491
                else
1492
                {
1493
                    dtMotors2.Rows[i].SetField(1, "NA");
1494
                    dtMotors2.Rows[i].SetField(2, "NA");
1495
                }
1496
            }
1497
            dgvMotors1.Invoke((Action)(() => dgvMotors1.Refresh()));
1498
            dgvMotors2.Invoke((Action)(() => dgvMotors2.Refresh()));
1499
        }
1500
 
1501
        /// <summary>
1502
        /// Convert decimal degrees to degrees, minutes, seconds, milliseconds 
1503
        /// </summary>
1504
        /// <param name="coord">the degree value as double</param>
1505
        /// <returns>0° 0' 0,0"</returns>
1506
        string _convertDegree(double coord)
1507
        {
1508
            //double minutes = (degree - Math.Floor(degree)) * 60.0;
1509
            //double seconds = (minutes - Math.Floor(minutes)) * 60.0;
1510
            //double tenths = (seconds - Math.Floor(seconds)) * 10.0;
1511
            //// get rid of fractional part
1512
            //minutes = Math.Floor(minutes);
1513
            //seconds = Math.Floor(seconds);
1514
            //tenths = Math.Floor(tenths);
1515
 
1516
 
1517
            //int sec = (int)Math.Round(coord * 3600);
1518
            //int deg = sec / 3600;
1519
            //sec = Math.Abs(sec % 3600);
1520
            //int min = sec / 60;
1521
            //sec %= 60;
1522
 
1523
            var ts = TimeSpan.FromHours(Math.Abs(coord));
1524
            double deg = Math.Sign(coord) * Math.Floor(ts.TotalHours);
1525
            int min = ts.Minutes;
1526
            int sec = ts.Seconds;
1527
            int milli = ts.Milliseconds;
1528
 
1529
            return deg.ToString("0° ") + min.ToString("0") + "' " + sec.ToString("0") + "," + milli.ToString() + "\"";
1530
        }
2257 - 1531
        /// <summary>
1532
        /// Clear the line in the  errorlog window 
1533
        /// containing the error string when error has ceased
1534
        /// </summary>
1535
        /// <param name="s">substring of errrormessage</param>
2254 - 1536
        void _clearErrorLog(string s)
1537
        {
1538
            rtfError.Invoke((Action)(() =>
1539
            {
1540
                if (rtfError.Text.Contains(s))
1541
                {
1542
                    int iLength = 0;
1543
                    int iStart = rtfError.Text.IndexOf(s);
1544
                    int iEnd = rtfError.Text.IndexOf('\n', iStart);
1545
                    if (iEnd > 0)
1546
                    {
1547
                        iLength = iEnd + 1;
1548
                        int iHttp = rtfError.Text.IndexOf("http", iEnd);
1549
                        if (iHttp == iLength)
1550
                        {
1551
                            int iEnd2 = rtfError.Text.IndexOf('\n', iLength);
1552
                            if (iEnd2 > iLength)
1553
                            {
1554
                                iLength = iEnd2 + 1;
1555
                                rtfError.Select(iStart, iLength);
1556
                                rtfError.SelectedText = string.Empty;
1557
                                if(rtfError.Text.Length < 2) _bErrorLog = false;
1558
                            }
1559
 
1560
                        }
1561
                        else
1562
                        {
1563
                            rtfError.Select(iStart, iLength);
1564
                            rtfError.SelectedText = string.Empty;
1565
                            if(rtfError.Text.Length < 2) _bErrorLog = false;
1566
                        }
1567
                    }
1568
                }
1569
            }));
1570
 
1571
        }
2257 - 1572
        /// <summary>
1573
        /// request the Waypoint at index
1574
        /// </summary>
1575
        /// <param name="index"></param>
1576
        void _getpWP(int index)
1577
        {
1578
            if (simpleSerialPort.Port.IsOpen)
1579
            {
1580
                Stream serialStream = simpleSerialPort.Port.BaseStream;
1581
                byte[] bytes = FlightControllerMessage.CreateMessage('x', 2, new byte[1] { (byte)index });
1582
                serialStream.Write(bytes, 0, bytes.Length);
1583
            }
1584
            else
1585
                Log(LogMsgType.Error, "NOT CONNECTED!");
1586
 
1587
        }
1588
        /// <summary>
1589
        /// Sending the serial channel values
1590
        /// </summary>
1591
        void _sendSerialData()
1592
        {
1593
            byte[] serData = new byte[12];
1594
            for(int i = 0; i < 12; i++)
1595
            {
1596
                serData[i] = unchecked((byte)(serChan[i] - 127));
1597
            }
1598
            _sendControllerMessage('y', 1, serData);
1599
        }
2259 - 1600
        /// <summary>
1601
        /// init the controls for displaying
1602
        /// and setting serial control channels
1603
        /// </summary>
2257 - 1604
        void _initSerialCtrl()
1605
        {
1606
            trckbarSerial1.Value = serChan[0];
1607
            textBoxSerial1.Text = serChanTitle[0];
1608
            lblTbSerial1.Text = serChan[0].ToString();
1609
            trckbarSerial2.Value = serChan[1];
1610
            textBoxSerial2.Text = serChanTitle[1];
1611
            lblTbSerial2.Text = serChan[1].ToString();
2259 - 1612
            trckbarSerial3.Value = serChan[2];
1613
            textBoxSerial3.Text = serChanTitle[2];
1614
            lblTbSerial3.Text = serChan[2].ToString();
1615
            trckbarSerial4.Value = serChan[3];
1616
            textBoxSerial4.Text = serChanTitle[3];
1617
            lblTbSerial4.Text = serChan[3].ToString();
2265 - 1618
            trckbarSerial5.Value = serChan[4];
1619
            textBoxSerial5.Text = serChanTitle[4];
1620
            lblTbSerial5.Text = serChan[4].ToString();
1621
            trckbarSerial6.Value = serChan[5];
1622
            textBoxSerial6.Text = serChanTitle[5];
1623
            lblTbSerial6.Text = serChan[5].ToString();
1624
            trckbarSerial7.Value = serChan[6];
1625
            textBoxSerial7.Text = serChanTitle[6];
1626
            lblTbSerial7.Text = serChan[6].ToString();
1627
            trckbarSerial8.Value = serChan[7];
1628
            textBoxSerial8.Text = serChanTitle[7];
1629
            lblTbSerial8.Text = serChan[7].ToString();
2280 - 1630
 
1631
            textBoxSerial1_val1.Text = serChan_sub[0].ToString();
1632
            textBoxSerial1_val2.Text = serChan_sub[1].ToString();
1633
            textBoxSerial1_val3.Text = serChan_sub[2].ToString();
1634
            textBoxSerial2_val1.Text = serChan_sub[3].ToString();
1635
            textBoxSerial2_val2.Text = serChan_sub[4].ToString();
1636
            textBoxSerial2_val3.Text = serChan_sub[5].ToString();
1637
            textBoxSerial3_val1.Text = serChan_sub[6].ToString();
1638
            textBoxSerial3_val2.Text = serChan_sub[7].ToString();
1639
            textBoxSerial3_val3.Text = serChan_sub[8].ToString();
1640
            textBoxSerial4_val1.Text = serChan_sub[9].ToString();
1641
            textBoxSerial4_val2.Text = serChan_sub[10].ToString();
1642
            textBoxSerial4_val3.Text = serChan_sub[11].ToString();
2257 - 1643
        }
2233 - 1644
        #endregion functions
1645
 
1646
        #region buttons
1647
        private void buttonReset_Click(object sender, EventArgs e)
1648
        {
1649
            _resetCtrl();
1650
        }
1651
        private void btnVersion_Click(object sender, EventArgs e)
1652
        {
1653
            _getVersion();
1654
        }
1655
        private void btnAnalogLabels_Click(object sender, EventArgs e)
1656
        {
1657
            _getAnalogLabels(0);
1658
        }
1659
        private void btnDbgData_Click(object sender, EventArgs e)
1660
        {
1661
            _readDebugData(false); //onetime reading of debug data --> subscription lasts 4sec - this means you will receive data for 4 seconds
1662
        }
1663
        private void btnSaveLabels_Click(object sender, EventArgs e)
1664
        {
1665
            switch (_iCtrlAct)
1666
            {
1667
                case 1:
1668
                    fileName = "FCLabelTexts.txt";
1669
                    break;
1670
                case 2:
1671
                    fileName = "NCLabelTexts.txt";
1672
                    break;
1673
                default:
1674
                    fileName = "NCLabelTexts.txt";
1675
                    break;
1676
            }
1677
            if (sAnalogLabel[0] != null)
1678
            {
1679
                File.WriteAllLines(filePath + "\\" + fileName, sAnalogLabel);
1680
                Console.WriteLine("Names saved to file");
1681
                _loadLabelFile();
1682
            }
1683
            else
1684
                Log(LogMsgType.Warning, "there's no data -> read first from fc/nc!");
1685
        }
1686
        private void btnLoadLabels_Click(object sender, EventArgs e)
1687
        {
1688
            _assignLabelNames();
1689
        }
1690
        private void btnReadLabelFile_Click(object sender, EventArgs e)
1691
        {
1692
            _loadLabelFile();
1693
        }
1694
        private void btnSwitchFC_Click(object sender, EventArgs e)
1695
        {
1696
            _switchToFC();
1697
        }
1698
        private void btnSwitchNC_Click(object sender, EventArgs e)
1699
        {
1700
            _switchToNC();
1701
        }
1702
        private void btnReadDbgCont_Click(object sender, EventArgs e)
1703
        {
1704
            _readCont(!bReadContinously);
1705
        }
1706
        private void btnReadBLCtrl_Click(object sender, EventArgs e)
1707
        {
1708
 
1709
            if (_iCtrlAct == 2) _readBLCtrl(false);
1710
            else Log(LogMsgType.Warning, "only available when connected to NC");
1711
        }
1712
        private void btnGetNaviData_Click(object sender, EventArgs e)
1713
        {
1714
            if (_iCtrlAct == 2) _readNavData(false);
1715
            else Log(LogMsgType.Warning, "only available when connected to NC");
1716
        }
1717
        private void btnConn_Click(object sender, EventArgs e)
1718
        {
1719
            simpleSerialPort.Connect(!simpleSerialPort.Port.IsOpen);
1720
        }
1721
        private void button3_Click(object sender, EventArgs e)
1722
        {
1723
            _getVersion(1);
1724
        }
1725
        private void button4_Click(object sender, EventArgs e)
1726
        {
1727
            _getVersion(2);
1728
        }
1729
        private void btnOSD_Click(object sender, EventArgs e)
1730
        {
1731
            if (iOSDPage > iOSDMax)
1732
                iOSDPage = 0;
1733
            _OSDMenue(iOSDPage);
1734
        }
1735
        private void btnOSDForward_Click(object sender, EventArgs e)
1736
        {
1737
            iOSDPage++;
1738
            if (iOSDPage > iOSDMax)
1739
                iOSDPage = 0;
1740
 
1741
            _OSDMenue(iOSDPage);
1742
        }
1743
        private void btnOSDBackward_Click(object sender, EventArgs e)
1744
        {
1745
            iOSDPage--;
1746
            if (iOSDPage < 0)
1747
                iOSDPage = iOSDMax;
1748
 
1749
            _OSDMenue(iOSDPage);
1750
        }
1751
        private void btnOSDAuto_Click(object sender, EventArgs e)
1752
        {
1753
            _OSDMenueAutoRefresh();
1754
        }
1755
        /// call the OSDMenue with Key 0x8
1756
        private void btnOSDLeave_Click(object sender, EventArgs e)
1757
        {
1758
            _OSDMenueAutoRefresh(8);
1759
        }
1760
        /// call the OSDMenue with Key 0x4
1761
        private void btnOSDEnter_Click(object sender, EventArgs e)
1762
        {
1763
            _OSDMenueAutoRefresh(4);
1764
        }
2257 - 1765
        private void btnGetWP_Click(object sender, EventArgs e)
1766
        {
1767
            if(cbWPIndex.Items.Count >0)
1768
            _getpWP((int)cbWPIndex.SelectedItem);
1769
            else
1770
                _getpWP(1);
1771
        }
2233 - 1772
        #endregion buttons
2259 - 1773
        #region serial control channels - buttons & events
2257 - 1774
        private void tbSerial1_Scroll(object sender, EventArgs e)
1775
        {
1776
            lblTbSerial1.Text = trckbarSerial1.Value.ToString();
1777
            serChan[0] = trckbarSerial1.Value;
1778
            if (!_init) _sendSerialData();
1779
        }
1780
        private void textBoxSerial1_TextChanged(object sender, EventArgs e)
1781
        {
1782
            serChanTitle[0] = textBoxSerial1.Text;
1783
        }
1784
        private void btnSer1_0_Click(object sender, EventArgs e)
1785
        {
1786
            trckbarSerial1.Value = 0;
1787
        }
1788
        private void btnSer1_127_Click(object sender, EventArgs e)
1789
        {
1790
            trckbarSerial1.Value = 127;
1791
        }
1792
        private void btnSer1_254_Click(object sender, EventArgs e)
1793
        {
1794
            trckbarSerial1.Value = 254;
1795
        }
1796
        private void trckbarSerial1_ValueChanged(object sender, EventArgs e)
1797
        {
1798
            lblTbSerial1.Text = trckbarSerial1.Value.ToString();
1799
            serChan[0] = trckbarSerial1.Value;
1800
            if (!_init) _sendSerialData();
1801
        }
1802
        private void textBoxSerial2_TextChanged(object sender, EventArgs e)
1803
        {
2259 - 1804
            serChanTitle[1] = textBoxSerial2.Text;
2257 - 1805
        }
1806
        private void trckbarSerial2_ValueChanged(object sender, EventArgs e)
1807
        {
1808
            lblTbSerial2.Text = trckbarSerial2.Value.ToString();
1809
            serChan[1] = trckbarSerial2.Value;
1810
            if (!_init) _sendSerialData();
1811
        }
1812
        private void btnSer2_0_Click(object sender, EventArgs e)
1813
        {
1814
            trckbarSerial2.Value = 0;
1815
        }
1816
        private void btnSer2_127_Click(object sender, EventArgs e)
1817
        {
1818
            trckbarSerial2.Value = 127;
1819
        }
1820
        private void btnSer2_254_Click(object sender, EventArgs e)
1821
        {
1822
            trckbarSerial2.Value = 254;
1823
        }
2259 - 1824
        private void textBoxSerial3_TextChanged(object sender, EventArgs e)
1825
        {
1826
            serChanTitle[2] = textBoxSerial3.Text;
1827
        }
1828
        private void trckbarSerial3_ValueChanged(object sender, EventArgs e)
1829
        {
1830
            lblTbSerial3.Text = trckbarSerial3.Value.ToString();
1831
            serChan[2] = trckbarSerial3.Value;
1832
            if (!_init) _sendSerialData();
1833
        }
1834
        private void btnSer3_0_Click(object sender, EventArgs e)
1835
        {
1836
            trckbarSerial3.Value = 0;
1837
        }
1838
        private void btnSer3_127_Click(object sender, EventArgs e)
1839
        {
1840
            trckbarSerial3.Value = 127;
1841
        }
1842
        private void btnSer3_254_Click(object sender, EventArgs e)
1843
        {
1844
            trckbarSerial3.Value = 254;
1845
        }
1846
        private void textBoxSerial4_TextChanged(object sender, EventArgs e)
1847
        {
1848
            serChanTitle[3] = textBoxSerial4.Text;
1849
        }
1850
        private void trckbarSerial4_ValueChanged(object sender, EventArgs e)
1851
        {
1852
            lblTbSerial4.Text = trckbarSerial4.Value.ToString();
1853
            serChan[3] = trckbarSerial4.Value;
1854
            if (!_init) _sendSerialData();
1855
        }
1856
        private void btnSer4_0_Click(object sender, EventArgs e)
1857
        {
1858
            trckbarSerial4.Value = 0;
1859
        }
1860
        private void btnSer4_127_Click(object sender, EventArgs e)
1861
        {
1862
            trckbarSerial4.Value = 127;
1863
        }
1864
        private void btnSer4_254_Click(object sender, EventArgs e)
1865
        {
1866
            trckbarSerial4.Value = 254;
1867
        }
2265 - 1868
        private void tbSerial5_Scroll(object sender, EventArgs e)
1869
        {
1870
            lblTbSerial5.Text = trckbarSerial5.Value.ToString();
1871
            serChan[4] = trckbarSerial5.Value;
1872
            if (!_init) _sendSerialData();
1873
        }
1874
        private void textBoxSerial5_TextChanged(object sender, EventArgs e)
1875
        {
1876
            serChanTitle[4] = textBoxSerial5.Text;
1877
        }
1878
        private void btnSer5_0_Click(object sender, EventArgs e)
1879
        {
1880
            trckbarSerial5.Value = 0;
1881
        }
1882
        private void btnSer5_127_Click(object sender, EventArgs e)
1883
        {
1884
            trckbarSerial5.Value = 127;
1885
        }
1886
        private void btnSer5_254_Click(object sender, EventArgs e)
1887
        {
1888
            trckbarSerial5.Value = 254;
1889
        }
1890
        private void trckbarSerial5_ValueChanged(object sender, EventArgs e)
1891
        {
1892
            lblTbSerial5.Text = trckbarSerial5.Value.ToString();
1893
            serChan[4] = trckbarSerial5.Value;
1894
            if (!_init) _sendSerialData();
1895
        }
1896
        private void tbSerial6_Scroll(object sender, EventArgs e)
1897
        {
1898
            lblTbSerial6.Text = trckbarSerial6.Value.ToString();
1899
            serChan[5] = trckbarSerial6.Value;
1900
            if (!_init) _sendSerialData();
1901
        }
1902
        private void textBoxSerial6_TextChanged(object sender, EventArgs e)
1903
        {
1904
            serChanTitle[5] = textBoxSerial6.Text;
1905
        }
1906
        private void btnSer6_0_Click(object sender, EventArgs e)
1907
        {
1908
            trckbarSerial6.Value = 0;
1909
        }
1910
        private void btnSer6_127_Click(object sender, EventArgs e)
1911
        {
1912
            trckbarSerial6.Value = 127;
1913
        }
1914
        private void btnSer6_254_Click(object sender, EventArgs e)
1915
        {
1916
            trckbarSerial6.Value = 254;
1917
        }
1918
        private void trckbarSerial6_ValueChanged(object sender, EventArgs e)
1919
        {
1920
            lblTbSerial6.Text = trckbarSerial6.Value.ToString();
1921
            serChan[5] = trckbarSerial6.Value;
1922
            if (!_init) _sendSerialData();
1923
        }
1924
        private void tbSerial7_Scroll(object sender, EventArgs e)
1925
        {
1926
            lblTbSerial7.Text = trckbarSerial7.Value.ToString();
1927
            serChan[6] = trckbarSerial7.Value;
1928
            if (!_init) _sendSerialData();
1929
        }
1930
        private void textBoxSerial7_TextChanged(object sender, EventArgs e)
1931
        {
1932
            serChanTitle[6] = textBoxSerial7.Text;
1933
        }
1934
        private void btnSer7_0_Click(object sender, EventArgs e)
1935
        {
1936
            trckbarSerial7.Value = 0;
1937
        }
1938
        private void btnSer7_127_Click(object sender, EventArgs e)
1939
        {
1940
            trckbarSerial7.Value = 127;
1941
        }
1942
        private void btnSer7_254_Click(object sender, EventArgs e)
1943
        {
1944
            trckbarSerial7.Value = 254;
1945
        }
1946
        private void trckbarSerial7_ValueChanged(object sender, EventArgs e)
1947
        {
1948
            lblTbSerial7.Text = trckbarSerial7.Value.ToString();
1949
            serChan[6] = trckbarSerial7.Value;
1950
            if (!_init) _sendSerialData();
1951
        }
1952
        private void tbSerial8_Scroll(object sender, EventArgs e)
1953
        {
1954
            lblTbSerial8.Text = trckbarSerial8.Value.ToString();
1955
            serChan[7] = trckbarSerial8.Value;
1956
            if (!_init) _sendSerialData();
1957
        }
1958
        private void textBoxSerial8_TextChanged(object sender, EventArgs e)
1959
        {
1960
            serChanTitle[7] = textBoxSerial8.Text;
1961
        }
1962
        private void btnSer8_0_Click(object sender, EventArgs e)
1963
        {
1964
            trckbarSerial8.Value = 0;
1965
        }
1966
        private void btnSer8_127_Click(object sender, EventArgs e)
1967
        {
1968
            trckbarSerial8.Value = 127;
1969
        }
1970
        private void btnSer8_254_Click(object sender, EventArgs e)
1971
        {
1972
            trckbarSerial8.Value = 254;
1973
        }
1974
        private void trckbarSerial8_ValueChanged(object sender, EventArgs e)
1975
        {
1976
            lblTbSerial8.Text = trckbarSerial8.Value.ToString();
1977
            serChan[7] = trckbarSerial8.Value;
1978
            if (!_init) _sendSerialData();
1979
        }
2280 - 1980
        /// <summary>
1981
        /// checks the input of a textbox
1982
        /// for valid data
1983
        /// in this case values 0-254
1984
        /// </summary>
1985
        /// <param name="sender"></param>
1986
        /// <param name="e"></param>
1987
        private void textBox_serial_KeyPress(object sender, KeyPressEventArgs e)
1988
        {
1989
            if ("1234567890,\b".IndexOf(e.KeyChar.ToString()) < 0) //general check for valid chars(0-9) and backspace (\b)
1990
                e.Handled = true;
1991
            else
1992
            if ("\b".IndexOf(e.KeyChar.ToString()) < 0)
1993
                if (Convert.ToInt16(((TextBox)sender).Text + e.KeyChar) > 254) //if valid and not backspace, check for upper limit of the resulting number
1994
                    e.Handled = true;
1995
                else
1996
                {
1997
                    int i = -1;
1998
                    switch (((TextBox)sender).Name)
1999
                    {
2000
                        case "textBoxSerial1_val1":
2001
                            i = 0;
2002
                            break;
2003
                        case "textBoxSerial1_val2":
2004
                            i = 1;
2005
                            break;
2006
                        case "textBoxSerial1_val3":
2007
                            i = 2;
2008
                            break;
2009
                        case "textBoxSerial2_val1":
2010
                            i = 3;
2011
                            break;
2012
                        case "textBoxSerial2_val2":
2013
                            i = 4;
2014
                            break;
2015
                        case "textBoxSerial2_val3":
2016
                            i = 5;
2017
                            break;
2018
                        case "textBoxSerial3_val1":
2019
                            i = 6;
2020
                            break;
2021
                        case "textBoxSerial3_val2":
2022
                            i = 7;
2023
                            break;
2024
                        case "textBoxSerial3_val3":
2025
                            i = 8;
2026
                            break;
2027
                        case "textBoxSerial4_val1":
2028
                            i = 9;
2029
                            break;
2030
                        case "textBoxSerial4_val2":
2031
                            i = 10;
2032
                            break;
2033
                        case "textBoxSerial4_val3":
2034
                            i = 11;
2035
                            break;
2036
                    }
2037
                    if (i > -1)
2038
                        serChan_sub[i] = Convert.ToInt16(((TextBox)sender).Text + e.KeyChar);
2039
                }
2040
        }
2041
 
2042
        private void btnSer1_val1_Click(object sender, EventArgs e)
2043
        {
2044
            if(textBoxSerial1_val1.Text.Length > 0)
2045
            {
2046
                trckbarSerial1.Value = Convert.ToInt16(textBoxSerial1_val1.Text);
2047
            }
2048
        }
2049
        private void btnSer1_val2_Click(object sender, EventArgs e)
2050
        {
2051
            if(textBoxSerial1_val2.Text.Length > 0)
2052
            {
2053
                trckbarSerial1.Value = Convert.ToInt16(textBoxSerial1_val2.Text);
2054
            }
2055
        }
2056
        private void btnSer1_val3_Click(object sender, EventArgs e)
2057
        {
2058
            if(textBoxSerial1_val3.Text.Length > 0)
2059
            {
2060
                trckbarSerial1.Value = Convert.ToInt16(textBoxSerial1_val3.Text);
2061
            }
2062
        }
2063
        private void btnSer2_val1_Click(object sender, EventArgs e)
2064
        {
2065
            if(textBoxSerial2_val1.Text.Length > 0)
2066
            {
2067
                trckbarSerial2.Value = Convert.ToInt16(textBoxSerial2_val1.Text);
2068
            }
2069
        }
2070
        private void btnSer2_val2_Click(object sender, EventArgs e)
2071
        {
2072
            if(textBoxSerial2_val2.Text.Length > 0)
2073
            {
2074
                trckbarSerial2.Value = Convert.ToInt16(textBoxSerial2_val2.Text);
2075
            }
2076
        }
2077
        private void btnSer2_val3_Click(object sender, EventArgs e)
2078
        {
2079
            if(textBoxSerial2_val3.Text.Length > 0)
2080
            {
2081
                trckbarSerial2.Value = Convert.ToInt16(textBoxSerial2_val3.Text);
2082
            }
2083
        }
2084
        private void btnSer3_val1_Click(object sender, EventArgs e)
2085
        {
2086
            if(textBoxSerial3_val1.Text.Length > 0)
2087
            {
2088
                trckbarSerial3.Value = Convert.ToInt16(textBoxSerial3_val1.Text);
2089
            }
2090
        }
2091
        private void btnSer3_val2_Click(object sender, EventArgs e)
2092
        {
2093
            if(textBoxSerial3_val2.Text.Length > 0)
2094
            {
2095
                trckbarSerial3.Value = Convert.ToInt16(textBoxSerial3_val2.Text);
2096
            }
2097
        }
2098
        private void btnSer3_val3_Click(object sender, EventArgs e)
2099
        {
2100
            if(textBoxSerial3_val3.Text.Length > 0)
2101
            {
2102
                trckbarSerial3.Value = Convert.ToInt16(textBoxSerial3_val3.Text);
2103
            }
2104
        }
2105
        private void btnSer4_val1_Click(object sender, EventArgs e)
2106
        {
2107
            if(textBoxSerial4_val1.Text.Length > 0)
2108
            {
2109
                trckbarSerial4.Value = Convert.ToInt16(textBoxSerial4_val1.Text);
2110
            }
2111
        }
2112
        private void btnSer4_val2_Click(object sender, EventArgs e)
2113
        {
2114
            if(textBoxSerial4_val2.Text.Length > 0)
2115
            {
2116
                trckbarSerial4.Value = Convert.ToInt16(textBoxSerial4_val2.Text);
2117
            }
2118
        }
2119
        private void btnSer4_val3_Click(object sender, EventArgs e)
2120
        {
2121
            if(textBoxSerial4_val3.Text.Length > 0)
2122
            {
2123
                trckbarSerial4.Value = Convert.ToInt16(textBoxSerial4_val3.Text);
2124
            }
2125
        }
2259 - 2126
        #endregion serial control channels
2233 - 2127
    }
2128
    public class IniFile
2129
    {
2130
        public string path;
2131
 
2132
        [DllImport("kernel32")]
2133
        private static extern long WritePrivateProfileString(string section,
2134
          string key, string val, string filePath);
2135
 
2136
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
2137
        static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer,
2138
               uint nSize, string lpFileName);
2139
 
2140
        [DllImport("kernel32")]
2141
        private static extern int GetPrivateProfileString(string section,
2142
          string key, string def, StringBuilder retVal,
2143
          int size, string filePath);
2144
 
2145
        public IniFile(string INIPath)
2146
        {
2147
            path = INIPath;
2148
        }
2149
 
2150
        public void IniWriteValue(string Section, string Key, string Value)
2151
        {
2152
            WritePrivateProfileString(Section, Key, Value, this.path);
2153
        }
2154
 
2155
        public string IniReadValue(string Section, string Key)
2156
        {
2157
            StringBuilder temp = new StringBuilder(255);
2158
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
2159
            return temp.ToString();
2160
        }
2161
        //Ini_sections auslesen in String-Array
2162
        public string[] IniSectionNames()
2163
        {
2164
 
2165
            //  uint MAX_BUFFER = 32767;
2166
            uint MAX_BUFFER = 8388608;
2167
            IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
2168
            uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
2169
            if (bytesReturned == 0)
2170
            {
2171
                Marshal.FreeCoTaskMem(pReturnedString);
2172
                return null;
2173
            }
2174
            string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
2175
            Marshal.FreeCoTaskMem(pReturnedString);
2176
            //use of Substring below removes terminating null for split
2177
            return local.Substring(0, local.Length - 1).Split('\0');
2178
 
2179
 
2180
        }
2181
    }
2182
    public static class ControlExtensions
2183
    {
2184
        /// <summary> 
2185
        /// Execute a threadsafe operation, when accessing a control via another thread 
2186
        /// action is a lamdaexpression
2187
        /// e.g. comboBox1.ExecuteThreadSafe(() => comboBox1.Enabled = true);
2188
        /// </summary>
2189
        /// <param name="control"> The control </param>
2190
        /// <param name="action"> The 'action' to perform </param>
2191
        public static void ExecuteThreadSafe(this Control control, Action action)
2192
        {
2193
            if (control.InvokeRequired)
2194
            {
2195
                control.BeginInvoke(action); //"BeginInvoke" is an async call -> threadsafety error when called to many times successively -> then take "Invoke"
2196
            }
2197
            else
2198
            {
2199
                action.Invoke();
2200
            }
2201
        }
2202
    }
2203
 
2204
}