|
@@ -3,7 +3,9 @@ using System.Collections.Generic;
|
3
|
3
|
using System.ComponentModel;
|
4
|
4
|
using System.Data;
|
5
|
5
|
using System.Drawing;
|
|
6
|
+using System.IO.Ports;
|
6
|
7
|
using System.Linq;
|
|
8
|
+using System.Runtime.InteropServices;
|
7
|
9
|
using System.Text;
|
8
|
10
|
using System.Threading.Tasks;
|
9
|
11
|
using System.Windows.Forms;
|
|
@@ -32,6 +34,7 @@ namespace Jdas_Mbic
|
32
|
34
|
private void JdasMbic_Load(object sender, EventArgs e)
|
33
|
35
|
{
|
34
|
36
|
ATT_TableLayoutSetting();
|
|
37
|
+ Serial_Initialize(ref comboBox_Port); // Uart SerialPort Find Init
|
35
|
38
|
}
|
36
|
39
|
|
37
|
40
|
private void ATT_TableLayoutSetting()
|
|
@@ -281,5 +284,232 @@ namespace Jdas_Mbic
|
281
|
284
|
dataGridView_TableSetting[1, TmpList[i]].Value = Convert.ToString(CurrentVal + SumVal);
|
282
|
285
|
}
|
283
|
286
|
}
|
|
287
|
+
|
|
288
|
+ private void button_PortOpen_Click(object sender, EventArgs e)
|
|
289
|
+ {
|
|
290
|
+
|
|
291
|
+ }
|
|
292
|
+ private void button_terminal_Click(object sender, EventArgs e)
|
|
293
|
+ {
|
|
294
|
+ Terminal tml = new Terminal();
|
|
295
|
+ tml.Show();
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ private void button3_Click(object sender, EventArgs e)
|
|
299
|
+ {
|
|
300
|
+
|
|
301
|
+ }
|
|
302
|
+ public void Serial_Initialize(ref ComboBox cb_port)
|
|
303
|
+ {
|
|
304
|
+
|
|
305
|
+ this.serialPort_JdasMbic = new System.IO.Ports.SerialPort();
|
|
306
|
+ this.serialPort_JdasMbic.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.sPort_DataReceived);
|
|
307
|
+
|
|
308
|
+ cb_port.BeginUpdate();
|
|
309
|
+ foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
|
|
310
|
+ {
|
|
311
|
+ cb_port.Items.Add(comport);
|
|
312
|
+ }
|
|
313
|
+ cb_port.EndUpdate();
|
|
314
|
+ //SerialPort 초기 설정.
|
|
315
|
+ // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
|
|
316
|
+ cb_port.DataSource = SerialPort.GetPortNames();
|
|
317
|
+ try
|
|
318
|
+ {
|
|
319
|
+ serialPort_JdasMbic.PortName = cb_port.SelectedItem.ToString();
|
|
320
|
+ serialPort_JdasMbic.BaudRate = (int)115200;
|
|
321
|
+ serialPort_JdasMbic.DataBits = (int)8;
|
|
322
|
+ serialPort_JdasMbic.Parity = System.IO.Ports.Parity.None;
|
|
323
|
+ serialPort_JdasMbic.StopBits = StopBits.One;
|
|
324
|
+ }
|
|
325
|
+ catch { }
|
|
326
|
+
|
|
327
|
+ }
|
|
328
|
+ private delegate void StringSend(string Text);
|
|
329
|
+ private delegate void GridviewSend(ref object gridview, string Text);
|
|
330
|
+ private delegate void ByteSend(byte[] Text);
|
|
331
|
+
|
|
332
|
+ int offset = 0;
|
|
333
|
+ byte[] rxBuffer;
|
|
334
|
+
|
|
335
|
+ private void sPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
336
|
+ {
|
|
337
|
+ string startStr = "V";
|
|
338
|
+ string endStr = "\n";
|
|
339
|
+
|
|
340
|
+ try
|
|
341
|
+ {
|
|
342
|
+
|
|
343
|
+ if (offset == 0)
|
|
344
|
+ {
|
|
345
|
+ rxBuffer = new byte[4096];
|
|
346
|
+ }
|
|
347
|
+
|
|
348
|
+ string rxString = "";
|
|
349
|
+ int intRecSize = serialPort_JdasMbic.BytesToRead;
|
|
350
|
+
|
|
351
|
+ if (intRecSize != 0)
|
|
352
|
+ {
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+ /* if (panel_GpsTest.Visible == true)
|
|
358
|
+ {
|
|
359
|
+ byte[] btdata = new byte[intRecSize];
|
|
360
|
+ serialPort_JdasMbic.Read(btdata, 0, intRecSize);
|
|
361
|
+ string data = Encoding.Default.GetString(rxBuffer);
|
|
362
|
+
|
|
363
|
+ this.Invoke(new StringSend(Data_Recv_Str), data);
|
|
364
|
+ }
|
|
365
|
+ else*/
|
|
366
|
+ {
|
|
367
|
+ serialPort_JdasMbic.Read(rxBuffer, offset, intRecSize);
|
|
368
|
+ offset += intRecSize;
|
|
369
|
+ for (int iTemp = 0; iTemp < offset; iTemp++)
|
|
370
|
+ {
|
|
371
|
+ rxString += Convert.ToChar(rxBuffer[iTemp]);
|
|
372
|
+ }
|
|
373
|
+ /*
|
|
374
|
+ *
|
|
375
|
+ * Example
|
|
376
|
+ * if (panel_SensorTest.Visible == true)
|
|
377
|
+ {
|
|
378
|
+ startStr = "V";
|
|
379
|
+ endStr = "\n";
|
|
380
|
+ }
|
|
381
|
+
|
|
382
|
+ else if (panel_Catm1Test.Visible == true)
|
|
383
|
+ {
|
|
384
|
+ startStr = "B";
|
|
385
|
+ endStr = "\n";
|
|
386
|
+ }
|
|
387
|
+ else if (panel_GpsTest.Visible == true)
|
|
388
|
+ {
|
|
389
|
+ startStr = "$";
|
|
390
|
+ endStr = "\n";
|
|
391
|
+ }
|
|
392
|
+
|
|
393
|
+ */
|
|
394
|
+ if (rxString.Contains(startStr))
|
|
395
|
+ {
|
|
396
|
+ rxString = rxString.Substring(rxString.IndexOf(startStr));
|
|
397
|
+ if (rxString.Contains(endStr))
|
|
398
|
+ {
|
|
399
|
+ this.Invoke(new StringSend(Data_Recv_Str), rxString);
|
|
400
|
+ //program.dataReceived(rxString, "Receive");
|
|
401
|
+ offset = 0;
|
|
402
|
+ }
|
|
403
|
+ }
|
|
404
|
+ else
|
|
405
|
+ {
|
|
406
|
+ this.Invoke(new StringSend(Data_Recv_Str), rxString);
|
|
407
|
+ offset = 0;
|
|
408
|
+ }
|
|
409
|
+ }
|
|
410
|
+
|
|
411
|
+ }
|
|
412
|
+ }
|
|
413
|
+ catch { }
|
|
414
|
+
|
|
415
|
+ }
|
|
416
|
+ int LineLimit = 500;
|
|
417
|
+ [DllImport("user32.dll")]
|
|
418
|
+ public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
|
|
419
|
+ private const int WM_SETREDRAW = 11;
|
|
420
|
+ public void Data_Recv_Str(string text)
|
|
421
|
+ {
|
|
422
|
+
|
|
423
|
+ System.Windows.Forms.RichTextBox tbReceived = null;
|
|
424
|
+ //tbReceived = richTextBox_Sensor;
|
|
425
|
+
|
|
426
|
+ int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
|
|
427
|
+ try
|
|
428
|
+ {
|
|
429
|
+ try
|
|
430
|
+ {
|
|
431
|
+ SendMessage(this.Handle, WM_SETREDRAW, false, 0);
|
|
432
|
+ }
|
|
433
|
+ catch (Exception e) { MessageBox.Show(e.StackTrace); }
|
|
434
|
+ if (tbReceived.Lines.Length > nLimitLines)
|
|
435
|
+ {
|
|
436
|
+ LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
|
|
437
|
+ while ((tempLines.Count - nLimitLines) > 0)
|
|
438
|
+ {
|
|
439
|
+ tempLines.RemoveFirst();
|
|
440
|
+ }
|
|
441
|
+ tbReceived.Lines = tempLines.ToArray();
|
|
442
|
+ }
|
|
443
|
+ try
|
|
444
|
+ {
|
|
445
|
+ SendMessage(this.Handle, WM_SETREDRAW, true, 0);
|
|
446
|
+ }
|
|
447
|
+ catch { return; }
|
|
448
|
+ /* Example
|
|
449
|
+ if (panel_SensorTest.Visible == true)
|
|
450
|
+ {
|
|
451
|
+
|
|
452
|
+ Sensor_TestFunc(text);
|
|
453
|
+ tbReceived.AppendText(text);
|
|
454
|
+ }
|
|
455
|
+ else
|
|
456
|
+ {
|
|
457
|
+ tbReceived.AppendText(text);
|
|
458
|
+ }
|
|
459
|
+
|
|
460
|
+ */
|
|
461
|
+
|
|
462
|
+ tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
|
|
463
|
+ tbReceived.ScrollToCaret();
|
|
464
|
+ }
|
|
465
|
+ catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
|
|
466
|
+ }
|
|
467
|
+ public void Data_Recv_Str(byte[] text)
|
|
468
|
+ {
|
|
469
|
+ string strtext = Encoding.Default.GetString(text);
|
|
470
|
+
|
|
471
|
+ System.Windows.Forms.RichTextBox tbReceived = null;
|
|
472
|
+
|
|
473
|
+// tbReceived = richTextBox_Sensor;
|
|
474
|
+
|
|
475
|
+ int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
|
|
476
|
+ try
|
|
477
|
+ {
|
|
478
|
+ try
|
|
479
|
+ {
|
|
480
|
+ SendMessage(this.Handle, WM_SETREDRAW, false, 0);
|
|
481
|
+ }
|
|
482
|
+ catch (Exception e) { MessageBox.Show(e.StackTrace); }
|
|
483
|
+ if (tbReceived.Lines.Length > nLimitLines)
|
|
484
|
+ {
|
|
485
|
+ LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
|
|
486
|
+ while ((tempLines.Count - nLimitLines) > 0)
|
|
487
|
+ {
|
|
488
|
+ tempLines.RemoveFirst();
|
|
489
|
+ }
|
|
490
|
+ tbReceived.Lines = tempLines.ToArray();
|
|
491
|
+ }
|
|
492
|
+ try
|
|
493
|
+ {
|
|
494
|
+ SendMessage(this.Handle, WM_SETREDRAW, true, 0);
|
|
495
|
+ }
|
|
496
|
+ catch { return; }
|
|
497
|
+ /* Example
|
|
498
|
+ if (panel_SensorTest.Visible == true)
|
|
499
|
+ {
|
|
500
|
+ Sensor_TestFunc(strtext);
|
|
501
|
+ tbReceived.AppendText(strtext);
|
|
502
|
+ }
|
|
503
|
+ else
|
|
504
|
+ {
|
|
505
|
+ tbReceived.AppendText(strtext);
|
|
506
|
+ }
|
|
507
|
+ */
|
|
508
|
+ tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
|
|
509
|
+ tbReceived.ScrollToCaret();
|
|
510
|
+ }
|
|
511
|
+ catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
|
|
512
|
+ }
|
|
513
|
+
|
284
|
514
|
}
|
285
|
515
|
}
|