S7NetPlus / s7netplus

S7.NET+ -- A .NET library to connect to Siemens Step7 devices
MIT License
1.29k stars 577 forks source link

how to catch "Address out of Range" 0.20.0 #510

Open RmpHymmen opened 11 months ago

RmpHymmen commented 11 months ago

How do i catch the Address out of range or object does not exist exception in this Sample? i need the lasterrorcode but threre is none anymore. The dokumentation is not uptodate.

        private async Task ReadDataBlock(SymbolInfo sym, CancellationToken stoppingToken)
        {
            try
            {
                sym.ActValue = await _plc.ReadSingleVariableAsync(sym.BitOffset, sym.TypeBitLength,
                    sym.DataBlockNumber, sym.eType, sym.eArea, stoppingToken);

                await CheckData(sym, stoppingToken);
            }
            catch (Exception ex)
            {
                var offset = sym.BitOffset;
                _logger.LogCritical($"PLC{_fileSuffix}: DB: {sym.DataBlockNumber}. ByteOffset: {(int)(offset / 8)}.{(int)(offset % 8)}");
                _logger.LogCritical($"PLC{_fileSuffix}: {ex.Message}");
                if (false)//Address out of range or object does not exist
                {
                    _logger.LogCritical($"PLC{_fileSuffix}: Couldn't find the Variable.");
                    SymbolList.First().Item1.Remove(sym);
                }
            }
        }

ReadSingleVariableAsync calls this

        private async Task<ulong> ReadSingleULIntAsync(long bitOffset, int db, CancellationToken stoppingToken, DataType type)
        {
            byte[] bytes = await ReadBytesAsync(type, db, (int)(bitOffset / 8), 8, stoppingToken);
            return ByteArrayToULInt(bytes);
        }

The Exception is thrown here in S7.net

        internal static void ValidateResponseCode(ReadWriteErrorCode statusCode)
        {
            switch (statusCode)
            {
                case ReadWriteErrorCode.ObjectDoesNotExist:
                    throw new Exception("Received error from PLC: Object does not exist.");
                case ReadWriteErrorCode.DataTypeInconsistent:
                    throw new Exception("Received error from PLC: Data type inconsistent.");
                case ReadWriteErrorCode.DataTypeNotSupported:
                    throw new Exception("Received error from PLC: Data type not supported.");
                case ReadWriteErrorCode.AccessingObjectNotAllowed:
                    throw new Exception("Received error from PLC: Accessing object not allowed.");
                case ReadWriteErrorCode.AddressOutOfRange:
                    throw new ArgumentOutOfRangeException("Received error from PLC: Address out of range.");
                case ReadWriteErrorCode.HardwareFault:
                    throw new Exception("Received error from PLC: Hardware fault.");
                case ReadWriteErrorCode.Success:
                    break;
                default:
                    throw new Exception($"Invalid response from PLC: statusCode={(byte)statusCode}.");
            }
        }