ModbusTcpCommunication.cs 8.41 KB
using HHECS.BllModel;
using HHECS.EquipmentModel;
using HslCommunication;
using HslCommunication.ModBus;

namespace HHECS.DAQClient.Communications
{
    public class ModbusTcpCommunication : ICommunication
    {
        public int CommunicationId { get; set; }

        public string IpAddress => client.IpAddress;

        private readonly ModbusTcpNet client = null!;

        private ModbusTcpCommunication() { }

        public ModbusTcpCommunication(int communicationId, string ipAddress, int port)
        {
            CommunicationId = communicationId;
            client = new ModbusTcpNet(ipAddress, port);
            //client.DataFormat = DataFormat.CDAB;
            client.SetPersistentConnection();
        }

        public BllResult ConnectServer()
        {
            try
            {
                var result = client.ConnectServer();
                if (!result.IsSuccess)
                {
                    return BllResultFactory.Error(result.Message);
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

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

        public BllResult Read(IEnumerable<DataItem> dataItems)
        {
            try
            {
                foreach (var item in dataItems)
                {
                    switch (item.DataType)
                    {
                        case EquipmentDataType.REAL:
                            var realResult = client.ReadFloat(item.DataAddress);
                            if (!realResult.IsSuccess)
                            {
                                item.Value = string.Empty;
                                return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{realResult.Message}");
                            }
                            item.Value = realResult.Content.ToString();
                            break;
                        case EquipmentDataType.BOOL:
                            var addressArray = item.DataAddress.Split('.');
                            var address = addressArray[0];
                            _ = int.TryParse(addressArray.Length >= 2 ? addressArray[1] : "0", out var index);
                            var boolBufferResult = client.Read(address, 1);
                            if (!boolBufferResult.IsSuccess)
                            {
                                item.Value = string.Empty;
                                return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{boolBufferResult.Message}");
                            }
                            item.Value = ((boolBufferResult.Content.Max() & (1 << index)) > 0).ToString();
                            break;
                        case EquipmentDataType.BYTE:
                            break;
                        case EquipmentDataType.WORD:
                            var int16Result = client.ReadInt16(item.DataAddress);
                            if (!int16Result.IsSuccess)
                            {
                                item.Value = string.Empty;
                                return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{int16Result.Message}");
                            }
                            item.Value = int16Result.Content.ToString();
                            break;
                        case EquipmentDataType.DWORD:
                            var uInt16Result = client.ReadUInt16(item.DataAddress);
                            if (!uInt16Result.IsSuccess)
                            {
                                item.Value = string.Empty;
                                return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{uInt16Result.Message}");
                            }
                            item.Value = uInt16Result.Content.ToString();
                            break;
                        case EquipmentDataType.INT:
                            var int32Result = client.ReadInt32(item.DataAddress);
                            if (!int32Result.IsSuccess)
                            {
                                item.Value = string.Empty;
                                return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{int32Result.Message}");
                            }
                            item.Value = int32Result.Content.ToString();
                            break;
                        case EquipmentDataType.DINT:
                            var uInt32Result = client.ReadUInt32(item.DataAddress);
                            if (!uInt32Result.IsSuccess)
                            {
                                item.Value = string.Empty;
                                return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{uInt32Result.Message}");
                            }
                            item.Value = uInt32Result.Content.ToString();
                            break;
                        case EquipmentDataType.CHAR:
                            break;
                        default:
                            break;
                    }
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"读取设备[{IpAddress}]数据失败:{ex.Message}");
            }
        }

        public BllResult Read(DataItem dataItem)
        {
            return Read(new List<DataItem> { dataItem });
        }

        public BllResult Write(IEnumerable<DataItem> dataItems)
        {
            try
            {
                var result = new OperateResult();
                foreach (var item in dataItems)
                {
                    switch (item.DataType)
                    {
                        case EquipmentDataType.REAL:
                            _ = float.TryParse(item.Value, out var v1);
                            result = client.Write(item.DataAddress, v1);
                            break;
                        case EquipmentDataType.BOOL:
                            _ = bool.TryParse(item.Value, out var v2);
                            result = client.Write(item.DataAddress, v2);
                            break;
                        case EquipmentDataType.BYTE:
                            break;
                        case EquipmentDataType.WORD:
                            _ = short.TryParse(item.Value, out var v3);
                            result = client.Write(item.DataAddress, v3);
                            break;
                        case EquipmentDataType.DWORD:
                            _ = ushort.TryParse(item.Value, out var v4);
                            result = client.Write(item.DataAddress, v4);
                            break;
                        case EquipmentDataType.INT:
                            _ = int.TryParse(item.Value, out var v5);
                            result = client.Write(item.DataAddress, v5);
                            break;
                        case EquipmentDataType.DINT:
                            _ = uint.TryParse(item.Value, out var v6);
                            result = client.Write(item.DataAddress, v6);
                            break;
                        case EquipmentDataType.CHAR:
                            break;
                        default:
                            break;
                    }

                    if (!result.IsSuccess)
                    {
                        return BllResultFactory.Success($"写入设备[{IpAddress}]数据失败,{result.Message}");
                    }
                }
                return BllResultFactory.Success($"写入设备[{IpAddress}]数据成功,数量:{dataItems.Count()}");
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"写入设备[{IpAddress}]数据失败:{ex.Message}");
            }
        }

        public BllResult Write(DataItem dataItem)
        {
            return Write(new List<DataItem> { dataItem });
        }
    }
}