完善 。S7 ,在枚举类中进行read/write操作
This commit is contained in:
vanhempi
3842992695
commit
6c3e537e6f
@ -4,7 +4,7 @@ import com.qgs.dc.opcua.controller.R;
|
||||
import com.qgs.dc.s7.my.s7connector.enmuc.PlcVarActual;
|
||||
import com.qgs.dc.s7.my.s7connector.enmuc.S7Client;
|
||||
|
||||
import com.qgs.dc.s7.my.s7connector.service.S7Service;
|
||||
|
||||
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -21,14 +21,14 @@ import java.util.Random;
|
||||
public class S7DemoController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class);
|
||||
|
||||
@Autowired
|
||||
S7Service s7Service;
|
||||
|
||||
|
||||
//demo1
|
||||
@PostMapping("/testReadAll")
|
||||
public R testReadAll() throws UnsupportedEncodingException, ParseException {
|
||||
for(PlcVarActual actual:PlcVarActual.values()){
|
||||
System.out.println(s7Service.read(actual,S7Client.S7_1500));;
|
||||
System.out.println(read(S7Client.S7_1500,actual));
|
||||
|
||||
}
|
||||
return R.ok();
|
||||
}
|
||||
@ -36,28 +36,28 @@ public class S7DemoController {
|
||||
//demo2
|
||||
@PostMapping("/readTest")
|
||||
public R getTestForS7() throws UnsupportedEncodingException, ParseException {
|
||||
Boolean heartBeat = (Boolean)s7Service.read(PlcVarActual.HeartBeat,S7Client.S7_1500);
|
||||
String ddtl = (String)s7Service.read(PlcVarActual.DTL,S7Client.S7_1500);
|
||||
List<Character> characters = (List<Character>)s7Service.read(PlcVarActual.CharArrays,S7Client.S7_1500);
|
||||
Boolean heartBeat = (Boolean)read(S7Client.S7_1500,PlcVarActual.HeartBeat);
|
||||
String ddtl = (String)read(S7Client.S7_1500,PlcVarActual.DTL);
|
||||
List<Character> characters = (List<Character>)read(S7Client.S7_1500,PlcVarActual.CharArrays);
|
||||
|
||||
List<Boolean> booleans = (List<Boolean>)s7Service.read(PlcVarActual.BooleanArrays,S7Client.S7_1500);
|
||||
String stri = (String)s7Service.read(PlcVarActual.STRING1,S7Client.S7_1500);
|
||||
List<Boolean> booleans = (List<Boolean>)read(S7Client.S7_1500,PlcVarActual.BooleanArrays);
|
||||
String stri = (String)read(S7Client.S7_1500,PlcVarActual.STRING1);
|
||||
|
||||
return R.ok().put("res",heartBeat).put("characters",characters).put("ddtl",ddtl).put("bools",booleans).put("str",stri);
|
||||
}
|
||||
|
||||
@PostMapping("/readTest2")
|
||||
public R getTest2ForS7() throws UnsupportedEncodingException, ParseException {
|
||||
// List<Character> characters = (List<Character>)s7Service.read(PlcVarActual.CharArrays,S7Client.S7_1500);
|
||||
// List<Character> characters = (List<Character>)read(PlcVarActual.CharArrays,S7Client.S7_1500);
|
||||
//
|
||||
// List<Boolean> booleans = (List<Boolean>)s7Service.read(PlcVarActual.BooleanArrays,S7Client.S7_1500);
|
||||
// String stri = (String)s7Service.read(PlcVarActual.STRING1,S7Client.S7_1500);
|
||||
// List<Boolean> booleans = (List<Boolean>)read(PlcVarActual.BooleanArrays,S7Client.S7_1500);
|
||||
// String stri = (String)read(PlcVarActual.STRING1,S7Client.S7_1500);
|
||||
//
|
||||
// return R.ok().put("res",heartBeat).put("characters",characters).put("ddtl",ddtl).put("bools",booleans).put("str",stri);
|
||||
String con = "1233ADSDA";
|
||||
s7Service.write(PlcVarActual.STRING1, con, S7Client.S7_1500);
|
||||
String read = (String)s7Service.read(PlcVarActual.STRING1, S7Client.S7_1500);
|
||||
String sub0 = (String)s7Service.read(PlcVarActual.SUBID0, S7Client.S7_1500);
|
||||
write(S7Client.S7_1500,PlcVarActual.STRING1, con);
|
||||
String read = (String)read(S7Client.S7_1500,PlcVarActual.STRING1);
|
||||
String sub0 = (String)read(S7Client.S7_1500,PlcVarActual.SUBID0);
|
||||
|
||||
|
||||
return R.ok().put("str",read).put("sub0",sub0);
|
||||
@ -67,8 +67,8 @@ public class S7DemoController {
|
||||
public R testForStrings() throws UnsupportedEncodingException, ParseException {
|
||||
//测试结果 l => 66ms
|
||||
long l = System.currentTimeMillis();
|
||||
String string = (String)s7Service.read(PlcVarActual.STRING1, S7Client.S7_1500);
|
||||
String[] subs = (String[])s7Service.read(PlcVarActual.SubIdArrays, S7Client.S7_1500);
|
||||
String string = (String)read(S7Client.S7_1500,PlcVarActual.STRING1);
|
||||
String[] subs = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays);
|
||||
long l1 = System.currentTimeMillis();
|
||||
|
||||
String[] toWrite = new String[60];
|
||||
@ -78,13 +78,13 @@ public class S7DemoController {
|
||||
}
|
||||
////测试结果 c => 57ms
|
||||
long c1 = System.currentTimeMillis();
|
||||
s7Service.write(PlcVarActual.SubIdArrays,toWrite,S7Client.S7_1500);
|
||||
write(S7Client.S7_1500,PlcVarActual.SubIdArrays,toWrite);
|
||||
long c2 = System.currentTimeMillis();
|
||||
String s = "cai xiang";
|
||||
s7Service.write(PlcVarActual.STRING1,s,S7Client.S7_1500);
|
||||
write(S7Client.S7_1500,PlcVarActual.STRING1,s);
|
||||
|
||||
String string2 = (String)s7Service.read(PlcVarActual.STRING1, S7Client.S7_1500);
|
||||
String[] subs2 = (String[])s7Service.read(PlcVarActual.SubIdArrays, S7Client.S7_1500);
|
||||
String string2 = (String)read(S7Client.S7_1500,PlcVarActual.STRING1);
|
||||
String[] subs2 = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays);
|
||||
|
||||
return R.ok().put("l",(l1-l)).put("c",(c2-c1));
|
||||
}
|
||||
@ -93,7 +93,7 @@ public class S7DemoController {
|
||||
public R testForString1200() throws UnsupportedEncodingException, ParseException {
|
||||
//测试结果 l => 66ms
|
||||
long l = System.currentTimeMillis();
|
||||
String[] subs = (String[])s7Service.read(PlcVarActual.SubIdArrays1200, S7Client.S7_1200);
|
||||
String[] subs = (String[])read(S7Client.S7_1200,PlcVarActual.SubIdArrays1200);
|
||||
long l1 = System.currentTimeMillis();
|
||||
|
||||
String[] toWrite = new String[60];
|
||||
@ -103,39 +103,56 @@ public class S7DemoController {
|
||||
}
|
||||
////测试结果 c => 57ms
|
||||
long c1 = System.currentTimeMillis();
|
||||
s7Service.write(PlcVarActual.SubIdArrays1200,toWrite,S7Client.S7_1200);
|
||||
write(S7Client.S7_1200,PlcVarActual.SubIdArrays1200,toWrite);
|
||||
long c2 = System.currentTimeMillis();
|
||||
|
||||
|
||||
String[] subs2 = (String[])s7Service.read(PlcVarActual.SubIdArrays1200, S7Client.S7_1200);
|
||||
String[] subs2 = (String[])read(S7Client.S7_1200,PlcVarActual.SubIdArrays1200);
|
||||
|
||||
return R.ok().put("l",(l1-l)).put("c",(c2-c1));
|
||||
}
|
||||
@PostMapping("/testFor1200")
|
||||
public R testFor1200() throws UnsupportedEncodingException, ParseException {
|
||||
Object subs = s7Service.read(PlcVarActual.INT1200, S7Client.S7_1200);
|
||||
//Object subs = read(PlcVarActual.INT1200, S7Client.S7_1200);
|
||||
Object read = read(S7Client.S7_1200, PlcVarActual.SubIdArrays1200);
|
||||
|
||||
String[] toWrite = new String[60];
|
||||
for(int i=0;i<60;i++){
|
||||
int i1 = new Random().nextInt(100);
|
||||
toWrite[i] = "2212"+ i1;
|
||||
}
|
||||
write(S7Client.S7_1200,PlcVarActual.SubIdArrays1200,toWrite);
|
||||
|
||||
Object read2 = read(S7Client.S7_1200, PlcVarActual.SubIdArrays1200);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
//PlcVarActual 到时候改成你们的 xxxPlcToWcs 或者 xxxWcsToPlc
|
||||
private Object read(S7Client s7Client,PlcVarActual var) throws UnsupportedEncodingException, ParseException {
|
||||
return s7Client.read(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getLength(), var.getStrSize(), var.getType());
|
||||
}
|
||||
private void write(S7Client s7Client,PlcVarActual var,Object newValue) {
|
||||
s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),newValue);
|
||||
}
|
||||
|
||||
//demo3
|
||||
@PostMapping("/writeTest")
|
||||
public R writeTest() throws PlcConnectionException, UnsupportedEncodingException {
|
||||
s7Service.write(PlcVarActual.HeartBeat, false, S7Client.S7_1500);
|
||||
write(S7Client.S7_1500,PlcVarActual.HeartBeat, false);
|
||||
|
||||
char[] charArrays_content = new char[2];
|
||||
charArrays_content[0] = '1';
|
||||
charArrays_content[1] = 'c';
|
||||
s7Service.write(PlcVarActual.CharArrays, charArrays_content, S7Client.S7_1500);
|
||||
write( S7Client.S7_1500,PlcVarActual.CharArrays, charArrays_content);
|
||||
|
||||
boolean[] boolArrays_content = new boolean[2];
|
||||
boolArrays_content[0] = true;
|
||||
boolArrays_content[1] = false;
|
||||
s7Service.write(PlcVarActual.BooleanArrays, boolArrays_content, S7Client.S7_1500);
|
||||
write(S7Client.S7_1500,PlcVarActual.BooleanArrays, boolArrays_content);
|
||||
|
||||
String str = "你好啊aa";
|
||||
//todo string 的读写有问题 待会看看
|
||||
s7Service.write(PlcVarActual.STRING1, str, S7Client.S7_1500);
|
||||
write(S7Client.S7_1500,PlcVarActual.STRING1, str);
|
||||
return R.ok().put("res",true);
|
||||
}
|
||||
|
||||
|
@ -54,6 +54,7 @@ public enum PlcVarActual {
|
||||
this.areaNumber = areaNumber;
|
||||
this.byteOffset = byteOffset;
|
||||
this.bitOffset = bitOffset;
|
||||
this.strSize = 0;
|
||||
}
|
||||
PlcVarActual(String name, PlcVar type,Integer length, DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset,Integer strSize){
|
||||
this.name = name;
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.qgs.dc.s7.my.s7connector.enmuc;
|
||||
|
||||
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.type.PlcVar;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -17,7 +22,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
*/
|
||||
public enum S7Client {
|
||||
//TODO 步骤1 这里是配置多PLC 的,,,有多个plc 就在这里配置一个枚举类
|
||||
//1500 虽然设备上显示机架-0 插槽-1(这个是默认的) 但是我们这里 rack、slot 都是0,反正我们这里就让电控rack和solt都是默认的 然后我们这边都配置0就行了。
|
||||
//1500 西门子200smart、1200、1500默认的 机架号=0 槽位号=1; 300/400 默认的 机架-0 插槽-2
|
||||
S7_1500("192.168.0.51",0,1,3,PlcVarActual.HeartBeat),
|
||||
//1500 机架-0 插槽-1
|
||||
S7_1200("192.168.0.52",0,1,3,PlcVarActual.HeartBeatFor1200)
|
||||
@ -65,9 +70,175 @@ public enum S7Client {
|
||||
check_ping();
|
||||
}
|
||||
|
||||
/**
|
||||
* PlcVar(byte[]) 转 java对象 对照表
|
||||
* 单体变量
|
||||
* Bool ===> Boolean
|
||||
* LREAL ===> Double
|
||||
* REAL ===> Float
|
||||
* DATE ===> String(yyyy-MM-dd 这种形式的类型)
|
||||
* DTL ===> String("年-月-日-工作日-时-分-秒" 这种格式)
|
||||
* TIME ===> Integer(单位 ms)
|
||||
* USINT ===> Integer
|
||||
* SINT ===> Integer
|
||||
* UINT ===> Integer
|
||||
* INT ===> Integer
|
||||
* DINT ===> Integer
|
||||
* UINT ===> Long
|
||||
* Byte ===> Integer(有符号)(默认)
|
||||
* Integer(无符号)(后续扩展)
|
||||
* Char ===> Character
|
||||
* WChar ===> Character
|
||||
* String ===> String (特殊)
|
||||
|
||||
|
||||
* 数组变量
|
||||
* BoolArray ===> List<Boolean>
|
||||
* ByteArray ===> List<Byte>
|
||||
* WordArray ===> List<Integer>
|
||||
* DWordArray ===> List<Integer>
|
||||
* CharArray ===> List<Character>
|
||||
* SIntArray ===> List<Integer>
|
||||
* IntArray ===> List<Integer>
|
||||
* DIntArray ===> List<Integer>
|
||||
* UIntArray ===> List<Integer>
|
||||
* USIntArray ===> List<Integer>
|
||||
* UDIntArray ===> List<Long>
|
||||
* StringArray ===> String[] (特殊)
|
||||
*
|
||||
*
|
||||
* */
|
||||
public Object read(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset, Integer length, Integer strSizes, PlcVar type) throws UnsupportedEncodingException, ParseException {
|
||||
S7Connector connector = getConnector();
|
||||
//String 类型比较特殊。 String[] 也是同理,Sring数组里面的子项 也是有两个字节的 readBytes。
|
||||
if(type.equals(PlcVar.STRING)){
|
||||
Integer readBytes = 2;
|
||||
byte[] read = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
readBytes,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
|
||||
Integer allLength = Integer.valueOf(read[1])+2;
|
||||
byte[] readF = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
allLength,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
return type.toObject(readF);
|
||||
}else if(type.equals(PlcVar.STRING_Array)){
|
||||
Integer arrayLength = length;
|
||||
Integer strSize = strSizes;
|
||||
|
||||
byte[] read = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
arrayLength*(strSize+2),
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
return ByteUtils.toStrArray(read, arrayLength, strSize);
|
||||
}else {
|
||||
Integer readBytes = type.getTransportSize().getSizeInBytes() * length;
|
||||
byte[] read = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
readBytes,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
return type.toObject(read);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* eg :
|
||||
* Object newValue = Boolean.FALSE
|
||||
* s7Service.write(PlcVarActual.HeartBeat, newValue, S7Client.S7_1200);
|
||||
*
|
||||
* PlcVar(byte[]) 转 java对象 对照表
|
||||
* 单体变量
|
||||
* Bool ===> Object newValue = Boolean.FALSE
|
||||
* LREAL ===> Object newValue = Boolean.FALSE
|
||||
* REAL ===> Object newValue = Boolean.FALSE
|
||||
* DATE ===> 暂时没需求(有问题找我)
|
||||
* DTL ===> 暂时没需求(有问题找我)
|
||||
* TIME ===> 暂时没需求(有问题找我)
|
||||
* USINT ===> Object newValue = new Integer(1)
|
||||
* SINT ===> Object newValue = new Integer(1)
|
||||
* UINT ===> Object newValue = new Integer(1)
|
||||
* INT ===> Object newValue = new Integer(1)
|
||||
* DINT ===> Object newValue = new Integer(1)
|
||||
* UINT ===> Object newValue = new Integer(1)
|
||||
* Byte ===> Object newValue = 0x11
|
||||
*
|
||||
* Char ===> Object newValue = 'a'
|
||||
* WChar ===> Object newValue = '菜'
|
||||
* String ===> Object newValue = '你好啊' (特殊)
|
||||
|
||||
|
||||
* 数组变量
|
||||
* 注意:在write的时候,你write的数量 一定要和 plc中存在的数量一一对应
|
||||
* BoolArray ===> boolean[] booleanArray = new boolean[2]; .... 赋予值
|
||||
* ByteArray ===> byte[] write_byteArrays = new byte[2];
|
||||
* WordArray ===> short[] shortArrays_content = new short[2];
|
||||
* DWordArray ===> int[] intArrays_content = new int[2];
|
||||
* CharArray ===> char[] charArrays_content = new char[2];
|
||||
* SIntArray ===> int[] sintArrays_content = new int[2];
|
||||
* IntArray ===> int[] iintArrays_content = new int[2];
|
||||
* DIntArray ===> int[] dintArrays_content = new int[2];
|
||||
* UIntArray ===> int[] uintArrays_content = new int[3];
|
||||
* USIntArray ===> int[] usintArrays_content = new int[3];
|
||||
* UDIntArray ===> int[] udintArrays_content = new int[3];
|
||||
* StringArray ===> String[] stringArrays_content = new String[3];
|
||||
* //如果有其他数据类型 这里没有找我扩展
|
||||
*
|
||||
*
|
||||
* */
|
||||
public void write(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset,Integer strSize, PlcVar type, Object newValue){
|
||||
S7Connector connector = getConnector();
|
||||
|
||||
//String 类型比较特殊。 String[] 也是同理,Sring数组里面的子项 也是有两个字节的 readBytes。
|
||||
if(type.equals(PlcVar.STRING)){
|
||||
connector.write(
|
||||
area,
|
||||
areaNumber,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
ByteUtils.strToBytes(newValue.toString(), strSize),
|
||||
type
|
||||
);
|
||||
}else if(type.equals(PlcVar.STRING_Array)){
|
||||
//todo here 检查 read write service
|
||||
connector.write(
|
||||
area,
|
||||
areaNumber,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
ByteUtils.strArrayToBytes((String[])newValue, strSize),
|
||||
type
|
||||
);
|
||||
}else {
|
||||
byte[] bytes = type.toBytes(newValue);
|
||||
connector.write(
|
||||
area,
|
||||
areaNumber,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
bytes,
|
||||
type
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public S7Connector getConnector() {
|
||||
@ -77,6 +248,7 @@ public enum S7Client {
|
||||
pickOne = (pickOne)%size;
|
||||
return s7Connector;
|
||||
}
|
||||
|
||||
private S7Connector connect(String host,Integer rack,Integer slot ){
|
||||
try {
|
||||
S7Connector connector = S7ConnectorFactory
|
||||
|
@ -1,293 +0,0 @@
|
||||
package com.qgs.dc.s7.my.s7connector.service;
|
||||
|
||||
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.utils.ByteUtils;
|
||||
import com.qgs.dc.s7.my.s7connector.enmuc.PlcVarActual;
|
||||
import com.qgs.dc.s7.my.s7connector.enmuc.S7Client;
|
||||
import com.qgs.dc.s7.my.s7connector.type.PlcVar;
|
||||
import com.qgs.dc.s7.my.s7connector.type.TransportSize;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.text.ParseException;
|
||||
|
||||
/**
|
||||
* @Desc: ""
|
||||
* @Author: caixiang
|
||||
* @DATE: 2022/1/17 14:20
|
||||
*/
|
||||
//tips: 外部使用这个service的read、write操作 要异常捕捉,然后在catch里面 用logger.info("记录下异常。"),这里的异常是不会被捕捉的只会向外抛。
|
||||
//关于对String、String[]操作需要注意:
|
||||
// ① 单体变量 length 都是1
|
||||
// ② 数组变量 length 就是 你要read 或者write数组的长度
|
||||
// ③ bitOffset 是针对Bool 来说的,其他变量bitOffset 都是0
|
||||
// ④ strSize 这个字段只是给 String变量 和 String[]变量 用的, 这个字段是plc中设置的最大String长度
|
||||
// ⑤ String类型 在write的时候禁止 write中文,因为plc中编码并没有采用UTF-8 而是采用ASCII
|
||||
// (同理也是,当plc中有设置String类型或者StringArray那么告诉他们必须指定长度 通常他们也会指定长度的。)
|
||||
@Component
|
||||
public class S7Service {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
S7Service(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* PlcVar(byte[]) 转 java对象 对照表
|
||||
* 单体变量
|
||||
* Bool ===> Boolean
|
||||
* LREAL ===> Double
|
||||
* REAL ===> Float
|
||||
* DATE ===> String(yyyy-MM-dd 这种形式的类型)
|
||||
* DTL ===> String("年-月-日-工作日-时-分-秒" 这种格式)
|
||||
* TIME ===> Integer(单位 ms)
|
||||
* USINT ===> Integer
|
||||
* SINT ===> Integer
|
||||
* UINT ===> Integer
|
||||
* INT ===> Integer
|
||||
* DINT ===> Integer
|
||||
* UINT ===> Long
|
||||
* Byte ===> Integer(有符号)(默认)
|
||||
* Integer(无符号)(后续扩展)
|
||||
* Char ===> Character
|
||||
* WChar ===> Character
|
||||
* String ===> String (特殊)
|
||||
|
||||
|
||||
* 数组变量
|
||||
* BoolArray ===> List<Boolean>
|
||||
* ByteArray ===> List<Byte>
|
||||
* WordArray ===> List<Integer>
|
||||
* DWordArray ===> List<Integer>
|
||||
* CharArray ===> List<Character>
|
||||
* SIntArray ===> List<Integer>
|
||||
* IntArray ===> List<Integer>
|
||||
* DIntArray ===> List<Integer>
|
||||
* UIntArray ===> List<Integer>
|
||||
* USIntArray ===> List<Integer>
|
||||
* UDIntArray ===> List<Long>
|
||||
* StringArray ===> String[] (特殊)
|
||||
*
|
||||
*
|
||||
* */
|
||||
public Object read(PlcVarActual plcVarActual,S7Client s7Client) throws UnsupportedEncodingException, ParseException {
|
||||
S7Connector connector = s7Client.getConnector();
|
||||
//String 类型比较特殊。 String[] 也是同理,Sring数组里面的子项 也是有两个字节的 readBytes。
|
||||
if(plcVarActual.getType().equals(PlcVar.STRING)){
|
||||
Integer readBytes = 2;
|
||||
byte[] read = connector.read(
|
||||
plcVarActual.getArea(),
|
||||
plcVarActual.getAreaNumber(),
|
||||
readBytes,
|
||||
plcVarActual.getByteOffset(),
|
||||
plcVarActual.getBitOffset(),
|
||||
plcVarActual.getType().getTransportSize()
|
||||
);
|
||||
|
||||
Integer allLength = Integer.valueOf(read[1])+2;
|
||||
byte[] readF = connector.read(
|
||||
plcVarActual.getArea(),
|
||||
plcVarActual.getAreaNumber(),
|
||||
allLength,
|
||||
plcVarActual.getByteOffset(),
|
||||
plcVarActual.getBitOffset(),
|
||||
plcVarActual.getType().getTransportSize()
|
||||
);
|
||||
return plcVarActual.getType().toObject(readF);
|
||||
}else if(plcVarActual.getType().equals(PlcVar.STRING_Array)){
|
||||
Integer arrayLength = plcVarActual.getLength();
|
||||
Integer strSize = plcVarActual.getStrSize();
|
||||
|
||||
byte[] read = connector.read(
|
||||
plcVarActual.getArea(),
|
||||
plcVarActual.getAreaNumber(),
|
||||
arrayLength*(strSize+2),
|
||||
plcVarActual.getByteOffset(),
|
||||
plcVarActual.getBitOffset(),
|
||||
plcVarActual.getType().getTransportSize()
|
||||
);
|
||||
return ByteUtils.toStrArray(read, arrayLength, strSize);
|
||||
}else {
|
||||
Integer readBytes = plcVarActual.getType().getTransportSize().getSizeInBytes() * plcVarActual.getLength();
|
||||
byte[] read = connector.read(
|
||||
plcVarActual.getArea(),
|
||||
plcVarActual.getAreaNumber(),
|
||||
readBytes,
|
||||
plcVarActual.getByteOffset(),
|
||||
plcVarActual.getBitOffset(),
|
||||
plcVarActual.getType().getTransportSize()
|
||||
);
|
||||
return plcVarActual.getType().toObject(read);
|
||||
}
|
||||
}
|
||||
public Object read(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset, Integer length, Integer strSizes, PlcVar type,S7Client s7Client) throws UnsupportedEncodingException, ParseException {
|
||||
S7Connector connector = s7Client.getConnector();
|
||||
//String 类型比较特殊。 String[] 也是同理,Sring数组里面的子项 也是有两个字节的 readBytes。
|
||||
if(type.equals(PlcVar.STRING)){
|
||||
Integer readBytes = 2;
|
||||
byte[] read = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
readBytes,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
|
||||
Integer allLength = Integer.valueOf(read[1])+2;
|
||||
byte[] readF = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
allLength,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
return type.toObject(readF);
|
||||
}else if(type.equals(PlcVar.STRING_Array)){
|
||||
Integer arrayLength = length;
|
||||
Integer strSize = strSizes;
|
||||
|
||||
byte[] read = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
arrayLength*(strSize+2),
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
return ByteUtils.toStrArray(read, arrayLength, strSize);
|
||||
}else {
|
||||
Integer readBytes = type.getTransportSize().getSizeInBytes() * length;
|
||||
byte[] read = connector.read(
|
||||
area,
|
||||
areaNumber,
|
||||
readBytes,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
type.getTransportSize()
|
||||
);
|
||||
return type.toObject(read);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* eg :
|
||||
* Object newValue = Boolean.FALSE
|
||||
* s7Service.write(PlcVarActual.HeartBeat, newValue, S7Client.S7_1200);
|
||||
*
|
||||
* PlcVar(byte[]) 转 java对象 对照表
|
||||
* 单体变量
|
||||
* Bool ===> Object newValue = Boolean.FALSE
|
||||
* LREAL ===> Object newValue = Boolean.FALSE
|
||||
* REAL ===> Object newValue = Boolean.FALSE
|
||||
* DATE ===> 暂时没需求(有问题找我)
|
||||
* DTL ===> 暂时没需求(有问题找我)
|
||||
* TIME ===> 暂时没需求(有问题找我)
|
||||
* USINT ===> Object newValue = new Integer(1)
|
||||
* SINT ===> Object newValue = new Integer(1)
|
||||
* UINT ===> Object newValue = new Integer(1)
|
||||
* INT ===> Object newValue = new Integer(1)
|
||||
* DINT ===> Object newValue = new Integer(1)
|
||||
* UINT ===> Object newValue = new Integer(1)
|
||||
* Byte ===> Object newValue = 0x11
|
||||
*
|
||||
* Char ===> Object newValue = 'a'
|
||||
* WChar ===> Object newValue = '菜'
|
||||
* String ===> Object newValue = '你好啊' (特殊)
|
||||
|
||||
|
||||
* 数组变量
|
||||
* 注意:在write的时候,你write的数量 一定要和 plc中存在的数量一一对应
|
||||
* BoolArray ===> boolean[] booleanArray = new boolean[2]; .... 赋予值
|
||||
* ByteArray ===> byte[] write_byteArrays = new byte[2];
|
||||
* WordArray ===> short[] shortArrays_content = new short[2];
|
||||
* DWordArray ===> int[] intArrays_content = new int[2];
|
||||
* CharArray ===> char[] charArrays_content = new char[2];
|
||||
* SIntArray ===> int[] sintArrays_content = new int[2];
|
||||
* IntArray ===> int[] iintArrays_content = new int[2];
|
||||
* DIntArray ===> int[] dintArrays_content = new int[2];
|
||||
* UIntArray ===> int[] uintArrays_content = new int[3];
|
||||
* USIntArray ===> int[] usintArrays_content = new int[3];
|
||||
* UDIntArray ===> int[] udintArrays_content = new int[3];
|
||||
* StringArray ===> String[] stringArrays_content = new String[3];
|
||||
* //如果有其他数据类型 这里没有找我扩展
|
||||
*
|
||||
*
|
||||
* */
|
||||
public void write(PlcVarActual plcVarActual,Object newValue,S7Client s7Client){
|
||||
S7Connector connector = s7Client.getConnector();
|
||||
|
||||
//String 类型比较特殊。 String[] 也是同理,Sring数组里面的子项 也是有两个字节的 readBytes。
|
||||
if(plcVarActual.getType().equals(PlcVar.STRING)){
|
||||
connector.write(
|
||||
plcVarActual.getArea(),
|
||||
plcVarActual.getAreaNumber(),
|
||||
plcVarActual.getByteOffset(),
|
||||
plcVarActual.getBitOffset(),
|
||||
ByteUtils.strToBytes(newValue.toString(), plcVarActual.getStrSize()),
|
||||
plcVarActual.getType()
|
||||
);
|
||||
}else if(plcVarActual.getType().equals(PlcVar.STRING_Array)){
|
||||
//todo here 检查 read write service
|
||||
connector.write(
|
||||
plcVarActual.getArea(),
|
||||
plcVarActual.getAreaNumber(),
|
||||
plcVarActual.getByteOffset(),
|
||||
plcVarActual.getBitOffset(),
|
||||
ByteUtils.strArrayToBytes((String[])newValue, plcVarActual.getStrSize()),
|
||||
plcVarActual.getType()
|
||||
);
|
||||
}else {
|
||||
byte[] bytes = plcVarActual.getType().toBytes(newValue);
|
||||
connector.write(
|
||||
plcVarActual.getArea(),
|
||||
plcVarActual.getAreaNumber(),
|
||||
plcVarActual.getByteOffset(),
|
||||
plcVarActual.getBitOffset(),
|
||||
bytes,
|
||||
plcVarActual.getType()
|
||||
);
|
||||
}
|
||||
}
|
||||
public void write(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset,Integer strSize, PlcVar type, Object newValue, S7Client s7Client){
|
||||
S7Connector connector = s7Client.getConnector();
|
||||
|
||||
//String 类型比较特殊。 String[] 也是同理,Sring数组里面的子项 也是有两个字节的 readBytes。
|
||||
if(type.equals(PlcVar.STRING)){
|
||||
connector.write(
|
||||
area,
|
||||
areaNumber,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
ByteUtils.strToBytes(newValue.toString(), strSize),
|
||||
type
|
||||
);
|
||||
}else if(type.equals(PlcVar.STRING_Array)){
|
||||
//todo here 检查 read write service
|
||||
connector.write(
|
||||
area,
|
||||
areaNumber,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
ByteUtils.strArrayToBytes((String[])newValue, strSize),
|
||||
type
|
||||
);
|
||||
}else {
|
||||
byte[] bytes = type.toBytes(newValue);
|
||||
connector.write(
|
||||
area,
|
||||
areaNumber,
|
||||
byteOffset,
|
||||
bitOffset,
|
||||
bytes,
|
||||
type
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Ladataan…
Viittaa uudesa ongelmassa
Block a user