mark for pull

This commit is contained in:
caixiang 2022-12-05 15:02:50 +08:00
parent ee5efcdd36
commit 1905384bf2
5 changed files with 184 additions and 29 deletions

View File

@ -98,6 +98,32 @@ public class S7DemoController {
return R.ok().put("str",read).put("sub0",sub0); return R.ok().put("str",read).put("sub0",sub0);
} }
@PostMapping("/testWriteBooleanArray")
public R testWriteBooleanArray() throws Exception {
List<Boolean> string = (List<Boolean>)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") @PostMapping("/testForString")
public R testForStrings() throws Exception { public R testForStrings() throws Exception {
//测试结果 l => 66ms //测试结果 l => 66ms
@ -193,6 +219,8 @@ public class S7DemoController {
return R.ok(); return R.ok();
} }
//PlcVarActual 到时候改成你们的 xxxPlcToWcs 或者 xxxWcsToPlc //PlcVarActual 到时候改成你们的 xxxPlcToWcs 或者 xxxWcsToPlc
/** /**
* return * return

View File

@ -6,15 +6,14 @@ package com.qgs.dc.s7.my.s7connector.api.utils;
* @DATE: 2021/12/16 9:08 * @DATE: 2021/12/16 9:08
*/ */
import com.qgs.dc.s7.my.s7connector.utils.CommonFunctions;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Stack;
public class ByteUtils { 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<Boolean> toBoolArray(byte[] b) throws UnsupportedEncodingException { public static List<Boolean> toBoolArray(byte[] b) throws UnsupportedEncodingException {
List<Boolean> res = new ArrayList<>(); List<Boolean> res = new ArrayList<>();
for(int i=0;i<b.length;i++){ for(int i=0;i<b.length;i++){
@ -263,9 +284,60 @@ public class ByteUtils {
return res; return res;
} }
public static boolean[] reverse(boolean[] b) {
boolean[] res = new boolean[8];
res[0] = b[7];
res[1] = b[6];
res[2] = b[5];
res[3] = b[4];
res[4] = b[3];
res[5] = b[2];
res[6] = b[1];
res[7] = b[0];
return res;
}
// public static List<Boolean> 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<Boolean> queue = new LinkedList<Boolean>();
for(int i=0;i<b.length; i++){
queue.add(b[i]);
}
//todo
int z = 0;
Integer boolLength = queue.size();
while (boolLength>0){
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<boolLength;i++){
input[i]= queue.poll();
}
Byte aByte = fromBooleanArray(reverse(input));
res[z] = aByte;
z++;
}
boolLength = boolLength-8;
}
return res;
}
// public st atic List<Boolean> toBoolArray(byte[] b) throws UnsupportedEncodingException {
// List<Boolean> res = new ArrayList<>(); // List<Boolean> res = new ArrayList<>();
// for(int i=0;i<b.length;i++){ // for(int i=0;i<b.length;i++){
// res.add(toBoolean(b[i])); // res.add(toBoolean(b[i]));
@ -754,20 +826,52 @@ public class ByteUtils {
} }
public static void main(String[] args) throws UnsupportedEncodingException { public static void main(String[] args) throws UnsupportedEncodingException {
//stringArray <=> bytes[] // //stringArray <=> bytes[]
String[] src = new String[3]; // String[] src = new String[3];
src[0]= "1"; // src[0]= "1";
src[1]= "30110012105301117222"; // src[1]= "30110012105301117222";
src[2]= "3"; // src[2]= "3";
byte[] bytes = strArrayToBytes(src, 18); // byte[] bytes = strArrayToBytes(src, 18);
String[] strings = toStrArray(bytes, 3, 18); // String[] strings = toStrArray(bytes, 3, 18);
System.out.println(bytes); // 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);
} }
/** /**

View File

@ -32,9 +32,10 @@ public enum PlcVarActual {
SubIdArrays("SubIdArrays",PlcVar.STRING_Array,60,DaveArea.DB,3,3270,0,18), 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)
; ;

View File

@ -1,26 +1,20 @@
package com.qgs.dc.s7.my.s7connector.enmuc; 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.DaveArea;
import com.qgs.dc.s7.my.s7connector.api.S7Connector; 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.factory.S7ConnectorFactory;
import com.qgs.dc.s7.my.s7connector.api.utils.ByteUtils; 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.exception.S7Exception;
import com.qgs.dc.s7.my.s7connector.type.PlcVar; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/** /**
* @Desc: "" * @Desc: ""
@ -149,7 +143,7 @@ public enum S7Client {
byte[] read = connector.read( byte[] read = connector.read(
area, area,
areaNumber, areaNumber,
CommonFunction.exactDivision(length,8), CommonFunctions.exactDivision(length,8),
byteOffset, byteOffset,
bitOffset, bitOffset,
type.getTransportSize() type.getTransportSize()
@ -247,6 +241,15 @@ public enum S7Client {
ByteUtils.strToBytes(newValue.toString(), strSize), ByteUtils.strToBytes(newValue.toString(), strSize),
type 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)){ }else if(type.equals(PlcVar.STRING_Array)){
//todo here 检查 read write service //todo here 检查 read write service
connector.write( connector.write(

View File

@ -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;
}
}