Hey Gunter,

one more notice.
I just submitted the PR with your ping method, which seems to work pretty well 
for S7 and Modbus-tcp as we checked.

If you want to use the pool, which basically works like a JDBC Connection pool, 
you can use something like this snippet:

```
private static PooledPlcDriverManager createPooledDriverManager() {
        return new PooledPlcDriverManager(pooledPlcConnectionFactory -> {
            GenericKeyedObjectPoolConfig<PlcConnection> poolConfig = new 
GenericKeyedObjectPoolConfig<>();
            poolConfig.setMinIdlePerKey(1);  // This should avoid problems with 
long running connect attempts??
            poolConfig.setTestOnBorrow(true);
            poolConfig.setTestOnReturn(true);
            return new GenericKeyedObjectPool<>(pooledPlcConnectionFactory, 
poolConfig);
        });
    }
```
(Taken from org.apache.plc4x.java.scraper.ScraperImpl).

The pool keeps a set of connections and hands them over if someone needs them. 
Before handing over and after getting them back, the pool verifies that the 
connection is still alive (TestOnBorrw, TestOnReturn) and in case it is not, 
the Connection is removed from the pool (and a new one is created when 
requested).
This should be a more elegant solution to your loop.
Especially as the pool would create more concurrent connections, if multiple 
requests are made concurrently.

Best
Julian

Am 29.03.19, 09:59 schrieb "Gunther Gruber" <[email protected]>:

    Hi Julian,
    
    i am not sure about this. according to documentation this has to be 
    enabled explicitly. 
    
https://support.industry.siemens.com/cs/document/87149213/how-do-you-define-the-true-constant-in-the-lad-fdb-editor-in-step-7-(tia-portal)-?dti=0&lc=en-AT.
    
    Gunther
    
    
    On 29.03.19 09:17, Julian Feinauer wrote:
    > Hi Gunter,
    >
    > I am currently looking through your code and wanted to prepare a commit, 
so just a question about the s7 implementation.
    > Is the M1.2 a generally available bit or is this something more specific 
to your use cases?
    > I know that the M are the Markers and generally all Boolean, but can 
there be a situation where this bit does not exist?
    >
    > Julian
    >
    > Am 28.03.19, 15:16 schrieb "Gunther Gruber" 
<[email protected]>:
    >
    >      Hi Julian,
    >      
    >      somhow my github account got flagged.
    >      
    >      I append the two functions below, i think it will take some time for 
github to respond on the mail i wrote.
    >      
    >      
    >      public boolean ping(String host, int port, int timeout) {
    >        Socket s = null;
    >        try {
    >          s = new Socket();
    >          s.connect(new InetSocketAddress(host, port), timeout);
    >          return true;
    >        } catch (Exception e) {
    >          return false;
    >        } finally {
    >          if (s != null) {
    >            try {
    >              s.close();
    >            } catch (Exception e) {
    >            }
    >          }
    >        }
    >      }
    >      
    >      private boolean channelPingCheck(int timeout) {
    >              String variable = "%M1.2:BOOL";
    >              return channelPingCheck(timeout, variable);
    >          }
    >      
    >          private boolean channelPingCheck(int timeout, String variable) {
    >      
    >      //      String variable = "%M1.2:BOOL";
    >      
    >      //      boolean expectedResult = true;
    >      
    >              try {
    >                  plcConnection = getPlcConnection();
    >              } catch (PlcConnectionException e) {
    >                  return false;
    >              }
    >      
    >              PlcReadRequest.Builder builder = 
plcConnection.readRequestBuilder();
    >      
    >              builder.addItem(variable, variable);
    >      
    >              PlcReadRequest readRequest = builder.build();
    >      
    >              PlcReadResponse result = null;
    >              try {
    >                  result = readRequest.execute().get(timeout, 
TimeUnit.MILLISECONDS);
    >              } catch (InterruptedException e) {
    >                  Thread.currentThread().interrupt();
    >              } catch (ExecutionException | TimeoutException e) {
    >                  return false;
    >              }
    >      
    >              if (result == null) {
    >                  return false;
    >              }
    >      
    >              Object content;
    >              content = result.getObject(variable);
    >              if (content == null) {
    >                  return false;
    >              }
    >              //we could compare against the real value of the object 
here, but then we need to be more specific about the variable type
    >              return true;
    >      
    >          }
    >   
    

Reply via email to