internal void RefreshAllForPollingManager() { _cachedValues.Clear(); Tag lastHandledTag = null; try { foreach (Tag tag in _tagsSnapshot) { if (!tag.IsActive) { continue; } lastHandledTag = tag; object value; if (ReadValueSynchronously(tag, out value)) { _cachedValues.Add(tag, value); } } } catch (Exception e) { RaiseCommunicationProblem(e); var exception = new PlcCommunicationException("Can't read tag " + lastHandledTag.FullName(), _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); exception.Tag = lastHandledTag; throw exception; } }
private bool IsAdsException(PlcCommunicationException exception) { var innerException = exception.InnerException; while (innerException != null) { if (innerException is AdsErrorException) { return(true); } innerException = innerException.InnerException; } return(false); }
/// <summary> /// Reads the element from the specified <paramref name="elementPath"/> and returns it. /// </summary> /// <param name="elementPath">The path to read from.</param> /// <param name="typeOfElement">The type of the desired element.</param> /// <returns>The read value.</returns> public object ReadElement(string elementPath, Type typeOfElement) { int handle = -1; try { handle = _twinCatClient.CreateVariableHandle(elementPath); return(_twinCatClient.ReadAny(handle, typeOfElement)); } catch (AdsErrorException e) { var exception = new PlcCommunicationException("Can't read tag " + elementPath, _path, _port.ToString(CultureInfo.InvariantCulture), string.Empty, e); throw exception; } finally { if (handle != -1) { _twinCatClient.DeleteVariableHandle(handle); } } }
private bool ReadValueSynchronously(Tag tag, out object newValue) { lock (_lock) { try { Connect(); } catch (AdsErrorException e) { var exception = new PlcCommunicationException("Can't read tag synchronously. Connection to PLC failed.", _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); exception.Tag = tag; throw exception; } } try { // try to read UDT if (_udtHandler.ContainsKey(tag.DataType)) { return(ReadUdt(tag, out newValue)); } using (var dataStream = new AdsStream(tag.BitSize == 0 ? 81 : tag.BitSize / 8)) { Address address = GetPlcAddress(tag); try { if (address == null) { int handle = _twinCatClient.CreateVariableHandle(tag.FullName()); try { _twinCatClient.Read(handle, dataStream); } finally { _twinCatClient.DeleteVariableHandle(handle); } } else { _twinCatClient.Read(address.IndexGroup, address.IndexOffset, dataStream); } } catch (AdsErrorException ex) { _logger.Warn("Couldn't read " + tag.FullName() + " because " + ex.ErrorCode.ToString() + " will try again..."); // try again try { if (address == null) { int handle = _twinCatClient.CreateVariableHandle(tag.FullName()); try { _twinCatClient.Read(handle, dataStream); } finally { _twinCatClient.DeleteVariableHandle(handle); } } else { _twinCatClient.Read(address.IndexGroup, address.IndexOffset, dataStream); } _logger.Info("Second try for " + tag.FullName() + " was successful"); } catch (AdsErrorException e) { var exception = new PlcCommunicationException("Can't read tag " + tag.FullName(), _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); exception.Tag = tag; throw exception; } } newValue = ParseValueFromStream(tag, dataStream); return(true); } } catch (Exception e) { _logger.Warn(tag.FullName() + " value change leaded to a unhandled exception", e); var exception = new PlcCommunicationException("Can't read tag " + tag.FullName(), _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); exception.Tag = tag; throw exception; } }
public void ReadTagSynchronously(Tag tag) { tag.LockValue(); try { lock (_lock) { if (!_twinCatClient.IsConnected) { try { _twinCatClient.Connect(_adsAddress, _adsPort); _twinCatClient.Timeout = Timeout; } catch (AdsException e) { throw new PlcCommunicationException("Can't read tag synchronously. Connection to PLC failed.", _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); } } } // try to read UDT if (_udtHandler.ContainsKey(tag.DataType)) { ReadUdt(tag); return; } var dataStream = new AdsStream(tag.BitSize == 0 ? 81 : tag.BitSize / 8); try { int handle = _twinCatClient.CreateVariableHandle(tag.FullName()); try { _twinCatClient.Read(handle, dataStream); } finally { _twinCatClient.DeleteVariableHandle(handle); } } catch (AdsErrorException e) { throw new PlcCommunicationException("Can't read tag " + tag.FullName(), _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); } ParseValueFromStream(tag, dataStream); } catch (Exception e) { if (_logger != null) { _logger.Warn(tag.FullName() + " value change leaded to a unhandled exception", e); } var exception = new PlcCommunicationException("Can't read tag " + tag.FullName(), _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); exception.Tag = tag; throw exception; } finally { tag.ReleaseValue(); } }