mark for pull

This commit is contained in:
caixiang 2022-12-02 17:06:59 +08:00
parent f00b1fe530
commit ee5efcdd36
5 changed files with 520 additions and 220 deletions

View File

@ -310,4 +310,181 @@ public class CommonFunction {
return str.toString(); return str.toString();
} }
/**
* 将int转为高字节在前低字节在后的byte数组大端
* @param n int
* @return byte[]
*/
public static byte[] intToByteBig(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* 将int转为低字节在前高字节在后的byte数组小端
* @param n int
* @return byte[]
*/
public static byte[] intToByteLittle(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* byte数组到int的转换(小端)
* @param bytes
* @return
*/
public static int bytes2IntLittle(byte[] bytes )
{
int int1=bytes[0]&0xff;
int int2=(bytes[1]&0xff)<<8;
int int3=(bytes[2]&0xff)<<16;
int int4=(bytes[3]&0xff)<<24;
return int1|int2|int3|int4;
}
/**
* byte数组到int的转换(大端)
* @param bytes
* @return
*/
public static int bytes2IntBig(byte[] bytes )
{
int int1=bytes[3]&0xff;
int int2=(bytes[2]&0xff)<<8;
int int3=(bytes[1]&0xff)<<16;
int int4=(bytes[0]&0xff)<<24;
return int1|int2|int3|int4;
}
/**
* 将short转为高字节在前低字节在后的byte数组大端
* @param n short
* @return byte[]
*/
public static byte[] shortToByteBig(short n) {
byte[] b = new byte[2];
b[1] = (byte) (n & 0xff);
b[0] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 将short转为低字节在前高字节在后的byte数组(小端)
* @param n short
* @return byte[]
*/
public static byte[] shortToByteLittle(short n) {
byte[] b = new byte[2];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 读取小端byte数组为short
* @param b
* @return
*/
public static short byteToShortLittle(byte[] b) {
return (short) (((b[1] << 8) | b[0] & 0xff));
}
/**
* 读取大端byte数组为short
* @param b
* @return
*/
public static short byteToShortBig(byte[] b) {
return (short) (((b[0] << 8) | b[1] & 0xff));
}
/**
* long类型转byte[] (大端)
* @param n
* @return
*/
public static byte[] longToBytesBig(long n) {
byte[] b = new byte[8];
b[7] = (byte) (n & 0xff);
b[6] = (byte) (n >> 8 & 0xff);
b[5] = (byte) (n >> 16 & 0xff);
b[4] = (byte) (n >> 24 & 0xff);
b[3] = (byte) (n >> 32 & 0xff);
b[2] = (byte) (n >> 40 & 0xff);
b[1] = (byte) (n >> 48 & 0xff);
b[0] = (byte) (n >> 56 & 0xff);
return b;
}
/**
* long类型转byte[] (小端)
* @param n
* @return
*/
public static byte[] longToBytesLittle(long n) {
byte[] b = new byte[8];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
b[4] = (byte) (n >> 32 & 0xff);
b[5] = (byte) (n >> 40 & 0xff);
b[6] = (byte) (n >> 48 & 0xff);
b[7] = (byte) (n >> 56 & 0xff);
return b;
}
/**
* byte[]转long类型(小端)
* @param array
* @return
*/
public static long bytesToLongLittle( byte[] array )
{
return ((((long) array[ 0] & 0xff) << 0)
| (((long) array[ 1] & 0xff) << 8)
| (((long) array[ 2] & 0xff) << 16)
| (((long) array[ 3] & 0xff) << 24)
| (((long) array[ 4] & 0xff) << 32)
| (((long) array[ 5] & 0xff) << 40)
| (((long) array[ 6] & 0xff) << 48)
| (((long) array[ 7] & 0xff) << 56));
}
/**
* byte[]转long类型(大端)
* @param array
* @return
*/
public static long bytesToLongBig( byte[] array )
{
return ((((long) array[ 0] & 0xff) << 56)
| (((long) array[ 1] & 0xff) << 48)
| (((long) array[ 2] & 0xff) << 40)
| (((long) array[ 3] & 0xff) << 32)
| (((long) array[ 4] & 0xff) << 24)
| (((long) array[ 5] & 0xff) << 16)
| (((long) array[ 6] & 0xff) << 8)
| (((long) array[ 7] & 0xff) << 0));
}
/**
* a 整除 b 如果有余数+1
* */
public static Integer exactDivision(Integer a,Integer b) {
int c = 0;
if(a%b!=0){
c = a/b+1;
}
return c;
}
} }

View File

@ -1,10 +1,14 @@
package com.qgs.dc.s7.my.s7connector; package com.qgs.dc.s7.my.s7connector;
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.utils.ByteUtils; import com.qgs.dc.s7.my.s7connector.api.utils.ByteUtils;
import com.qgs.dc.s7.my.s7connector.enmuc.S7Client; import com.qgs.dc.s7.my.s7connector.enmuc.S7Client;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List; import java.util.List;
/** /**
@ -13,238 +17,287 @@ import java.util.List;
* @DATE: 2021/12/10 10:17 * @DATE: 2021/12/10 10:17
*/ */
public class MainForRead { public class MainForRead {
public static void main(String[] args) throws Exception {
//前言
//DB3.1.1 中间那个1是byte区后面那个1 是bit
//缺陷 不支持 DB3.1.1
//Create connection /**
// S7Connector connector = * 将byte转换为一个长度为8的boolean数组(每bit代表一个boolean值)
// S7ConnectorFactory *
// .buildTCPConnector() * @param b byte, return(将数组翻转过来)
// .withHost("192.168.0.51") * @return boolean数组
// .withRack(0) //optional rack 是机架号 */
// .withSlot(0) //optional slot 是插槽号
// .build();
S7Connector connector = S7Client.S7_1500.getConnector(); public static boolean[] getBooleanArray(byte b,boolean returns) {
// // [0]
// byte[] bool = connector.read(DaveArea.DB, 3, 1, 3266,0);
// byte[] bool2 = connector.read(DaveArea.DB, 3, 1, 3266,1);
// System.out.println("DB3.0-bool : " + ByteUtils.toBoolean(bool));
// System.out.println("DB3.0-bool2 : " + ByteUtils.toBoolean(bool2));
//bool3 => [1] 0000 0001 ==> 1000 0000 10.......
//bool3 => [3] 0000 0011 ==> 1100 0000 11.......
//bool3 => [10] 0000 1010 ==> 0101 0000 0101.....
//bool3 => [25] 0001 1001 ==> 1001 1000 10011....
byte[] bool3 = connector.read(DaveArea.DB, 3, 1, 3267);
System.out.println("DB3.0-bool3 : " + ByteUtils.toBoolean(bool3));
boolean[] array = new boolean[8];
for (int i = 7; i >= 0; i--) { //对于byte的每bit进行判定
array[i] = (b & 1) == 1; //判定byte的最后一位是否为1若为1则是true否则是false
b = (byte) (b >> 1); //将byte右移一位
}
if(returns){
boolean[] array1 = new boolean[8];
array1[0] = array[7];
array1[1] = array[6];
array1[2] = array[5];
array1[3] = array[4];
array1[4] = array[3];
array1[5] = array[2];
array1[6] = array[1];
array1[7] = array[0];
array = array1;
}
return array;
}
public static void main(String[] args) throws UnsupportedEncodingException {
//1000 0101
//1010 0001
}
// public static void main(String[] args) throws Exception {
// //前言
// //DB3.1.1 中间那个1是byte区后面那个1 是bit
// //缺陷 不支持 DB3.1.1
// //
// byte[] bool4 = connector.read(DaveArea.DB, 3, 1, 0); // //Create connection
//// S7Connector connector =
//// S7ConnectorFactory
//// .buildTCPConnector()
//// .withHost("192.168.0.51")
//// .withRack(0) //optional rack 是机架号
//// .withSlot(0) //optional slot 是插槽号
//// .build();
//
// S7Connector connector = S7Client.S7_1500.getConnector();
//
// // // [0]
//// byte[] bool = connector.read(DaveArea.DB, 3, 1, 3266,0);
//// byte[] bool2 = connector.read(DaveArea.DB, 3, 1, 3266,1);
//// System.out.println("DB3.0-bool : " + ByteUtils.toBoolean(bool));
//// System.out.println("DB3.0-bool2 : " + ByteUtils.toBoolean(bool2));
//
//
//
// //bool3 => [1] 0000 0001 ==> 1000 0000 10.......
// //bool3 => [3] 0000 0011 ==> 1100 0000 11.......
// //bool3 => [10] 0000 1010 ==> 0101 0000 0101.....
// //bool3 => [25] 0001 1001 ==> 1001 1000 10011....
// byte[] bool3 = connector.read(DaveArea.DB, 3, 1, 3267);
// System.out.println("DB3.0-bool3 : " + ByteUtils.toBoolean(bool3)); // System.out.println("DB3.0-bool3 : " + ByteUtils.toBoolean(bool3));
// //
// // byte[] boolArrays = connector.read(DaveArea.DB, 3, 11, 3262);
// //非常规
// {
// //注意1:
// //lreal 要小端拿到字节流要翻转一下
// //参数一读取方式一般默认是DB区域块
// //参数二区域块编号
// //参数三区域数据类型大小int 2字节real 4字节
// //参数四区域偏移量
// //[-64, 40, 51, 51, 51, 51, 51, 51] -64==0xc0(要翻转一下才能用)
// //1100 0000 0010 1000 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011
//
// byte[] lreal = connector.read(DaveArea.DB, 3, 8, 26,0);
// System.out.println("DB3.26-lreal : "+ ByteUtils.forLReal(lreal));
//
// byte[] real = connector.read(DaveArea.DB, 3, 4, 22,0);
// System.out.println("DB3.22-real : "+ ByteUtils.forReal(real));
//
// }
// {
// //data 是有符号的双字节
// //注意2
// //[2, -38] 就就代表1990-1-1 因为这就是其实位置 后续位置要1990+n的 D#1992-01-01
// //[4, 72] 1993-1-1
// //0000 0100 0100 1000 72+1024=1096 1990-1-1 + 1096() = 1993-1-1
// byte[] date = connector.read(DaveArea.DB, 3, 2, 42);
//// System.out.println("DB3.42-DATE : "+addDate("1990-01-01",byte2short(date)));
// Long aLong = Long.valueOf(ByteUtils.toInt(date[0], date[1]).toString());
// System.out.println("DB3.42-DATE : "+ByteUtils.addDate("1990-01-01",aLong));
// }
//
// {
// //[7, -78, 1, 1, 5, 0, 0, 0, 0, 0, 0, 0] DTL#1970-01-01-00:00:00 1998-1-1 星期五 0.0.0.0
// //0000 0111 (1100 1110)=>(1011 0010 178) 178+256+512+1024=1970
// //(注意第二个字节是负数要转成补码形式表示(因为在通行传输中 字节是原码形式传输的但是java中long int byte... 都是以补码形式保存的 java帮你自动保存了其实这是不对的所以你要转换一下) 参考https://blog.csdn.net/csdn_ds/article/details/79106006)
// //[7, -68, 1, 21, 2, 0, 0, 0, 0, 0, 0, 0]
// //0000 0111 (1100 0100)=>(1011 1100 188) 188+256+512+1024=1980
// byte[] dtl = connector.read(DaveArea.DB, 3, 12, 44);
// byte[] year = new byte[2];
// year[0] = dtl[0];
// year[1] = dtl[1];
// byte[] month = new byte[1];
// month[0] = dtl[2];
//// System.out.println("DB3.12-DTL : " + byteArrayByteUtils.toUInt(year)+"-"+byteArrayByteUtils.toUInt(month));
// System.out.println("DB3.44-DTL : " + ByteUtils.toInt(year[0],year[1])+"-"+ByteUtils.toInt(month[0]));
// }
//
// {
// //[59, -102, -55, -1] T#11D_13H_46M_39S_999MS
// //0011 1011
// byte[] time = connector.read(DaveArea.DB, 3, 4, 34);
// System.out.println("DB3.34-time : " + ByteUtils.toInt(time[0],time[1],time[2],time[3]) +" ms");
// }
//
// //常规
// {
// // [0]
// byte[] bool = connector.read(DaveArea.DB, 3, 1, 0);
// System.out.println("DB3.0-bool : " + ByteUtils.toBoolean(bool));
//
// //todo 就像这种情况的话这个工具就无法读取了 这个后续可以改他的源码 就是addreesByte/Bit那里 后续再说好了
// byte[] bool2 = connector.read(DaveArea.DB, 3, 1, 3266);
// System.out.println("DB3.3266-bool : " + ByteUtils.toBoolean(bool2));
//
//
// //byte 目前读取以 有符号的十进制
// //[8]
// //[-63]
// //1011 1111
// byte[] byteOne = connector.read(DaveArea.DB, 3, 1, 1);
// System.out.println("DB3.1-byteOne-有符号 : " + ByteUtils.toInt(byteOne[0])); //有符号 整形
//
// System.out.println("DB3.1-byteOne-无符号 : " + ByteUtils.toUInt(byteOne[0])); //无符号 整形
//
//
// // [0, 5] word 2/8/16进制 可无符号 可有符号就看你真没用
// byte[] word = connector.read(DaveArea.DB, 3, 2, 2);
// System.out.println("DB3.2-word : " + ByteUtils.toInt(word[0],word[1]));
//
// // [-1, -1, -1, -4] 把java自动转换成byte类型 的补码形式 ,如果电控变量是这个要问下他是无符号还是有符号的
// // [FF, FF, FF, FC] 原码
// byte[] dword = connector.read(DaveArea.DB, 3, 4, 4);
// System.out.println("DB3.4-dword : " + ByteUtils.toInt(dword[0],dword[1],dword[2],dword[3])); //带符号的 整形
//
// //[11]
// byte[] usint = connector.read(DaveArea.DB, 3, 1, 8);
// System.out.println("DB3.8-usint : " + ByteUtils.toUInt(usint[0]));
//
// byte[] sint = connector.read(DaveArea.DB, 3, 1, 9);
// System.out.println("DB3.9-sint : " + ByteUtils.toInt(sint[0]));
//
// byte[] uint = connector.read(DaveArea.DB, 3, 2, 10);
// System.out.println("DB3.10-uint : " + ByteUtils.toUInt(uint[0],uint[1]));
//
// //[-1, -111] -111
// //[-1, -64] -64
// //[-4, 25] -999
// byte[] ints = connector.read(DaveArea.DB, 3, 2, 12);
// System.out.println("DB3.12-ints : " + ByteUtils.toInt(ints[0],ints[1]));
//
// byte[] dint = connector.read(DaveArea.DB, 3, 4, 14);
// System.out.println("DB3.14-dint : " + ByteUtils.toInt(dint[0],dint[1],dint[2],dint[3]));
//
// byte[] udint = connector.read(DaveArea.DB, 3, 4, 18);
// System.out.println("DB3.18-udint : " + ByteUtils.toUInt(udint[0],udint[1],udint[2],udint[3]));
//
// //plc 中char 是1个字节 注意char wchar 都是ascii码格式的
// byte[] chars = connector.read(DaveArea.DB, 3, 1, 58);
// Character charss = ByteUtils.toChar(chars);
// System.out.println("DB3.58-char : " + charss);
// //plc 中wchar 是2个字节
// byte[] wchar = connector.read(DaveArea.DB, 3, 2, 60);
// Character wchars =ByteUtils.toChar(wchar);
// System.out.println("DB3.60-wchar : " + wchars);
//
// //n+2 个字节 这里的n 就是实际content的字节数 "123"=> n=3 ;; "@123" ==> n=4
// //[-2, 4, 64, 65, 83, 68] @ASD 前面-2 4两个字节就是上面的2字节 是无用的
// //-2 代表 字符串中存储最大的总字节数 4代表字符数 后面跟着的 是内容最多256个字节
// byte[] str = connector.read(DaveArea.DB, 3, 6, 62);
// String string = ByteUtils.toStr(str);
// System.out.println("DB3.62-str : " +string );
//
// }
//
// {
// try {
// //byte 占用一个字节如果是数组的话就读取2个要事先知道点表规定数组长度实际就是读取 DB3.830
// //bytes = byteLength * arrayLength 举例1byteLength * 2arrayLength =2bytes
// byte[] boolArrays = connector.read(DaveArea.DB, 3, 2, 830);
// List<Boolean> booleans = ByteUtils.toBoolArray(boolArrays); // List<Boolean> booleans = ByteUtils.toBoolArray(boolArrays);
// System.out.println("DB3.830-boolArrays : " +booleans ); // System.out.println("DB3.830-boolArrays : " +booleans );
// //
// byte[] byteArrays = connector.read(DaveArea.DB, 3, 2, 832); // byte[] boolArrays14 = connector.read(DaveArea.DB, 3, 16, 3264);
// List<Byte> bytes = ByteUtils.toByteArray(byteArrays); // List<Boolean> booleans14 = ByteUtils.toBoolArray(boolArrays14);
// System.out.println("DB3.832-byteArrays : " +bytes ); // System.out.println("DB3.3268-boolArrays : " +booleans14 );
// //
// byte[] charArrays = connector.read(DaveArea.DB, 3, 2, 834); // //
// List<Character> chars = ByteUtils.toCharArray(charArrays); //// byte[] bool4 = connector.read(DaveArea.DB, 3, 1, 0);
// System.out.println("DB3.834-charArrays : " +chars ); //// System.out.println("DB3.0-bool3 : " + ByteUtils.toBoolean(bool3));
// //
// byte[] wordArrays = connector.read(DaveArea.DB, 3, 4, 836); ////
// List<Integer> words = ByteUtils.toWordArray(wordArrays); ////
// System.out.println("DB3.836-wordArrays : " +words ); //// //非常规
//// {
//// //注意1:
//// //lreal 要小端拿到字节流要翻转一下
//// //参数一读取方式一般默认是DB区域块
//// //参数二区域块编号
//// //参数三区域数据类型大小int 2字节real 4字节
//// //参数四区域偏移量
//// //[-64, 40, 51, 51, 51, 51, 51, 51] -64==0xc0(要翻转一下才能用)
//// //1100 0000 0010 1000 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011
////
//// byte[] lreal = connector.read(DaveArea.DB, 3, 8, 26,0);
//// System.out.println("DB3.26-lreal : "+ ByteUtils.forLReal(lreal));
////
//// byte[] real = connector.read(DaveArea.DB, 3, 4, 22,0);
//// System.out.println("DB3.22-real : "+ ByteUtils.forReal(real));
////
//// }
// //
// byte[] dwordArrays = connector.read(DaveArea.DB, 3, 8, 840); //// {
// List<Integer> dwords = ByteUtils.toDWordArray(dwordArrays); //// //data 是有符号的双字节
// System.out.println("DB3.840-dwordArrays : " +dwords ); //// //注意2
// //// //[2, -38] 就就代表1990-1-1 因为这就是其实位置 后续位置要1990+n的 D#1992-01-01
// byte[] sintArrays = connector.read(DaveArea.DB, 3, 2, 852); //// //[4, 72] 1993-1-1
// List<Integer> sints = ByteUtils.toSIntArray(sintArrays); //// //0000 0100 0100 1000 72+1024=1096 1990-1-1 + 1096() = 1993-1-1
// System.out.println("DB3.852-sintArrays : " +sints ); //// byte[] date = connector.read(DaveArea.DB, 3, 2, 42);
// ////// System.out.println("DB3.42-DATE : "+addDate("1990-01-01",byte2short(date)));
// byte[] intArrays = connector.read(DaveArea.DB, 3, 4, 848); //// Long aLong = Long.valueOf(ByteUtils.toInt(date[0], date[1]).toString());
// List<Integer> ints = ByteUtils.toIntArray(intArrays); //// System.out.println("DB3.42-DATE : "+ByteUtils.addDate("1990-01-01",aLong));
// System.out.println("DB3.848-intArrays : " +ints ); //// }
// ////
// byte[] dintArrays = connector.read(DaveArea.DB, 3, 8, 854); //// {
// List<Integer> dints = ByteUtils.toDIntArray(dintArrays); //// //[7, -78, 1, 1, 5, 0, 0, 0, 0, 0, 0, 0] DTL#1970-01-01-00:00:00 1998-1-1 星期五 0.0.0.0
// System.out.println("DB3.852-dintArrays : " +dints); //// //0000 0111 (1100 1110)=>(1011 0010 178) 178+256+512+1024=1970
//// //(注意第二个字节是负数要转成补码形式表示(因为在通行传输中 字节是原码形式传输的但是java中long int byte... 都是以补码形式保存的 java帮你自动保存了其实这是不对的所以你要转换一下) 参考https://blog.csdn.net/csdn_ds/article/details/79106006)
//// //[7, -68, 1, 21, 2, 0, 0, 0, 0, 0, 0, 0]
//// //0000 0111 (1100 0100)=>(1011 1100 188) 188+256+512+1024=1980
//// byte[] dtl = connector.read(DaveArea.DB, 3, 12, 44);
//// byte[] year = new byte[2];
//// year[0] = dtl[0];
//// year[1] = dtl[1];
//// byte[] month = new byte[1];
//// month[0] = dtl[2];
////// System.out.println("DB3.12-DTL : " + byteArrayByteUtils.toUInt(year)+"-"+byteArrayByteUtils.toUInt(month));
//// System.out.println("DB3.44-DTL : " + ByteUtils.toInt(year[0],year[1])+"-"+ByteUtils.toInt(month[0]));
//// }
////
//// {
//// //[59, -102, -55, -1] T#11D_13H_46M_39S_999MS
//// //0011 1011
//// byte[] time = connector.read(DaveArea.DB, 3, 4, 34);
//// System.out.println("DB3.34-time : " + ByteUtils.toInt(time[0],time[1],time[2],time[3]) +" ms");
//// }
////
//// //常规
//// {
//// // [0]
//// byte[] bool = connector.read(DaveArea.DB, 3, 1, 0);
//// System.out.println("DB3.0-bool : " + ByteUtils.toBoolean(bool));
////
//// //todo 就像这种情况的话这个工具就无法读取了 这个后续可以改他的源码 就是addreesByte/Bit那里 后续再说好了
//// byte[] bool2 = connector.read(DaveArea.DB, 3, 1, 3266);
//// System.out.println("DB3.3266-bool : " + ByteUtils.toBoolean(bool2));
////
////
//// //byte 目前读取以 有符号的十进制
//// //[8]
//// //[-63]
//// //1011 1111
//// byte[] byteOne = connector.read(DaveArea.DB, 3, 1, 1);
//// System.out.println("DB3.1-byteOne-有符号 : " + ByteUtils.toInt(byteOne[0])); //有符号 整形
////
//// System.out.println("DB3.1-byteOne-无符号 : " + ByteUtils.toUInt(byteOne[0])); //无符号 整形
////
////
//// // [0, 5] word 2/8/16进制 可无符号 可有符号就看你真没用
//// byte[] word = connector.read(DaveArea.DB, 3, 2, 2);
//// System.out.println("DB3.2-word : " + ByteUtils.toInt(word[0],word[1]));
////
//// // [-1, -1, -1, -4] 把java自动转换成byte类型 的补码形式 ,如果电控变量是这个要问下他是无符号还是有符号的
//// // [FF, FF, FF, FC] 原码
//// byte[] dword = connector.read(DaveArea.DB, 3, 4, 4);
//// System.out.println("DB3.4-dword : " + ByteUtils.toInt(dword[0],dword[1],dword[2],dword[3])); //带符号的 整形
////
//// //[11]
//// byte[] usint = connector.read(DaveArea.DB, 3, 1, 8);
//// System.out.println("DB3.8-usint : " + ByteUtils.toUInt(usint[0]));
////
//// byte[] sint = connector.read(DaveArea.DB, 3, 1, 9);
//// System.out.println("DB3.9-sint : " + ByteUtils.toInt(sint[0]));
////
//// byte[] uint = connector.read(DaveArea.DB, 3, 2, 10);
//// System.out.println("DB3.10-uint : " + ByteUtils.toUInt(uint[0],uint[1]));
////
//// //[-1, -111] -111
//// //[-1, -64] -64
//// //[-4, 25] -999
//// byte[] ints = connector.read(DaveArea.DB, 3, 2, 12);
//// System.out.println("DB3.12-ints : " + ByteUtils.toInt(ints[0],ints[1]));
////
//// byte[] dint = connector.read(DaveArea.DB, 3, 4, 14);
//// System.out.println("DB3.14-dint : " + ByteUtils.toInt(dint[0],dint[1],dint[2],dint[3]));
////
//// byte[] udint = connector.read(DaveArea.DB, 3, 4, 18);
//// System.out.println("DB3.18-udint : " + ByteUtils.toUInt(udint[0],udint[1],udint[2],udint[3]));
////
//// //plc 中char 是1个字节 注意char wchar 都是ascii码格式的
//// byte[] chars = connector.read(DaveArea.DB, 3, 1, 58);
//// Character charss = ByteUtils.toChar(chars);
//// System.out.println("DB3.58-char : " + charss);
//// //plc 中wchar 是2个字节
//// byte[] wchar = connector.read(DaveArea.DB, 3, 2, 60);
//// Character wchars =ByteUtils.toChar(wchar);
//// System.out.println("DB3.60-wchar : " + wchars);
////
//// //n+2 个字节 这里的n 就是实际content的字节数 "123"=> n=3 ;; "@123" ==> n=4
//// //[-2, 4, 64, 65, 83, 68] @ASD 前面-2 4两个字节就是上面的2字节 是无用的
//// //-2 代表 字符串中存储最大的总字节数 4代表字符数 后面跟着的 是内容最多256个字节
//// byte[] str = connector.read(DaveArea.DB, 3, 6, 62);
//// String string = ByteUtils.toStr(str);
//// System.out.println("DB3.62-str : " +string );
////
//// }
////
//// {
//// try {
//// //byte 占用一个字节如果是数组的话就读取2个要事先知道点表规定数组长度实际就是读取 DB3.830
//// //bytes = byteLength * arrayLength 举例1byteLength * 2arrayLength =2bytes
//// byte[] boolArrays = connector.read(DaveArea.DB, 3, 2, 830);
//// List<Boolean> booleans = ByteUtils.toBoolArray(boolArrays);
//// System.out.println("DB3.830-boolArrays : " +booleans );
////
//// byte[] byteArrays = connector.read(DaveArea.DB, 3, 2, 832);
//// List<Byte> bytes = ByteUtils.toByteArray(byteArrays);
//// System.out.println("DB3.832-byteArrays : " +bytes );
////
//// byte[] charArrays = connector.read(DaveArea.DB, 3, 2, 834);
//// List<Character> chars = ByteUtils.toCharArray(charArrays);
//// System.out.println("DB3.834-charArrays : " +chars );
////
//// byte[] wordArrays = connector.read(DaveArea.DB, 3, 4, 836);
//// List<Integer> words = ByteUtils.toWordArray(wordArrays);
//// System.out.println("DB3.836-wordArrays : " +words );
////
//// byte[] dwordArrays = connector.read(DaveArea.DB, 3, 8, 840);
//// List<Integer> dwords = ByteUtils.toDWordArray(dwordArrays);
//// System.out.println("DB3.840-dwordArrays : " +dwords );
////
//// byte[] sintArrays = connector.read(DaveArea.DB, 3, 2, 852);
//// List<Integer> sints = ByteUtils.toSIntArray(sintArrays);
//// System.out.println("DB3.852-sintArrays : " +sints );
////
//// byte[] intArrays = connector.read(DaveArea.DB, 3, 4, 848);
//// List<Integer> ints = ByteUtils.toIntArray(intArrays);
//// System.out.println("DB3.848-intArrays : " +ints );
////
//// byte[] dintArrays = connector.read(DaveArea.DB, 3, 8, 854);
//// List<Integer> dints = ByteUtils.toDIntArray(dintArrays);
//// System.out.println("DB3.852-dintArrays : " +dints);
////
////
//// byte[] usintArrays = connector.read(DaveArea.DB, 3, 3, 3240);
//// List<Integer> usints = ByteUtils.toUSIntArray(usintArrays);
//// System.out.println("DB3.3240-usintArrays : " +usints );
////
//// byte[] uintArrays = connector.read(DaveArea.DB, 3, 6, 3256);
//// List<Integer> uints = ByteUtils.toUIntArray(uintArrays);
//// System.out.println("DB3.3256-uintArrays : " +uints );
////
//// byte[] udintArrays = connector.read(DaveArea.DB, 3, 12, 3244);
//// List<Long> udints = ByteUtils.toUDIntArray(udintArrays);
//// System.out.println("DB3.852-udintArrays : " +udints);
//// }catch (Exception e){
//// e.printStackTrace();
//// }
////
//// };
// //
// //
// byte[] usintArrays = connector.read(DaveArea.DB, 3, 3, 3240);
// List<Integer> usints = ByteUtils.toUSIntArray(usintArrays);
// System.out.println("DB3.3240-usintArrays : " +usints );
// //
// byte[] uintArrays = connector.read(DaveArea.DB, 3, 6, 3256);
// List<Integer> uints = ByteUtils.toUIntArray(uintArrays);
// System.out.println("DB3.3256-uintArrays : " +uints );
// //
// byte[] udintArrays = connector.read(DaveArea.DB, 3, 12, 3244); //
// List<Long> udints = ByteUtils.toUDIntArray(udintArrays); // //Write to DB100 10 bytes
// System.out.println("DB3.852-udintArrays : " +udints); //
// }catch (Exception e){ //
// e.printStackTrace(); //
// //connector.write(DaveArea.DB, 3, 830, byteArrays);
//
// //Close connection
//
// //connector.close();
// } // }
// //
// };
//Write to DB100 10 bytes
//connector.write(DaveArea.DB, 3, 830, byteArrays);
//Close connection
//connector.close();
}
} }

View File

@ -217,14 +217,62 @@ public class ByteUtils {
return res; return res;
} }
/**
* 将byte转换为一个长度为8的boolean数组(每bit代表一个boolean值)
*
* @param b byte, return(将数组翻转过来)
* @return boolean数组
*/
public static boolean[] getBooleanArray(byte b,boolean returns) {
boolean[] array = new boolean[8];
for (int i = 7; i >= 0; i--) { //对于byte的每bit进行判定
array[i] = (b & 1) == 1; //判定byte的最后一位是否为1若为1则是true否则是false
b = (byte) (b >> 1); //将byte右移一位
}
if(returns){
boolean[] array1 = new boolean[8];
array1[0] = array[7];
array1[1] = array[6];
array1[2] = array[5];
array1[3] = array[4];
array1[4] = array[3];
array1[5] = array[2];
array1[6] = array[1];
array1[7] = array[0];
array = array1;
}
return array;
}
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++){
res.add(toBoolean(b[i])); boolean[] booleanArray = getBooleanArray(b[i], true);
for(int j=0;j<booleanArray.length;j++){
res.add(booleanArray[j]);
}
} }
return res; return res;
} }
// public static List<Boolean> toBoolArray(byte[] b) throws UnsupportedEncodingException {
// List<Boolean> res = new ArrayList<>();
// for(int i=0;i<b.length;i++){
// res.add(toBoolean(b[i]));
// }
// return res;
// }
public static List<Byte> toByteArray(byte[] b) throws UnsupportedEncodingException { public static List<Byte> toByteArray(byte[] b) throws UnsupportedEncodingException {
List<Byte> res = new ArrayList<>(); List<Byte> res = new ArrayList<>();
for(int i=0;i<b.length;i++){ for(int i=0;i<b.length;i++){

View File

@ -26,6 +26,11 @@ public enum PlcVarActual {
//数组变量 //数组变量
CharArrays("CharArrays",PlcVar.CHAR_Array,2,DaveArea.DB,3,834,0), CharArrays("CharArrays",PlcVar.CHAR_Array,2,DaveArea.DB,3,834,0),
BooleanArrays("BooleanArrays",PlcVar.BOOL_Array,2,DaveArea.DB,3,830,0), BooleanArrays("BooleanArrays",PlcVar.BOOL_Array,2,DaveArea.DB,3,830,0),
BooleanArrays16("BooleanArrays16",PlcVar.BOOL_Array,16,DaveArea.DB,3,3268,0),
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,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)

View File

@ -1,5 +1,6 @@
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;
@ -143,6 +144,22 @@ public enum S7Client {
type.getTransportSize() type.getTransportSize()
); );
return type.toObject(readF); return type.toObject(readF);
}else if(type.equals(PlcVar.BOOL_Array)){
byte[] read = connector.read(
area,
areaNumber,
CommonFunction.exactDivision(length,8),
byteOffset,
bitOffset,
type.getTransportSize()
);
List<Boolean> booleans = ByteUtils.toBoolArray(read);
List<Boolean> res = new ArrayList<Boolean>();
for(int i=0;i<length;i++){
res.add(i,booleans.get(i));
}
return res;
}else if(type.equals(PlcVar.STRING_Array)){ }else if(type.equals(PlcVar.STRING_Array)){
Integer arrayLength = length; Integer arrayLength = length;
Integer strSize = strSizes; Integer strSize = strSizes;