plc4x使用

連接緩存?Connection Cache

引入maven依賴

<dependency>

? ? ? <groupId>org.apache.plc4x</groupId>

? ? ? <artifactId>plc4j-connection-cache</artifactId>

? ? ? <version>0.12.0</version>

? ? </dependency>

配置

PlcConnectionManager connectionManager = CachedPlcConnectionManager.getBuilder()

? ? ? ? ? ? .withMaxLeaseTime(Duration.ofSeconds(10))

? ? ? ? ? ? .withMaxWaitTime(Duration.ofMinutes(1))

? ? ? ? ? ? .build();

例子:

public static void main(String[] args) throws Exception {

? ? ? ? PlcConnectionManager connectionManager = CachedPlcConnectionManager.getBuilder().build();

? ? ? ? for (int i = 0; i < 10000; i++) {

? ? ? ? ? ? try(PlcConnection connection = connectionManager.getConnection("s7://192.168.1.192")) {

? ? ? ? ? ? ? ? if (connection.isConnected()){

? ? ? ? ? ? ? ? ? ? PlcReadRequest.Builder builder = connection.readRequestBuilder();

? ? ? ? ? ? ? ? ? ? builder.addTagAddress("PollingValue", "%DB1:4.0:BOOL");

? ? ? ? ? ? ? ? ? ? PlcReadRequest readRequest = builder.build();

? ? ? ? ? ? ? ? ? ? PlcReadResponse syncResponse = readRequest.execute().get(2000, TimeUnit.MILLISECONDS);

? ? ? ? ? ? ? ? ? ? printResponse(syncResponse);

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? logger.info("PLC is not connected, let's try again to connect");

? ? ? ? ? ? ? ? ? ? connection.connect();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } catch (PlcConnectionException e){

? ? ? ? ? ? ? ? logger.error("Connection exception in trying to connect", e);

? ? ? ? ? ? } catch (CancellationException e){

? ? ? ? ? ? ? ? logger.error("Polling Thread canceled", e);

? ? ? ? ? ? } catch (IllegalStateException e){

? ? ? ? ? ? ? ? logger.error("Error in Netty state machine", e);

? ? ? ? ? ? } catch (ExecutionException e){

? ? ? ? ? ? ? ? logger.error("Interrupted Exception fired", e);

? ? ? ? ? ? } catch (TimeoutException e) {

? ? ? ? ? ? ? ? logger.error("Timeout exception fired", e);

? ? ? ? ? ? }

? ? ? ? ? ? TimeUnit.MILLISECONDS.sleep(100);

? ? ? ? }

? ? ? ? System.exit(0);

? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容