示例#1
0
        public async Task <bool> ConnectAsync()
        {
            _logger.LogInformation($"Connecting to inverter at {_address}:{_port}");

            if (_modbusClient != null)
            {
                _logger.LogError($"Already connected");
                throw new InvalidOperationException("Already connected");
            }

            _modbusClient = new ModbusClient(_address, _port);
            _modbusClient.connectedChanged += ConnectionChanged;

            try
            {
                await _modbusClient.ConnectAsync();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }