using LB_VisionControl; 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 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(); } } } }