C#上位机序列4: 动画效果(模拟PLC设备运行)

 

涉及:控件数据绑定,动画效果,PLC通信(单个点位读取,批量读取寄存器字节数组,根据点位类型解析)

 

using HslCommunication.ModBus;
using HslCommunication.Profinet.Melsec;
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PLCUI
{
    public partial class MainForm : Form
    {
        ModbusTcpNet client = new ModbusTcpNet("127.0.0.1", 502);

        static PLCData1 PlcData1 = new PLCData1();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 方式2:基于事件的方式,将控件和数据进行绑定,优点:在任何地方改变变量的值,所绑定的控件也能同时改变
            lblValue_100.DataBindings.Add("Text", PlcData1, "D100");
            ucRightBelt1.DataBindings.Add("PlcValue", PlcData1, "D100");

            // 真实PLC数据
            txtAddressValue.DataBindings.Add("Text", PlcData1, "D150");// 绑定类变量

            // modbus
            PLCData2.Add(new MelsecPoint() { Type = BaseEnumDataType.TShort, Address = "150" });
        }

        private void btnSimulate_Click(object sender, EventArgs e)
        { 
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 模拟数据
            PlcData1.D100++;

            if (PlcData1.D100 > 15)
            {
                PlcData1.D100 = 0;
            }

            // 方式1:基于轮询的方式,查询到plc数据,然后进行更新
            //lblValue_D100.Text = PlcData1.D100.ToString();
            //int status = 0;
            //if (PlcData1.D100 >= 0 && PlcData1.D100 <= 5)
            //{
            //    status = 0;
            //}
            //else if (PlcData1.D100 > 5 && PlcData1.D100 <= 10)
            //{
            //    status = 1;
            //}
            //else if (PlcData1.D100 > 10 && PlcData1.D100 <= 15)
            //{
            //    status = 2;
            //}
            //ucRightBelt1.ShowImage(status); 
        } 

        private void btnRead_Click(object sender, EventArgs e)
        {
            try 
            {
                // 单次读取一个点位
                var val = client.ReadInt16(txtAddressKey.Text.Trim()).Content; PlcData1.D150 = val; 
                MessageBox.Show($"读取成功,{val}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"读取错误:{ex.Message}");
            }
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            try
            { 
                short val = Convert.ToInt16(txtAddressValue.Text.Trim());
                client.Write(txtAddressKey.Text.Trim(), val); 
                MessageBox.Show($"写入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"写入错误:{ex.Message}");
            }
        }

        private void btnBathModbus_Click(object sender, EventArgs e)
        {
            // 批量读取
            string address = "150";
            string startaddress_read = "100";
            var all = client.Read(startaddress_read.ToString(), 100).Content;// 获取所有,如果获取100-200,想要获取101,相对起始位置应该为:1 
            int address_diff = GetAddress(address) - GetAddress(startaddress_read);// 相对起始位置

            var val = PLCData2.Get(address, address_diff, all, client.ByteTransform);
            MessageBox.Show($"Modbus读取成功,点位:{address},值:{val}");
        } 

        private void btnBathMelsec_Click(object sender, EventArgs e)
        {
            string address = "D300";
            string startaddress_read = "D100";
            PLCData2.Add(new MelsecPoint() { Type = BaseEnumDataType.TDouble, Address = address });// TShort TInt TLong TFloat TDouble 

            MelsecFxSerialOverTcp melsecFxSerialOverTcp = new MelsecFxSerialOverTcp("192.168.30.248", 5551);
            melsecFxSerialOverTcp.ConnectServer();
            var all = melsecFxSerialOverTcp.Read(startaddress_read, 500).Content;
             
            int address_diff = GetAddress(address) - GetAddress(startaddress_read);
            var val = PLCData2.Get(address, address_diff, all, melsecFxSerialOverTcp.ByteTransform);
            MessageBox.Show($"Melsec读取成功,点位:{address},值:{val}");
            melsecFxSerialOverTcp.ConnectClose();
        }

        int GetAddress(string straddress)
        {
            string temp = Regex.Replace(straddress, "[a-z]", "", RegexOptions.IgnoreCase);
            return Convert.ToInt32(temp);
        }
    }
}

 

posted @ 2023-09-06 09:18  CHHC  阅读(710)  评论(0编辑  收藏  举报