示例#1
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;
            }
            }
            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.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]));
                }
                res = cell + ": " + val + "\n";
                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);
            }
        }
示例#2
0
        public static async Task <ModuleHandle> CreateHandleFromConfiguration(ModuleConfig config)
        {
            Modbus.Slaves.ModuleHandle moduleHandle = null;
            foreach (var config_pair in config.SlaveConfigs)
            {
                Console.WriteLine("Creating Modbus Handle");
                ModbusSlaveConfig slaveConfig = config_pair.Value;
                switch (slaveConfig.GetConnectionType())
                {
                case ModbusConstants.ConnectionType.ModbusTCP:
                {
                    Console.WriteLine("Type: TCP");
                    if (moduleHandle == null)
                    {
                        moduleHandle = new Modbus.Slaves.ModuleHandle();
                    }

                    ModbusSlaveSession slave = new ModbusTCPSlaveSession(slaveConfig);
                    await slave.InitSession();

                    moduleHandle.ModbusSessionList.Add(slave);
                    break;
                }

                case ModbusConstants.ConnectionType.ModbusRTU:
                {
                    Console.WriteLine("Type: RTU");
                    if (moduleHandle == null)
                    {
                        moduleHandle = new Modbus.Slaves.ModuleHandle();
                    }

                    ModbusSlaveSession slave = new ModbusRTUSlaveSession(slaveConfig);
                    await slave.InitSession();

                    moduleHandle.ModbusSessionList.Add(slave);
                    break;
                }

                case ModbusConstants.ConnectionType.ModbusASCII:
                {
                    Console.WriteLine("Type: ASCII (UNSUPPORTED)");
                    break;
                }

                case ModbusConstants.ConnectionType.Unknown:
                {
                    Console.WriteLine("Type: UNKNWON");
                    break;
                }
                }
            }
            return(moduleHandle);
        }
示例#3
0
        public static async Task <ModuleHandle> CreateHandleFromConfiguration(ModuleConfig config)
        {
            Modbus.Slaves.ModuleHandle moduleHandle = null;
            foreach (var config_pair in config.SlaveConfigs)
            {
                ModbusSlaveConfig slaveConfig = config_pair.Value;
                switch (slaveConfig.GetConnectionType())
                {
                case ModbusConstants.ConnectionType.ModbusTCP:
                {
                    if (moduleHandle == null)
                    {
                        moduleHandle = new Modbus.Slaves.ModuleHandle();
                    }

                    ModbusSlaveSession slave = new ModbusTCPSlaveSession(slaveConfig);
                    await slave.InitSession();

                    moduleHandle.ModbusSessionList.Add(slave);
                    break;
                }

                case ModbusConstants.ConnectionType.ModbusRTU:
                {
                    if (moduleHandle == null)
                    {
                        moduleHandle = new Modbus.Slaves.ModuleHandle();
                    }

                    ModbusSlaveSession slave = new ModbusRTUSlaveSession(slaveConfig);
                    await slave.InitSession();

                    moduleHandle.ModbusSessionList.Add(slave);
                    break;
                }

                case ModbusConstants.ConnectionType.ModbusASCII:
                {
                    break;
                }

                case ModbusConstants.ConnectionType.Unknown:
                {
                    break;
                }
                }
            }
            return(moduleHandle);
        }
示例#4
0
 public ModbusRTUSlaveSession(ModbusSlaveConfig conf)
     : base(conf)
 {
 }
示例#5
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);
            }
        }
示例#6
0
 public ModbusSlaveSession(ModbusSlaveConfig conf)
 {
     config = conf;
 }
示例#7
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);
            }
        }
示例#8
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);
        }
示例#9
0
 public ModbusRTUSlaveSession(ModbusSlaveConfig conf, HandleResultDelegate resultHandler)
     : base(conf, resultHandler)
 {
 }
示例#10
0
        public static async Task <ModuleHandle> CreateHandleFromConfiguration(ModuleConfig config, ModbusSlaveSession.HandleResultDelegate resultHandler)
        {
            Modbus.Slaves.ModuleHandle moduleHandle = null;
            foreach (var config_pair in config.SlaveConfigs)
            {
                ModbusSlaveConfig slaveConfig = config_pair.Value;
                switch (slaveConfig.GetConnectionType())
                {
                case ModbusConstants.ConnectionType.ModbusTCP:
                {
                    if (moduleHandle == null)
                    {
                        moduleHandle = new Modbus.Slaves.ModuleHandle();
                    }
                    if (slaveConfig.TcpPort <= 0)
                    {
                        slaveConfig.TcpPort = ModbusConstants.DefaultTcpPort;
                    }
                    if (slaveConfig.RetryCount <= 0)
                    {
                        slaveConfig.RetryCount = ModbusConstants.DefaultRetryCount;
                    }
                    if (slaveConfig.RetryInterval <= 0)
                    {
                        slaveConfig.RetryInterval = ModbusConstants.DefaultRetryInterval;
                    }

                    ModbusSlaveSession slave = new ModbusTCPSlaveSession(slaveConfig, resultHandler);
                    await slave.InitSession();

                    moduleHandle.ModbusSessionList.Add(slave);
                    break;
                }

                case ModbusConstants.ConnectionType.ModbusRTU:
                {
                    if (moduleHandle == null)
                    {
                        moduleHandle = new Modbus.Slaves.ModuleHandle();
                    }
                    if (slaveConfig.RetryCount <= 0)
                    {
                        slaveConfig.RetryCount = ModbusConstants.DefaultRetryCount;
                    }
                    if (slaveConfig.RetryInterval <= 0)
                    {
                        slaveConfig.RetryInterval = ModbusConstants.DefaultRetryInterval;
                    }

                    ModbusSlaveSession slave = new ModbusRTUSlaveSession(slaveConfig, resultHandler);
                    await slave.InitSession();

                    moduleHandle.ModbusSessionList.Add(slave);
                    break;
                }

                case ModbusConstants.ConnectionType.ModbusASCII:
                {
                    break;
                }

                case ModbusConstants.ConnectionType.Unknown:
                {
                    break;
                }
                }
            }
            return(moduleHandle);
        }
示例#11
0
 public ModbusSlaveSession(ModbusSlaveConfig conf, HandleResultDelegate resultHandler)
 {
     config          = conf;
     messageDelegate = resultHandler;
 }