using System;
using System.Drawing;
using System.Windows.Forms;
namespace MesWork.Pages
{
///
/// 系统设置页面 — 扫码枪连接管理
///
public partial class UC_SystemSettings : UserControl
{
// OP10 控件
private ComboBox cmb_Com1;
private Button btn_Connect1, btn_Disconnect1, btn_ManualSend1;
private Label lbl_Status1, lbl_ScanTime1;
private TextBox txt_Barcode1;
// OP20 控件
private ComboBox cmb_Com2;
private Button btn_Connect2, btn_Disconnect2, btn_ManualSend2;
private Label lbl_Status2, lbl_ScanTime2;
private TextBox txt_Barcode2;
public UC_SystemSettings()
{
InitializeComponent();
if (BarcodeManager.IsEnabled)
{
BuildScannerPanel(grp_Scanner1, "OP10", ref cmb_Com1, ref lbl_Status1, ref txt_Barcode1, ref lbl_ScanTime1,
ref btn_Connect1, ref btn_Disconnect1, ref btn_ManualSend1);
BuildScannerPanel(grp_Scanner2, "OP20", ref cmb_Com2, ref lbl_Status2, ref txt_Barcode2, ref lbl_ScanTime2,
ref btn_Connect2, ref btn_Disconnect2, ref btn_ManualSend2);
timer_Refresh.Start();
}
else
{
BuildScannerPanel(grp_Scanner1, "OP10", ref cmb_Com1, ref lbl_Status1, ref txt_Barcode1, ref lbl_ScanTime1,
ref btn_Connect1, ref btn_Disconnect1, ref btn_ManualSend1);
BuildScannerPanel(grp_Scanner2, "OP20", ref cmb_Com2, ref lbl_Status2, ref txt_Barcode2, ref lbl_ScanTime2,
ref btn_Connect2, ref btn_Disconnect2, ref btn_ManualSend2);
grp_Scanner1.Enabled = false;
grp_Scanner2.Enabled = false;
}
}
///
/// 动态构建单个扫码枪面板的控件
///
private void BuildScannerPanel(GroupBox grp, string opName,
ref ComboBox cmb, ref Label lblStatus, ref TextBox txtBar, ref Label lblTime,
ref Button btnConn, ref Button btnDisc, ref Button btnSend)
{
var normalFont = new Font("微软雅黑", 10F);
var boldFont = new Font("微软雅黑", 10F, FontStyle.Bold);
int y1 = 35, y2 = 75, y3 = 115;
// Row 1: COM口选择 + 连接/断开
var lblCom = new Label { Text = "COM口:", Font = normalFont, ForeColor = Color.FromArgb(55, 65, 81),
Location = new Point(20, y1 + 4), AutoSize = true };
grp.Controls.Add(lblCom);
cmb = new ComboBox { Font = normalFont, Location = new Point(90, y1), Size = new Size(110, 28),
DropDownStyle = ComboBoxStyle.DropDownList };
RefreshComPorts(cmb, opName);
grp.Controls.Add(cmb);
// 局部变量捕获(ref参数不能在lambda中使用)
var localCmb = cmb;
var localTxtBar = txtBar;
btnConn = new Button
{
Text = "🔗 连接", Font = boldFont, Size = new Size(90, 30), Location = new Point(215, y1 - 2),
BackColor = Color.FromArgb(74, 144, 217), ForeColor = Color.White,
FlatStyle = FlatStyle.Flat, Cursor = Cursors.Hand
};
btnConn.FlatAppearance.BorderSize = 0;
btnConn.Click += (s, e) => DoConnect(opName, localCmb);
grp.Controls.Add(btnConn);
btnDisc = new Button
{
Text = "⛔ 断开", Font = boldFont, Size = new Size(90, 30), Location = new Point(315, y1 - 2),
BackColor = Color.FromArgb(239, 68, 68), ForeColor = Color.White,
FlatStyle = FlatStyle.Flat, Cursor = Cursors.Hand
};
btnDisc.FlatAppearance.BorderSize = 0;
btnDisc.Click += (s, e) => DoDisconnect(opName);
grp.Controls.Add(btnDisc);
// 刷新COM口列表按钮
var btnRefresh = new Button
{
Text = "🔄", Font = normalFont, Size = new Size(36, 30), Location = new Point(415, y1 - 2),
FlatStyle = FlatStyle.Flat, Cursor = Cursors.Hand,
BackColor = Color.FromArgb(229, 231, 235)
};
btnRefresh.FlatAppearance.BorderSize = 0;
btnRefresh.Click += (s, e) => RefreshComPorts(localCmb, opName);
grp.Controls.Add(btnRefresh);
lblStatus = new Label { Text = "● 未连接", Font = boldFont, ForeColor = Color.FromArgb(156, 163, 175),
Location = new Point(470, y1 + 4), AutoSize = true };
grp.Controls.Add(lblStatus);
// Row 2: 最近码值 + 手动发送
var lblBar = new Label { Text = "码值:", Font = normalFont, ForeColor = Color.FromArgb(55, 65, 81),
Location = new Point(20, y2 + 4), AutoSize = true };
grp.Controls.Add(lblBar);
txtBar = new TextBox { Font = normalFont, Location = new Point(90, y2), Size = new Size(300, 28),
BorderStyle = BorderStyle.FixedSingle };
localTxtBar = txtBar; // 更新局部变量
grp.Controls.Add(txtBar);
btnSend = new Button
{
Text = "📤 手动发送", Font = boldFont, Size = new Size(120, 30), Location = new Point(405, y2 - 2),
BackColor = Color.FromArgb(5, 150, 105), ForeColor = Color.White,
FlatStyle = FlatStyle.Flat, Cursor = Cursors.Hand
};
btnSend.FlatAppearance.BorderSize = 0;
btnSend.Click += (s, e) => DoManualSend(opName, localTxtBar);
grp.Controls.Add(btnSend);
// Row 3: 扫码时间
var lblTimeLbl = new Label { Text = "扫码时间:", Font = normalFont, ForeColor = Color.FromArgb(55, 65, 81),
Location = new Point(20, y3 + 4), AutoSize = true };
grp.Controls.Add(lblTimeLbl);
lblTime = new Label { Text = "--", Font = normalFont, ForeColor = Color.FromArgb(31, 41, 55),
Location = new Point(110, y3 + 4), AutoSize = true };
grp.Controls.Add(lblTime);
}
// ── 操作方法 ──
private void DoConnect(string opName, ComboBox cmb)
{
string com = cmb.SelectedItem?.ToString();
if (string.IsNullOrEmpty(com))
{
MessageBox.Show("请选择COM口", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
BarcodeScanner scanner;
if (!BarcodeManager.Scanners.TryGetValue(opName, out scanner))
{
scanner = new BarcodeScanner(opName, MesWorkForm.BarcodeBaudRate);
scanner.BarcodeReceived += (op, code) => BarcodeManager.ManualSend(op, code);
BarcodeManager.Scanners[opName] = scanner;
}
bool ok = scanner.Connect(com);
MessageBox.Show(ok ? $"{opName} 扫码枪已连接 ({com})" : $"{opName} 连接失败,请检查COM口",
"连接结果", MessageBoxButtons.OK, ok ? MessageBoxIcon.Information : MessageBoxIcon.Warning);
}
private void DoDisconnect(string opName)
{
if (BarcodeManager.Scanners.TryGetValue(opName, out var scanner))
{
scanner.Disconnect();
MessageBox.Show($"{opName} 扫码枪已断开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void DoManualSend(string opName, TextBox txtBar)
{
string barcode = txtBar.Text.Trim();
if (string.IsNullOrEmpty(barcode))
{
MessageBox.Show("请输入条码值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
BarcodeManager.ManualSend(opName, barcode);
MessageBox.Show($"已手动发送条码:{barcode}\n工位:{opName}", "发送成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void RefreshComPorts(ComboBox cmb, string opName)
{
cmb.Items.Clear();
var ports = BarcodeManager.GetAvailablePorts();
foreach (var p in ports)
cmb.Items.Add(p);
// 自动选中当前配置的COM口或已连接的COM口
if (BarcodeManager.Scanners.TryGetValue(opName, out var scanner) && !string.IsNullOrEmpty(scanner.ComPort))
{
int idx = cmb.Items.IndexOf(scanner.ComPort);
if (idx >= 0) cmb.SelectedIndex = idx;
}
}
// ── 定时刷新状态 ──
private void timer_Refresh_Tick(object sender, EventArgs e)
{
RefreshScannerStatus("OP10", lbl_Status1, txt_Barcode1, lbl_ScanTime1);
RefreshScannerStatus("OP20", lbl_Status2, txt_Barcode2, lbl_ScanTime2);
}
private void RefreshScannerStatus(string opName, Label lblStatus, TextBox txtBar, Label lblTime)
{
if (lblStatus == null) return;
if (BarcodeManager.Scanners.TryGetValue(opName, out var scanner))
{
if (scanner.IsConnected)
{
lblStatus.Text = $"● 已连接 ({scanner.ComPort})";
lblStatus.ForeColor = Color.FromArgb(16, 185, 129); // 绿色
}
else
{
lblStatus.Text = $"● 未连接 ({scanner.ComPort})";
lblStatus.ForeColor = Color.FromArgb(239, 68, 68); // 红色
}
// 仅在有新扫码时更新(避免覆盖用户手动输入)
if (!string.IsNullOrEmpty(scanner.LastBarcode) && txtBar.Text != scanner.LastBarcode && !txtBar.Focused)
txtBar.Text = scanner.LastBarcode;
lblTime.Text = scanner.LastScanTime.HasValue
? scanner.LastScanTime.Value.ToString("yyyy-MM-dd HH:mm:ss")
: "--";
}
else
{
lblStatus.Text = "● 未配置";
lblStatus.ForeColor = Color.FromArgb(156, 163, 175);
lblTime.Text = "--";
}
}
}
}