forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialInjectGPS.cs
143 lines (129 loc) · 4.32 KB
/
SerialInjectGPS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MissionPlanner.Comms;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using log4net;
namespace MissionPlanner
{
public partial class SerialInjectGPS : Form
{
private static readonly ILog log =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
internal static ICommsSerial comPort = new SerialPort();
private System.Threading.Thread t12;
private static bool threadrun = false;
static TcpListener listener;
// Thread signal.
public static ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
public SerialInjectGPS()
{
InitializeComponent();
CMB_serialport.Items.AddRange(SerialPort.GetPortNames());
CMB_serialport.Items.Add("UDP Host - 14550");
CMB_serialport.Items.Add("UDP Client");
CMB_serialport.Items.Add("TCP Client");
CMB_serialport.Items.Add("NTRIP");
if (threadrun)
{
BUT_connect.Text = Strings.Stop;
}
MissionPlanner.Utilities.Tracking.AddPage(this.GetType().ToString(), this.Text);
}
private void BUT_connect_Click(object sender, EventArgs e)
{
if (comPort.IsOpen)
{
threadrun = false;
comPort.Close();
BUT_connect.Text = Strings.Connect;
}
else
{
try
{
switch (CMB_serialport.Text)
{
case "NTRIP":
comPort = new CommsNTRIP();
CMB_baudrate.Text = "0";
break;
case "TCP Client":
comPort = new TcpSerial();
break;
case "UDP Host - 14550":
comPort = new UdpSerial();
break;
case "UDP Client":
comPort = new UdpSerialConnect();
break;
default:
comPort = new SerialPort();
comPort.PortName = CMB_serialport.Text;
break;
}
}
catch
{
CustomMessageBox.Show(Strings.InvalidPortName);
return;
}
try
{
comPort.BaudRate = int.Parse(CMB_baudrate.Text);
}
catch
{
CustomMessageBox.Show(Strings.InvalidBaudRate);
return;
}
try
{
comPort.Open();
}
catch
{
CustomMessageBox.Show("Error Connecting\nif using com0com please rename the ports to COM??");
return;
}
t12 = new System.Threading.Thread(new System.Threading.ThreadStart(mainloop))
{
IsBackground = true,
Name = "injectgps"
};
t12.Start();
BUT_connect.Text = Strings.Stop;
}
}
private void mainloop()
{
threadrun = true;
int counter = 0;
while (threadrun)
{
try
{
// limit to 110 byte packets
byte[] buffer = new byte[110];
while (comPort.BytesToRead > 0)
{
int read = comPort.Read(buffer, 0, Math.Min(buffer.Length, comPort.BytesToRead));
MainV2.comPort.InjectGpsData(buffer, (byte)read);
}
System.Threading.Thread.Sleep(10);
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
}
}