1
hutongqing
2024-12-17 f142e86b68f5480f342442b1a4ae4c6a4fc3d912
WIDESEAWCS_Server/WIDESEAWCS_Communicator/Inovance/InovanceTcpCommunicator.cs
@@ -183,6 +183,38 @@
                    case TypeCode.Char:
                        return plc.Write(address, Convert.ToChar(value));
                    default:
                        if (value is int[])
                        {
                            return plc.Write(address, value as int[]);
                        }
                        else if (value is uint[])
                        {
                            return plc.Write(address, value as uint[]);
                        }
                        else if (value is short[])
                        {
                            return plc.Write(address, value as short[]);
                        }
                        else if (value is ushort[])
                        {
                            return plc.Write(address, value as ushort[]);
                        }
                        else if (value is bool[])
                        {
                            return plc.Write(address, value as bool[]);
                        }
                        else if (value is float[])
                        {
                            return plc.Write(address, value as float[]);
                        }
                        else if (value is double[])
                        {
                            return plc.Write(address, value as double[]);
                        }
                        else if (value is byte[])
                        {
                            return plc.Write(address, value as byte[]);
                        }
                        throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, type.Name, address), CommunicationErrorType.TypeError);
                }
            }
@@ -197,29 +229,51 @@
            }
        }
        private object Read(string address, TypeCode typeCode)
        private object Read(string address, TypeCode typeCode, ushort length = 1)
        {
            try
            {
                switch (typeCode)
                {
                    case TypeCode.Int32:
                        return (int)GetContent(plc.ReadInt32(address), address);
                        if (length > 1)
                            return (int[])GetContent(plc.ReadInt32(address, length), address);
                        else
                            return (int)GetContent(plc.ReadInt32(address), address);
                    case TypeCode.UInt32:
                        return (uint)GetContent(plc.ReadUInt32(address), address);
                        if (length > 1)
                            return (uint[])GetContent(plc.ReadUInt32(address, length), address);
                        else
                            return (uint)GetContent(plc.ReadUInt32(address), address);
                    case TypeCode.Int16:
                        return (short)GetContent(plc.ReadInt16(address), address);
                        if (length > 1)
                            return (short[])GetContent(plc.ReadInt16(address, length), address);
                        else
                            return (short)GetContent(plc.ReadInt16(address), address);
                    case TypeCode.UInt16:
                        return (ushort)GetContent(plc.ReadUInt16(address), address);
                        if (length > 1)
                            return (ushort[])GetContent(plc.ReadUInt16(address, length), address);
                        else
                            return (ushort)GetContent(plc.ReadUInt16(address), address);
                    case TypeCode.Single:
                        return (float)GetContent(plc.ReadFloat(address), address);
                        if (length > 1)
                            return (float[])GetContent(plc.ReadFloat(address, length), address);
                        else
                            return (float)GetContent(plc.ReadFloat(address), address);
                    case TypeCode.Boolean:
                        return (bool)GetContent(plc.ReadBool(address), address);
                        if (length > 1)
                            return (bool[])GetContent(plc.ReadBool(address, length), address);
                        else
                            return (bool)GetContent(plc.ReadBool(address), address);
                    case TypeCode.Byte:
                        if (length > 1)
                            return (byte)GetContent(plc.Read(address, length), address);
                        return (byte)GetContent(plc.ReadByte(address), address);
                    case TypeCode.String:
                        return (string)GetContent(plc.ReadString(address, 1), address);
                        return (string)GetContent(plc.ReadString(address, length), address);
                    case TypeCode.Char:
                        if (length > 1)
                            return (char)GetContent(plc.Read(address, length), address);
                        return (char)GetContent(plc.ReadByte(address), address);
                    default:
                        throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, typeCode.ToString(), address), CommunicationErrorType.TypeError);
@@ -278,7 +332,7 @@
                Ping();
                return operateResult.IsSuccess;
            }
            catch(Exception ex)
            catch (Exception ex)
            {
                LogNet.WriteException(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, ex.Message), ex);
                //连接异常时抛出自定义异常类
@@ -325,6 +379,11 @@
        {
            Type type = typeof(T);
            return (T)Read(address, Type.GetTypeCode(type));
        }
        public override T[] Read<T>(string address, ushort length)
        {
            Type type = typeof(T);
            return (T[])Read(address, Type.GetTypeCode(type), length);
        }
        public override object ReadAsObj(string address, string dataType)
@@ -409,6 +468,11 @@
            return GetResult(Write(address, value), address, value);
        }
        public override bool Write<T>(string address, T[] values)
        {
            return GetResult(Write(address, values), address, values);
        }
        public override bool WriteCustomer<T>(string address, [NotNull] T value)
        {