Net 主要是用于SIEMENS PLC通讯

这篇博客介绍了如何使用C#连接和交互Simatic S7系列的PLC,包括读写DB、M地址以及实现批量操作。通过示例代码展示了读取和写入布尔、字、数值、字符串等不同数据类型的方法,并演示了映射类读取和批量读写功能。
摘要由CSDN通过智能技术生成

模拟器:Nettoplcsim-S7

连接PLC

        private void btnConnect_Click(object sender, EventArgs e)
        {
            plc = new Plc(CpuType.S71200, "192.168.18.5", 0, 1);
            
            try
            {
                plc.Open();
            }
            catch (Exception)
            {
                Console.WriteLine($"连接到PLC设备失败:IsConnect = {plc.IsConnected}");
                return;
            }
        }

读取数据

        private void btnRead_Click(object sender, EventArgs e)
        {
            //DBX读取位,比如bool类型;DBW读取字;DBD读取数值
            var db1 = (bool)plc.Read("DB1.DBX0.0");

            var db2 = (ushort)plc.Read("DB1.DBW2.0");

            var db3 = ((uint)plc.Read("DB1.DBD4.0")).ConvertToFloat();

            //dword
            var db4 = (uint)plc.Read("DB1.DBD8.0");

            //dint
            var db5 = (uint)plc.Read("DB1.DBD12");

            //string 
            var db6 = plc.Read(DataType.DataBlock, 1, 16, VarType.String, 255);

            txtP1.Texts = db1.ToString();
            txtP2.Texts = db2.ToString();
            txtP3.Texts = db3.ToString();
            txtP4.Texts = db4.ToString();
            txtP5.Texts = db5.ToString();
            txtP6.Texts = db6.ToString().Trim();
        }

M地址读写

        private void btnReadM_Click(object sender, EventArgs e)
        {
            var db1 = (bool)plc.Read("I0.1");
            var db2 = (bool)plc.Read("Q0.0");
            var db3 = plc.Read("MW1");
            var db4 = plc.Read("M0.0");
        }

        private void btnWriteM_Click(object sender, EventArgs e)
        {
            plc.Write("I0.1", true);
            plc.Write("Q0.0", true);
            plc.Write("MW1", (ushort)2);
            plc.Write("M0.0", true);
        }

写DB

        private void btnWrite_Click(object sender, EventArgs e)
        {
            plc.Write("DB1.DBW0.0", false);
            plc.Write("DB1.DBW2.0", (ushort)44);

            //写real
            float val = 135.7f;
            plc.Write("DB1.DBD4.0", val.ConvertToUInt());

            //写dword
            uint dval = 1024;
            plc.Write("DB1.DBD8.0", dval);

            //写dint
            uint ival = 230;
            plc.Write("DB1.DBD12.0", ival);

            //写string
            string sval = "trueideal@gmail.com";
            byte[] databytes = S7.Net.Types.String.ToByteArray(sval);

            plc.WriteBytes(DataType.DataBlock, 1, 16, databytes);
        }

映射类读取

    public class CDb1
    {
        public bool P_01 { get; set; }
        public ushort P_02 { get; set; }

        public float P_03 { get; set; }

        //dword
        public int P_04 { get; set; }
        
        //dint
        public int P_05 { get; set; }
    }
 private void btnReadClass_Click(object sender, EventArgs e)
        {
            var db1 = new CDb1();
            plc.ReadClass(db1, 1, 0);
            txtP1.Texts = db1.P_01.ToString();
            txtP2.Texts = db1.P_02.ToString();
            txtP3.Texts = db1.P_03.ToString();
            txtP4.Texts = ((uint)db1.P_04).ToString();
            txtP5.Texts = ((uint)db1.P_05).ToString();
        }

批量读

            List<DataItem> datas = new List<DataItem>();
            datas.Add(new DataItem
            {
                DataType=DataType.DataBlock,
                VarType=VarType.Bit,
                DB=1,
                BitAdr=0,
                Count=1,
                StartByteAdr=0,
                Value=new object()
            });

plc.ReadMultipleVars(datas);

批量写

 List<DataItem> datas = new List<DataItem>();
            datas.Add(new DataItem
            {
                DataType = DataType.DataBlock,
                VarType = VarType.Bit,
                DB = 1,
                BitAdr = 0,
                Count = 1,
                StartByteAdr = 0,
                Value = new object()
            });
datas[0].Value = true;
            datas[1].Value = (ushort)126;
            datas[2].Value = 98.45f;
            datas[3].Value = 2900;
            datas[4].Value = 150;
            datas[5].Value = "  this is test by wl9981";

plc.Write(datas.ToArray());

bytes 读取连续数据

            byte[] bs = plc.ReadBytes(DataType.DataBlock, 1, 0,255);
            var db1 = S7.Net.Types.Boolean.GetValue(bs.Take(1).ToArray()[0],0);
            var db2 = S7.Net.Types.Int.FromByteArray(bs.Skip(2).Take(2).ToArray());
            var db3 = S7.Net.Types.Real.FromByteArray(bs.Skip(4).Take(4).ToArray());
            var db4 = S7.Net.Types.DWord.FromByteArray(bs.Skip(8).Take(4).ToArray());
            var db5 = S7.Net.Types.DInt.FromByteArray(bs.Skip(12).Take(4).ToArray());
            var db6 = S7.Net.Types.String.FromByteArray(bs.Skip(16).ToArray());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值