PlcConnectTest.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import com.github.s7connector.api.DaveArea;
  2. import com.github.s7connector.api.S7Connector;
  3. import com.github.s7connector.api.factory.S7ConnectorFactory;
  4. public class PlcConnectTest {
  5. public static void main(String[] args) {
  6. String ip = "192.168.88.99";
  7. int[] slots = {2, 1, 0};
  8. for (int slot : slots) {
  9. System.out.println("==== try rack=0 slot=" + slot + " ====");
  10. S7Connector c = null;
  11. try {
  12. long t0 = System.currentTimeMillis();
  13. c = S7ConnectorFactory.buildTCPConnector()
  14. .withHost(ip).withPort(102).withRack(0).withSlot(slot).withTimeout(8000).build();
  15. System.out.println("build ok in " + (System.currentTimeMillis() - t0) + "ms");
  16. byte[] data = c.read(DaveArea.DB, 9051, 38, 0);
  17. System.out.println("read DB9051 ok len=" + (data == null ? -1 : data.length));
  18. c.close();
  19. System.out.println("SUCCESS slot=" + slot);
  20. return;
  21. } catch (Throwable e) {
  22. Throwable cur = e;
  23. while (cur.getCause() != null && cur.getCause() != cur) {
  24. cur = cur.getCause();
  25. }
  26. System.out.println("FAIL: " + e.getClass().getSimpleName() + "(" + e.getMessage()
  27. + ") <- " + cur.getClass().getSimpleName() + ": " + cur.getMessage());
  28. try {
  29. if (c != null) {
  30. c.close();
  31. }
  32. } catch (Exception ignored) {
  33. }
  34. }
  35. }
  36. System.out.println("==== try OLD default (no rack/slot) ====");
  37. try {
  38. S7Connector c = S7ConnectorFactory.buildTCPConnector().withHost(ip).withTimeout(8000).build();
  39. byte[] data = c.read(DaveArea.DB, 9051, 38, 0);
  40. System.out.println("OLD SUCCESS len=" + data.length);
  41. c.close();
  42. } catch (Throwable e) {
  43. Throwable cur = e;
  44. while (cur.getCause() != null && cur.getCause() != cur) {
  45. cur = cur.getCause();
  46. }
  47. System.out.println("OLD FAIL: " + e.getMessage() + " <- " + cur);
  48. }
  49. }
  50. }