Пример #1
0
        public string ReadUnitData(ModbusExchangeableUnit unit)
        {
            if (_modbusSerial == null)
            {
                return("Нет подключения");
            }
            try
            {
                List <ushort> readRegisters = new List <ushort>();

                foreach (ModbusDataBlock block in unit.GetReadMap())
                {
                    readRegisters.AddRange(_modbusSerial.ReadHoldingRegisters(SlaveAddress,
                                                                              block.Offset, block.Size));
                }
                unit.SetPropertiesDataFromRegisters(readRegisters.ToArray());
            }
            catch (Exception exception)
            {
                return(exception.Message);
            }
            return("OK");
        }
Пример #2
0
        public string WriteUnitData(ModbusExchangeableUnit unit)
        {
            if (_modbusSerial == null)
            {
                return("Нет подключения");
            }
            try
            {
                foreach (ModbusDataBlock block in unit.GetWriteMap())
                {
                    /*if (block.Size == 1)
                     *  _modbusSerial.WriteSingleRegister(SlaveAddress, block.Offset,
                     *      unit.GetPropertiesDataAsRegisters()[block.AbsoluteOffset]);
                     * else
                     * {
                     *  List<ushort> registersToWrite = new List<ushort>();
                     *  for (int j = block.AbsoluteOffset; j < block.AbsoluteOffset + block.Size; j++)
                     *      registersToWrite.Add(unit.GetPropertiesDataAsRegisters()[j]);
                     *
                     *  _modbusSerial.WriteMultipleRegisters(SlaveAddress, block.Offset, registersToWrite.ToArray());
                     * }*/
                    //петя попросил только 16-ю функцию
                    List <ushort> registersToWrite = new List <ushort>();
                    for (int j = block.AbsoluteOffset; j < block.AbsoluteOffset + block.Size; j++)
                    {
                        registersToWrite.Add(unit.GetPropertiesDataAsRegisters()[j]);
                    }

                    _modbusSerial.WriteMultipleRegisters(SlaveAddress, block.Offset, registersToWrite.ToArray());
                }
            }
            catch (Exception exception)
            {
                return(exception.Message);
            }
            return("OK");
        }
Пример #3
0
 public void Copy(ModbusExchangeableUnit unit)
 {
     ushort[] temp = unit.GetPropertiesDataAsRegisters();
     SetPropertiesDataFromRegisters(temp);
 }