Ejemplo n.º 1
0
 public void GetDouble()
 {
     Assert.Equal(0.0, ModbusUtility.GetDouble(0, 0, 0, 0));
     Assert.Equal(1.0, ModbusUtility.GetDouble(16368, 0, 0, 0));
     Assert.Equal(Math.PI, ModbusUtility.GetDouble(16393, 8699, 21572, 11544));
     Assert.Equal(500.625, ModbusUtility.GetDouble(16511, 18944, 0, 0));
 }
Ejemplo n.º 2
0
 private double DeriveDouble(ushort b3, ushort b2, ushort b1, ushort b0)
 {
     return(ModbusUtility.GetDouble(b3, b2, b1, b0));
 }
Ejemplo n.º 3
0
        public void UpdateData(ushort[] registers, int startAddress)
        {
            //TODO проверить режим отоброжанения, и изменять нужный массив

            int t = 0;

            switch (_mode)
            {
            case 0:    //ushort format - 1 word
                if (_config.Amount == _lastCount & _lastIntro == _config.StartAddress)
                {
                    for (int i = 0; i < registers.Length; i++)
                    {
                        UshortList[i].Value = registers[i];
                    }
                }
                else
                {
                    _lastCount = _config.Amount;
                    _lastIntro = _config.StartAddress;
                    UshortList.Clear();
                    for (int i = 0; i < registers.Length; i++)
                    {
                        UshortList.Add(new UshortPoint(_config.StartAddress + i, registers[i]));
                    }
                }
                break;


            case 1:    //uint format - 2 words
                t = (_config.Amount / 2) * 2;
                if (_config.Amount == _lastCount & _lastIntro == _config.StartAddress)
                {
                    int r = 0;
                    for (int i = 0; i < t; i += 2)
                    {
                        UIntList[r++].Value = ModbusUtility.GetUInt32(registers[i], registers[i + 1]);
                    }
                }
                else
                {
                    _lastCount = _config.Amount;
                    _lastIntro = _config.StartAddress;
                    UIntList.Clear();
                    for (int i = 0; i < t; i += 2)
                    {
                        UIntList.Add(new UIntPoint(_config.StartAddress + i, ModbusUtility.GetUInt32(registers[i], registers[i + 1])));
                    }
                }
                break;



            case 2:    //ulong format - 4 words
                t = (_config.Amount / 4) * 4;
                if (_config.Amount == _lastCount & _lastIntro == _config.StartAddress)
                {
                    int r = 0;
                    for (int i = 0; i < t; i += 4)
                    {
                        ULongList[r++].Value = ModbusUtility.GetUInt64(registers[i], registers[i + 1], registers[i + 2], registers[i + 3]);
                    }
                }
                else
                {
                    _lastCount = _config.Amount;
                    _lastIntro = _config.StartAddress;
                    ULongList.Clear();
                    for (int i = 0; i < t; i += 4)
                    {
                        ULongList.Add(new ULongPoint(_config.StartAddress + i, ModbusUtility.GetUInt64(registers[i], registers[i + 1], registers[i + 2], registers[i + 3])));
                    }
                }
                break;


            case 3:    //hex format
                if (_config.Amount == _lastCount & _lastIntro == _config.StartAddress)
                {
                    for (int i = 0; i < registers.Length; i++)
                    {
                        HexList[i].Value = registers[i].ToString("X2");
                    }
                }
                else
                {
                    _lastCount = _config.Amount;
                    _lastIntro = _config.StartAddress;
                    HexList.Clear();
                    for (int i = 0; i < registers.Length; i++)
                    {
                        HexList.Add(new HexPoint(_config.StartAddress + i, registers[i].ToString("X2")));
                    }
                }
                break;

            case 4:

                if (_config.Amount == _lastCount & _lastIntro == _config.StartAddress)
                {
                    for (int i = 0; i < registers.Length; i++)
                    {
                        BitList[i].Value = Convert.ToString(registers[i], 2).PadLeft(16, '0');
                    }
                }
                else
                {
                    _lastCount = _config.Amount;
                    _lastIntro = _config.StartAddress;
                    BitList.Clear();
                    for (int i = 0; i < registers.Length; i++)
                    {
                        BitList.Add(new BitPoint(_config.StartAddress + i, Convert.ToString(registers[i], 2).PadLeft(16, '0')));
                    }
                }
                break;

            case 5:
                t = (_config.Amount / 2) * 2;
                if (_config.Amount == _lastCount & _lastIntro == _config.StartAddress)
                {
                    int r = 0;
                    for (int i = 0; i < t; i += 2)
                    {
                        FloatList[r++].Value = ModbusUtility.GetSingle(registers[i], registers[i + 1]);
                    }
                }
                else
                {
                    _lastCount = _config.Amount;
                    _lastIntro = _config.StartAddress;
                    FloatList.Clear();
                    for (int i = 0; i < t; i += 2)
                    {
                        FloatList.Add(new FloatPoint(_config.StartAddress + i, ModbusUtility.GetSingle(registers[i], registers[i + 1])));
                    }
                }
                break;

            case 6:
                t = (_config.Amount / 4) * 4;
                if (_config.Amount == _lastCount & _lastIntro == _config.StartAddress)
                {
                    int r = 0;
                    for (int i = 0; i < t; i += 4)
                    {
                        DoubleList[r++].Value = ModbusUtility.GetDouble(registers[i + 3], registers[i + 2], registers[i + 1], registers[i]);
                    }
                }
                else
                {
                    _lastCount = _config.Amount;
                    _lastIntro = _config.StartAddress;
                    DoubleList.Clear();
                    for (int i = 0; i < t; i += 4)
                    {
                        DoubleList.Add(new DoublePoint(_config.StartAddress + i, ModbusUtility.GetDouble(registers[i + 3], registers[i + 2], registers[i + 1], registers[i])));
                    }
                }
                break;
            }
            UpdateTableSource(_mode);
        }