package com.mes.tools;
|
|
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
|
import com.github.xingshuangs.iot.protocol.s7.service.MultiAddressWrite;
|
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
|
import com.google.common.primitives.Bytes;
|
|
import java.nio.charset.StandardCharsets;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class S7control {
|
|
S7PLC s7PLC; // PLC通讯类实例
|
|
public S7control(EPlcType plcType, String ip, int port, int rack, int slot) {
|
if (s7PLC == null) {
|
s7PLC = new S7PLC(plcType, ip, port, 0, 0);
|
}
|
}
|
|
/**
|
* 关闭西门子s7通讯连接
|
*/
|
public void CloseS7client() {
|
if (s7PLC == null) {
|
s7PLC.close();
|
}
|
s7PLC.checkConnected();
|
}
|
|
/**
|
* s7通讯连接状态
|
*/
|
public boolean CheckConnected() {
|
return s7PLC.checkConnected();
|
}
|
|
/**
|
* 按指定的地址 写入一个word
|
*
|
* @param address 地址
|
* @param data word的值
|
*/
|
public void WriteWord(String address, short data) {
|
if (s7PLC == null) {
|
return;
|
}
|
s7PLC.writeInt16(address, data);
|
}
|
|
/**
|
* 从某地址连续 写入多个word
|
*
|
* @param address 地址
|
* @param datas word的值
|
*/
|
public void WriteWord(String address, List<Short> datas) {
|
if (s7PLC == null) {
|
return;
|
}
|
// s7PLC.write(address, data);
|
List<String> addresslist = GetAddressList(address, datas.size(), 16);
|
MultiAddressWrite addressWrite = new MultiAddressWrite();
|
for (int i = 0; i < datas.size(); i++) {
|
addressWrite.addInt16(addresslist.get(i), datas.get(i));
|
}
|
s7PLC.writeMultiData(addressWrite);
|
}
|
|
/**
|
* 按指定的地址 写入多个word
|
*
|
* @param address 地址
|
* @param datas word的值
|
*/
|
|
|
/**
|
* 按指定的地址 写入一个Bit
|
*
|
* @param address 地址
|
* @param data Bit的值
|
*/
|
public void WriteBit(String address, Boolean data) {
|
if (s7PLC == null) {
|
return;
|
}
|
s7PLC.writeBoolean(address, data);
|
}
|
|
/**
|
* 按指定的地址 写入多个bit
|
*
|
* @param address 地址
|
* @param datas bit的值
|
*/
|
public void WriteBit(List<String> address, List<Boolean> datas) {
|
if (s7PLC == null) {
|
return;
|
}
|
// s7PLC.write(address, data);
|
|
MultiAddressWrite addressWrite = new MultiAddressWrite();
|
for (int i = 0; i < address.size(); i++) {
|
addressWrite.addBoolean(address.get(i), datas.get(i));
|
}
|
s7PLC.writeMultiData(addressWrite);
|
}
|
|
/**
|
* 从某地址连续 写入多个bit
|
*
|
* @param address 地址
|
* @param datas word的值
|
*/
|
public void WriteBit(String address, List<Boolean> datas) {
|
if (s7PLC == null) {
|
return;
|
}
|
// s7PLC.write(address, data);
|
List<String> addresslist = GetAddressList(address, datas.size(), 1);
|
MultiAddressWrite addressWrite = new MultiAddressWrite();
|
for (int i = 0; i < datas.size(); i++) {
|
addressWrite.addBoolean(addresslist.get(i), datas.get(i));
|
}
|
s7PLC.writeMultiData(addressWrite);
|
}
|
|
/**
|
* 按指定的地址 写入多个byte
|
*
|
* @param address 地址
|
* @param datas byte的值
|
*/
|
public void WriteByte(String address, byte[] datas) {
|
if (s7PLC == null) {
|
return;
|
}
|
// s7PLC.write(address, data);
|
s7PLC.writeByte(address, datas);
|
}
|
|
/**
|
* 按指定的地址 读取word结果集
|
*
|
* @param address 地址
|
* @return 结果
|
*/
|
public List<Short> ReadWord(List<String> address) {
|
if (s7PLC == null) {
|
return null;
|
}
|
|
try {
|
return s7PLC.readInt16(address);
|
} catch (Exception e) {
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
return null;
|
}
|
}
|
|
|
private int getIndexFromAddress(String address) {
|
|
// 可以解析出地址中的数字部分,并转换为整数
|
return 0;
|
}
|
|
private String getAddressFromIndex(int index) {
|
|
// 整数转换为地址格式的字符串
|
return "";
|
}
|
|
/**
|
* 按指定的地址 读取word结果集
|
*
|
* @param address 地址
|
* @param count 连续读多少个word
|
* @return 结果
|
*/
|
public List<Short> ReadWord(String address, int count) {
|
if (s7PLC == null) {
|
return null;
|
}
|
|
List<String> addresslist = GetAddressList(address, count, 16);
|
try {
|
return s7PLC.readInt16(addresslist);
|
} catch (Exception e) {
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
|
return null;
|
}
|
}
|
|
/**
|
* 按指定的地址 读取byte结果集
|
*
|
* @param address 地址
|
* @param count 连续读多少个byte
|
* @return 结果
|
*/
|
public byte[] ReadByte(String address, int count) {
|
if (s7PLC == null) {
|
return null;
|
}
|
// List<String> addresslist = GetAddressList(address, count, 16);
|
|
try {
|
return s7PLC.readByte(address, count);
|
} catch (Exception e) {
|
// 处理异常
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
return null;
|
}
|
|
}
|
|
/**
|
* 按指定的地址 按bit位 0 flase 1 true 读取结果
|
*
|
* @param addresslist 地址集
|
* @return Boolean结果
|
*/
|
public List<Boolean> ReadBits(List<String> addresslist) {
|
if (s7PLC == null) {
|
return null;
|
}
|
return s7PLC.readBoolean(addresslist);
|
}
|
|
//读取不连续地址bit
|
public List<Boolean> readBits(List<String> addressList) {
|
if (s7PLC == null || addressList.isEmpty()) {
|
return null;
|
}
|
|
List<Boolean> values = new ArrayList<>();
|
for (String address : addressList) {
|
try {
|
boolean value = s7PLC.readBoolean(address);
|
values.add(value);
|
} catch (Exception e) {
|
// 处理异常
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
}
|
}
|
|
return values;
|
}
|
|
|
//读取StringList
|
public List<String> readStrings(List<String> addressList) {
|
if (s7PLC == null) {
|
return null;
|
}
|
List<String> result = new ArrayList<>();
|
for (String address : addressList) {
|
try {
|
byte[] bytes = s7PLC.readByte(address, 14);
|
if (bytes != null) {
|
String str = new String(bytes, StandardCharsets.UTF_8);
|
result.add(str);
|
}
|
} catch (Exception e) {
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
result.add(null);
|
}
|
}
|
|
return result;
|
}
|
//读取字符串
|
public String readString(String address) {
|
if (s7PLC == null) {
|
return null;
|
}
|
try {
|
byte[] bytes = s7PLC.readByte(address, 14);
|
if (bytes != null) {
|
return new String(bytes, StandardCharsets.UTF_8);
|
}
|
} catch (Exception e) {
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
return null;
|
}
|
return null;
|
}
|
|
|
//不连续地址写入Word
|
public void WriteWord(List<String> address, List<Short> datas) {
|
if (s7PLC == null) {
|
return;
|
}
|
|
for (int i = 0; i < address.size(); i++) {
|
String addr = address.get(i);
|
short data = datas.get(i);
|
|
if (addr.contains("-")) {
|
outmesid(String.valueOf(data), addr); // 单独处理带破折号的地址
|
} else {
|
s7PLC.writeInt16(addr, data); // 将数据写入单个地址
|
}
|
}
|
}
|
|
|
//字符串写入
|
public void outmesid(String data, String addr) {
|
// System.out.println("outmesid: " + data);
|
List<Byte> glassidlist = new ArrayList<>();
|
String[] parts = addr.split("-");
|
if (parts.length == 2) {
|
addr = parts[0]; // 只保留 "-" 前面的部分
|
}
|
for (char iditem : data.toCharArray()) {
|
glassidlist.add(Byte.valueOf(String.valueOf(iditem)));
|
}
|
byte[] bytes = Bytes.toArray(glassidlist);
|
WriteByte(addr, bytes);
|
}
|
|
//读取不连续word
|
public List<Short> readWords(List<String> addresses) {
|
if (s7PLC == null) {
|
return null;
|
}
|
List<Short> data = new ArrayList<>();
|
|
for (String address : addresses) {
|
try {
|
// 单个地址
|
Short value = s7PLC.readInt16(address);
|
data.add(value);
|
} catch (Exception e) {
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
|
}
|
|
}
|
return data;
|
}
|
|
//读取时间
|
public Long readtime(String address) {
|
if (s7PLC == null) {
|
return null;
|
}
|
try {
|
return s7PLC.readTime(address);
|
} catch (Exception e) {
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
return null;
|
}
|
}
|
|
|
public void writetime(String address, long datas) {
|
if (s7PLC == null)
|
return;
|
|
|
s7PLC.writeTime(address, datas); // 将数据写入单个地址
|
}
|
|
|
private int extractAddressNumber(String address) {
|
String numberStr = address.replaceAll("\\D+", ""); // 使用正则表达式提取数字部分
|
return Integer.parseInt(numberStr);
|
}
|
|
|
/**
|
* 从指定的地址开始 连续按bit位读取
|
*
|
* @param address 地址
|
* @param count 长度
|
* @return Boolean结果
|
*/
|
public List<Boolean> ReadBits(String address, int count) {
|
if (s7PLC == null)
|
return null;
|
List<String> addresslist = GetAddressList(address, count, 1);
|
try {
|
return s7PLC.readBoolean(addresslist);
|
} catch (Exception e) {
|
System.out.println("读取 " + address + " 失败:" + e.getMessage());
|
return null;
|
}
|
|
}
|
|
;
|
|
|
private List<String> GetAddressList(String address, int count, int addedbit) {
|
List<String> addresslist = new ArrayList<String>();
|
|
String[] stringdatas = address.trim().split("\\.");
|
if (stringdatas.length < 2 || !address.startsWith("DB")) {
|
return null;
|
}
|
int dbwindex = 0;
|
int bitindex = 0;
|
if (stringdatas.length == 2) {
|
dbwindex = Integer.parseInt(stringdatas[1]);
|
} else if (stringdatas.length == 3) {
|
dbwindex = Integer.parseInt(stringdatas[1]);
|
bitindex = Integer.parseInt(stringdatas[2]);
|
} else {
|
return null;
|
}
|
|
addresslist.add(address);
|
for (int i = 0; i < count - 1; i++) {
|
|
int bitcurrent = bitindex + addedbit;
|
if (bitcurrent > 7) {
|
dbwindex += bitcurrent / 8;
|
bitindex = 0;
|
} else {
|
bitindex = bitcurrent;
|
}
|
|
String endstr = stringdatas.length == 3 ? "." + bitindex : "";
|
addresslist.add(stringdatas[0] + "." + dbwindex + endstr);
|
}
|
return addresslist;
|
}
|
}
|