LB_VisionProcesses/Communicators/CommunicatorForm.cs
@@ -56,11 +56,26 @@
        private void CommunicatorForm_Load(object sender, EventArgs e)
        {
            // 初始化数据类型
            cmbDataType.Items.Clear();
            cmbDataType.Items.AddRange(new string[] { "String", "Bool", "Byte", "Int", "DInt", "Real", "Double", "Word", "DWord" });
            // 绑定索引改变事件
            cmbDataType.SelectedIndexChanged += (s, ev) =>
            {
                if (communicator != null)
                {
                    communicator.CommunicatorConnections.Add("数据类型", cmbDataType.Text);
                }
            };
            // 使用 Enum.GetValues 获取 enum 类型的所有值
            foreach (CommunicatorType type in Enum.GetValues(typeof(CommunicatorType)))
            {
                cmbType.Items.Add(type.ToString());
            }
            // ... (保持不变)
            //选择Com会触发ValueChanged事件,没有输入通讯口的情况下选择到-1
            if (comConfig != null)
@@ -118,10 +133,13 @@
                txtIP.Visible = false;
                this.lblAddress.Visible = false;
                this.txtAddress.Visible = false;
                this.lblDataType.Visible = false;
                this.cmbDataType.Visible = false;
                lblIP.Text = "串口号";
                lblIP.Text = "波特率";
                lblIP.Text = "波特率"; // Bug: 这里 lblPort 应该被设置为 "波特率",但原代码复用了 lblIP? 不,lblIP.Text被设了两次。
                lblPort.Text = "波特率"; // 修正原代码的潜在Bug
                cmbIP.Text = communicator.CommunicatorConnections["地址"].ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"].ToString();
@@ -136,10 +154,12 @@
                txtIP.Visible = true;
                this.lblAddress.Visible = false;
                this.txtAddress.Visible = false;
                this.lblDataType.Visible = false;
                this.cmbDataType.Visible = false;
                lblIP.Text = "监控文件";
                lblIP.Text = "写入文件";
                lblPort.Text = "写入文件"; // 修正原代码可能的错误
                txtIP.Text = communicator.CommunicatorConnections["地址"].ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"].ToString();
@@ -153,12 +173,21 @@
                txtIP.Visible = true;
                this.lblAddress.Visible = true;
                this.txtAddress.Visible = true;
                this.lblDataType.Visible = true;
                this.cmbDataType.Visible = true;
                lblIP.Text = "IP";
                lblIP.Text = "槽";
                lblPort.Text = "槽"; // 原代码这里是 lblIP.Text="槽" 覆盖了 "IP"
                txtIP.Text = communicator.CommunicatorConnections["地址"].ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"].ToString();
                this.txtAddress.Text = communicator.CommunicatorConnections["变量地址"]?.ToString();
                if (communicator.CommunicatorConnections.Contains("数据类型"))
                    this.cmbDataType.Text = communicator.CommunicatorConnections["数据类型"].ToString();
                else
                    this.cmbDataType.Text = "String";
                this.grpSetting.ForeColor = SystemColors.Control;
                btnRuleSend.Visible = false;
            }
@@ -168,12 +197,14 @@
                btnRuleSend.Enabled = false;
                this.lblAddress.Visible = false;
                this.txtAddress.Visible = false;
                this.lblDataType.Visible = false;
                this.cmbDataType.Visible = false;
                cmbIP.Visible = false;
                txtIP.Visible = true;
                lblIP.Text = " IP";
                lblIP.Text = "端口";
                lblPort.Text = "端口";
                txtIP.Text = communicator.CommunicatorConnections["地址"].ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"].ToString();
@@ -198,6 +229,13 @@
            {
                communicator.CommunicatorConnections.Add("地址", cmbIP.SelectedItem.ToString());
                communicator.CommunicatorConnections.Add("端口", txtPort.Text);
            }
            else if (communicator is SiemensLBS7)
            {
                communicator.CommunicatorConnections.Add("地址", txtIP.Text);
                communicator.CommunicatorConnections.Add("端口", txtPort.Text);
                communicator.CommunicatorConnections.Add("变量地址", txtAddress.Text);
                communicator.CommunicatorConnections.Add("数据类型", cmbDataType.Text);
            }
            else
            {
@@ -263,14 +301,37 @@
            MessageBox.Show(result ? "断开成功" : "断开失败,原因是:" + communicator.Msg);
        }
        private void ShowLogMsg(string msg)
        {
            // 如果当前不是 UI 线程,则通过 Invoke 将操作调度到 UI 线程
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>((message) =>
                {
                    this.txtReceiveMsg.AppendText("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] " + message + "\r\n");
                    this.txtReceiveMsg.ScrollToCaret();
                }), msg);
            }
            else
            {
                this.txtReceiveMsg.AppendText("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] " + msg + "\r\n");
                this.txtReceiveMsg.ScrollToCaret();
            }
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (communicator == null)
                return;
            communicator.SendMessage(txtSendMsg.Text);
            ShowSendMsg(txtSendMsg.Text);
            if (communicator.SendMessage(txtSendMsg.Text))
            {
                ShowSendMsg(txtSendMsg.Text);
            }
            else
            {
                ShowLogMsg(communicator.Msg);
            }
        }
        private void btnRuleSend_Click(object sender, EventArgs e)
@@ -287,7 +348,14 @@
            //HexByte = strToHexByte(strSendMsg + crcString);
            SendMsg = SendMsg + crcString;
            communicator.SendMessage(SendMsg);
            if (communicator.SendMessage(SendMsg))
            {
                ShowSendMsg(SendMsg);
            }
            else
            {
                ShowLogMsg(communicator.Msg);
            }
        }
        private void btnRun_Click(object sender, EventArgs e)