diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/PlcVarActual.java b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/PlcVarActual.java index f9b4066..21e808f 100644 --- a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/PlcVarActual.java +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/PlcVarActual.java @@ -31,7 +31,7 @@ public enum PlcVarActual { BooleanArrays8("BooleanArrays8",PlcVar.BOOL_Array,1,DaveArea.DB,3,3262,0), - SubIdArrays("SubIdArrays",PlcVar.STRING_Array,60,DaveArea.DB,3,3270,0,18), + SubIdArrays("SubIdArrays",PlcVar.STRING_Array,60,DaveArea.DB,3,3268,0,18), SubIdArrays1200("SubIdArrays1200",PlcVar.STRING_Array,60,DaveArea.DB,1,20,0,18), diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7ClientNew.java b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7ClientNew.java index 31fff9d..3df51b8 100644 --- a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7ClientNew.java +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7ClientNew.java @@ -23,8 +23,8 @@ import java.util.List; public enum S7ClientNew { //TODO 步骤1 这里是配置多PLC 的,,,有多个plc 就在这里配置一个枚举类 //1500 西门子200smart、1200、1500默认的 机架号=0 槽位号=1; 300/400 默认的 机架-0 插槽-2 - S7_1200("192.168.0.52",0,1,1,PlcVarActual.HeartBeatFor1200), - +// S7_1200("192.168.0.52",0,1,1,PlcVarActual.HeartBeatFor1200), + S7_15001("192.168.0.51",0,1,1,PlcVarActual.HeartBeat), S7_1500("192.168.0.51",0,1,1,PlcVarActual.HeartBeat), //1500 机架-0 插槽-1 //后续 在这里扩展 多PLC应用。 @@ -232,7 +232,7 @@ public enum S7ClientNew { * * * */ - public void write(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset,Integer strSize, PlcVar type, Object newValue) throws Exception { + public void write(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset,Integer strSize, PlcVar type, Object newValue) { S7Connector connector = getConnector(); S7RetryTemplate.getInstance().execute( @@ -290,19 +290,32 @@ public enum S7ClientNew { } } ); - - - - } +// public void retry1() throws InterruptedException { +// S7RetryTemplate instance = S7RetryTemplate.getInstance(); +// System.out.println(instance.hashCode()+ ", "+Thread.currentThread().getName()); +// instance.execute( +// retryContext -> { +// System.out.println("retry1 : " + Thread.currentThread().getName() ); +// Thread.sleep(10000); +// System.out.println("retry1 sleepdown : " + Thread.currentThread().getName() ); +// return true; +// }, +// retryContext -> { +// System.out.println("retry1 err: " + Thread.currentThread().getName() ); +// return false; +// } +// ); +// } + /** * desc: 传入的connection 是需要被替换的 * return: * 1 代表替换成功 * -1 代表替换失败(创建connection失败,原因:出现异常.原来的connection也被舍弃掉了) * */ - private Integer replaceConnector(S7Connector oldOne){ + private synchronized Integer replaceConnector(S7Connector oldOne){ connections.remove(oldOne); S7Connector connect = connect(host, rack, slot); if(connect == null){ @@ -314,7 +327,8 @@ public enum S7ClientNew { } - public S7Connector getConnector() { + //虽然是枚举类,但这个是对象锁(不是类锁) + public synchronized S7Connector getConnector() { int size = connections.size(); S7Connector s7Connector = connections.get((pickOne + size) % size); pickOne+=1; @@ -322,7 +336,9 @@ public enum S7ClientNew { return s7Connector; } - private S7Connector connect(String host,Integer rack,Integer slot ){ + + + private synchronized S7Connector connect(String host,Integer rack,Integer slot ){ try { S7Connector connector = S7ConnectorFactory .buildTCPConnector() @@ -343,7 +359,7 @@ public enum S7ClientNew { connectionPool(); } - private void connectionPool(){ + private synchronized void connectionPool(){ for(int i=0;i var.getLength() ){ + ss = new String[var.getLength()]; + for(int i=0;i< var.getLength();i++){ + ss[i] = s[i]; + } + } + s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),ss); + }else { + s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),newValue); + } + } + private Object read(S7ClientNew s7Client,PlcVarActual var) { + try { + return s7Client.read(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getLength(), var.getStrSize(), var.getType()); + }catch (Exception e){ + logger.error("host:"+s7Client.getHost()+" ; read 操作出现问题: "+e.getMessage()); + e.printStackTrace(); + return null; + } + } + + @Override + public void run(ApplicationArguments args) throws Exception { + + executor.execute(new Runnable() { + @Override + public void run() { + while (true){ + //read one + logger.info(Thread.currentThread().getName()+" , subId : "+Arrays.toString((String[])read(S7ClientNew.S7_1500,PlcVarActual.SubIdArrays))); + try { + Thread.sleep(300); + } catch (InterruptedException e) { + throw new S7ParseDataException(e); + } + + + //write one + String[] toWrite = new String[60]; + for(int i=0;i<60;i++){ + toWrite[i] = RandomStringUtils.randomAlphanumeric(18); + + } + try { + write(S7ClientNew.S7_1500,PlcVarActual.SubIdArrays,toWrite); + } catch (Exception e) { + throw new S7ParseDataException(e); + } + + } + } + }); + executor.execute(new Runnable() { + @Override + public void run() { + while (true){ + logger.info(Thread.currentThread().getName()+" , subId : "+Arrays.toString((String[])read(S7ClientNew.S7_1500,PlcVarActual.SubIdArrays))); + + try { + Thread.sleep(300); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + //write one + String[] toWrite = new String[60]; + for(int i=0;i<60;i++){ + toWrite[i] = RandomStringUtils.randomAlphanumeric(18); + + } + try { + write(S7ClientNew.S7_15001,PlcVarActual.SubIdArrays,toWrite); + } catch (Exception e) { + throw new S7ParseDataException(e); + } + } + } + }); + + + } +} diff --git a/src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java b/src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java new file mode 100644 index 0000000..ddf0648 --- /dev/null +++ b/src/main/java/com/qgs/dc/s7/retrydemo/lock/LockDemo.java @@ -0,0 +1,51 @@ +package com.qgs.dc.s7.retrydemo.lock; + +import com.qgs.dc.s7.my.s7connector.enmuc.S7ClientNew; + +/** + * @Desc: "" + * @Author: caixiang + * @DATE: 2022/12/23 12:08 + */ +public class LockDemo { + + //对象所 + public synchronized void func1() { + System.out.println("func1!"); + try { + Thread.sleep(10000); + System.out.println("sleep time over!"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public synchronized void func2() { + System.out.println("func2!"); + } + + public static void main(String[] args) { + LockDemo test = new LockDemo(); + LockDemo test2 = new LockDemo(); + Thread t1 = new Thread(() -> { +// test.func1(); +// try { +// S7ClientNew.S7_1500.retry1(); +// } catch (InterruptedException e) { +// throw new RuntimeException(e); +// } + }, "t1"); + + Thread t2 = new Thread(() -> { +// test2.func2(); +// try { +// S7ClientNew.S7_1500.retry1(); +// } catch (InterruptedException e) { +// throw new RuntimeException(e); +// } + }, "t2"); + + t1.start(); + t2.start(); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 1531f67..82b9105 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,7 +8,7 @@ server: spring: rabbitmq: # 如果是rabbitmq+haproxy+keepalived集群 ,,那么192.168.0.176是haproxy代理的地址(严格来说是keepalived的vip) - addresses: 172.16.21.191:5672 # 新版rabbitmq 版本还未测试 + addresses: 192.168.0.175:5672 # 新版rabbitmq 版本还未测试 #addresses: 172.16.21.133:5672 username: cigs password: cigs