| | |
| | | * @param datas word的值 |
| | | */ |
| | | public void WriteWord(List<String> address, List<Short> datas) { |
| | | if (s7PLC==null) |
| | | if (s7PLC == null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | |
| | | MultiAddressWrite addressWrite = new MultiAddressWrite(); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | addressWrite.addInt16(address.get(i), datas.get(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); // 将数据写入单个地址 |
| | | } |
| | | } |
| | | s7PLC.writeMultiData(addressWrite); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | |
| | | 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位读取 |
| | | * |