使用HslCommunication连接西门子SiemensPLCS.S1200

using HslCommunication;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HslCommunicationPLC.Siemens
{
    public class clsPLC : IDisposable
    {
        private SiemensS7Net siemensTcpNet = null;
        private SiemensPLCS siemensPLCSelected = SiemensPLCS.S1200;
        private bool isconn = false;
        public bool IsConn
        {
            get { return isconn; }
        }

        public clsHost(PlcConfig plcConfig)
        {
            siemensPLCSelected = plcConfig.siemensPLCS;
            siemensTcpNet = new SiemensS7Net(siemensPLCSelected, plcConfig.IpAddress);
            //siemensTcpNet = new SiemensS7Net(siemensPLCSelected)
            //{
            //    IpAddress = plcConfig.IpAddress,
            //    Port = plcConfig.Port,
            //    //Rack = plcConfig.Rack,
            //    //Slot = plcConfig.Slot,
            //    //ConnectionType = plcConfig.ConnectionType,
            //    //LocalTSAP = plcConfig.LocalTSAP
            //};

            //if (siemensPLCSelected == SiemensPLCS.S400)
            //{
            //    siemensTcpNet.Slot = byte.Parse("3");
            //}
            //else if (siemensPLCSelected == SiemensPLCS.S1200)
            //{
            //    siemensTcpNet.Slot = byte.Parse("0");
            //}
            //else if (siemensPLCSelected == SiemensPLCS.S300)
            //{
            //    siemensTcpNet.Slot = byte.Parse("2");
            //}
            //else if (siemensPLCSelected == SiemensPLCS.S1500)
            //{
            //    siemensTcpNet.Slot = byte.Parse("0");
            //}
        }

        public bool Connect()
        {
            string strEM = "";
            return Connect(ref strEM);
        }

        public bool Connect(ref string strEM)
        {
            siemensTcpNet.ConnectClose();
            OperateResult connect = siemensTcpNet.ConnectServer();
            isconn = connect.IsSuccess;
            if (!isconn) strEM = connect.Message;

            return isconn;
        }

        public bool Disconnect()
        {
            OperateResult connect = siemensTcpNet.ConnectClose();
            if (connect.IsSuccess) isconn = false;

            return connect.IsSuccess;
        }

        public bool ReadBlock(string StartAddress, ref short[] Content)
        {
            string strEM = "";
            return ReadBlock(StartAddress, ref Content, ref strEM);
        }

        public bool ReadPLCbit(string PLCbit, ref bool Content)
        {
            string strEM = "";
            return ReadPLCbit(PLCbit, ref Content, ref strEM);
        }

        public bool ReadPLCbit(string StartAddress, ref bool Content, ref string strEM)
        {
            OperateResult<bool> read = siemensTcpNet.ReadBool(StartAddress);
            Content = read.Content;
            isconn = read.IsSuccess;
            if (!isconn) strEM = read.Message;

            return read.IsSuccess;
        }


        public bool ReadBlock(string StartAddress, ref short[] Content, ref string strEM)
        {
            OperateResult<short[]> read = siemensTcpNet.ReadInt16(StartAddress, ushort.Parse(Content.Length.ToString()));
            Content = read.Content;
            isconn = read.IsSuccess;
            if (!isconn) strEM = read.Message;

            return read.IsSuccess;
        }

        public bool WriteBlock(string StartAddress, short[] Content)
        {
            string strEM = "";
            return WriteBlock(StartAddress, Content, ref strEM);
        }

        public bool WriteBlock(string StartAddress, short[] Content, ref string strEM)
        {
            OperateResult write = siemensTcpNet.Write(StartAddress, Content);
            isconn = write.IsSuccess;
            if (!isconn) strEM = write.Message;

            return write.IsSuccess;
        }

        public bool Write(string StartAddress, short value)
        {
            string strEM = "";
            return Write(StartAddress, value, ref strEM);
        }

        public bool Write(string StartAddress, short value, ref string strEM)
        {
            OperateResult write = siemensTcpNet.Write(StartAddress, value);
            isconn = write.IsSuccess;
            if (!isconn) strEM = write.Message;

            return write.IsSuccess;
        }

        public bool WriteBit(string address, bool value)
        {
            string strEM = "";
            return WriteBit(address, value, ref strEM);
        }

        public bool WriteBit(string address, bool value, ref string strEM)
        {
            OperateResult write = siemensTcpNet.Write(address, value);
            isconn = write.IsSuccess;
            if (!isconn) strEM = write.Message;

            return write.IsSuccess;
        }

        #region IDisposeable interface implementation

        ~clsHost()
        {
            // Do not re-create Dispose clean-up code here.
            // Calling Dispose(false) is optimal in terms of
            // readability and maintainability.
            Dispose(false);
        }

        private bool disposed = false;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // Dispose managed resources.
                    siemensTcpNet.Dispose();
                }
                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // If disposing is false,
                // only the following code is executed.

                // Note disposing has been done.
                disposed = true;
            }
        }

        #endregion IDisposeable interface implementation

    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xdpcxq1029

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值