完善 。S7 ,不在引用第三方依赖。

This commit is contained in:
caixiang 2022-04-02 14:02:39 +08:00
parent ff431c9e00
commit 3842992695
3 changed files with 39 additions and 9 deletions

View File

@ -74,7 +74,7 @@ public class S7DemoController {
String[] toWrite = new String[60];
for(int i=0;i<60;i++){
int i1 = new Random().nextInt(100);
toWrite[i] = "3011001210530111"+ i1;
toWrite[i] = "2212"+ i1;
}
////测试结果 c => 57ms
long c1 = System.currentTimeMillis();
@ -88,6 +88,29 @@ public class S7DemoController {
return R.ok().put("l",(l1-l)).put("c",(c2-c1));
}
@PostMapping("/testForString1200")
public R testForString1200() throws UnsupportedEncodingException, ParseException {
//测试结果 l => 66ms
long l = System.currentTimeMillis();
String[] subs = (String[])s7Service.read(PlcVarActual.SubIdArrays1200, S7Client.S7_1200);
long l1 = 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();
s7Service.write(PlcVarActual.SubIdArrays1200,toWrite,S7Client.S7_1200);
long c2 = System.currentTimeMillis();
String[] subs2 = (String[])s7Service.read(PlcVarActual.SubIdArrays1200, S7Client.S7_1200);
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);

View File

@ -18,9 +18,9 @@ import java.util.concurrent.ScheduledExecutorService;
public enum S7Client {
//TODO 步骤1 这里是配置多PLC 有多个plc 就在这里配置一个枚举类
//1500 虽然设备上显示机架-0 插槽-1这个是默认的 但是我们这里 rackslot 都是0反正我们这里就让电控rack和solt都是默认的 然后我们这边都配置0就行了
S7_1500("192.168.0.51",0,0,3,PlcVarActual.HeartBeat),
S7_1500("192.168.0.51",0,1,3,PlcVarActual.HeartBeat),
//1500 机架-0 插槽-1
S7_1200("192.168.0.52",0,0,3,PlcVarActual.HeartBeatFor1200)
S7_1200("192.168.0.52",0,1,3,PlcVarActual.HeartBeatFor1200)
//后续 在这里扩展 多PLC应用

View File

@ -67,13 +67,13 @@ public final class TCPConnection extends S7Connection {
(byte) 0xC1, //parameter-codedst-tsap 上位机
(byte) 0x02, //parameter-length
(byte) 0x01, //Source rack 机架
(byte) 0x00, //Source slot 槽位号
(byte) 0x10, //Source TSAP: 0x01->PG; 0x02->OP; 0x03->S7单边通讯(服务端模式); 0x10->S7双边通讯;;; 这里是0x01
(byte) 0x00, //机架(rack)(前4个bit位)+ cpu槽位(slot)(后4个bit位) ,, 因为是上位机端 所以这里都是00(rack=0 slot=2)
(byte) 0xC2, //parameter-codedst-tsap PLC
(byte) 0x02, //parameter-length
(byte) 0x01, //Destination rack 机架 (Des rack 指的就是 plc rack) (msgOut[17])
(byte) 0x02, //Destination slot 槽位号 (Des slot 指的就是 plc slot) (msgOut[18])
(byte) 0x03, //Destination TSAP: 0x01->PG; 0x02->OP; 0x03->S7单边通讯(服务端模式); 0x10->S7双边通讯;;; 这里是0x01
(byte) 0x01, //机架(rack)(前4个bit位)+ cpu槽位(slot)(后4个bit位),, 西门子200smart12001500默认 机架号=0,槽位号=1 ;;;; 西门子300400默认 机架号=0,槽位号=2
(byte) 0xC0, //parameter-codetpdu-size
(byte) 0x01, //parameter-length
@ -84,8 +84,10 @@ public final class TCPConnection extends S7Connection {
//之所以目的位置4 是因为前面4个字节是TPKT的内容(第一次握手)
// msgOut 是这个socketChannel 往外写的 bytes上位机 => plc msgIn 是指这个socketChanne 外部往里写的bytes上位机 <= plc
System.arraycopy(b4, 0, this.msgOut, 4, b4.length);
this.msgOut[17] = (byte) (this.rack + 1);
this.msgOut[18] = (byte) this.slot;
//this.msgOut[17] = (byte) (this.rack + 1); //这里写的不太对这里应该是配置dst-tsap(plc)
//this.msgOut[18] = (byte) this.slot;
this.msgOut[18] = combineToByte(rack,slot);
this.sendISOPacket(b4.length);
this.readISOPacket();
/*
@ -95,6 +97,11 @@ public final class TCPConnection extends S7Connection {
return this.negPDUlengthRequest();
}
private static byte combineToByte(Integer i,Integer j){
Integer cc = ((i << 4)+j);
return cc.byteValue();
}
/**
* 构建S7COMM 的COTP部分 3个字节第二次握手
*