示例#1
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);
        }
示例#2
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);
        }
示例#3
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);
        }