使用 Java 连接 PLC 仿真
整理自《使用 java 连接 PLC 仿真的库》原稿。
1、常用库
| 库 | 说明 |
|---|---|
| s7connector | 常用 S7 连接库之一 |
| iot-communication | 本稿示例所用库 |
| 英文教程 | iot-communication S7 说明 |
| HslCommunication | 另一类工业通信库 |
2、代码示例(iot-communication)
将 IP 换成你的仿真 / 实机地址(原稿示例为 172.16.8.205):
java
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
public class Test {
public static void main(String[] args) {
// long connection mode, persistence = true
S7PLC s7PLC = new S7PLC(EPlcType.S1500, "172.16.8.205");
s7PLC.writeBoolean("DB1.0.0", true);
boolean res = s7PLC.readBoolean("DB1.0.0");
// close it manually, if you want to use it all the time, you do not need to close it
System.out.println(res);
s7PLC.close();
}
}要点:
EPlcType按实际 PLC 型号选择(示例为 S1500)- 地址字符串遵循该库约定(如
DB1.0.0) - 长连接可持续使用;不再用时再
close()
仿真侧先按 PLC 模拟工具使用 起好实例与 IP,再跑本示例。
