diff --git a/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java b/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java index 5c4f476..cbc5a83 100644 --- a/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java +++ b/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java @@ -98,6 +98,32 @@ public class S7DemoController { return R.ok().put("str",read).put("sub0",sub0); } + @PostMapping("/testWriteBooleanArray") + public R testWriteBooleanArray() throws Exception { + + List string = (List)read(S7Client.S7_1500,PlcVarActual.BooleanArrays1200); + System.out.println(); + boolean[] res = new boolean[15]; + res[0] = false; + res[1] = true; + res[2] = false; + res[3] = true; + res[4] = false; + res[5] = true; + res[6] = false; + res[7] = false; + res[8] = false; + res[9] = true; + res[10] = false; + res[11] = false; + res[12] = true; + res[13] = false; + res[14] = true; + write(S7Client.S7_1500,PlcVarActual.BooleanArrays1200, res); + System.out.println(); + return R.ok(); + } + @PostMapping("/testForString") public R testForStrings() throws Exception { //测试结果 l => 66ms @@ -193,6 +219,8 @@ public class S7DemoController { return R.ok(); } + + //PlcVarActual 到时候改成你们的 xxxPlcToWcs 或者 xxxWcsToPlc /** * return diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/api/utils/ByteUtils.java b/src/main/java/com/qgs/dc/s7/my/s7connector/api/utils/ByteUtils.java index 8539a4b..0dc4d6b 100644 --- a/src/main/java/com/qgs/dc/s7/my/s7connector/api/utils/ByteUtils.java +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/api/utils/ByteUtils.java @@ -6,15 +6,14 @@ package com.qgs.dc.s7.my.s7connector.api.utils; * @DATE: 2021/12/16 9:08 */ +import com.qgs.dc.s7.my.s7connector.utils.CommonFunctions; + import java.io.UnsupportedEncodingException; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Stack; +import java.util.*; public class ByteUtils { @@ -252,6 +251,28 @@ public class ByteUtils { } + + public static Byte fromBooleanArray(boolean[] a) { + byte b = 0; + if (a[7]) + b += 1; + if (a[6]) + b += 2; + if (a[5]) + b += 4; + if (a[4]) + b += 8; + if (a[3]) + b += 16; + if (a[2]) + b += 32; + if (a[1]) + b += 64; + if (a[0]) + b += 128; + return b; + } + public static List toBoolArray(byte[] b) throws UnsupportedEncodingException { List res = new ArrayList<>(); for(int i=0;i toBoolArray(byte[] b) throws UnsupportedEncodingException { + //bool array to byte array + public static byte[] toByteArray(boolean[] b) throws UnsupportedEncodingException { + Integer byteLength = CommonFunctions.exactDivision(b.length, 8); + byte[] res = new byte[byteLength]; + Queue queue = new LinkedList(); + for(int i=0;i0){ + boolean[] input = new boolean[8]; + + if(boolLength>=8){ + for(int i = 0;i<8;i++){ + input[i]= queue.poll(); + } + + + Byte aByte = fromBooleanArray(reverse(input)); + res[z] = aByte; + z++; + }else { + for(int i=0;i toBoolArray(byte[] b) throws UnsupportedEncodingException { // List res = new ArrayList<>(); // for(int i=0;i bytes[] - String[] src = new String[3]; - src[0]= "1"; - src[1]= "30110012105301117222"; - src[2]= "3"; - byte[] bytes = strArrayToBytes(src, 18); - String[] strings = toStrArray(bytes, 3, 18); - System.out.println(bytes); +// //stringArray <=> bytes[] +// String[] src = new String[3]; +// src[0]= "1"; +// src[1]= "30110012105301117222"; +// src[2]= "3"; +// byte[] bytes = strArrayToBytes(src, 18); +// String[] strings = toStrArray(bytes, 3, 18); +// System.out.println(bytes); +// +// //string <=> bytes[] +// String s = "12323221"; +// byte[] bytes1 = strToBytes(s, 18); +// String s1 = toStr(bytes1); +// System.out.println(s1); + + boolean[] res = new boolean[22]; + res[0] = false; + res[1] = true; + res[2] = false; + res[3] = true; + res[4] = false; + res[5] = true; + res[6] = false; + res[7] = false; + + res[8] = false; + res[9] = true; + res[10] = false; + res[11] = false; + res[12] = true; + res[13] = false; + res[14] = true; + res[15] = false; + + + res[16] = true; + res[17] = false; + res[18] = false; + res[19] = false; + res[20] = true; + res[21] = false; + + byte[] bytes = toByteArray(res); + System.out.println(); + - //string <=> bytes[] - String s = "12323221"; - byte[] bytes1 = strToBytes(s, 18); - String s1 = toStr(bytes1); - System.out.println(s1); } /** 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 54fcfad..f9b4066 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 @@ -32,9 +32,10 @@ public enum PlcVarActual { SubIdArrays("SubIdArrays",PlcVar.STRING_Array,60,DaveArea.DB,3,3270,0,18), - SubIdArrays1200("SubIdArrays1200",PlcVar.STRING_Array,60,DaveArea.DB,1,20,0,18) + SubIdArrays1200("SubIdArrays1200",PlcVar.STRING_Array,60,DaveArea.DB,1,20,0,18), + BooleanArrays1200("BooleanArrays1200",PlcVar.BOOL_Array,15,DaveArea.DB,3,3264,0) ; diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java index 2fb3d3a..0619712 100644 --- a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java @@ -1,26 +1,20 @@ package com.qgs.dc.s7.my.s7connector.enmuc; -import com.qgs.dc.common.utils.CommonFunction; + import com.qgs.dc.s7.my.s7connector.api.DaveArea; import com.qgs.dc.s7.my.s7connector.api.S7Connector; import com.qgs.dc.s7.my.s7connector.api.factory.S7ConnectorFactory; import com.qgs.dc.s7.my.s7connector.api.utils.ByteUtils; import com.qgs.dc.s7.my.s7connector.exception.S7Exception; import com.qgs.dc.s7.my.s7connector.type.PlcVar; -import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; +import com.qgs.dc.s7.my.s7connector.utils.CommonFunctions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.ResponseEntity; - -import java.io.UnsupportedEncodingException; -import java.text.ParseException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; + /** * @Desc: "" @@ -149,7 +143,7 @@ public enum S7Client { byte[] read = connector.read( area, areaNumber, - CommonFunction.exactDivision(length,8), + CommonFunctions.exactDivision(length,8), byteOffset, bitOffset, type.getTransportSize() @@ -247,6 +241,15 @@ public enum S7Client { ByteUtils.strToBytes(newValue.toString(), strSize), type ); + }else if(type.equals(PlcVar.BOOL_Array)){ + connector.write( + area, + areaNumber, + byteOffset, + bitOffset, + ByteUtils.toByteArray((boolean[])newValue), + type + ); }else if(type.equals(PlcVar.STRING_Array)){ //todo here 检查 read write service connector.write( diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/utils/CommonFunctions.java b/src/main/java/com/qgs/dc/s7/my/s7connector/utils/CommonFunctions.java new file mode 100644 index 0000000..c29bb4a --- /dev/null +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/utils/CommonFunctions.java @@ -0,0 +1,19 @@ +package com.qgs.dc.s7.my.s7connector.utils; + +/** + * @Desc: "" + * @Author: caixiang + * @DATE: 2022/12/5 11:11 + */ +public class CommonFunctions { + /** + * a 整除 b ,如果有余数+1 + * */ + public static Integer exactDivision(Integer a,Integer b) { + int c = 0; + if(a%b!=0){ + c = a/b+1; + } + return c; + } +}