| | |
| | | * @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); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 按指定的地址 写入一个Bit |
| | |
| | | } |
| | | 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结果集 |
| | |
| | | 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 ""; |
| | | } |
| | | |
| | | /** |
| | |
| | | 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 读取结果 |
| | |
| | | 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位读取 |
| | | * |