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

            //Body
            //function code
            operation.Request[m_dataBodyOffset] = operation.FunctionCode;
            //address
            byte[] address_byte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int16)(operation.Address)));
            operation.Request[m_dataBodyOffset + 1] = address_byte[0];
            operation.Request[m_dataBodyOffset + 2] = address_byte[1];
            //count
            byte[] count_byte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int16)operation.Count));
            operation.Request[m_dataBodyOffset + 3] = count_byte[0];
            operation.Request[m_dataBodyOffset + 4] = count_byte[1];

            if (GetCRC(operation.Request, 6, out UInt16 crc))
            {
                byte[] crc_byte = BitConverter.GetBytes(crc);
                operation.Request[m_dataBodyOffset + 5] = crc_byte[0];
                operation.Request[m_dataBodyOffset + 6] = crc_byte[1];
            }
        }
示例#2
0
        protected void 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)ModbusConstants.FunctionCodeType.ReadCoils:
            case (byte)ModbusConstants.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)ModbusConstants.FunctionCodeType.ReadHoldingRegisters:
            case (byte)ModbusConstants.FunctionCodeType.ReadInputRegisters:
            {
                count       = x.Response[m_dataBodyOffset + 1];
                step_size   = 2;
                start_digit = (x.Response[m_dataBodyOffset] == 3) ? 4 : 3;
                break;
            }
            }

            byte[] tValue = new byte[4];

            string res  = "";
            string cell = "";
            string val  = "";

            for (int i = 0; i < count; i += step_size)
            {
                res  = "";
                cell = "";
                val  = "";
                if (step_size == 1)
                {
                    cell = string.Format(x.OutFormat, (char)x.EntityType, 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.EntityType, x.Address + (i / 2) + 1);
                    val  = string.Format("{0,00000}", ((x.Response[m_dataBodyOffset + 2 + i]) * 0x100 + x.Response[m_dataBodyOffset + 3 + i]));

                    //Console.WriteLine(">> " + i.ToString() + ":" + (x.Response[m_dataBodyOffset + 2 + i]).ToString());
                    //Console.WriteLine(">> " + i.ToString() + ":" + (x.Response[m_dataBodyOffset + 3 + i]).ToString());

                    // Little Endian
                    tValue[3 - i]       = (x.Response[m_dataBodyOffset + 2 + i]);
                    tValue[3 - (i + 1)] = (x.Response[m_dataBodyOffset + 3 + i]);
                }
            }

            //Console.WriteLine(ByteArrayToString(tValue));
            if (x.Signed)
            {
                val = (((Double)(BitConverter.ToInt32(tValue))) / ((double)x.Divisor)).ToString(x.NumberFormat);
            }
            else
            {
                val = (((Double)(BitConverter.ToUInt32(tValue))) / ((double)x.Divisor)).ToString(x.NumberFormat);
            }

            res = cell + ": " + val + "\n";
            if (x.Debug)
            {
                Console.WriteLine(res);
            }

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

            value_list.Add(value);

            if (value_list.Count > 0)
            {
                PrepareOutMessage(config.HwId, x.CorrelationId, value_list);
            }
        }
示例#3
0
 protected abstract void EncodeRead(ReadOperation operation);
示例#4
0
        public void Validate()
        {
            List <string> invalidConfigs = new List <string>();

            foreach (var config_pair in SlaveConfigs)
            {
                ModbusSlaveConfig slaveConfig = config_pair.Value;
                if (slaveConfig == null)
                {
                    Console.WriteLine($"{config_pair.Key} is null, remove from dictionary...");
                    invalidConfigs.Add(config_pair.Key);
                    continue;
                }
                if (slaveConfig.TcpPort == null || slaveConfig.TcpPort <= 0)
                {
                    Console.WriteLine($"Invalid TcpPort: {slaveConfig.TcpPort}, set to DefaultTcpPort: {ModbusConstants.DefaultTcpPort}");
                    slaveConfig.TcpPort = ModbusConstants.DefaultTcpPort;
                }
                if (slaveConfig.RetryCount == null || slaveConfig.RetryCount <= 0)
                {
                    Console.WriteLine($"Invalid RetryCount: {slaveConfig.RetryCount}, set to DefaultRetryCount: {ModbusConstants.DefaultRetryCount}");
                    slaveConfig.RetryCount = ModbusConstants.DefaultRetryCount;
                }
                if (slaveConfig.RetryInterval == null || slaveConfig.RetryInterval <= 0)
                {
                    Console.WriteLine($"Invalid RetryInterval: {slaveConfig.RetryInterval}, set to DefaultRetryInterval: {ModbusConstants.DefaultRetryInterval}");
                    slaveConfig.RetryInterval = ModbusConstants.DefaultRetryInterval;
                }
                List <string> invalidOperations = new List <string>();
                foreach (var operation_pair in slaveConfig.Operations)
                {
                    ReadOperation operation = operation_pair.Value;
                    if (operation == null)
                    {
                        Console.WriteLine($"{operation_pair.Key} is null, remove from dictionary...");
                        invalidOperations.Add(operation_pair.Key);
                        continue;
                    }
                    if (operation.StartAddress.Length < 5)
                    {
                        Console.WriteLine($"{operation_pair.Key} has invalid StartAddress {operation.StartAddress}, remove from dictionary...");
                        invalidOperations.Add(operation_pair.Key);
                        continue;
                    }
                    ParseEntity(operation.StartAddress, true, out ushort address_int16, out byte function_code, out byte entity_type);

                    if (operation.Count <= 0)
                    {
                        Console.WriteLine($"{operation_pair.Key} has invalid Count {operation.Count}, remove from dictionary...");
                        invalidOperations.Add(operation_pair.Key);
                        continue;
                    }
                    if (operation.Count > 127 && ((char)entity_type == (char)ModbusConstants.EntityType.HoldingRegister || (char)entity_type == (char)ModbusConstants.EntityType.InputRegister))
                    {
                        Console.WriteLine($"{operation_pair.Key} has invalid Count, must be 1~127, {operation.Count}, remove from dictionary...");
                        invalidOperations.Add(operation_pair.Key);
                        continue;
                    }
                    if (operation.CorrelationId == "" || operation.CorrelationId == null)
                    {
                        Console.WriteLine($"Empty CorrelationId: {operation.CorrelationId}, set to DefaultCorrelationId: {ModbusConstants.DefaultCorrelationId}");
                        operation.CorrelationId = ModbusConstants.DefaultCorrelationId;
                    }

                    if (operation.NumberFormat == "" || operation.NumberFormat == null)
                    {
                        Console.WriteLine($"Empty NumberFormat: {operation.NumberFormat}, set to 'N0'");
                        operation.NumberFormat = "N0";
                    }

                    if (operation.Divisor == 0 || operation.Divisor == null)
                    {
                        Console.WriteLine($"Invalid Divisor: {operation.Divisor}, set to '1'");
                        operation.Divisor = 1;
                    }

                    operation.EntityType   = entity_type;
                    operation.Address      = address_int16;
                    operation.FunctionCode = function_code;
                    //output format
                    if (operation.StartAddress.Length == 5)
                    {
                        operation.OutFormat = "{0}{1:0000}";
                    }
                    else if (operation.StartAddress.Length == 6)
                    {
                        operation.OutFormat = "{0}{1:00000}";
                    }
                }
                foreach (var in_op in invalidOperations)
                {
                    slaveConfig.Operations.Remove(in_op);
                }
            }
            foreach (var in_slave in invalidConfigs)
            {
                SlaveConfigs.Remove(in_slave);
            }
        }
示例#5
0
        public bool IsValidate()
        {
            bool ret = true;

            foreach (var config_pair in SlaveConfigs)
            {
                ModbusSlaveConfig slaveConfig = config_pair.Value;
                if (slaveConfig.TcpPort <= 0)
                {
                    Console.WriteLine($"Invalid TcpPort: {slaveConfig.TcpPort}, set to DefaultTcpPort: {ModbusConstants.DefaultTcpPort}");
                    slaveConfig.TcpPort = ModbusConstants.DefaultTcpPort;
                }
                if (slaveConfig.RetryCount <= 0)
                {
                    Console.WriteLine($"Invalid RetryCount: {slaveConfig.RetryCount}, set to DefaultRetryCount: {ModbusConstants.DefaultRetryCount}");
                    slaveConfig.RetryCount = ModbusConstants.DefaultRetryCount;
                }
                if (slaveConfig.RetryInterval <= 0)
                {
                    Console.WriteLine($"Invalid RetryInterval: {slaveConfig.RetryInterval}, set to DefaultRetryInterval: {ModbusConstants.DefaultRetryInterval}");
                    slaveConfig.RetryInterval = ModbusConstants.DefaultRetryInterval;
                }
                foreach (var operation_pair in slaveConfig.Operations)
                {
                    ReadOperation operation = operation_pair.Value;
                    ParseEntity(operation.StartAddress, true, out ushort address_int16, out byte function_code, out byte entity_type);

                    if (operation.Count <= 0)
                    {
                        Console.WriteLine($"Invalid Count: {operation.Count}");
                        ret = false;
                    }
                    if (operation.Count > 127 && ((char)entity_type == (char)ModbusConstants.EntityType.HoldingRegister || (char)entity_type == (char)ModbusConstants.EntityType.InputRegister))
                    {
                        Console.WriteLine($"Invalid Count: {operation.Count}, must be 1~127");
                        ret = false;
                    }
                    if (operation.CorrelationId == "" || operation.CorrelationId == null)
                    {
                        Console.WriteLine($"Empty CorrelationId: {operation.CorrelationId}, set to DefaultCorrelationId: {ModbusConstants.DefaultCorrelationId}");
                        operation.CorrelationId = ModbusConstants.DefaultCorrelationId;
                    }
                    if (ret)
                    {
                        operation.EntityType   = entity_type;
                        operation.Address      = address_int16;
                        operation.FunctionCode = function_code;
                        //output format
                        if (operation.StartAddress.Length == 5)
                        {
                            operation.OutFormat = "{0}{1:0000}";
                        }
                        else if (operation.StartAddress.Length == 6)
                        {
                            operation.OutFormat = "{0}{1:00000}";
                        }
                    }
                }
            }

            return(ret);
        }