pom.xml
<dependency>
<groupId>com.github.s7connector</groupId>
<artifactId>s7connector</artifactId>
<version>2.1</version>
</dependency>
maven下载不了的,下载包,评论或者私自内免费给
DB212 类:
String类型的我的占用25个字节,要看实际情况
package com.alnan.entity;
import com.github.s7connector.api.annotation.S7Variable;
import com.github.s7connector.impl.utils.S7Type;
import lombok.Data;
import java.io.Serializable;
@Data
public class DB212 implements Serializable {
@S7Variable(type = S7Type.STRING,byteOffset = 32,bitOffset = 0, size = 25)
public int PW_plateId;
@S7Variable(type = S7Type.WORD,byteOffset = 88,bitOffset = 0)
public int PW_plateLength;
@S7Variable(type = S7Type.WORD,byteOffset = 92,bitOffset = 0)
public int PW_plateWidth;
@S7Variable(type = S7Type.WORD,byteOffset = 208,bitOffset = 0)
public int PW_send;
@S7Variable(type = S7Type.WORD,byteOffset = 212,bitOffset = 0)
public int ACT_PW_1;
@S7Variable(type = S7Type.WORD,byteOffset = 216,bitOffset = 0)
public int ACT_PW_2;
@S7Variable(type = S7Type.WORD,byteOffset = 260,bitOffset = 0)
public int ACT_HY_UP_1;
@S7Variable(type = S7Type.WORD,byteOffset = 264,bitOffset = 0)
public int ACT_HY_DOWN_1;
@S7Variable(type = S7Type.WORD,byteOffset = 276,bitOffset = 0)
public int ACT_HY_UP_2;
@S7Variable(type = S7Type.WORD,byteOffset = 280,bitOffset = 0)
public int ACT_HY_DOWN_2;
@S7Variable(type = S7Type.WORD,byteOffset = 292,bitOffset = 0)
public int ACT_HY_UP_3;
@S7Variable(type = S7Type.WORD,byteOffset = 296,bitOffset = 0)
public int ACT_HY_DOWN_3;
@S7Variable(type = S7Type.WORD,byteOffset = 308,bitOffset = 0)
public int ACT_HY_UP_4;
@S7Variable(type = S7Type.WORD,byteOffset = 312,bitOffset = 0)
public int ACT_HY_DOWN_4;
@S7Variable(type = S7Type.WORD,byteOffset = 324,bitOffset = 0)
public int ACT_HY_UP_5;
@S7Variable(type = S7Type.WORD,byteOffset = 328,bitOffset = 0)
public int ACT_HY_DOWN_5;
@S7Variable(type = S7Type.WORD,byteOffset = 340,bitOffset = 0)
public int ACT_LC_UP_1;
@S7Variable(type = S7Type.WORD,byteOffset = 344,bitOffset = 0)
public int ACT_LC_DOWN_1;
@S7Variable(type = S7Type.WORD,byteOffset = 356,bitOffset = 0)
public int ACT_LC_UP_2;
@S7Variable(type = S7Type.WORD,byteOffset = 360,bitOffset = 0)
public int ACT_LC_DOWN_2;
}
应用:
//一级发数据
S7Connector s7Connector = S7ConnectorFactory.buildTCPConnector()
.withHost("192.168.100.10")//ip
.withPort(102)
.withTimeout(1000) //连接超时时间
.withRack(0)
.withSlot(1)
.build();
s7Connector.write(DaveArea.DB, 212, 32, pzString(plateNo));//212是PLC地址,32是从第多少字节开始写入,
s7Connector.write(DaveArea.DB,212,88, ByteBuffer.allocate(4).putInt(Integer.valueOf(length)).array());
s7Connector.write(DaveArea.DB,212,92, ByteBuffer.allocate(4).putInt(Integer.valueOf(width)).array());
s7Connector.write(DaveArea.DB,212,208, ByteBuffer.allocate(4).putInt(1).array());
System.out.println(s7Serializer.dispense(DB212.class,212,0));
s7Connector.close();
public byte[] pzString(String out_string){
// 写入字符串
//第一个参数:DaveArea.DB 表示读取PLC的地址区域为DB
//第二个参数:DB块地址,若plc中是DB1000,则填1000
//第三个参数:偏移量
//第四个参数:写入的数据 二进制数组byte[] ,总长度为10的话,有效数据只能是10-8,第一位代表总长,第二位,代表有效数据字节
int writeStrLength = 25;//地址块大小
byte[] writeBytes = new byte[writeStrLength];
writeBytes[0] = (byte) writeStrLength;//写入本字符串块总宽度
out_string = out_string.trim();//清除掉两边的空串
int availableEffectCharLength = 0;//有效字符数控制
if (out_string.length() > writeStrLength - 2) {//>writeStrLength-2 截断到最大有效数据位
availableEffectCharLength = writeStrLength - 2;
} else {//<=writeStrLength-2
availableEffectCharLength = out_string.length();
}
writeBytes[1] = (byte) availableEffectCharLength;//写入有效字节数
byte[] strBytes = out_string.getBytes(StandardCharsets.US_ASCII);
for (int i = 0; i < availableEffectCharLength; i++) {
writeBytes[i + 2] = strBytes[i];
}
return writeBytes;
}
基本上能用起来了
具体细节:
https://blog.csdn.net/qq_51383028/article/details/140549849