package com.example.springboot.component;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
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;
|
|
public class S7control {
|
|
S7PLC s7PLC; // PLC通讯类实例
|
private EPlcType plcType = EPlcType.S1500; // 西门子PLC类型
|
private String ip = "192.168.10.1"; // plc ip地址
|
private int port = 102; // plc 端口号
|
|
private static volatile S7control instance = null;
|
|
private S7control() {
|
if (s7PLC == null)
|
s7PLC = new S7PLC(plcType, ip, port,0,0);
|
}
|
|
// 单例模式 获取类的唯一实例
|
public static S7control getinstance() {
|
if (instance == null) {
|
synchronized (S7control.class) {
|
if (instance == null)
|
instance = new S7control();
|
}
|
}
|
return instance;
|
}
|
|
/**
|
* 关闭西门子s7通讯连接
|
*/
|
public void CloseS7client() {
|
if (s7PLC == null)
|
s7PLC.close();
|
}
|
|
/**
|
* 按指定的地址 写入一个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的值
|
*/
|
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("-")) {
|
// 处理范围地址
|
String[] range = addr.split("-");
|
if (range.length == 2) {
|
String startAddr = range[0].trim();
|
String endAddr = range[1].trim();
|
|
int startIndex = Integer.parseInt(startAddr.substring(startAddr.indexOf('.') + 1));
|
int endIndex = Integer.parseInt(endAddr.substring(endAddr.indexOf('.') + 1));
|
|
for (int j = startIndex; j <= endIndex; j++) {
|
String currentAddress = startAddr.substring(0, startAddr.indexOf('.') + 1) + j;
|
s7PLC.writeInt16(currentAddress, data); // 将数据写入当前地址
|
}
|
}
|
} else {
|
// 处理单个地址
|
s7PLC.writeInt16(addr, data); // 将数据写入单个地址
|
}
|
}
|
}
|
|
|
|
|
|
/**
|
* 按指定的地址 写入一个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;
|
return s7PLC.readInt16(address);
|
}
|
|
public List<Short> readWords(List<String> addresses) {
|
if (s7PLC == null) {
|
return null;
|
}
|
|
List<Short> data = new ArrayList<>();
|
|
for (String address : addresses) {
|
if (address.contains("-")) {
|
String[] range = address.split("-");
|
String startAddress = range[0];
|
String endAddress = range[1];
|
|
if (startAddress.equals(endAddress)) {
|
// 单个地址
|
Short value = s7PLC.readInt16(startAddress);
|
data.add(value);
|
} else {
|
// 范围地址
|
int startIndex = getIndexFromAddress(startAddress);
|
int endIndex = getIndexFromAddress(endAddress);
|
|
for (int i = startIndex; i <= endIndex; i++) {
|
String currentAddress = getAddressFromIndex(i);
|
Short value = s7PLC.readInt16(currentAddress);
|
data.add(value);
|
}
|
}
|
} else {
|
// 单个地址
|
Short value = s7PLC.readInt16(address);
|
data.add(value);
|
}
|
}
|
|
return data;
|
}
|
|
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);
|
return s7PLC.readInt16(addresslist);
|
}
|
/**
|
* 按指定的地址 读取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);
|
return s7PLC.readByte(address,count);
|
}
|
|
/**
|
* 按指定的地址 按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);
|
}
|
|
public List<Boolean> readBits(List<String> addressList) {
|
if (s7PLC == null)
|
return null;
|
|
List<Boolean> result = new ArrayList<>();
|
|
for (String address : addressList) {
|
if (address.contains("~")) {
|
String[] range = address.split("~");
|
String startAddress = range[0];
|
String endAddress = range[1];
|
|
int startIndex = extractAddressNumber(startAddress);
|
int endIndex = extractAddressNumber(endAddress);
|
|
String prefix = startAddress.substring(0, startAddress.indexOf(".") + 1);
|
|
for (int i = startIndex; i <= endIndex; i++) {
|
String newAddress = prefix + i;
|
result.add(s7PLC.readBoolean(newAddress));
|
}
|
} else {
|
result.add(s7PLC.readBoolean(address));
|
}
|
}
|
|
return result;
|
}
|
|
|
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);
|
return s7PLC.readBoolean(addresslist);
|
}
|
|
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) {
|
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;
|
}
|
}
|