-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrRobotRobotConnection.cs
212 lines (184 loc) · 6.82 KB
/
DrRobotRobotConnection.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
211
212
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 System.Diagnostics;
using System.Xml;
using System.Threading;
using System.Text.RegularExpressions;
namespace DrRobot.JaguarControl
{
public partial class DrRobotRobotConnection : Form
{
public RobotConfig robotConfig = new RobotConfig();
private RobotConfig.RobotConfigTableRow row = null;
public static Process p1 = null;
private const string configFile = "c:\\DrRobotAppFile\\OutDoorRobotConfig.xml";
//public static JaguarCtrl controlForm = null;
public JaguarCtrl controlForm = null;
public DrRobotRobotConnection(JaguarCtrl jc)
{
InitializeComponent();
controlForm = jc;
DrRobotRobotConnection_Load();
}
private void DrRobotRobotConnection_Load()
{
robotConfig.Clear();
try
{
robotConfig.ReadXml(configFile);
}
catch
{
MessageBox.Show("Unable to open robot configure file!", "DrRobot Jaguar Control", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
row = (RobotConfig.RobotConfigTableRow)robotConfig.RobotConfigTable.Rows[0];
txtRobotID.Text = row.RobotID;
txtRobotIP.Text = row.RobotIP;
txtGPSIP.Text = row.GPSIP;
txtCameraIP.Text = row.CameraIP;
txtCameraPWD.Text = row.CameraPWD;
txtCameraID.Text = row.CameraUser;
}
public void connectRobot()
{
//save any change to xml file
row.LaserRangeIP = txtRobotIP.Text;
row.RobotID = txtRobotID.Text;
row.RobotIP = txtRobotIP.Text;
row.GPSIP = txtGPSIP.Text;
row.IMUIP = txtGPSIP.Text;
row.CameraIP = txtCameraIP.Text;
//row.CameraPWD = txtCameraPWD.Text;
row.CameraUser = txtCameraID.Text;
try
{
robotConfig.WriteXml(configFile);
}
catch
{
}
//write gateway config file
XmlTextWriter textWriter = new XmlTextWriter("C:\\DrRobotAppFile\\gatewayConfig.xml", null);
textWriter.WriteStartDocument();
// Write comments
textWriter.WriteComment("For Gateway Communication");
// Write first element
textWriter.WriteStartElement("GateWaySetting", "");
// Write next element
textWriter.WriteStartElement("RobotID", "");
textWriter.WriteString(row.RobotID);
textWriter.WriteEndElement();
textWriter.WriteStartElement("RobotMethod", "");
textWriter.WriteString("WiFi");
//textWriter.WriteString("Serial");
textWriter.WriteEndElement();
textWriter.WriteStartElement("RobotCom", "");
textWriter.WriteString("COM1");
textWriter.WriteEndElement();
textWriter.WriteStartElement("RobotIP", "");
textWriter.WriteString(row.RobotIP);
textWriter.WriteEndElement();
textWriter.WriteStartElement("RobotPort", "");
textWriter.WriteString("10001");
textWriter.WriteEndElement();
// Ends the document.
textWriter.WriteEndElement();
textWriter.WriteEndDocument();
// close writer
textWriter.Close();
Thread.Sleep(1000);
//start one gateway
p1 = new Process();
p1.StartInfo.FileName = "C:\\DrRobotAppFile\\WirobotGateway.exe";
p1.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p1.Start();
Thread.Sleep(1000);
controlForm.robotCfg = robotConfig;
this.Close();
}
private void btnConnect_Click(object sender, EventArgs e)
{
connectRobot();
}
private void DrRobotRobotConnection_FormClosing(object sender, FormClosingEventArgs e)
{
DrRobotRobotConnection_Kill();
}
public void DrRobotRobotConnection_Kill()
{
try
{
if (p1 != null)
p1.Kill();
}
catch
{
}
}
/// <summary>
/// method to validate an IP address
/// using regular expressions. The pattern
/// being used will validate an ip address
/// with the range of 1.0.0.0 to 255.255.255.255
/// </summary>
/// <param name="addr">Address to validate</param>
/// <returns></returns>
public bool IsValidIP(string addr)
{
//create our match pattern
string pattern = @"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$";
//create our Regular Expression object
Regex check = new Regex(pattern);
//boolean variable to hold the status
bool valid = false;
//check to make sure an ip address was provided
if (addr == "")
{
//no address provided so return false
valid = false;
}
else
{
//address provided so use the IsMatch Method
//of the Regular Expression object
valid = check.IsMatch(addr, 0);
}
//return the results
return valid;
}
private void txtRobotIP_Leave(object sender, EventArgs e)
{
string addre = txtRobotIP.Text;
if (!IsValidIP(addre))
{
MessageBox.Show("This is an invalid address!", "DrRobot Jaguar Control", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtRobotIP.Focus();
}
}
private void txtGPSIP_Leave(object sender, EventArgs e)
{
string addre = txtGPSIP.Text;
if (!IsValidIP(addre))
{
MessageBox.Show("This is an invalid address!", "DrRobot Jaguar Control", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtGPSIP.Focus();
}
}
private void txtCameraIP_Leave(object sender, EventArgs e)
{
string addre = txtCameraIP.Text;
if (!IsValidIP(addre))
{
MessageBox.Show("This is an invalid address!", "DrRobot Motion/Power Control", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtCameraIP.Focus();
}
}
}
}