Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RmpHymmen opened this issue Sep 12, 2023 · 0 comments
Open

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

RmpHymmen opened this issue Sep 12, 2023 · 0 comments

Comments

@RmpHymmen
Copy link

RmpHymmen commented Sep 12, 2023

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}.");
            }
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant