forked from amatteo78/GPSClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmDefault.cs
210 lines (188 loc) · 9.17 KB
/
FrmDefault.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System.Net.Sockets;
using System.Text.RegularExpressions;
namespace GPSClient
{
public partial class FrmDefault : Form
{
//Inizialize variable boolStop for Start/Stop Stream
bool boolStop = false;
public FrmDefault()
{
InitializeComponent();
txtPort.Text = "5005";
}
public static bool IsValidIP(string addr) //Check if IP is Valid
{
//Create our Regular Expression object
Regex check = new("^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
//Check to make sure an ip address was provided
if (addr == "")
{
//No address provided so return false
return false;
}
else
{
//Address provided so use the IsMatch Method
//Of the Regular Expression object
return check.IsMatch(addr, 0);
}
}
private void ReceiveDataGPS()
{
Task.Run(() =>
{
//Inizialize TcpCLient
TcpClient tcpClient = new();
//Check if IP Address and Port are present
if (string.IsNullOrEmpty(txtGpsServer.Text) || string.IsNullOrEmpty(txtPort.Text))
{
MessageBox.Show("GPS Server IP Address and/or Port can't be Empty", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (!IsValidIP(txtGpsServer.Text))
{
MessageBox.Show("IP Address must be need !!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
//'command.Invoke' manage object outside thread
btnStart.Invoke(new Action(() =>
{
btnStart.Enabled = false;
}));
//Estabilish connection and check if it's valid
IAsyncResult? resultConn = tcpClient.BeginConnect(txtGpsServer.Text, Convert.ToInt16(txtPort.Text), null, null);
bool resultSuccess = resultConn.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
if (!resultSuccess)
{
MessageBox.Show("Failed to connect !!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
btnStart.Invoke(new Action(() =>
{
btnStart.Enabled = true;
}));
}
else
{
//Start capture stream from GPS Server
StreamReader streamReader = new(tcpClient.GetStream());
try
{
//Read line by line and print each one in txtBox like debug
string? data = streamReader.ReadLine();
while (data != null)
{
txtResult.Invoke(new Action(() =>
{
txtResult.Text += data + Environment.NewLine;
txtResult.SelectionStart = txtResult.TextLength;
txtResult.ScrollToCaret();
txtResult.Refresh();
}));
//Read lines start with "$GPGLL" to keep Lat and Long, then I'll copy them in boxs Lat and Long
if (data.StartsWith("$GPGLL"))
{
//Format string NMEA Standard to keep Lat and Long, Index 1 for Lat and Index 3 for Long
string LatLong = data.ToString();
string[] arrLatLong = LatLong.Split(",");
string Lat;
string Long;
if (string.IsNullOrEmpty(arrLatLong[1]))
{
Lat = "Lat - GPS No Signal";
}
else
{
string BeforeDot = arrLatLong[1][..arrLatLong[1].IndexOf('.')];
string Degree = BeforeDot[..^2];
string Minutes = BeforeDot[^2..] + arrLatLong[1][arrLatLong[1].IndexOf('.')..];
string MinutesCalculated = (Convert.ToDecimal(Minutes) / 60).ToString();
Lat = Degree + "." + MinutesCalculated[(MinutesCalculated.IndexOf('.') + 1)..];
}
if (string.IsNullOrEmpty(arrLatLong[3]))
{
Long = "Long - GPS No Signal";
}
else
{
string BeforeDot = arrLatLong[3][..arrLatLong[3].IndexOf('.')];
string Degree = BeforeDot[..^2];
string Minutes = BeforeDot[^2..] + arrLatLong[3][arrLatLong[3].IndexOf('.')..];
string MinutesCalculated = (Convert.ToDecimal(Minutes) / 60).ToString();
Long = Degree + "." + MinutesCalculated[(MinutesCalculated.IndexOf('.') + 1)..];
}
//If South or West put a minus before the value.
Lat = arrLatLong[2] == "S" ? ("-" + Lat) : Lat;
Long = arrLatLong[4] == "W" ? ("-" + Long) : Long;
//Converting to decimal to setting a format.
Lat = Convert.ToDecimal(Lat).ToString("00.000000");
Long = Convert.ToDecimal(Long).ToString("000.000000");
txtLat.Invoke(new Action(() =>
{
//Read index 1 for keep Lat + 2 for N
txtLat.Text = Lat;
}));
txtLong.Invoke(new Action(() =>
{
//Read index 3 for keep Long + 4 for E
txtLong.Text = Long;
}));
}
data = streamReader.ReadLine();
//Check variable Stop, if true stop Stream and close Tcpclient
if (boolStop)
{
streamReader.Close();
tcpClient.Close();
return;
}
}
}
catch //(Exception ex)
{
btnStart.Invoke(new Action(() =>
{
btnStart.Enabled = true;
}));
streamReader.Close();
tcpClient.Close();
MessageBox.Show("Problem Occured !!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//MessageBox.Show(ex.ToString());
}
}
}
});
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
//Change bool state to Stop stream
boolStop = true;
Application.Exit();
}
private void btnStart_Click(object sender, EventArgs e)
{
//Call function "ReceiveDataGPS"
//Manage separately thread for data received otherwise freeze application
boolStop = false;
txtResult.Text = "";
txtLat.Text = "";
txtLong.Text = "";
ReceiveDataGPS();
}
private void btnStop_Click(object sender, EventArgs e)
{
//Change bool state to Stop stream
boolStop = true;
btnStart.Enabled = true;
}
private void infoToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmInfo frmInfo = new();
frmInfo.ShowDialog();
}
private void FrmDefault_FormClosing(object sender, FormClosingEventArgs e)
{
//Change bool state to Stop stream
boolStop = true;
}
}
}