| | |
| | | |
| | | private S7control() { |
| | | if (s7PLC == null) |
| | | s7PLC = new S7PLC(plcType, ip, port,0,0); |
| | | s7PLC = new S7PLC(plcType, ip, port, 0, 0); |
| | | } |
| | | |
| | | // 单例模式 获取类的唯一实例 |
| | |
| | | } |
| | | return instance; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 关闭西门子s7通讯连接 |
| | | */ |
| | | public void CloseS7client() { |
| | | if (s7PLC == null) |
| | | s7PLC.close(); |
| | | s7PLC.checkConnected(); |
| | | s7PLC.checkConnected(); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * 按指定的地址 写入一个word |
| | | * |
| | | * |
| | | * @param address 地址 |
| | | * @param data word的值 |
| | | */ |
| | | public void WriteWord(String address, short data) { |
| | | if (s7PLC==null) |
| | | { |
| | | 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) |
| | | if (s7PLC == null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | List<String> addresslist = GetAddressList(address, datas.size(), 16); |
| | |
| | | |
| | | /** |
| | | * 按指定的地址 写入多个word |
| | | * |
| | | * |
| | | * @param address 地址 |
| | | * @param datas word的值 |
| | | */ |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 按指定的地址 写入一个Bit |
| | | * |
| | | * |
| | | * @param address 地址 |
| | | * @param data Bit的值 |
| | | */ |
| | | public void WriteBit(String address, Boolean data) { |
| | | if (s7PLC==null) |
| | | 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) |
| | | if (s7PLC == null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | |
| | |
| | | |
| | | /** |
| | | * 从某地址连续 写入多个bit |
| | | * |
| | | * |
| | | * @param address 地址 |
| | | * @param datas word的值 |
| | | */ |
| | | public void WriteBit(String address, List<Boolean> datas) { |
| | | if (s7PLC==null) |
| | | if (s7PLC == null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | List<String> addresslist = GetAddressList(address, datas.size(), 1); |
| | |
| | | } |
| | | s7PLC.writeMultiData(addressWrite); |
| | | } |
| | | /** |
| | | |
| | | /** |
| | | * 按指定的地址 写入多个byte |
| | | * |
| | | * |
| | | * @param address 地址 |
| | | * @param datas byte的值 |
| | | */ |
| | | public void WriteByte(String address, byte[] datas) { |
| | | if (s7PLC==null) |
| | | if (s7PLC == null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | s7PLC.writeByte(address, datas); |
| | |
| | | |
| | | /** |
| | | * 按指定的地址 读取word结果集 |
| | | * |
| | | * |
| | | * @param address 地址 |
| | | * @return 结果 |
| | | */ |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | private int getIndexFromAddress(String address) { |
| | | |
| | | // 可以解析出地址中的数字部分,并转换为整数 |
| | |
| | | |
| | | /** |
| | | * 按指定的地址 读取word结果集 |
| | | * |
| | | * |
| | | * @param address 地址 |
| | | * @param count 连续读多少个word |
| | | * @return 结果 |
| | |
| | | |
| | | /** |
| | | * 按指定的地址 读取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); |
| | | if (s7PLC == null) |
| | | return null; |
| | | // List<String> addresslist = GetAddressList(address, count, 16); |
| | | |
| | | try { |
| | | return s7PLC.readByte(address, count); |
| | | }catch (Exception e) { |
| | | } catch (Exception e) { |
| | | // 处理异常 |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | return null; |
| | |
| | | * @return Boolean结果 |
| | | */ |
| | | public List<Boolean> ReadBits(List<String> addresslist) { |
| | | if (s7PLC==null) |
| | | return null; |
| | | 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()); |
| | | //读取不连续地址bit |
| | | public List<Boolean> readBits(List<String> addressList) { |
| | | if (s7PLC == null || addressList.isEmpty()) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | return values; |
| | | } |
| | | |
| | | |
| | | //读取String |
| | | 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); |
| | | 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()); |
| | | } |
| | | } catch (Exception e) { |
| | | System.out.println("读取 " + address + " 失败:" + e.getMessage()); |
| | | result.add(null); |
| | | } |
| | | |
| | | return values; |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | //读取String |
| | | 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 List<String> readStringsandword(List<String> addressList) { |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | //不连续地址写入Word |
| | | //不连续地址写入Word |
| | | public void WriteWord(List<String> address, List<Short> datas) { |
| | | if (s7PLC == null) |
| | | return; |
| | |
| | | short data = datas.get(i); |
| | | |
| | | if (addr.contains("-")) { |
| | | outmesid(String.valueOf(data),addr); // 单独处理带破折号的地址 |
| | | 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<>(); |
| | |
| | | S7control.getinstance().WriteByte(addr, bytes); |
| | | } |
| | | |
| | | //读取不连续word |
| | | //读取不连续word |
| | | public List<Short> readWords(List<String> addresses) { |
| | | if (s7PLC == null) { |
| | | return null; |
| | |
| | | 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 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) { |
| | |
| | | |
| | | s7PLC.writeTime(address, datas); // 将数据写入单个地址 |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | private int extractAddressNumber(String address) { |
| | |
| | | return null; |
| | | } |
| | | |
| | | }; |
| | | |
| | | |
| | | public void writeString(String addr,String data) { |
| | | s7PLC.writeString(addr,data); |
| | | } |
| | | |
| | | |
| | | |
| | | ; |
| | | |
| | | |
| | | public void writeString(String addr, String data) { |
| | | s7PLC.writeString(addr, data); |
| | | } |
| | | |
| | | |
| | | private List<String> GetAddressList(String address, int count, int addedbit) { |
| | | List<String> addresslist = new ArrayList<String>(); |
| | |
| | | } else |
| | | return null; |
| | | |
| | | addresslist.add(address); |
| | | for (int i = 0; i < count-1; i++) { |
| | | addresslist.add(address); |
| | | for (int i = 0; i < count - 1; i++) { |
| | | |
| | | int bitcurrent = bitindex + addedbit; |
| | | if (bitcurrent > 7) { |
| | |
| | | } else |
| | | bitindex = bitcurrent; |
| | | |
| | | String endstr=stringdatas.length==3?"." + bitindex:""; |
| | | String endstr = stringdatas.length == 3 ? "." + bitindex : ""; |
| | | addresslist.add(stringdatas[0] + "." + dbwindex + endstr); |
| | | } |
| | | return addresslist; |