ReadSerialTest.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright (C) 2006-2007 Serotonin Software Technologies Inc.
  3. @author Matthew Lohbihler
  4. */
  5. package com.serotonin.modbus4j.test;
  6. import com.serotonin.modbus4j.ModbusFactory;
  7. import com.serotonin.modbus4j.ModbusMaster;
  8. /**
  9. * @author Matthew Lohbihler
  10. */
  11. public class ReadSerialTest {
  12. public static void main(String[] args) throws Exception {
  13. String commPortId = "COM1";
  14. int baudRate = 9600;
  15. int flowControlIn = 0;
  16. int flowControlOut = 0;
  17. int dataBits = 8;
  18. int stopBits = 2;
  19. int parity = 0;
  20. TestSerialPortWrapper wrapper = new TestSerialPortWrapper(commPortId, baudRate, flowControlIn, flowControlOut, dataBits, stopBits, parity);
  21. ModbusMaster master = new ModbusFactory().createRtuMaster(wrapper);
  22. master.setTimeout(200);
  23. master.setRetries(1);
  24. master.init();
  25. for (int i = 1; i < 5; i++) {
  26. long start = System.currentTimeMillis();
  27. System.out.print("Testing " + i + "... ");
  28. System.out.println(master.testSlaveNode(i));
  29. System.out.println("Time: " + (System.currentTimeMillis() - start));
  30. }
  31. // try {
  32. // System.out.println(master.send(new ReadHoldingRegistersRequest(1, 0, 1)));
  33. // }
  34. // catch (Exception e) {
  35. // e.printStackTrace();
  36. // }
  37. // try {
  38. // // ReadCoilsRequest request = new ReadCoilsRequest(2, 65534, 1);
  39. // ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master
  40. // .send(new ReadHoldingRegistersRequest(2, 0, 1));
  41. // System.out.println(response);
  42. // }
  43. // catch (Exception e) {
  44. // e.printStackTrace();
  45. // }
  46. // System.out.println(master.scanForSlaveNodes());
  47. master.destroy();
  48. }
  49. }