chunxiaqiu
2026-03-18 46431fb658701489f8d5de4475b02df728c51f36
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
 
using LB_VisionControl;
using LB_VisionProcesses.Communicators;
using VisionControl.Forms;
 
namespace LB_SmartVision.Forms.Pages.CommunicatorPage
{
    public partial class CommunicatorsEditPage : UserControl
    {
        public Action<string, LogInfoType> LogInfo;
 
        public CommunicatorsEditPage()
        {
            InitializeComponent();
 
            Name = "CommunicatorsPage";
            Text = "通讯设置";
            //this.Font = new Font("Microsoft YaHei UI", 16F, FontStyle.Regular, GraphicsUnit.Point, 0);
        }
 
        private void CommunicatorsEditPage_Paint(object sender, PaintEventArgs e)
        {
            uiFlowLayoutPanel1.Controls.Clear();
 
            if (GlobalVar.dicCommunicators.Count <= 0)
                return;
 
            foreach (var item in GlobalVar.dicCommunicators)
            {
                string ClassName = item.Value.GetType().Name;// "TCP"
                string IP = item.Value.CommunicatorConnections["地址"].ToString();//"127.0.0.1"
                string PORT = item.Value.CommunicatorConnections["端口"].ToString();//"1111"
 
                if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                    return;
 
                string DataType = item.Value.CommunicatorConnections.Contains("数据类型") ?
                    item.Value.CommunicatorConnections["数据类型"].ToString() : "String";
                string CommunicatorConnectionString = $"({ClassName}){IP}:{PORT} [{DataType}]";
 
                UserItem flow = new UserItem(new string[] { "测试", "刷新", "移除", "重命名" });
                //flow.SetDPIScale();
                flow.Name = item.Key;
                flow.Text = CommunicatorConnectionString;
                if (item.Value.bConnected)
                    flow.state = State.Pass;
                else
                    flow.state = State.Error;
                LoadFlowEvent(flow);
 
                uiFlowLayoutPanel1.Controls.Add(flow);
            }
        }
 
        private void uiButton1_Click(object sender, System.EventArgs e)
        {
            CreateCommunicatorForm createCommunicatorForm = new CreateCommunicatorForm();
            createCommunicatorForm.ShowDialog();
 
            if (createCommunicatorForm.bCreate)
            {
                BaseCommunicator communicator = createCommunicatorForm.communicator;
                string Name = "通讯口" + GlobalVar.dicCommunicators.Count;
                if (GlobalVar.dicCommunicators.ContainsKey(Name))
                    Name += "副本";
 
                string ClassName = communicator.GetType().Name;// "TCP"
                string IP = communicator.CommunicatorConnections["地址"].ToString();//"127.0.0.1"
                string PORT = communicator.CommunicatorConnections["端口"].ToString();//"1111"
 
                if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                    return;
 
                string DataType = communicator.CommunicatorConnections.Contains("数据类型") ?
                    communicator.CommunicatorConnections["数据类型"].ToString() : "String";
                string CommunicatorConnectionString = $"({ClassName}){IP}:{PORT} [{DataType}]";
 
                GlobalVar.dicCommunicators.TryAdd(Name, communicator);
                communicator.CommunicatorName = Name;
                LogInfo?.Invoke(string.Format("添加通讯口[{0}]成功", Name), LogInfoType.PASS);
            }
            this.Invalidate();
        }
 
        private void uiButton2_Click(object sender, System.EventArgs e)
        {
            //清除用Clear方法
            uiFlowLayoutPanel1.Controls.Clear();
            foreach (var communicator in GlobalVar.dicCommunicators.Values)
            {
                communicator.Disconnect();
            }
            GlobalVar.dicCommunicators.Clear();
            this.Refresh();
        }
 
        private void LoadFlowEvent(UserItem flow)
        {
            //按键1为测试
            flow.MenuItem1ClickedEvent += TestEvent;
            //按键2为刷新
            flow.MenuItem2ClickedEvent += ReconnectEvent;
            //按键3为移除
            flow.MenuItem3ClickedEvent += DeleteEvent;
            //按键4为重命名
            flow.MenuItem4ClickedEvent += RenameEvent;
        }
 
        private void RemoveFlowEvent(UserItem flow)
        {
            //按键1为测试
            flow.MenuItem1ClickedEvent -= TestEvent;
            //按键2为刷新
            flow.MenuItem2ClickedEvent -= ReconnectEvent;
            //按键3为移除
            flow.MenuItem3ClickedEvent -= DeleteEvent;
            //按键4为重命名
            flow.MenuItem4ClickedEvent -= RenameEvent;
        }
 
        // 测试通讯口
        private void TestEvent(string Name, string Text)
        {
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                UserItem flow = (UserItem)uiFlowLayoutPanel1.Controls[i];
                string name = flow.Name;
                if (name != Name)
                    continue;
 
                if (GlobalVar.dicCommunicators.ContainsKey(name))
                {
                    CommunicatorForm communicatorFForm = new CommunicatorForm(GlobalVar.dicCommunicators[name], name);
                    communicatorFForm.CommunicatorChanged += CommunicatorFForm_CommunicatorChanged;
                    communicatorFForm.Show();
                }
            }
        }
 
        private void CommunicatorFForm_CommunicatorChanged(BaseCommunicator communicator)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<BaseCommunicator>(CommunicatorFForm_CommunicatorChanged), communicator);
                return;
            }
 
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                if (uiFlowLayoutPanel1.Controls[i] is UserItem flow && flow.Name == communicator.CommunicatorName)
                {
                    string ClassName = communicator.GetType().Name;
                    string IP = communicator.CommunicatorConnections["地址"].ToString();
                    string PORT = communicator.CommunicatorConnections["端口"].ToString();
                    string DataType = communicator.CommunicatorConnections.Contains("数据类型") ?
                        communicator.CommunicatorConnections["数据类型"].ToString() : "String";
 
                    flow.Text = $"({ClassName}){IP}:{PORT} [{DataType}]";
                    flow.Refresh();
                    break;
                }
            }
        }
 
        // 移除通讯口
        private void DeleteEvent(string Name, string Text)
        {
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                UserItem flow = (UserItem)uiFlowLayoutPanel1.Controls[i];
                string name = flow.Name;
                if (name != Name)
                    continue;
 
                RemoveFlowEvent(flow);
                uiFlowLayoutPanel1.Controls.Remove(uiFlowLayoutPanel1.Controls[i]);
                GlobalVar.dicCommunicators.TryRemove(name, out BaseCommunicator BaseCommunicator);
                if (BaseCommunicator != null)
                    LogInfo?.Invoke(string.Format("移除通讯口[{0}]", name), LogInfoType.INFO);
            }
        }
 
        private void RenameEvent(string Name, string Text)
        {
            RenameForm renameForm = new RenameForm(Name);
            // 订阅事件
            renameForm.ShowDialog();
            if (!renameForm.bRename)
                return;
 
            if (GlobalVar.dicCommunicators.ContainsKey(renameForm.strNewName))
            {
                MessageBox.Show("命名重复!", "异常");
                return;
            }
 
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                UserItem flow = (UserItem)uiFlowLayoutPanel1.Controls[i];
                string name = flow.Name;
                if (name != Name)
                    continue;
                flow.Name = renameForm.strNewName;
                string oldName = name;
                string newName = renameForm.strNewName;
 
                if (GlobalVar.dicCommunicators.ContainsKey(oldName))
                    GlobalVar.dicCommunicators.TryRename(oldName, newName);
                foreach (var item in GlobalVar.dicMotionControlData.Keys)
                {
                    foreach (var item1 in GlobalVar.dicMotionControlData[item].Keys)
                    {
                        if (GlobalVar.dicMotionControlData[item][item1].CommunicatorsName.Contains(oldName))
                        {
                            GlobalVar.dicMotionControlData[item][item1].CommunicatorsName = newName;
                        }
                    }
                }
 
                flow.Refresh();
            }
        }
 
        // 重连流程
        private void ReconnectEvent(string Name, string Text)
        {
            for (int i = 0; i < uiFlowLayoutPanel1.Controls.Count; i++)
            {
                UserItem flow = (UserItem)uiFlowLayoutPanel1.Controls[i];
                string name = flow.Name;
                if (name != Name)
                    continue;
 
                GlobalVar.dicCommunicators[name].Disconnect();
                Thread.Sleep(50);
                GlobalVar.dicCommunicators[name].Connect();
                if (GlobalVar.dicCommunicators[name].bConnected)
                {
                    flow.state = State.Pass;
                    LogInfo?.Invoke(string.Format("重连通讯口[{0}]成功", name), LogInfoType.PASS);
                }
                else
                {
                    flow.state = State.Error;
                    LogInfo?.Invoke(string.Format("重连通讯口[{0}]失败", name), LogInfoType.ERROR);
                }
                flow.Refresh();
            }
        }
    }
}