MelsecCommunication.cs 1.99 KB
using HHECS.BllModel;
using HslCommunication.Profinet.Melsec;

namespace HHECS.DAQWebClient.Communications
{
    public class MelsecCommunication : ICommunication
    {
        public readonly MelsecMcNet Melsec = null!;
        public string IpAddress => Melsec.IpAddress;
        public int CommunicationId { get; set; }

        private MelsecCommunication() { }

        public MelsecCommunication(int communicationId, string ipAddress, int port = 6000)
        {
            CommunicationId = communicationId;
            Melsec = new MelsecMcNet(ipAddress, port);
            Melsec.SetPersistentConnection();
        }

        public BllResult ConnectServer()
        {
            var result = Melsec.ConnectServer();
            if (!result.IsSuccess)
            {
                return BllResultFactory.Error(result.Message);
            }
            return BllResultFactory.Success();
        }

        public BllResult ConnectClose()
        {
            Melsec.ConnectClose();
            return BllResultFactory.Success();
        }

        // Other methods for reading/writing data would go here
        public BllResult Read(IEnumerable<DataItem> dataItems)
        {
            try
            {
                //待完善
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        public BllResult Read(DataItem dataItem)
        {
            return Read(new DataItem[] { dataItem });
        }

        public BllResult Write(IEnumerable<DataItem> dataItems)
        {
            try
            {
                //待完善
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        public BllResult Write(DataItem dataItem)
        {
            return Write(new DataItem[] { dataItem });
        }
    }
}