| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import com.github.s7connector.api.DaveArea;
- import com.github.s7connector.api.S7Connector;
- import com.github.s7connector.api.factory.S7ConnectorFactory;
- public class PlcConnectTest {
- public static void main(String[] args) {
- String ip = "192.168.88.99";
- int[] slots = {2, 1, 0};
- for (int slot : slots) {
- System.out.println("==== try rack=0 slot=" + slot + " ====");
- S7Connector c = null;
- try {
- long t0 = System.currentTimeMillis();
- c = S7ConnectorFactory.buildTCPConnector()
- .withHost(ip).withPort(102).withRack(0).withSlot(slot).withTimeout(8000).build();
- System.out.println("build ok in " + (System.currentTimeMillis() - t0) + "ms");
- byte[] data = c.read(DaveArea.DB, 9051, 38, 0);
- System.out.println("read DB9051 ok len=" + (data == null ? -1 : data.length));
- c.close();
- System.out.println("SUCCESS slot=" + slot);
- return;
- } catch (Throwable e) {
- Throwable cur = e;
- while (cur.getCause() != null && cur.getCause() != cur) {
- cur = cur.getCause();
- }
- System.out.println("FAIL: " + e.getClass().getSimpleName() + "(" + e.getMessage()
- + ") <- " + cur.getClass().getSimpleName() + ": " + cur.getMessage());
- try {
- if (c != null) {
- c.close();
- }
- } catch (Exception ignored) {
- }
- }
- }
- System.out.println("==== try OLD default (no rack/slot) ====");
- try {
- S7Connector c = S7ConnectorFactory.buildTCPConnector().withHost(ip).withTimeout(8000).build();
- byte[] data = c.read(DaveArea.DB, 9051, 38, 0);
- System.out.println("OLD SUCCESS len=" + data.length);
- c.close();
- } catch (Throwable e) {
- Throwable cur = e;
- while (cur.getCause() != null && cur.getCause() != cur) {
- cur = cur.getCause();
- }
- System.out.println("OLD FAIL: " + e.getMessage() + " <- " + cur);
- }
- }
- }
|