Ejemplo n.º 1
0
        public bool TryConnectModbus(ModbusSystem config)
        {
            try
            {
                if (tcpClient != null)
                {
                    tcpClient.Dispose();
                    tcpClient = null;
                    //master.Dispose();
                    //master = null;
                }
                _config   = config;
                tcpClient = new TcpClient(config.IpAddress, config.PortNum);
                var factory = new ModbusFactory();

                master = factory.CreateMaster(tcpClient);

                return(true);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "#### MODBUS CONNECTING FAILED ####");
                return(false);
            }
        }
Ejemplo n.º 2
0
        private void LoadConfigModbusMapper(IServiceCollection services)
        {
            string sqlite_conn_str         = Configuration.GetConnectionString("sqlite");
            SqliteAccessManager dam        = new SqliteAccessManager(sqlite_conn_str);
            ModbusSystem        modbusList = Configuration.GetSection("Modbus").Get <ModbusSystem>();

            using (DataAccess da = new DataAccess(dam.SessionFactory))
            {
                modbusList.GroupPoints        = da.Select <GroupPoint>();
                modbusList.GroupDigitalPoints = da.Select <EventGroupPoint>();
                services.AddSingleton(modbusList);
                services.AddSingleton <IModbusFactory, ModbusConnectionFactory>();

                EventMap map = new EventMap();
                map.Load();

                services.AddSingleton(map);

                AbsMqttBase.SetDefaultLoggerName("nlog.config", true);
                EventPublisherWorker worker = new EventPublisherWorker();
                worker.Initialize();

                services.AddSingleton(worker);

                services.AddSingleton <Microsoft.Extensions.Hosting.IHostedService, ModbusBackgroundService>();
                services.AddSingleton <Microsoft.Extensions.Hosting.IHostedService, ModbusDigitalProcessingService>();
                services.AddSingleton <Microsoft.Extensions.Hosting.IHostedService, ControlSubscribeWorker>();
                //services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, ModbusDigitalProcessingService>();
            }
        }
Ejemplo n.º 3
0
 public ControlSubscribeWorker(IModbusFactory modbus_factory,
                               IRedisConnectionFactory redisFactory,
                               IConfiguration configuration, ModbusSystem modbusSystem)
 {
     config        = configuration;
     modbusFactory = modbus_factory;
     SiteId        = configuration.GetSection("SiteId").Get <int>();
     DeviceIndex   = configuration.GetSection("DeviceIndex").Get <int>();
     modbus        = modbusSystem;
     redisDb       = redisFactory.Connection().GetDatabase(1);
     CancelToken   = Program.CancellationTokenSource.Token;
     this.Initialize();
 }
Ejemplo n.º 4
0
 public ModbusBackgroundService(ILoggerFactory loggerFactory, IModbusFactory modbus_factory,
                                MqttClientProxyCollection mqttClientProxies, IRedisConnectionFactory redisFactory,
                                IConfiguration configuration, ModbusSystem modbusSystem)
 {
     logger        = loggerFactory.CreateLogger <ModbusBackgroundService>();
     config        = configuration;
     modbusFactory = modbus_factory;
     mqtt_clients  = mqttClientProxies;
     mqtt_config   = configuration.GetSection("MQTTBrokers").Get <MqttConfig>();
     SiteId        = configuration.GetSection("SiteId").Get <int>();
     DeviceIndex   = configuration.GetSection("DeviceIndex").Get <int>();
     modbus        = modbusSystem;
     redis         = redisFactory.Connection().GetDatabase(1);
 }
 public ModbusDigitalProcessingService(Microsoft.Extensions.Logging.ILoggerFactory loggerFactory,
                                       IDataAccess mysql_dataAccess, IRedisConnectionFactory redisFactory,
                                       IModbusFactory modbus_factory, IConfiguration configuration, EventPublisherWorker worker, EventMap map,
                                       ModbusSystem modbusSystem)
 {
     logger               = loggerFactory.CreateLogger <ModbusBackgroundService>();
     config               = configuration;
     modbusFactory        = modbus_factory;
     eventPublisherWorker = worker;
     UpdatePeriod         = configuration.GetSection("EventPollInterval").Get <TimeSpan>();
     modbus               = modbusSystem;
     SiteId               = configuration.GetSection("SiteId").Get <short>();
     redis       = redisFactory.Connection().GetDatabase();
     DeviceIndex = configuration.GetSection("DeviceIndex").Get <int>();
     dbAccess    = mysql_dataAccess;
     event_map   = map;
 }
Ejemplo n.º 6
0
 public ControlLogService(ModbusSystem modbusSystem)
 {
     loggerWriter           = new StreamWriter(".\\ControlLog" + modbusSystem.SlaveId + ".log", true);
     loggerWriter.AutoFlush = true;
 }
Ejemplo n.º 7
0
 public ModbusConnectionFactory(ILoggerFactory loggerFactory, ModbusSystem modbusConfig)
 {
     //config = configYaml;
     logger  = loggerFactory.CreateLogger <ModbusConnectionFactory>();
     _config = modbusConfig;
 }