Modbus.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * ============================================================================
  3. * GNU General Public License
  4. * ============================================================================
  5. *
  6. * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
  7. * @author Matthew Lohbihler
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.serotonin.cdc.modbus4j;
  22. import com.serotonin.cdc.messaging.DefaultMessagingExceptionHandler;
  23. import com.serotonin.cdc.messaging.MessagingExceptionHandler;
  24. import com.serotonin.cdc.modbus4j.code.RegisterRange;
  25. import com.serotonin.cdc.modbus4j.exception.ModbusTransportException;
  26. /**
  27. * Base level for masters and slaves/listeners
  28. *
  29. * TODO: - handle echoing in RS485
  30. *
  31. * @author mlohbihler
  32. */
  33. public class Modbus {
  34. public static final int DEFAULT_MAX_READ_BIT_COUNT = 2000;
  35. public static final int DEFAULT_MAX_READ_REGISTER_COUNT = 125;
  36. public static final int DEFAULT_MAX_WRITE_REGISTER_COUNT = 120;
  37. private MessagingExceptionHandler exceptionHandler = new DefaultMessagingExceptionHandler();
  38. private int maxReadBitCount = DEFAULT_MAX_READ_BIT_COUNT;
  39. private int maxReadRegisterCount = DEFAULT_MAX_READ_REGISTER_COUNT;
  40. private int maxWriteRegisterCount = DEFAULT_MAX_WRITE_REGISTER_COUNT;
  41. public int getMaxReadCount(int registerRange) {
  42. switch (registerRange) {
  43. case RegisterRange.COIL_STATUS:
  44. case RegisterRange.INPUT_STATUS:
  45. return maxReadBitCount;
  46. case RegisterRange.HOLDING_REGISTER:
  47. case RegisterRange.INPUT_REGISTER:
  48. return maxReadRegisterCount;
  49. }
  50. return -1;
  51. }
  52. public void validateNumberOfBits(int bits) throws ModbusTransportException {
  53. if (bits < 1 || bits > maxReadBitCount)
  54. throw new ModbusTransportException("Invalid number of bits: " + bits);
  55. }
  56. public void validateNumberOfRegisters(int registers) throws ModbusTransportException {
  57. if (registers < 1 || registers > maxReadRegisterCount)
  58. throw new ModbusTransportException("Invalid number of registers: " + registers);
  59. }
  60. public void setExceptionHandler(MessagingExceptionHandler exceptionHandler) {
  61. if (exceptionHandler == null)
  62. this.exceptionHandler = new DefaultMessagingExceptionHandler();
  63. else
  64. this.exceptionHandler = exceptionHandler;
  65. }
  66. public MessagingExceptionHandler getExceptionHandler() {
  67. return exceptionHandler;
  68. }
  69. public int getMaxReadBitCount() {
  70. return maxReadBitCount;
  71. }
  72. public void setMaxReadBitCount(int maxReadBitCount) {
  73. this.maxReadBitCount = maxReadBitCount;
  74. }
  75. public int getMaxReadRegisterCount() {
  76. return maxReadRegisterCount;
  77. }
  78. public void setMaxReadRegisterCount(int maxReadRegisterCount) {
  79. this.maxReadRegisterCount = maxReadRegisterCount;
  80. }
  81. public int getMaxWriteRegisterCount() {
  82. return maxWriteRegisterCount;
  83. }
  84. public void setMaxWriteRegisterCount(int maxWriteRegisterCount) {
  85. this.maxWriteRegisterCount = maxWriteRegisterCount;
  86. }
  87. }