Example #1
0
        public ReaderSaverErrors ReadDeviceConfiguration(Device configuration)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            try
            {
                FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate);
                configuration.Configuration = (DeviceConfiguration)formatter.Deserialize(fs);
                fs.Close();
            }
            catch (Exception)
            {
                return ReaderSaverErrors.CodeSerializationError;
            }
            configuration.Configuration.ConfigurationReadFromDevice = false;
            return ReaderSaverErrors.CodeOk;
        }
Example #2
0
        public ReaderSaverErrors ReadDeviceConfiguration(Device configuration)
        {
            configuration.Configuration.ConfigurationReadFromDevice = false;
            ReaderSaverErrors retCode = CheckDeviceHeaderValidityAndInitConfiguration(configuration.Configuration);
            if (retCode != ReaderSaverErrors.CodeOk)
                return retCode;
            List<object> listOfConfigurationItems = configuration.Configuration.ToList();

            retCode = PerformReading(ref listOfConfigurationItems);
            if (retCode != ReaderSaverErrors.CodeOk)
                return retCode;

            if (!configuration.Configuration.FromList(listOfConfigurationItems))
                return ReaderSaverErrors.CodeInvalidConfigurationSize;

            if (retCode == ReaderSaverErrors.CodeOk)
                configuration.Configuration.ConfigurationReadFromDevice = true;
            return retCode;
        }
Example #3
0
 public DeviceValidator(Device device)
 {
     _device = device;
     ValidationErrorList = new List<string>();
 }
Example #4
0
        public ReaderSaverErrors SaveDeviceConfiguration(Device configuration)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            try
            {
                FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate);
                formatter.Serialize(fs, configuration.Configuration);
                fs.Close();
            }
            catch (Exception)
            {
                return ReaderSaverErrors.CodeSerializationError;
            }
            return ReaderSaverErrors.CodeOk;
        }
Example #5
0
        public ReaderSaverErrors SaveDeviceConfiguration(Device configuration)
        {
            if (_protocol.IsConnected)
            {
                ReaderSaverErrors retCode = CheckDeviceHeaderValidityAndInitConfiguration(configuration.Configuration,true);
                if (retCode != ReaderSaverErrors.CodeOk)
                    return retCode;

                configuration.Configuration.LastConfigurationTime.ConfigurationTime = DateTime.Now;
                List<object> configurationItems = configuration.Configuration.ToList();
                configurationItems.RemoveRange(0,1);//remove readonly header

                UInt16 registerNumberNeeded = (UInt16)((SizeofHelper.SizeOfPublicPropertiesWithModbusAttribute(configurationItems,
                    ModbusRegisterAccessType.AccessReadWrite) + 1) / 2);

                if ((registerNumberNeeded > 0) &&
                    (registerNumberNeeded <= (UInt16.MaxValue - RegisterWriteAddressOffset) + 1))
                {
                    if (registerNumberNeeded <= _protocol.WriteRegistersPerQueryCapacity)
                    {
                        if (_protocol.PresetMultipleRegisters(SlaveAddress, RegisterWriteAddressOffset, configurationItems, BigEndianOrder) != ModbusErrorCode.CodeOk)
                            return ReaderSaverErrors.CodeModbusCommunicationError;
                        return ReaderSaverErrors.CodeOk;
                    }

                    UInt16 currentDeviceOffset = RegisterWriteAddressOffset;
                    ModbusDataPoint<UInt16>[] forcedValues;

                    object[] tempArray = configurationItems.ToArray();
                    Type firstElementType = tempArray[0].GetType();

                    ModbusDataMappingHelper.ConvertObjectsVaulesToRegisters(tempArray, 0,
                        (UInt32) configurationItems.Count,
                        BigEndianOrder, out forcedValues, firstElementType);

                    for (UInt16 query = 0; query < (UInt16)((registerNumberNeeded + _protocol.WriteRegistersPerQueryCapacity - 1) / _protocol.WriteRegistersPerQueryCapacity); query++)
                    {
                        UInt32 currentArrayOffset = (UInt16)(query * _protocol.WriteRegistersPerQueryCapacity);
                        if (_protocol.PresetMultipleRegisters(SlaveAddress,
                                                            currentDeviceOffset,
                                                            Array.ConvertAll(forcedValues, b => (object) b),
                                                            (UInt32)(query * _protocol.WriteRegistersPerQueryCapacity),
                                                            forcedValues.Length - (currentArrayOffset) > _protocol.WriteRegistersPerQueryCapacity ? _protocol.WriteRegistersPerQueryCapacity : (UInt32)(forcedValues.Length - (currentArrayOffset)),
                                                            BigEndianOrder) != ModbusErrorCode.CodeOk)
                            return ReaderSaverErrors.CodeModbusCommunicationError;

                        currentDeviceOffset += _protocol.WriteRegistersPerQueryCapacity;
                    }
                    return ReaderSaverErrors.CodeOk;
                }
            }
            return ReaderSaverErrors.CodeComPortNotConnected;
        }