| | |
| | | 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; |
| | | |
| | |
| | | /** |
| | | * 关闭西门子s7通讯连接 |
| | | */ |
| | | public void CloseS7client() { |
| | | if (s7PLC == null) { |
| | | public void closeS7client() { |
| | | if (s7PLC != null) { |
| | | s7PLC.close(); |
| | | } |
| | | s7PLC.checkConnected(); |
| | | } |
| | | |
| | | /** |
| | | * 重启西门子s7通讯连接 |
| | | */ |
| | | public boolean reStartS7client() { |
| | | if (s7PLC != null) { |
| | | try { |
| | | s7PLC.hotRestart(); |
| | | return true; |
| | | } catch (Exception ex) { |
| | | return false; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * s7通讯连接状态 |
| | | */ |
| | | public boolean CheckConnected() { |
| | | public boolean checkConnected() { |
| | | return s7PLC.checkConnected(); |
| | | } |
| | | |
| | |
| | | * @param address 地址 |
| | | * @param data word的值 |
| | | */ |
| | | public void WriteWord(String address, short data) { |
| | | public boolean writeWord(String address, int data) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | s7PLC.writeInt16(address, data); |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | do { |
| | | try { |
| | | s7PLC.writeUInt16(address, data); |
| | | result = true; |
| | | } catch (Exception ex) { |
| | | reStartS7client(); |
| | | } finally { |
| | | tryCount -= 1; |
| | | } |
| | | } |
| | | while (!result && tryCount > 0); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param address 地址 |
| | | * @param datas word的值 |
| | | */ |
| | | public void WriteWord(String address, List<Short> datas) { |
| | | public boolean writeWord(String address, List<Integer> datas) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | // s7PLC.write(address, data); |
| | | List<String> addresslist = GetAddressList(address, datas.size(), 16); |
| | | 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)); |
| | | addressWrite.addUInt16(addresslist.get(i), datas.get(i)); |
| | | } |
| | | s7PLC.writeMultiData(addressWrite); |
| | | do { |
| | | try { |
| | | s7PLC.writeMultiData(addressWrite); |
| | | result = true; |
| | | } catch (Exception ex) { |
| | | reStartS7client(); |
| | | } finally { |
| | | tryCount -= 1; |
| | | } |
| | | } |
| | | while (!result && tryCount > 0); |
| | | return result; |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param address 地址 |
| | | * @param data Bit的值 |
| | | */ |
| | | public void WriteBit(String address, Boolean data) { |
| | | public boolean writeBit(String address, Boolean data) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | s7PLC.writeBoolean(address, data); |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | do { |
| | | try { |
| | | s7PLC.writeBoolean(address, data); |
| | | result = true; |
| | | } catch (Exception ex) { |
| | | reStartS7client(); |
| | | } finally { |
| | | tryCount -= 1; |
| | | } |
| | | } |
| | | while (!result && tryCount > 0); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param address 地址 |
| | | * @param datas bit的值 |
| | | */ |
| | | public void WriteBit(List<String> address, List<Boolean> datas) { |
| | | public boolean writeBit(List<String> address, List<Boolean> datas) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | // 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); |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | do { |
| | | try { |
| | | s7PLC.writeMultiData(addressWrite); |
| | | result = true; |
| | | } catch (Exception ex) { |
| | | reStartS7client(); |
| | | } finally { |
| | | tryCount -= 1; |
| | | } |
| | | } |
| | | while (!result && tryCount > 0); |
| | | return result; |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param address 地址 |
| | | * @param datas word的值 |
| | | */ |
| | | public void WriteBit(String address, List<Boolean> datas) { |
| | | public boolean writeBit(String address, List<Boolean> datas) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | |
| | | // s7PLC.write(address, data); |
| | | List<String> addresslist = GetAddressList(address, datas.size(), 1); |
| | | 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); |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | do { |
| | | try { |
| | | s7PLC.writeMultiData(addressWrite); |
| | | result = true; |
| | | } catch (Exception ex) { |
| | | reStartS7client(); |
| | | } finally { |
| | | tryCount -= 1; |
| | | } |
| | | } |
| | | while (!result && tryCount > 0); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param address 地址 |
| | | * @param datas byte的值 |
| | | */ |
| | | public void WriteByte(String address, byte[] datas) { |
| | | public boolean writeByte(String address, byte[] datas) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | // s7PLC.write(address, data); |
| | | s7PLC.writeByte(address, datas); |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | do { |
| | | try { |
| | | s7PLC.writeByte(address, datas); |
| | | result = true; |
| | | } catch (Exception ex) { |
| | | reStartS7client(); |
| | | } finally { |
| | | tryCount -= 1; |
| | | } |
| | | } |
| | | while (!result && tryCount > 0); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param address 地址 |
| | | * @return 结果 |
| | | */ |
| | | public List<Short> ReadWord(List<String> address) { |
| | | public List<Integer> readWord(List<String> address) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | |
| | | List<Integer> result = null; |
| | | try { |
| | | return s7PLC.readInt16(address); |
| | | result = s7PLC.readUInt16(address); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | return null; |
| | | |
| | | } finally { |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | |
| | | private int getIndexFromAddress(String address) { |
| | | |
| | | // 可以解析出地址中的数字部分,并转换为整数 |
| | | return 0; |
| | | } |
| | | |
| | | private String getAddressFromIndex(int index) { |
| | | |
| | | // 整数转换为地址格式的字符串 |
| | | return ""; |
| | | } |
| | | |
| | | /** |
| | | * 按指定的地址 读取word结果集 |
| | |
| | | * @param count 连续读多少个word |
| | | * @return 结果 |
| | | */ |
| | | public List<Short> ReadWord(String address, int count) { |
| | | public List<Integer> readWord(String address, int count) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | |
| | | List<String> addresslist = GetAddressList(address, count, 16); |
| | | List<Integer> result = null; |
| | | List<String> addresslist = getAddressList(address, count, 16); |
| | | try { |
| | | return s7PLC.readInt16(addresslist); |
| | | result = s7PLC.readUInt16(addresslist); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | |
| | | return null; |
| | | } finally { |
| | | return result; |
| | | } |
| | | } |
| | | |
| | |
| | | * @param count 连续读多少个byte |
| | | * @return 结果 |
| | | */ |
| | | public byte[] ReadByte(String address, int count) { |
| | | public byte[] readByte(String address, int count) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | // List<String> addresslist = GetAddressList(address, count, 16); |
| | | |
| | | byte[] bytes = null; |
| | | try { |
| | | return s7PLC.readByte(address, count); |
| | | bytes = s7PLC.readByte(address, count); |
| | | } catch (Exception e) { |
| | | // 处理异常 |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | return null; |
| | | } finally { |
| | | return bytes; |
| | | } |
| | | |
| | | } |
| | |
| | | * @param addresslist 地址集 |
| | | * @return Boolean结果 |
| | | */ |
| | | public List<Boolean> ReadBits(List<String> addresslist) { |
| | | public List<Boolean> readBits(List<String> addresslist) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | return s7PLC.readBoolean(addresslist); |
| | | List<Boolean> values = new ArrayList<>(); |
| | | try { |
| | | values = s7PLC.readBoolean(addresslist); |
| | | } catch (Exception e) { |
| | | // 处理异常 |
| | | s7PLC.hotRestart(); |
| | | } finally { |
| | | return values; |
| | | } |
| | | } |
| | | |
| | | //读取不连续地址bit |
| | | /* //读取不连续地址bit |
| | | public List<Boolean> readBits(List<String> addressList) { |
| | | if (s7PLC == null || addressList.isEmpty()) { |
| | | return null; |
| | |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | String result = null; |
| | | try { |
| | | return s7PLC.readString(address); |
| | | result = s7PLC.readString(address); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | return null; |
| | | } finally { |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | |
| | | //不连续地址写入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; |
| | | } |
| | | |
| | | //读取单个word |
| | | public Short readWord(String address) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | // 单个地址 |
| | | return s7PLC.readInt16(address); |
| | | } catch (Exception e) { |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | //读取时间 |
| | | public Long readtime(String address) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | Long result = null; |
| | | try { |
| | | return s7PLC.readTime(address); |
| | | result = s7PLC.readTime(address); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | return null; |
| | | } finally { |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | |
| | | 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); |
| | | public boolean writetime(String address, long datas) { |
| | | if (s7PLC == null) { |
| | | return false; |
| | | } |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | do { |
| | | try { |
| | | s7PLC.writeTime(address, datas); // 将数据写入单个地址 |
| | | result = true; |
| | | } catch (Exception ex) { |
| | | reStartS7client(); |
| | | } finally { |
| | | tryCount -= 1; |
| | | } |
| | | } |
| | | while (!result && tryCount > 0); |
| | | return result; |
| | | } |
| | | |
| | | |
| | |
| | | * @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()); |
| | | public List<Boolean> readBits(String address, int count) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | |
| | | List<Boolean> values = new ArrayList<>(); |
| | | List<String> addresslist = getAddressList(address, count, 1); |
| | | try { |
| | | values = s7PLC.readBoolean(addresslist); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | } finally { |
| | | return values; |
| | | } |
| | | } |
| | | |
| | | ; |
| | | |
| | | |
| | | private List<String> GetAddressList(String address, int count, int addedbit) { |
| | | private List<String> getAddressList(String address, int count, int addedbit) { |
| | | List<String> addresslist = new ArrayList<String>(); |
| | | |
| | | String[] stringdatas = address.trim().split("\\."); |