Ejemplo n.º 1
0
 public void Write(WriteParameter[] writeParameters)
 {
     foreach (var writeParameter in writeParameters)
     {
         if (_values.ContainsKey(writeParameter.Address))
             _values[writeParameter.Address] = writeParameter.Value;
         else
             _values.Add(writeParameter.Address, writeParameter.Value);
     }
 }
Ejemplo n.º 2
0
 public void Write(WriteParameter[] writeParameters)
 {
     var itemValues = new ItemValue[writeParameters.Length];
     for (var i = 0; i < writeParameters.Length; i++)
     {
         var writeParameter = writeParameters[i];
         var item = new Item {ItemName = writeParameter.Address};
         itemValues[i] = new ItemValue(item) {Value = writeParameter.Value};
     }
     _server.Write(itemValues);
 }
Ejemplo n.º 3
0
        public void Write(WriteParameter[] writeParameters)
        {
            lock (_itemValues)
            {
                var itemValues = new ItemValue[writeParameters.Length];
                var items = new Item[writeParameters.Length];
                for (var i = 0; i < writeParameters.Length; i++)
                {
                    var writeParameter = writeParameters[i];
                    var item = new Item { ItemName = writeParameter.Address };
                    items[i] = item;
                    itemValues[i] = new ItemValue(item) { Value = writeParameter.Value };
                }
                _server.Write(itemValues);

                var readResult = _server.Read(items);
                foreach (var itemValueResult in readResult)
                {
                    if (_itemValues.ContainsKey(itemValueResult.ItemName))
                    {
                        _itemValues[itemValueResult.ItemName] = itemValueResult.Value;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void WriteOnce(WriteParameter[] writeParameters)
        {
            if (writeParameters == null)
                return;
            var result = new bool[writeParameters.Count()];

            Dictionary<byte, SlaveWriteInfo> writeInfos;
            GetParametersWriteInfo(writeParameters, out writeInfos);

            foreach (var slaveWriteInfo in writeInfos)
            {
                var slaveAddress = slaveWriteInfo.Key;
                var writeInfo = slaveWriteInfo.Value;
                SortWriteInfoDevices(writeInfo);
                for (var i = 0; i < writeInfo.Coils.Count; ++i)
                {
                    var coil = writeInfo.Coils[i];
                    var address = coil.Address;
                    const int maxLength = MaxWriteBytesLength * 8;
                    var bools = new List<bool>(maxLength) { coil.WriteParameter.Value as bool? == true };
                    for (var j = 1; j < maxLength && i + j < writeInfo.Coils.Count; ++j)
                    {
                        var c = writeInfo.Coils[i + j];
                        if (c.Address == address + j)
                            bools.Add(c.WriteParameter.Value as bool? == true);
                        else
                            break;
                    }
                    if (_mb.WriteCoils(slaveAddress, address, BoolsToBytes(bools), bools.Count))
                    {
                        for (var j = i; j < bools.Count + i; ++j)
                        {
                            result[writeInfo.Coils[j].Index] = true;
                        }
                    }
                    i += bools.Count - 1;
                }

                for (var i = 0; i < writeInfo.HoldingRegisters.Count; ++i)
                {
                    var hr = writeInfo.HoldingRegisters[i];
                    var address = hr.Address;
                    const int maxLength = MaxWriteBytesLength / 2;
                    var bytes = new byte[MaxWriteBytesLength];
                    var length = address.Value;
                    var valueBytes = GetBytes(hr.WriteParameter.Value);
                    valueBytes = _bytesToRegistersConverter.GetBytes(valueBytes, 0, hr.Address.Value);
                    for (var j = 0; j < valueBytes.Length; ++j)
                    {
                        bytes[j] = valueBytes[j];
                    }
                    var oldI = i;
                    for (var j = 1; i + j < writeInfo.HoldingRegisters.Count; ++j)
                    {
                        var h = writeInfo.HoldingRegisters[i + j];
                        if (h.Address.Key <= address.Key + length)
                        {
                            var newLength = h.Address.Key + h.Address.Value - address.Key;
                            if (newLength < maxLength)
                            {
                                var vBytes = GetBytes(h.WriteParameter.Value);
                                vBytes = _bytesToRegistersConverter.GetBytes(vBytes, 0, h.Address.Value);
                                for (var k = 0; k < vBytes.Length; ++k)
                                {
                                    bytes[k + (h.Address.Key - address.Key) * 2] = vBytes[k];
                                }
                                if (newLength > length)
                                    length = newLength;
                                ++i;
                                continue;
                            }
                        }
                        break;
                    }
                    var byteList = new List<byte>(length * 2);
                    for (var j = 0; j < length * 2; ++j)
                    {
                        byteList.Add(bytes[j]);
                    }
                    if (_mb.WriteHoldingRegisters(slaveAddress, address.Key, byteList, length))
                    {
                        for (var j = oldI; j <= i; ++j)
                        {
                            result[writeInfo.HoldingRegisters[j].Index] = true;
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public void Write(WriteParameter[] writeParameters)
 {
     for (var i = 1; i <= _tryCount; i++)
     {
         try
         {
             WriteOnce(writeParameters);
             return;
         }
         catch
         {
             if (i >= _tryCount)
                 throw;
         }
     }
 }
Ejemplo n.º 6
0
 public void Write(WriteParameter[] writeParameters)
 {
     _ioDevice.Write(writeParameters);
 }
Ejemplo n.º 7
0
 public void Write(WriteParameter[] writeParameters)
 {
     if (!_opened)
         throw new Exception(NotOpenedExceptionMessage);
     if (writeParameters.Length == 1
         && writeParameters[0].Address == Address
         && writeParameters[0].Value.Equals(Value))
     {
         throw new Exception(WriteExceptionMessage);
     }
 }