271 lines
10 KiB
Java
271 lines
10 KiB
Java
package com.cnbm.s7.controller;
|
|
|
|
import com.cnbm.s7.entity.R;
|
|
import com.cnbm.s7.entity.AGVInfoCallBack;
|
|
|
|
import com.cnbm.s7.s7connector.enmuc.PlcVarActual;
|
|
|
|
|
|
import com.cnbm.s7.s7connector.enmuc.S7Client;
|
|
import com.cnbm.s7.s7connector.type.PlcVar;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.text.ParseException;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
@RestController
|
|
@RequestMapping("/s7")
|
|
public class S7DemoController {
|
|
private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class);
|
|
|
|
// @Autowired
|
|
// InfluxDBClient influxDBClient;
|
|
//
|
|
// @PostMapping("/insert")
|
|
// public void insert() throws InterruptedException {
|
|
// Event event = new Event();
|
|
// event.setTime(Instant.now());
|
|
// event.setTransationId("asasd11");
|
|
// event.setArgName("argName11");
|
|
// event.setArgValue(7d);
|
|
// Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEventSSS");
|
|
// influxDBClient.makeWriteApi().writePoint(asProcessCompleteEvent);
|
|
// }
|
|
|
|
@PostMapping(value = "testFor")
|
|
private void testFor(@RequestBody AGVInfoCallBack agvInfoCallBack) {
|
|
|
|
System.out.println(agvInfoCallBack.toString());
|
|
}
|
|
|
|
//demo1
|
|
@PostMapping("/testReadAll")
|
|
public R testReadAll() throws UnsupportedEncodingException, ParseException {
|
|
for(PlcVarActual actual:PlcVarActual.values()){
|
|
System.out.println(read(S7Client.S7_1500,actual));
|
|
}
|
|
return R.ok();
|
|
}
|
|
// public Point insert(Event event, String measurement){
|
|
// Point point = Point.measurement(measurement)
|
|
// .addTag("transationId", event.getTransationId())
|
|
// .addTag("argName", event.getArgName())
|
|
// .addField("argValue", event.getArgValue())
|
|
// .time(event.getTime().toEpochMilli(), WritePrecision.MS);
|
|
// return point;
|
|
// }
|
|
//demo2
|
|
@PostMapping("/readTest")
|
|
public R getTestForS7() throws UnsupportedEncodingException, ParseException {
|
|
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>)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 Exception {
|
|
// List<Character> characters = (List<Character>)read(PlcVarActual.CharArrays,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";
|
|
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);
|
|
}
|
|
|
|
@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")
|
|
public R testForStrings() throws Exception {
|
|
//测试结果 l => 66ms
|
|
long l = System.currentTimeMillis();
|
|
String[] subs = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays);
|
|
long l1 = System.currentTimeMillis();
|
|
|
|
long dl = System.currentTimeMillis();
|
|
String string = (String)read(S7Client.S7_1500,PlcVarActual.STRING1);
|
|
long dl1 = System.currentTimeMillis();
|
|
|
|
String[] toWrite = new String[60];
|
|
for(int i=0;i<60;i++){
|
|
int i1 = new Random().nextInt(100);
|
|
toWrite[i] = "2212"+ i1;
|
|
}
|
|
////测试结果 c => 57ms
|
|
long c1 = System.currentTimeMillis();
|
|
write(S7Client.S7_1500,PlcVarActual.SubIdArrays,toWrite);
|
|
long c2 = System.currentTimeMillis();
|
|
String s = "cai xiang";
|
|
write(S7Client.S7_1500,PlcVarActual.STRING1,s);
|
|
|
|
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)).put("dl",(dl1-dl));
|
|
}
|
|
|
|
@PostMapping("/testForString1200")
|
|
public R testForString1200() throws Exception {
|
|
//测试结果 l => 66ms
|
|
long l = System.currentTimeMillis();
|
|
String[] subs = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays1200); //65ms
|
|
long l1 = System.currentTimeMillis();
|
|
//System.out.println(Arrays.toString(subs));
|
|
|
|
String[] toWrite = new String[63];
|
|
for(int i=0;i<63;i++){
|
|
//int i1 = new Random().nextInt(100);
|
|
toWrite[i] = "abcd-"+ i;
|
|
}
|
|
////测试结果 c => 57ms
|
|
long c1 = System.currentTimeMillis();
|
|
write(S7Client.S7_1500,PlcVarActual.SubIdArrays1200,toWrite);
|
|
long c2 = System.currentTimeMillis();
|
|
|
|
String[] subs2 = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays1200);
|
|
logger.info("正常测试: l:"+(l1-l)+"c:"+(c2-c1)+";;read1:"+Arrays.toString(subs)+";;read2:"+Arrays.toString(subs2));
|
|
|
|
return R.ok().put("l",(l1-l)).put("c",(c2-c1));
|
|
}
|
|
|
|
@PostMapping("/testForString1500")
|
|
public R testForString1500() throws Exception {
|
|
//测试结果 l => 66ms
|
|
long l = System.currentTimeMillis();
|
|
String[] subs = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays); //25ms
|
|
long l1 = System.currentTimeMillis();
|
|
System.out.println(Arrays.toString(subs));
|
|
String[] toWrite = new String[63];
|
|
for(int i=0;i<63;i++){
|
|
//int i1 = new Random().nextInt(100);
|
|
toWrite[i] = "abcd-"+ i;
|
|
}
|
|
////测试结果 c => 57ms
|
|
long c1 = System.currentTimeMillis();
|
|
write(S7Client.S7_1500,PlcVarActual.SubIdArrays,toWrite);
|
|
long c2 = System.currentTimeMillis();
|
|
|
|
|
|
String[] subs2 = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays);
|
|
|
|
return R.ok().put("l",(l1-l)).put("c",(c2-c1));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/testFor1200")
|
|
public R testFor1200() throws Exception {
|
|
//Object subs = read(PlcVarActual.INT1200, S7Client.S7_1500);
|
|
Object read = read(S7Client.S7_1500, 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_1500,PlcVarActual.SubIdArrays1200,toWrite);
|
|
|
|
Object read2 = read(S7Client.S7_1500, PlcVarActual.SubIdArrays1200);
|
|
return R.ok();
|
|
}
|
|
|
|
|
|
|
|
//PlcVarActual 到时候改成你们的 xxxPlcToWcs 或者 xxxWcsToPlc
|
|
/**
|
|
* return
|
|
* 成功: 返回相应的object对象
|
|
* 失败: 返回null
|
|
* */
|
|
private Object read(S7Client s7Client,PlcVarActual var) throws UnsupportedEncodingException, ParseException {
|
|
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;
|
|
}
|
|
}
|
|
private void write(S7Client s7Client,PlcVarActual var,Object newValue) throws Exception {
|
|
if(var.getType().equals(PlcVar.STRING_Array)){
|
|
String[] s = (String[])newValue;
|
|
String[] ss = (String[])newValue;
|
|
if(s.length > 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);
|
|
}
|
|
}
|
|
|
|
// //demo3
|
|
// @PostMapping("/writeTest")
|
|
// public R writeTest() throws PlcConnectionException, UnsupportedEncodingException {
|
|
// write(S7Client.S7_1500,PlcVarActual.HeartBeat, false);
|
|
//
|
|
// char[] charArrays_content = new char[2];
|
|
// charArrays_content[0] = '1';
|
|
// charArrays_content[1] = 'c';
|
|
// write( S7Client.S7_1500,PlcVarActual.CharArrays, charArrays_content);
|
|
//
|
|
// boolean[] boolArrays_content = new boolean[2];
|
|
// boolArrays_content[0] = true;
|
|
// boolArrays_content[1] = false;
|
|
// write(S7Client.S7_1500,PlcVarActual.BooleanArrays, boolArrays_content);
|
|
//
|
|
// String str = "你好啊aa";
|
|
// //todo string 的读写有问题 待会看看
|
|
// write(S7Client.S7_1500,PlcVarActual.STRING1, str);
|
|
// return R.ok().put("res",true);
|
|
// }
|
|
|
|
}
|