| | |
| | | |
| | | private S7control() { |
| | | if (s7PLC == null) |
| | | s7PLC = new S7PLC(plcType, ip, port); |
| | | s7PLC.connect(); |
| | | System.out.println(ip); |
| | | s7PLC = new S7PLC(plcType, ip, port,0,0); |
| | | } |
| | | |
| | | // 单例模式 获取类的唯一实例 |
| | |
| | | * @param data word的值 |
| | | */ |
| | | public void WriteWord(String address, short data) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | { |
| | | return; |
| | | } |
| | | s7PLC.writeInt16(address, data); |
| | | } |
| | | |
| | |
| | | * @param datas word的值 |
| | | */ |
| | | public void WriteWord(String address, List<Short> datas) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | List<String> addresslist = GetAddressList(address, datas.size(), 16); |
| | |
| | | * @param datas word的值 |
| | | */ |
| | | public void WriteWord(List<String> address, List<Short> datas) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | |
| | |
| | | s7PLC.writeMultiData(addressWrite); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 按指定的地址 写入一个Bit |
| | | * |
| | |
| | | * @param data Bit的值 |
| | | */ |
| | | public void WriteBit(String address, Boolean data) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | return; |
| | | s7PLC.writeBoolean(address, data); |
| | | } |
| | |
| | | * @param datas bit的值 |
| | | */ |
| | | public void WriteBit(List<String> address, List<Boolean> datas) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | |
| | |
| | | * @param datas word的值 |
| | | */ |
| | | public void WriteBit(String address, List<Boolean> datas) { |
| | | if (!s7PLC.checkConnected()) |
| | | 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) |
| | | return; |
| | | // s7PLC.write(address, data); |
| | | s7PLC.writeByte(address, datas); |
| | | } |
| | | |
| | | /** |
| | | * 按指定的地址 读取word结果集 |
| | |
| | | * @return 结果 |
| | | */ |
| | | public List<Short> ReadWord(List<String> address) { |
| | | if (!s7PLC.checkConnected()) |
| | | return null; |
| | | |
| | | 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 ""; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 结果 |
| | | */ |
| | | public List<Short> ReadWord(String address, int count) { |
| | | if (!s7PLC.checkConnected()) |
| | | return null; |
| | | if (s7PLC==null) |
| | | return null; |
| | | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return Boolean结果 |
| | | */ |
| | | public List<Boolean> ReadBits(List<String> addresslist) { |
| | | if (!s7PLC.checkConnected()) |
| | | return null; |
| | | if (s7PLC==null) |
| | | return null; |
| | | return s7PLC.readBoolean(addresslist); |
| | | } |
| | | |
| | |
| | | * @return Boolean结果 |
| | | */ |
| | | public List<Boolean> ReadBits(String address, int count) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | return null; |
| | | List<String> addresslist = GetAddressList(address, count, 1); |
| | | return s7PLC.readBoolean(addresslist); |
| | | } |
| | | |
| | | private List<String> GetAddressList(String address, int count, int addedbit) { |
| | | List<String> addresslist = new ArrayList<>(); |
| | | List<String> addresslist = new ArrayList<String>(); |
| | | |
| | | String[] stringdatas = address.split("."); |
| | | String[] stringdatas = address.trim().split("\\."); |
| | | if (stringdatas.length < 2 || !address.startsWith("DB")) |
| | | return null; |
| | | int dbwindex = 0; |
| | |
| | | } else |
| | | return null; |
| | | |
| | | for (int i = 0; i < count; i++) { |
| | | addresslist.add(address); |
| | | for (int i = 0; i < count-1; i++) { |
| | | |
| | | int bitcurrent = bitindex + addedbit; |
| | | if (bitcurrent > 7) { |
| | |
| | | bitindex = 0; |
| | | } else |
| | | bitindex = bitcurrent; |
| | | addresslist.add(stringdatas[0] + "." + dbwindex + "." + bitindex); |
| | | |
| | | String endstr=stringdatas.length==3?"." + bitindex:""; |
| | | addresslist.add(stringdatas[0] + "." + dbwindex + endstr); |
| | | } |
| | | return addresslist; |
| | | } |