| | |
| | | public class S7control { |
| | | |
| | | S7PLC s7PLC; // PLC通讯类实例 |
| | | private EPlcType plcType = EPlcType.S1200; // 西门子PLC类型 |
| | | private String ip = "127.0.0.1"; // plc ip地址 |
| | | private int port = 21; // plc 端口号 |
| | | private EPlcType plcType = EPlcType.S1500; // 西门子PLC类型 |
| | | private String ip = "192.168.10.1"; // plc ip地址 |
| | | private int port = 102; // plc 端口号 |
| | | |
| | | private static volatile S7control instance = null; |
| | | |
| | | private S7control() { |
| | | if (s7PLC == null) |
| | | s7PLC = new S7PLC(plcType, ip, port); |
| | | 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); |
| | | |
| | |
| | | * @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); |
| | |
| | | * @return 结果 |
| | | */ |
| | | public List<Short> ReadWord(List<String> address) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | return null; |
| | | return s7PLC.readInt16(address); |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | public List<Short> ReadWord(String address, int count) { |
| | | if (!s7PLC.checkConnected()) |
| | | if (s7PLC==null) |
| | | return null; |
| | | |
| | | List<String> addresslist = GetAddressList(address, count, 16); |
| | |
| | | * @return Boolean结果 |
| | | */ |
| | | public List<Boolean> ReadBits(List<String> addresslist) { |
| | | if (!s7PLC.checkConnected()) |
| | | 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; |