C3204
2026-01-07 75d1950a9c3d8a32090bd95cea271ec36e7c09dc
LB_SmartVision/Forms/Pages/CommunicatorPage/CommunicatorForm.cs
@@ -1,18 +1,38 @@
using LB_VisionProcesses.Communicators;
using LB_VisionProcesses.Communicators.SiemensS7;
using LB_VisionProcesses.Communicators.TCom;
using ReaLTaiizor.Forms;
using RJCP.IO.Ports;
using S7.Net;
using System.Diagnostics;
using System.Linq;
namespace LB_SmartVision.Forms.Pages.CommunicatorPage
{
    public partial class CommunicatorForm : MaterialForm
    public delegate void CommunicatorChange(BaseCommunicator communicator);
    public partial class CommunicatorForm : Form
    {
        BaseCommunicator communicator { get; set; }
        public event CommunicatorChange CommunicatorChanged;
        private void communicatorChanged(BaseCommunicator communicator)
        {
            if (CommunicatorChanged != null)
            {
                CommunicatorChanged(communicator);
            }
        }
        public CommunicatorForm()
        {
            InitializeComponent();
            cmbType.Items.Clear();
            if (communicator != null && !string.IsNullOrEmpty(communicator.CommunicatorConnections["型号"]?.ToString()))
            {
                cmbType.Items.Add(communicator.CommunicatorConnections["型号"]?.ToString());
            }
        }
        public CommunicatorForm(BaseCommunicator communicator, string name)
@@ -24,6 +44,14 @@
            cmbIP.Enabled = false;
            txtIP.Enabled = false;
            txtPort.Enabled = false;
            lblType.Visible = false;
            lblAddress.Visible = false;
            cmbType.Visible = false;
            txtAddress.Visible = false;
            this.Text = name;
            if (communicator is UARTPort)
@@ -39,28 +67,61 @@
                txtIP.Visible = false;
                cmbIP.Visible = true;
                this.btnRuleSend.Visible = true;
                lblType.Visible = false;
                lblAddress.Visible = false;
                cmbType.Visible = false;
                txtAddress.Visible = false;
                lblIP.Text = "COM口";
                lblPort.Text = "波特率";
            }
            else if (communicator is TCPClient || communicator is TCPServer)
            {
                txtIP.Text = communicator.CommunicatorConnections["地址"]?.ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"]?.ToString();
                txtPort.SelectedText = communicator.CommunicatorConnections["端口"]?.ToString();
                txtIP.Visible = true;
                cmbIP.Visible = false;
                this.btnRuleSend.Visible = false;
                lblType.Visible = false;
                lblAddress.Visible = false;
                cmbType.Visible = false;
                txtAddress.Visible = false;
                lblIP.Text = "地址";
                lblPort.Text = "端口";
            }
            else if (communicator is LocalMonitor)
            else if (communicator is SiemensLBS7)
            {
                txtIP.Text = communicator.CommunicatorConnections["地址"]?.ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"]?.ToString();
                cmbType.Items.Clear();
                if (!string.IsNullOrEmpty(communicator.CommunicatorConnections["型号"]?.ToString()))
                {
                    cmbType.Items.Add(communicator.CommunicatorConnections["型号"]?.ToString());
                    cmbType.Text = communicator.CommunicatorConnections["型号"]?.ToString();
                }
                if (!string.IsNullOrEmpty(communicator.CommunicatorConnections["变量地址"]?.ToString()))
                {
                    txtAddress.Text = communicator.CommunicatorConnections["变量地址"]?.ToString();
                }
                txtIP.Visible = true;
                cmbIP.Visible = false;
                this.btnRuleSend.Visible = false;
                lblIP.Text = "监控文件";
                lblPort.Text = "写入文件";
                lblType.Visible = true;
                lblAddress.Visible = true;
                cmbType.Visible = true;
                txtAddress.Visible = true;
                lblIP.Text = "IP";
                lblPort.Text = "槽";
            }
            this.communicator = communicator;
@@ -78,6 +139,14 @@
                    return;
                //取消回调函数
                communicator.MessageReceived -= ShowReceiveMsg;
                if (communicator != null)
                {
                    communicatorChanged(communicator);
                    if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Keys.Count > 0 && GlobalVar.dicCommunicators.Keys.Contains(communicator.CommunicatorName))
                    {
                        GlobalVar.dicCommunicators[communicator.CommunicatorName] = communicator;
                    }
                }
            }
            catch (Exception ex)
            {
@@ -185,5 +254,18 @@
            if (communicator.SendMessage(SendMsg))
                ShowSendMsg(SendMsg);
        }
        private void txtAddress_TextChanged(object sender, EventArgs e)
        {
            if (communicator != null && !communicator.CommunicatorConnections.Contains("变量地址"))
            {
                communicator.CommunicatorConnections.Add("变量地址", txtAddress.Text);
            }
            else if (communicator != null)
            {
                communicator.CommunicatorConnections["变量地址"] = txtAddress.Text;
                communicatorChanged(communicator);
            }
        }
    }
}