轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-04-01 4d6c9ad3c9714d5f8520c6f52c3ed6ecb75b76b8
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
using LB_VisionControls;
using LB_VisionProcesses.BarcodeReaders;
using LB_VisionProcesses.BarcodeReaders.Huayray;
using System;
using System.Collections.Concurrent;
using System.Windows.Forms;
 
namespace LB_SmartVision.Forms.Pages.BarcodeReaderPage
{
    public partial class BarcodeReadersEditPage : UserControl
    {
        public Action<string, LogInfoType> LogInfo;
 
 
        public BarcodeReadersEditPage()
        {
            InitializeComponent();
 
            Name = "BarcodeReadersEditPage";
            Text = "读码器设置";
        }
 
        private void BarcodeReadersEditPage_Paint(object sender, PaintEventArgs e)
        {
            uiFlowLayoutPanel1.Controls.Clear();
 
            if (GlobalVar.dicBarcodeReaders.Count <= 0)
                return;
 
            foreach (var item in GlobalVar.dicBarcodeReaders)
            {
                string ReaderSN = item.Key;
                string ReaderBrand = item.Value.Brand.ToString();
 
                if (string.IsNullOrEmpty(ReaderSN) || string.IsNullOrEmpty(ReaderBrand))
                    continue;
 
                UserItem flow = new UserItem(new string[] { "测试", "刷新", "移除" });
                flow.Name = ReaderSN;
                flow.Text = $"[{ReaderBrand}] {ReaderSN}";
                if (item.Value.IsConnected)
                    flow.state = State.Pass;
                else
                    flow.state = State.Error;
                LoadFlowEvent(flow);
 
                uiFlowLayoutPanel1.Controls.Add(flow);
            }
        }
 
 
        private void uiButton1_Click(object sender, System.EventArgs e)
        {
            CreateBarcodeReaderForm createForm = new CreateBarcodeReaderForm();
            createForm.ShowDialog();
 
            if (createForm.bCreate)
            {
                BarcodeReaderBase reader = createForm.barcodeReader;
                if (GlobalVar.dicBarcodeReaders.ContainsKey(reader.SN))
                {
                    MessageBox.Show($"读码器[{reader.SN}]已存在!", "异常");
                    return;
                }
 
                string ReaderSN = reader.SN;
                string ReaderBrand = reader.Brand.ToString();
 
                if (string.IsNullOrEmpty(ReaderSN) || string.IsNullOrEmpty(ReaderBrand))
                    return;
 
                GlobalVar.dicBarcodeReaders.TryAdd(ReaderSN, reader);
                LogInfo?.Invoke(string.Format("添加读码器[{0}][{1}]成功", ReaderBrand, ReaderSN), LogInfoType.PASS);
            }
            this.Invalidate();
        }
 
        private void uiButton2_Click(object sender, System.EventArgs e)
        {
            //清除用Clear方法
            uiFlowLayoutPanel1.Controls.Clear();
            foreach (var reader in GlobalVar.dicBarcodeReaders.Values)
            {
                reader.Dispose();
            }
            GlobalVar.dicBarcodeReaders.Clear();
        }
 
        private void LoadFlowEvent(UserItem flow)
        {
            //按键1为测试
            flow.MenuItem1ClickedEvent += TestEvent;
            //按键2为刷新
            flow.MenuItem2ClickedEvent += ReconnectEvent;
            //按键3为移除
            flow.MenuItem3ClickedEvent += DeleteEvent;
        }
 
        private void RemoveFlowEvent(UserItem flow)
        {
            //按键1为测试
            flow.MenuItem1ClickedEvent -= TestEvent;
            //按键2为刷新
            flow.MenuItem2ClickedEvent -= ReconnectEvent;
            //按键3为移除
            flow.MenuItem3ClickedEvent -= DeleteEvent;
        }
 
        // 测试读码器
        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.dicBarcodeReaders.ContainsKey(name))
                {
                    BarcodeReaderForm readerForm = new BarcodeReaderForm(GlobalVar.dicBarcodeReaders[name]);
                    readerForm.Show();
                }
            }
        }
 
        // 移除读码器
        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]);
                if (GlobalVar.dicBarcodeReaders.TryRemove(name, out BarcodeReaderBase barcodeReader))
                {
                    barcodeReader.Dispose();
                    LogInfo?.Invoke(string.Format("移除读码器[{0}]", name), LogInfoType.INFO);
                }
                else
                    LogInfo?.Invoke(string.Format("移除读码器[{0}]失败", name), LogInfoType.ERROR);
 
            }
        }
 
        // 重连读码器
        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;
 
                if (!GlobalVar.dicBarcodeReaders.ContainsKey(name))
                {
                    LogInfo?.Invoke(string.Format("读码器[{0}]不存在", name), LogInfoType.ERROR);
                    flow.state = State.Error;
                    flow.Refresh();
                    return;
                }
 
                var reader = GlobalVar.dicBarcodeReaders[name];
                if (reader == null)
                {
                    LogInfo?.Invoke(string.Format("读码器[{0}]实例为空", name), LogInfoType.ERROR);
                    flow.state = State.Error;
                    flow.Refresh();
                    return;
                }
 
                try
                {
                    // 先释放旧实例的资源
                    reader.Dispose();
 
                    // 添加短暂延迟,确保SDK底层资源完全释放
                    System.Threading.Thread.Sleep(500);
 
                    // 强制垃圾回收,确保相机对象被完全释放
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
 
                    // 从字典中移除旧实例
                    GlobalVar.dicBarcodeReaders.TryRemove(name, out _);
 
                    // 创建新实例
                    BarcodeReaderBase newReader = null;
                    switch (reader.Brand)
                    {
                        case BarcodeReaderBrand.Huayray:
                            newReader = new HRBarcodeReader();
                            break;
                        default:
                            throw new Exception($"不支持的读码器品牌: {reader.Brand}");
                    }
 
                    if (newReader.Open(name))
                    {
                        // 添加到字典
                        GlobalVar.dicBarcodeReaders.TryAdd(name, newReader);
                        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);
                    }
                }
                catch (Exception ex)
                {
                    flow.state = State.Error;
                    LogInfo?.Invoke(string.Format("重连读码器[{0}]异常: {1}", name, ex.Message), LogInfoType.ERROR);
                }
                flow.Refresh();
            }
        }
    }
}