Form1.cs
6.4 KB
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
using AGVLib;
using Common;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace AgvComTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string[] server = AppConfigSetting.GetStrVal("server", "127.0.0.1:12000").Split(':');
if (server != null && server.Length == 2)
{
txtIP.Text = server[0];
txtPort.Text = server[1];
}
client = new Client();
client.ConnectedEvent += Client_ConnectedEvent;
client.ReceivedEvent += Client_ReceivedEvent;
comboBox1.Items.AddRange(Enum.GetNames(typeof(NodeStatus)));
comboBox2.Items.AddRange(Enum.GetNames(typeof(NodeLevel)));
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
button1.Enabled = true;
button2.Enabled = false;
}
private void Client_ReceivedEvent(Node node)
{
this.Invoke(new Action(()=>
{
try
{
txtJSON.Text = "";
txtBytes.Text = "";
groupBox4.Text = $"字节码";
string json = JsonHelper.SerializeObject(node);
textBox1.Text = node.name;
txtNodeId.Text = node.id.ToString();
checkBox1.Checked = node.shielded;
txtShelfId.Text = node.shelf_id;
txtRemark.Text = node.remark;
comboBox1.SelectedIndex = (int)node.status;
comboBox2.SelectedIndex = (int)node.level;
byte[] bytes = AGVLib.Common.Encode(node);
txtBytes.Text = StringHelper.ToHexString(bytes);
txtJSON.Text = json;
groupBox4.Text = $"字节码-长度为{bytes.Length}";
}
catch
{
txtJSON.Text = "收到的数据解码失败";
}
}));
}
private void Client_ConnectedEvent(bool status)
{
label8.Invoke(new Action(()=> { label8.BackColor = status ? Color.Green : Color.Red; }));
}
private Client client;
private void button1_Click(object sender, EventArgs e)
{
client.Connect(txtIP.Text, int.Parse(txtPort.Text));
button1.Enabled = false;
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
client.Close();
button1.Enabled = true;
button2.Enabled = false;
}
/// <summary>
/// 编码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
txtBytes.Text = "";
txtJSON.Text = "";
groupBox4.Text = $"字节码";
int status = comboBox1.SelectedIndex;
int level = comboBox2.SelectedIndex;
Node node = new Node()
{
name = textBox1.Text,
id = int.Parse(txtNodeId.Text),
shielded = checkBox1.Checked,
shelf_id = txtShelfId.Text,
remark = txtRemark.Text,
status = (NodeStatus)comboBox1.SelectedIndex,
level = (NodeLevel)comboBox2.SelectedIndex
};
byte[] bytes = AGVLib.Common.Encode(node);
string json = JsonHelper.SerializeObject(node);
txtBytes.Text = StringHelper.ToHexString(bytes);
groupBox4.Text = $"字节码-长度为{bytes.Length}";
txtJSON.Text = json;
}
catch
{
txtBytes.Text = "编码失败";
txtJSON.Text = "编码失败";
}
}
private void button5_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtBytes.Text)) return;
try
{
txtJSON.Text = "";
groupBox4.Text = $"字节码";
byte[] bytes = StringHelper.StrToHexByte(txtBytes.Text);
Node node = AGVLib.Common.Decode(bytes);
string json = JsonHelper.SerializeObject(node);
txtJSON.Text = json;
textBox1.Text = node.name;
txtNodeId.Text = node.id.ToString();
checkBox1.Checked = node.shielded;
txtShelfId.Text = node.shelf_id;
txtRemark.Text = node.remark;
comboBox1.SelectedIndex = (int)node.status;
comboBox2.SelectedIndex = (int)node.level;
groupBox4.Text = $"字节码-长度为{bytes.Length}";
}
catch
{
txtJSON.Text = "解码失败";
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
txtBytes.Text = "";
txtJSON.Text = "";
groupBox4.Text = $"字节码";
int status = comboBox1.SelectedIndex;
int level = comboBox2.SelectedIndex;
Node node = new Node()
{
name = textBox1.Text,
id = int.Parse(txtNodeId.Text),
shelf_id = txtShelfId.Text,
remark = txtRemark.Text,
status = (NodeStatus)comboBox1.SelectedIndex,
level = (NodeLevel)comboBox2.SelectedIndex
};
client.Shielded = checkBox1.Checked;
byte[] bytes = AGVLib.Common.Encode(node);
string json = JsonHelper.SerializeObject(node);
txtBytes.Text = StringHelper.ToHexString(bytes);
txtJSON.Text = json;
client.SetStatus(node);
groupBox4.Text = $"字节码-长度为{bytes.Length}";
}
catch
{
txtBytes.Text = "发送失败";
txtJSON.Text = "发送失败";
}
}
private void txtPort_TextChanged(object sender, EventArgs e)
{
try
{
AppConfigSetting.SetStrVal("server",$"{txtIP.Text}:{txtPort.Text}");
}
catch { }
}
}
}