| | |
| | | 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 java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | |
| | | s7PLC.close(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 重启西门子s7通讯连接 |
| | | */ |
| | | public void reStartS7client() { |
| | | public boolean reStartS7client() { |
| | | if (s7PLC != null) { |
| | | try { |
| | | s7PLC.hotRestart(); |
| | | return true; |
| | | } catch (Exception ex) { |
| | | return false; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | |
| | | * @param address 地址 |
| | | * @param data word的值 |
| | | */ |
| | | public void writeWord(String address, int data) { |
| | | public boolean writeWord(String address, int data) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | else if(!s7PLC.checkConnected()) { |
| | | s7PLC.hotRestart(); |
| | | } |
| | | 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<Integer> datas) { |
| | | public boolean writeWord(String address, List<Integer> datas) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | else if(!s7PLC.checkConnected()) { |
| | | s7PLC.hotRestart(); |
| | | } |
| | | boolean result = false; |
| | | int tryCount = 2; |
| | | // 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.addUInt16(addresslist.get(i), datas.get(i)); |
| | | } |
| | | 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; |
| | | } |
| | | else if(!s7PLC.checkConnected()) { |
| | | s7PLC.hotRestart(); |
| | | } |
| | | 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; |
| | | } |
| | | else if(!s7PLC.checkConnected()) { |
| | | s7PLC.hotRestart(); |
| | | } |
| | | // s7PLC.write(address, data); |
| | | |
| | | MultiAddressWrite addressWrite = new MultiAddressWrite(); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | addressWrite.addBoolean(address.get(i), datas.get(i)); |
| | | } |
| | | 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; |
| | | } |
| | | else if(!s7PLC.checkConnected()) { |
| | | s7PLC.hotRestart(); |
| | | } |
| | | |
| | | // 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)); |
| | | } |
| | | 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; |
| | | } |
| | | else if(!s7PLC.checkConnected()) { |
| | | s7PLC.hotRestart(); |
| | | } |
| | | // s7PLC.write(address, data); |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | |
| | | List<Integer> result = null; |
| | | try { |
| | | return s7PLC.readUInt16(address); |
| | | result = s7PLC.readUInt16(address); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | } finally { |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | |
| | | List<Integer> result = null; |
| | | List<String> addresslist = getAddressList(address, count, 16); |
| | | try { |
| | | return s7PLC.readUInt16(addresslist); |
| | | result = s7PLC.readUInt16(addresslist); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | |
| | | return null; |
| | | } finally { |
| | | return result; |
| | | } |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |
| | |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | List<Boolean> values = new ArrayList<>(); |
| | | try { |
| | | return s7PLC.readBoolean(addresslist); |
| | | values = s7PLC.readBoolean(addresslist); |
| | | } catch (Exception e) { |
| | | // 处理异常 |
| | | s7PLC.hotRestart(); |
| | | return null; |
| | | } finally { |
| | | return values; |
| | | } |
| | | |
| | | } |
| | | |
| | | /* //读取不连续地址bit |
| | |
| | | }*/ |
| | | |
| | | |
| | | |
| | | |
| | | //读取字符串 |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | //读取时间 |
| | |
| | | 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) { |
| | | public boolean writetime(String address, long datas) { |
| | | if (s7PLC == null) { |
| | | return; |
| | | return false; |
| | | } |
| | | else if(!s7PLC.checkConnected()) |
| | | { |
| | | s7PLC.hotRestart(); |
| | | } |
| | | |
| | | |
| | | 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; |
| | | } |
| | | |
| | | |
| | |
| | | if (s7PLC == null) { |
| | | return null; |
| | | } |
| | | List<Boolean> values = new ArrayList<>(); |
| | | List<String> addresslist = getAddressList(address, count, 1); |
| | | try { |
| | | return s7PLC.readBoolean(addresslist); |
| | | values = s7PLC.readBoolean(addresslist); |
| | | } catch (Exception e) { |
| | | s7PLC.hotRestart(); |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | return null; |
| | | } finally { |
| | | return values; |
| | | } |
| | | |
| | | } |
| | | |
| | | ; |