Net 主要是用于SIEMENS PLC通讯

模拟器:Nettoplcsim-S7

Net 主要是用于SIEMENS PLC通讯_第1张图片

连接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 = "[email protected]";
            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 datas = new List();
            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 datas = new List();
            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());

 

你可能感兴趣的:(NET)