Exemple #1
0
        protected override void EncodeRead(ReadOperation operation)
        {
            //uid
            operation.Request[0] = operation.UnitId;

            //Body
            //function code
            operation.Request[this.FunctionCodeOffset] = (byte)operation.FunctionCode;
            //address
            byte[] addressBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int16)(operation.Address)));
            operation.Request[this.FunctionCodeOffset + 1] = addressBytes[0];
            operation.Request[this.FunctionCodeOffset + 2] = addressBytes[1];
            //count
            byte[] entityCountBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(operation.Decoder.GetEntityCount(operation.Count)));
            operation.Request[this.FunctionCodeOffset + 3] = entityCountBytes[0];
            operation.Request[this.FunctionCodeOffset + 4] = entityCountBytes[1];

            if (this.GetCRC(operation.Request, 6, out UInt16 crc))
            {
                byte[] crcBytes = BitConverter.GetBytes(crc);
                operation.Request[this.FunctionCodeOffset + 5] = crcBytes[0];
                operation.Request[this.FunctionCodeOffset + 6] = crcBytes[1];
            }
        }
        protected List <ModbusOutValue> ProcessResponse(ModbusSlaveConfig config, ReadOperation x)
        {
            int count       = 0;
            int step_size   = 0;
            int start_digit = 0;
            List <ModbusOutValue> value_list = new List <ModbusOutValue>();

            switch (x.Response[m_dataBodyOffset])//function code
            {
            case (byte)FunctionCodeType.ReadCoils:
            case (byte)FunctionCodeType.ReadInputs:
            {
                count       = x.Response[m_dataBodyOffset + 1] * 8;
                count       = (count > x.Count) ? x.Count : count;
                step_size   = 1;
                start_digit = x.Response[m_dataBodyOffset] - 1;
                break;
            }

            case (byte)FunctionCodeType.ReadHoldingRegisters:
            case (byte)FunctionCodeType.ReadInputRegisters:
            {
                count       = x.Response[m_dataBodyOffset + 1];
                step_size   = 2;
                start_digit = (x.Response[m_dataBodyOffset] == 3) ? 4 : 3;
                break;
            }
            }
            var initialCell = string.Format(x.OutFormat, (char)x.Entity, x.Address + 1);

            if (x.ValueType == ModbusValueType.Basic)
            {
                for (int i = 0; i < count; i += step_size)
                {
                    string res  = "";
                    string cell = "";
                    string val  = "";
                    if (step_size == 1)
                    {
                        cell = string.Format(x.OutFormat, (char)x.Entity, x.Address + i + 1);
                        val  = string.Format("{0}", (x.Response[m_dataBodyOffset + 2 + (i / 8)] >> (i % 8)) & 0b1);
                    }
                    else if (step_size == 2)
                    {
                        cell = string.Format(x.OutFormat, (char)x.Entity, x.Address + (i / 2) + 1);
                        val  = string.Format("{0,00000}", ((x.Response[m_dataBodyOffset + 2 + i]) * 0x100 + x.Response[m_dataBodyOffset + 3 + i]));
                    }
                    res = cell + ": " + val + "\n";
                    Console.WriteLine(res);

                    ModbusOutValue value = new ModbusOutValue()
                    {
                        DisplayName = x.DisplayName, Address = cell, Value = val
                    };
                    value_list.Add(value);
                }
            }
            //We need to merge complex value
            else
            {
                var bytesA = x.Response.SubArray(m_dataBodyOffset + 2, step_size * x.Count);
                var val    = ModbusComplexValuesHandler.MergeComplexValue(bytesA, x.ValueType, this.config.EndianSwap, this.config.MidEndianSwap);

                ModbusOutValue value = new ModbusOutValue()
                {
                    DisplayName = x.DisplayName, Address = initialCell, Value = val
                };
                value_list.Add(value);

                var res = initialCell + " : " + val + "\n";
                Console.WriteLine(res);
            }

            if (value_list.Count > 0)
            {
                PrepareOutMessage(config.HwId, x.CorrelationId, value_list);
            }

            return(value_list);
        }
 protected abstract void EncodeRead(ReadOperation operation);