ExceptionResult class.
27 | * 28 | * @author Matthew Lohbihler 29 | * @version 5.0.0 30 | */ 31 | public class ExceptionResult { 32 | private final byte exceptionCode; 33 | private final String exceptionMessage; 34 | 35 | /** 36 | *Constructor for ExceptionResult.
37 | * 38 | * @param exceptionCode a byte. 39 | */ 40 | public ExceptionResult(byte exceptionCode) { 41 | this.exceptionCode = exceptionCode; 42 | exceptionMessage = ExceptionCode.getExceptionMessage(exceptionCode); 43 | } 44 | 45 | /** 46 | *Getter for the field exceptionCode
.
Getter for the field exceptionMessage
.
NodeScanListener interface.
27 | * 28 | * @author Matthew Lohbihler 29 | * @version 5.0.0 30 | */ 31 | public interface NodeScanListener extends ProgressiveTaskListener { 32 | /** 33 | *nodeFound.
34 | * 35 | * @param nodeNumber a int. 36 | */ 37 | void nodeFound(int nodeNumber); 38 | } 39 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ProcessImageListener.java: -------------------------------------------------------------------------------- 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, seeProcessImageListener interface.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public interface ProcessImageListener { 30 | /** 31 | *coilWrite.
32 | * 33 | * @param offset a int. 34 | * @param oldValue a boolean. 35 | * @param newValue a boolean. 36 | */ 37 | public void coilWrite(int offset, boolean oldValue, boolean newValue); 38 | 39 | /** 40 | *holdingRegisterWrite.
41 | * 42 | * @param offset a int. 43 | * @param oldValue a short. 44 | * @param newValue a short. 45 | */ 46 | public void holdingRegisterWrite(int offset, short oldValue, short newValue); 47 | } 48 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/base/BaseMessageParser.java: -------------------------------------------------------------------------------- 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, seeAbstract BaseMessageParser class.
29 | * 30 | * @author Matthew Lohbihler 31 | * @version 5.0.0 32 | */ 33 | abstract public class BaseMessageParser implements MessageParser { 34 | protected final boolean master; 35 | 36 | /** 37 | *Constructor for BaseMessageParser.
38 | * 39 | * @param master a boolean. 40 | */ 41 | public BaseMessageParser(boolean master) { 42 | this.master = master; 43 | } 44 | 45 | /** {@inheritDoc} */ 46 | @Override 47 | public IncomingMessage parseMessage(ByteQueue queue) throws Exception { 48 | try { 49 | return parseMessageImpl(queue); 50 | } 51 | catch (ArrayIndexOutOfBoundsException e) { 52 | // Means that we ran out of data trying to read the message. Just return null. 53 | return null; 54 | } 55 | } 56 | 57 | /** 58 | *parseMessageImpl.
59 | * 60 | * @param queue a {@link com.serotonin.modbus4j.sero.util.queue.ByteQueue} object. 61 | * @return a {@link com.serotonin.modbus4j.sero.messaging.IncomingMessage} object. 62 | * @throws java.lang.Exception if any. 63 | */ 64 | abstract protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception; 65 | } 66 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/base/RangeAndOffset.java: -------------------------------------------------------------------------------- 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, seeRangeAndOffset class.
27 | * 28 | * @author Matthew Lohbihler 29 | * @version 5.0.0 30 | */ 31 | public class RangeAndOffset { 32 | private int range; 33 | private int offset; 34 | 35 | /** 36 | *Constructor for RangeAndOffset.
37 | * 38 | * @param range a int. 39 | * @param offset a int. 40 | */ 41 | public RangeAndOffset(int range, int offset) { 42 | this.range = range; 43 | this.offset = offset; 44 | } 45 | 46 | /** 47 | * This constructor provides a best guess at the function and offset the user wants, with the assumption that the 48 | * offset will never go over 9999. 49 | * 50 | * @param registerId a int. 51 | */ 52 | public RangeAndOffset(int registerId) { 53 | if (registerId < 10000) { 54 | this.range = RegisterRange.COIL_STATUS; 55 | this.offset = registerId - 1; 56 | } 57 | else if (registerId < 20000) { 58 | this.range = RegisterRange.INPUT_STATUS; 59 | this.offset = registerId - 10001; 60 | } 61 | else if (registerId < 40000) { 62 | this.range = RegisterRange.INPUT_REGISTER; 63 | this.offset = registerId - 30001; 64 | } 65 | else { 66 | this.range = RegisterRange.HOLDING_REGISTER; 67 | this.offset = registerId - 40001; 68 | } 69 | } 70 | 71 | /** 72 | *Getter for the field range
.
Getter for the field offset
.
SlaveAndRange class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class SlaveAndRange { 30 | private final int slaveId; 31 | private final int range; 32 | 33 | /** 34 | *Constructor for SlaveAndRange.
35 | * 36 | * @param slaveId a int. 37 | * @param range a int. 38 | */ 39 | public SlaveAndRange(int slaveId, int range) { 40 | ModbusUtils.validateSlaveId(slaveId, true); 41 | 42 | this.slaveId = slaveId; 43 | this.range = range; 44 | } 45 | 46 | /** 47 | *Getter for the field range
.
Getter for the field slaveId
.
Setter for the field writeMaskRegister
.
Getter for the field writeMaskRegister
.
FunctionCode class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class FunctionCode { 30 | /** ConstantREAD_COILS=1
*/
31 | public static final byte READ_COILS = 1;
32 | /** Constant READ_DISCRETE_INPUTS=2
*/
33 | public static final byte READ_DISCRETE_INPUTS = 2;
34 | /** Constant READ_HOLDING_REGISTERS=3
*/
35 | public static final byte READ_HOLDING_REGISTERS = 3;
36 | /** Constant READ_INPUT_REGISTERS=4
*/
37 | public static final byte READ_INPUT_REGISTERS = 4;
38 | /** Constant WRITE_COIL=5
*/
39 | public static final byte WRITE_COIL = 5;
40 | /** Constant WRITE_REGISTER=6
*/
41 | public static final byte WRITE_REGISTER = 6;
42 | /** Constant READ_EXCEPTION_STATUS=7
*/
43 | public static final byte READ_EXCEPTION_STATUS = 7;
44 | /** Constant WRITE_COILS=15
*/
45 | public static final byte WRITE_COILS = 15;
46 | /** Constant WRITE_REGISTERS=16
*/
47 | public static final byte WRITE_REGISTERS = 16;
48 | /** Constant REPORT_SLAVE_ID=17
*/
49 | public static final byte REPORT_SLAVE_ID = 17;
50 | /** Constant WRITE_MASK_REGISTER=22
*/
51 | public static final byte WRITE_MASK_REGISTER = 22;
52 |
53 | /**
54 | * toString.
55 | * 56 | * @param code a byte. 57 | * @return a {@link java.lang.String} object. 58 | */ 59 | public static String toString(byte code) { 60 | return Integer.toString(code & 0xff); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/exception/ErrorResponseException.java: -------------------------------------------------------------------------------- 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, seeErrorResponseException class.
28 | * 29 | * @author Matthew Lohbihler 30 | * @version 5.0.0 31 | */ 32 | public class ErrorResponseException extends Exception { 33 | private static final long serialVersionUID = -1; 34 | 35 | private final ModbusRequest originalRequest; 36 | private final ModbusResponse errorResponse; 37 | 38 | /** 39 | *Constructor for ErrorResponseException.
40 | * 41 | * @param originalRequest a {@link com.serotonin.modbus4j.msg.ModbusRequest} object. 42 | * @param errorResponse a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 43 | */ 44 | public ErrorResponseException(ModbusRequest originalRequest, ModbusResponse errorResponse) { 45 | this.originalRequest = originalRequest; 46 | this.errorResponse = errorResponse; 47 | } 48 | 49 | /** 50 | *Getter for the field errorResponse
.
Getter for the field originalRequest
.
IllegalDataAddressException class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class IllegalDataAddressException extends ModbusTransportException { 30 | private static final long serialVersionUID = -1; 31 | 32 | /** 33 | *Constructor for IllegalDataAddressException.
34 | */ 35 | public IllegalDataAddressException() { 36 | super(); 37 | } 38 | 39 | /** 40 | *Constructor for IllegalDataAddressException.
41 | * 42 | * @param slaveId a int. 43 | */ 44 | public IllegalDataAddressException(int slaveId) { 45 | super(slaveId); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/exception/IllegalDataTypeException.java: -------------------------------------------------------------------------------- 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, seeIllegalDataTypeException class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class IllegalDataTypeException extends ModbusIdException { 30 | private static final long serialVersionUID = -1; 31 | 32 | /** 33 | *Constructor for IllegalDataTypeException.
34 | * 35 | * @param message a {@link java.lang.String} object. 36 | */ 37 | public IllegalDataTypeException(String message) { 38 | super(message); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/exception/IllegalFunctionException.java: -------------------------------------------------------------------------------- 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, seeIllegalFunctionException class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class IllegalFunctionException extends ModbusTransportException { 30 | private static final long serialVersionUID = -1; 31 | 32 | private final byte functionCode; 33 | 34 | /** 35 | *Constructor for IllegalFunctionException.
36 | * 37 | * @param functionCode a byte. 38 | * @param slaveId a int. 39 | */ 40 | public IllegalFunctionException(byte functionCode, int slaveId) { 41 | super("Function code: 0x" + Integer.toHexString(functionCode & 0xff), slaveId); 42 | this.functionCode = functionCode; 43 | } 44 | 45 | /** 46 | *Getter for the field functionCode
.
IllegalSlaveIdException class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class IllegalSlaveIdException extends ModbusIdException { 30 | private static final long serialVersionUID = -1; 31 | 32 | /** 33 | *Constructor for IllegalSlaveIdException.
34 | * 35 | * @param message a {@link java.lang.String} object. 36 | */ 37 | public IllegalSlaveIdException(String message) { 38 | super(message); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/exception/InvalidDataConversionException.java: -------------------------------------------------------------------------------- 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, seeInvalidDataConversionException class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class InvalidDataConversionException extends RuntimeException { 30 | private static final long serialVersionUID = -1; 31 | 32 | /** 33 | *Constructor for InvalidDataConversionException.
34 | * 35 | * @param message a {@link java.lang.String} object. 36 | */ 37 | public InvalidDataConversionException(String message) { 38 | super(message); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/exception/ModbusIdException.java: -------------------------------------------------------------------------------- 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, seeModbusIdException class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class ModbusIdException extends RuntimeException { 30 | private static final long serialVersionUID = -1; 31 | 32 | /** 33 | *Constructor for ModbusIdException.
34 | * 35 | * @param message a {@link java.lang.String} object. 36 | */ 37 | public ModbusIdException(String message) { 38 | super(message); 39 | } 40 | 41 | /** 42 | *Constructor for ModbusIdException.
43 | * 44 | * @param cause a {@link java.lang.Throwable} object. 45 | */ 46 | public ModbusIdException(Throwable cause) { 47 | super(cause); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/exception/ModbusInitException.java: -------------------------------------------------------------------------------- 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, seeModbusInitException class.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public class ModbusInitException extends Exception { 30 | private static final long serialVersionUID = -1; 31 | 32 | /** 33 | *Constructor for ModbusInitException.
34 | */ 35 | public ModbusInitException() { 36 | super(); 37 | } 38 | 39 | /** 40 | *Constructor for ModbusInitException.
41 | * 42 | * @param message a {@link java.lang.String} object. 43 | * @param cause a {@link java.lang.Throwable} object. 44 | */ 45 | public ModbusInitException(String message, Throwable cause) { 46 | super(message, cause); 47 | } 48 | 49 | /** 50 | *Constructor for ModbusInitException.
51 | * 52 | * @param message a {@link java.lang.String} object. 53 | */ 54 | public ModbusInitException(String message) { 55 | super(message); 56 | } 57 | 58 | /** 59 | *Constructor for ModbusInitException.
60 | * 61 | * @param cause a {@link java.lang.Throwable} object. 62 | */ 63 | public ModbusInitException(Throwable cause) { 64 | super(cause); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/exception/SlaveIdNotEqual.java: -------------------------------------------------------------------------------- 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, seeAbstract IpMessage class.
27 | * 28 | * @author Matthew Lohbihler 29 | * @version 5.0.0 30 | */ 31 | abstract public class IpMessage { 32 | protected final ModbusMessage modbusMessage; 33 | 34 | /** 35 | *Constructor for IpMessage.
36 | * 37 | * @param modbusMessage a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 38 | */ 39 | public IpMessage(ModbusMessage modbusMessage) { 40 | this.modbusMessage = modbusMessage; 41 | } 42 | 43 | /** 44 | *Getter for the field modbusMessage
.
IpMessageResponse interface.
29 | * 30 | * @author Matthew Lohbihler 31 | * @version 5.0.0 32 | */ 33 | public interface IpMessageResponse extends OutgoingResponseMessage, IncomingResponseMessage { 34 | /** 35 | *getModbusResponse.
36 | * 37 | * @return a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 38 | */ 39 | ModbusResponse getModbusResponse(); 40 | } 41 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/encap/EncapMessage.java: -------------------------------------------------------------------------------- 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, seeEncapMessage class.
30 | * 31 | * @author Matthew Lohbihler 32 | * @version 5.0.0 33 | */ 34 | public class EncapMessage extends IpMessage { 35 | /** 36 | *Constructor for EncapMessage.
37 | * 38 | * @param modbusMessage a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 39 | */ 40 | public EncapMessage(ModbusMessage modbusMessage) { 41 | super(modbusMessage); 42 | } 43 | 44 | /** 45 | *getMessageData.
46 | * 47 | * @return an array of {@link byte} objects. 48 | */ 49 | public byte[] getMessageData() { 50 | ByteQueue msgQueue = new ByteQueue(); 51 | 52 | // Write the particular message. 53 | modbusMessage.write(msgQueue); 54 | 55 | // Write the CRC 56 | ModbusUtils.pushShort(msgQueue, ModbusUtils.calculateCRC(modbusMessage)); 57 | 58 | // Return the data. 59 | return msgQueue.popAll(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/encap/EncapMessageParser.java: -------------------------------------------------------------------------------- 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, seeEncapMessageParser class.
29 | * 30 | * @author Matthew Lohbihler 31 | * @version 5.0.0 32 | */ 33 | public class EncapMessageParser extends BaseMessageParser { 34 | /** 35 | *Constructor for EncapMessageParser.
36 | * 37 | * @param master a boolean. 38 | */ 39 | public EncapMessageParser(boolean master) { 40 | super(master); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 46 | if (master) 47 | return EncapMessageResponse.createEncapMessageResponse(queue); 48 | return EncapMessageRequest.createEncapMessageRequest(queue); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/encap/EncapMessageRequest.java: -------------------------------------------------------------------------------- 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, seeEncapMessageRequest class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class EncapMessageRequest extends EncapMessage implements OutgoingRequestMessage, IncomingRequestMessage { 37 | static EncapMessageRequest createEncapMessageRequest(ByteQueue queue) throws ModbusTransportException { 38 | // Create the modbus response. 39 | ModbusRequest request = ModbusRequest.createModbusRequest(queue); 40 | EncapMessageRequest encapRequest = new EncapMessageRequest(request); 41 | 42 | // Check the CRC 43 | ModbusUtils.checkCRC(encapRequest.modbusMessage, queue); 44 | 45 | return encapRequest; 46 | } 47 | 48 | /** 49 | *Constructor for EncapMessageRequest.
50 | * 51 | * @param modbusRequest a {@link com.serotonin.modbus4j.msg.ModbusRequest} object. 52 | */ 53 | public EncapMessageRequest(ModbusRequest modbusRequest) { 54 | super(modbusRequest); 55 | } 56 | 57 | /** {@inheritDoc} */ 58 | @Override 59 | public boolean expectsResponse() { 60 | return modbusMessage.getSlaveId() != 0; 61 | } 62 | 63 | /** 64 | *getModbusRequest.
65 | * 66 | * @return a {@link com.serotonin.modbus4j.msg.ModbusRequest} object. 67 | */ 68 | public ModbusRequest getModbusRequest() { 69 | return (ModbusRequest) modbusMessage; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/encap/EncapMessageResponse.java: -------------------------------------------------------------------------------- 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, seeEncapMessageResponse class.
31 | * 32 | * @author Matthew Lohbihler 33 | * @version 5.0.0 34 | */ 35 | public class EncapMessageResponse extends EncapMessage implements IpMessageResponse { 36 | static EncapMessageResponse createEncapMessageResponse(ByteQueue queue) throws ModbusTransportException { 37 | // Create the modbus response. 38 | ModbusResponse response = ModbusResponse.createModbusResponse(queue); 39 | EncapMessageResponse encapResponse = new EncapMessageResponse(response); 40 | 41 | // Check the CRC 42 | ModbusUtils.checkCRC(encapResponse.modbusMessage, queue); 43 | 44 | return encapResponse; 45 | } 46 | 47 | /** 48 | *Constructor for EncapMessageResponse.
49 | * 50 | * @param modbusResponse a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 51 | */ 52 | public EncapMessageResponse(ModbusResponse modbusResponse) { 53 | super(modbusResponse); 54 | } 55 | 56 | /** 57 | *getModbusResponse.
58 | * 59 | * @return a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 60 | */ 61 | public ModbusResponse getModbusResponse() { 62 | return (ModbusResponse) modbusMessage; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/encap/EncapRequestHandler.java: -------------------------------------------------------------------------------- 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, seeEncapRequestHandler class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class EncapRequestHandler extends BaseRequestHandler { 37 | /** 38 | *Constructor for EncapRequestHandler.
39 | * 40 | * @param slave a {@link com.serotonin.modbus4j.ModbusSlaveSet} object. 41 | */ 42 | public EncapRequestHandler(ModbusSlaveSet slave) { 43 | super(slave); 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 48 | EncapMessageRequest tcpRequest = (EncapMessageRequest) req; 49 | ModbusRequest request = tcpRequest.getModbusRequest(); 50 | ModbusResponse response = handleRequestImpl(request); 51 | if (response == null) 52 | return null; 53 | return new EncapMessageResponse(response); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/encap/EncapWaitingRoomKeyFactory.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.ip.encap; 2 | 3 | import com.serotonin.modbus4j.ip.IpMessage; 4 | import com.serotonin.modbus4j.msg.ModbusMessage; 5 | import com.serotonin.modbus4j.sero.messaging.IncomingResponseMessage; 6 | import com.serotonin.modbus4j.sero.messaging.OutgoingRequestMessage; 7 | import com.serotonin.modbus4j.sero.messaging.WaitingRoomKey; 8 | import com.serotonin.modbus4j.sero.messaging.WaitingRoomKeyFactory; 9 | 10 | /** 11 | *EncapWaitingRoomKeyFactory class.
12 | * 13 | * @author Matthew Lohbihler 14 | * @version 5.0.0 15 | */ 16 | public class EncapWaitingRoomKeyFactory implements WaitingRoomKeyFactory { 17 | /** {@inheritDoc} */ 18 | @Override 19 | public WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request) { 20 | return createWaitingRoomKey(((IpMessage) request).getModbusMessage()); 21 | } 22 | 23 | /** {@inheritDoc} */ 24 | @Override 25 | public WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response) { 26 | return createWaitingRoomKey(((IpMessage) response).getModbusMessage()); 27 | } 28 | 29 | /** 30 | *createWaitingRoomKey.
31 | * 32 | * @param msg a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 33 | * @return a {@link com.serotonin.modbus4j.sero.messaging.WaitingRoomKey} object. 34 | */ 35 | public WaitingRoomKey createWaitingRoomKey(ModbusMessage msg) { 36 | return new EncapWaitingRoomKey(msg.getSlaveId(), msg.getFunctionCode()); 37 | } 38 | 39 | class EncapWaitingRoomKey implements WaitingRoomKey { 40 | private final int slaveId; 41 | private final byte functionCode; 42 | 43 | public EncapWaitingRoomKey(int slaveId, byte functionCode) { 44 | this.slaveId = slaveId; 45 | this.functionCode = functionCode; 46 | } 47 | 48 | @Override 49 | public int hashCode() { 50 | final int prime = 31; 51 | int result = 1; 52 | result = prime * result + functionCode; 53 | result = prime * result + slaveId; 54 | return result; 55 | } 56 | 57 | @Override 58 | public boolean equals(Object obj) { 59 | if (this == obj) 60 | return true; 61 | if (obj == null) 62 | return false; 63 | if (getClass() != obj.getClass()) 64 | return false; 65 | EncapWaitingRoomKey other = (EncapWaitingRoomKey) obj; 66 | if (functionCode != other.functionCode) 67 | return false; 68 | if (slaveId != other.slaveId) 69 | return false; 70 | return true; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/xa/XaMessageParser.java: -------------------------------------------------------------------------------- 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, seeXaMessageParser class.
29 | * 30 | * @author Matthew Lohbihler 31 | * @version 5.0.0 32 | */ 33 | public class XaMessageParser extends BaseMessageParser { 34 | /** 35 | *Constructor for XaMessageParser.
36 | * 37 | * @param master a boolean. 38 | */ 39 | public XaMessageParser(boolean master) { 40 | super(master); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 46 | if (master) 47 | return XaMessageResponse.createXaMessageResponse(queue); 48 | return XaMessageRequest.createXaMessageRequest(queue); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/xa/XaMessageResponse.java: -------------------------------------------------------------------------------- 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, seeXaMessageResponse class.
31 | * 32 | * @author Matthew Lohbihler 33 | * @version 5.0.0 34 | */ 35 | public class XaMessageResponse extends XaMessage implements IpMessageResponse { 36 | static XaMessageResponse createXaMessageResponse(ByteQueue queue) throws ModbusTransportException { 37 | // Remove the XA header 38 | int transactionId = ModbusUtils.popShort(queue); 39 | int protocolId = ModbusUtils.popShort(queue); 40 | if (protocolId != ModbusUtils.IP_PROTOCOL_ID) 41 | throw new ModbusTransportException("Unsupported IP protocol id: " + protocolId); 42 | ModbusUtils.popShort(queue); // Length, which we don't care about. 43 | 44 | // Create the modbus response. 45 | ModbusResponse response = ModbusResponse.createModbusResponse(queue); 46 | return new XaMessageResponse(response, transactionId); 47 | } 48 | 49 | /** 50 | *Constructor for XaMessageResponse.
51 | * 52 | * @param modbusResponse a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 53 | * @param transactionId a int. 54 | */ 55 | public XaMessageResponse(ModbusResponse modbusResponse, int transactionId) { 56 | super(modbusResponse, transactionId); 57 | } 58 | 59 | /** 60 | *getModbusResponse.
61 | * 62 | * @return a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 63 | */ 64 | public ModbusResponse getModbusResponse() { 65 | return (ModbusResponse) modbusMessage; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/xa/XaRequestHandler.java: -------------------------------------------------------------------------------- 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, seeXaRequestHandler class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class XaRequestHandler extends BaseRequestHandler { 37 | /** 38 | *Constructor for XaRequestHandler.
39 | * 40 | * @param slave a {@link com.serotonin.modbus4j.ModbusSlaveSet} object. 41 | */ 42 | public XaRequestHandler(ModbusSlaveSet slave) { 43 | super(slave); 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 48 | XaMessageRequest tcpRequest = (XaMessageRequest) req; 49 | ModbusRequest request = tcpRequest.getModbusRequest(); 50 | ModbusResponse response = handleRequestImpl(request); 51 | if (response == null) 52 | return null; 53 | return new XaMessageResponse(response, tcpRequest.transactionId); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/ip/xa/XaWaitingRoomKeyFactory.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.ip.xa; 2 | 3 | import com.serotonin.modbus4j.msg.ModbusMessage; 4 | import com.serotonin.modbus4j.sero.messaging.IncomingResponseMessage; 5 | import com.serotonin.modbus4j.sero.messaging.OutgoingRequestMessage; 6 | import com.serotonin.modbus4j.sero.messaging.WaitingRoomKey; 7 | import com.serotonin.modbus4j.sero.messaging.WaitingRoomKeyFactory; 8 | 9 | /** 10 | *XaWaitingRoomKeyFactory class.
11 | * 12 | * @author Matthew Lohbihler 13 | * @version 5.0.0 14 | */ 15 | public class XaWaitingRoomKeyFactory implements WaitingRoomKeyFactory { 16 | /** {@inheritDoc} */ 17 | @Override 18 | public WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request) { 19 | return createWaitingRoomKey((XaMessage) request); 20 | } 21 | 22 | /** {@inheritDoc} */ 23 | @Override 24 | public WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response) { 25 | return createWaitingRoomKey((XaMessage) response); 26 | } 27 | 28 | /** 29 | *createWaitingRoomKey.
30 | * 31 | * @param msg a {@link com.serotonin.modbus4j.ip.xa.XaMessage} object. 32 | * @return a {@link com.serotonin.modbus4j.sero.messaging.WaitingRoomKey} object. 33 | */ 34 | public WaitingRoomKey createWaitingRoomKey(XaMessage msg) { 35 | return new XaWaitingRoomKey(msg.getTransactionId(), msg.getModbusMessage()); 36 | } 37 | 38 | class XaWaitingRoomKey implements WaitingRoomKey { 39 | private final int transactionId; 40 | private final int slaveId; 41 | private final byte functionCode; 42 | 43 | public XaWaitingRoomKey(int transactionId, ModbusMessage msg) { 44 | this.transactionId = transactionId; 45 | this.slaveId = msg.getSlaveId(); 46 | this.functionCode = msg.getFunctionCode(); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | final int prime = 31; 52 | int result = 1; 53 | result = prime * result + functionCode; 54 | result = prime * result + slaveId; 55 | result = prime * result + transactionId; 56 | return result; 57 | } 58 | 59 | @Override 60 | public boolean equals(Object obj) { 61 | if (this == obj) 62 | return true; 63 | if (obj == null) 64 | return false; 65 | if (getClass() != obj.getClass()) 66 | return false; 67 | XaWaitingRoomKey other = (XaWaitingRoomKey) obj; 68 | if (functionCode != other.functionCode) 69 | return false; 70 | if (slaveId != other.slaveId) 71 | return false; 72 | if (transactionId != other.transactionId) 73 | return false; 74 | return true; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ExceptionResponse.java: -------------------------------------------------------------------------------- 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, seeExceptionResponse class.
28 | * 29 | * @author Matthew Lohbihler 30 | * @version 5.0.0 31 | */ 32 | public class ExceptionResponse extends ModbusResponse { 33 | private final byte functionCode; 34 | 35 | /** 36 | *Constructor for ExceptionResponse.
37 | * 38 | * @param slaveId a int. 39 | * @param functionCode a byte. 40 | * @param exceptionCode a byte. 41 | * @throws com.serotonin.modbus4j.exception.ModbusTransportException if any. 42 | */ 43 | public ExceptionResponse(int slaveId, byte functionCode, byte exceptionCode) throws ModbusTransportException { 44 | super(slaveId); 45 | this.functionCode = functionCode; 46 | setException(exceptionCode); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public byte getFunctionCode() { 52 | return functionCode; 53 | } 54 | 55 | /** {@inheritDoc} */ 56 | @Override 57 | protected void readResponse(ByteQueue queue) { 58 | // no op 59 | } 60 | 61 | /** {@inheritDoc} */ 62 | @Override 63 | protected void writeResponse(ByteQueue queue) { 64 | // no op 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReadCoilsResponse.java: -------------------------------------------------------------------------------- 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, seeReadCoilsResponse class.
28 | * 29 | * @author Matthew Lohbihler 30 | * @version 5.0.0 31 | */ 32 | public class ReadCoilsResponse extends ReadResponse { 33 | ReadCoilsResponse(int slaveId, byte[] data) throws ModbusTransportException { 34 | super(slaveId, data); 35 | } 36 | 37 | ReadCoilsResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public byte getFunctionCode() { 44 | return FunctionCode.READ_COILS; 45 | } 46 | 47 | /** {@inheritDoc} */ 48 | @Override 49 | public String toString() { 50 | return "ReadCoilsResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId + ", getFunctionCode()=" 51 | + getFunctionCode() + ", isException()=" + isException() + ", getExceptionMessage()=" 52 | + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode() + ", toString()=" 53 | + super.toString(false) + "]"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReadDiscreteInputsRequest.java: -------------------------------------------------------------------------------- 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, seeReadDiscreteInputsRequest class.
29 | * 30 | * @author Matthew Lohbihler 31 | * @version 5.0.0 32 | */ 33 | public class ReadDiscreteInputsRequest extends ReadBinaryRequest { 34 | /** 35 | *Constructor for ReadDiscreteInputsRequest.
36 | * 37 | * @param slaveId a int. 38 | * @param startOffset a int. 39 | * @param numberOfBits a int. 40 | * @throws com.serotonin.modbus4j.exception.ModbusTransportException if any. 41 | */ 42 | public ReadDiscreteInputsRequest(int slaveId, int startOffset, int numberOfBits) throws ModbusTransportException { 43 | super(slaveId, startOffset, numberOfBits); 44 | } 45 | 46 | ReadDiscreteInputsRequest(int slaveId) throws ModbusTransportException { 47 | super(slaveId); 48 | } 49 | 50 | /** {@inheritDoc} */ 51 | @Override 52 | public byte getFunctionCode() { 53 | return FunctionCode.READ_DISCRETE_INPUTS; 54 | } 55 | 56 | @Override 57 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 58 | return new ReadDiscreteInputsResponse(slaveId, getData(processImage)); 59 | } 60 | 61 | /** {@inheritDoc} */ 62 | @Override 63 | protected boolean getBinary(ProcessImage processImage, int index) throws ModbusTransportException { 64 | return processImage.getInput(index); 65 | } 66 | 67 | @Override 68 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 69 | return new ReadDiscreteInputsResponse(slaveId); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReadDiscreteInputsResponse.java: -------------------------------------------------------------------------------- 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, seeReadDiscreteInputsResponse class.
28 | * 29 | * @author Matthew Lohbihler 30 | * @version 5.0.0 31 | */ 32 | public class ReadDiscreteInputsResponse extends ReadResponse { 33 | ReadDiscreteInputsResponse(int slaveId, byte[] data) throws ModbusTransportException { 34 | super(slaveId, data); 35 | } 36 | 37 | ReadDiscreteInputsResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public byte getFunctionCode() { 44 | return FunctionCode.READ_DISCRETE_INPUTS; 45 | } 46 | 47 | /** {@inheritDoc} */ 48 | @Override 49 | public String toString() { 50 | return "ReadDiscreteInputsResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId 51 | + ", getFunctionCode()=" + getFunctionCode() + ", isException()=" + isException() 52 | + ", getExceptionMessage()=" + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode() 53 | + super.toString(false) + "]"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReadExceptionStatusRequest.java: -------------------------------------------------------------------------------- 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, seeReadExceptionStatusRequest class.
31 | * 32 | * @author Matthew Lohbihler 33 | * @version 5.0.0 34 | */ 35 | public class ReadExceptionStatusRequest extends ModbusRequest { 36 | /** 37 | *Constructor for ReadExceptionStatusRequest.
38 | * 39 | * @param slaveId a int. 40 | * @throws com.serotonin.modbus4j.exception.ModbusTransportException if any. 41 | */ 42 | public ReadExceptionStatusRequest(int slaveId) throws ModbusTransportException { 43 | super(slaveId); 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | @Override 48 | public void validate(Modbus modbus) { 49 | // no op 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | protected void writeRequest(ByteQueue queue) { 55 | // no op 56 | } 57 | 58 | /** {@inheritDoc} */ 59 | @Override 60 | protected void readRequest(ByteQueue queue) { 61 | // no op 62 | } 63 | 64 | @Override 65 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 66 | return new ReadExceptionStatusResponse(slaveId); 67 | } 68 | 69 | @Override 70 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 71 | return new ReadExceptionStatusResponse(slaveId, processImage.getExceptionStatus()); 72 | } 73 | 74 | /** {@inheritDoc} */ 75 | @Override 76 | public byte getFunctionCode() { 77 | return FunctionCode.READ_EXCEPTION_STATUS; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReadExceptionStatusResponse.java: -------------------------------------------------------------------------------- 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, seeReadExceptionStatusResponse class.
29 | * 30 | * @author Matthew Lohbihler 31 | * @version 5.0.0 32 | */ 33 | public class ReadExceptionStatusResponse extends ModbusResponse { 34 | private byte exceptionStatus; 35 | 36 | ReadExceptionStatusResponse(int slaveId) throws ModbusTransportException { 37 | super(slaveId); 38 | } 39 | 40 | ReadExceptionStatusResponse(int slaveId, byte exceptionStatus) throws ModbusTransportException { 41 | super(slaveId); 42 | this.exceptionStatus = exceptionStatus; 43 | } 44 | 45 | /** {@inheritDoc} */ 46 | @Override 47 | public byte getFunctionCode() { 48 | return FunctionCode.READ_EXCEPTION_STATUS; 49 | } 50 | 51 | /** {@inheritDoc} */ 52 | @Override 53 | protected void readResponse(ByteQueue queue) { 54 | exceptionStatus = queue.pop(); 55 | } 56 | 57 | /** {@inheritDoc} */ 58 | @Override 59 | protected void writeResponse(ByteQueue queue) { 60 | queue.push(exceptionStatus); 61 | } 62 | 63 | /** 64 | *Getter for the field exceptionStatus
.
ReadHoldingRegistersResponse class.
28 | * 29 | * @author Matthew Lohbihler 30 | * @version 5.0.0 31 | */ 32 | public class ReadHoldingRegistersResponse extends ReadResponse { 33 | ReadHoldingRegistersResponse(int slaveId, byte[] data) throws ModbusTransportException { 34 | super(slaveId, data); 35 | } 36 | 37 | ReadHoldingRegistersResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public byte getFunctionCode() { 44 | return FunctionCode.READ_HOLDING_REGISTERS; 45 | } 46 | 47 | /** {@inheritDoc} */ 48 | @Override 49 | public String toString() { 50 | return "ReadHoldingRegistersResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId 51 | + ", getFunctionCode()=" + getFunctionCode() + ", isException()=" + isException() 52 | + ", getExceptionMessage()=" + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode() 53 | + ", toString()=" + super.toString(true) + "]"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReadInputRegistersResponse.java: -------------------------------------------------------------------------------- 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, seeReadInputRegistersResponse class.
28 | * 29 | * @author Matthew Lohbihler 30 | * @version 5.0.0 31 | */ 32 | public class ReadInputRegistersResponse extends ReadResponse { 33 | ReadInputRegistersResponse(int slaveId, byte[] data) throws ModbusTransportException { 34 | super(slaveId, data); 35 | } 36 | 37 | ReadInputRegistersResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public byte getFunctionCode() { 44 | return FunctionCode.READ_INPUT_REGISTERS; 45 | } 46 | 47 | /** {@inheritDoc} */ 48 | @Override 49 | public String toString() { 50 | return "ReadInputRegistersResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId 51 | + ", getFunctionCode()=" + getFunctionCode() + ", isException()=" + isException() 52 | + ", getExceptionMessage()=" + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode() 53 | + ", toString()=" + super.toString(true) + "]"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReportSlaveIdRequest.java: -------------------------------------------------------------------------------- 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, seeReportSlaveIdRequest class.
31 | * 32 | * @author Matthew Lohbihler 33 | * @version 5.0.0 34 | */ 35 | public class ReportSlaveIdRequest extends ModbusRequest { 36 | /** 37 | *Constructor for ReportSlaveIdRequest.
38 | * 39 | * @param slaveId a int. 40 | * @throws com.serotonin.modbus4j.exception.ModbusTransportException if any. 41 | */ 42 | public ReportSlaveIdRequest(int slaveId) throws ModbusTransportException { 43 | super(slaveId); 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | @Override 48 | public void validate(Modbus modbus) { 49 | // no op 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | protected void writeRequest(ByteQueue queue) { 55 | // no op 56 | } 57 | 58 | /** {@inheritDoc} */ 59 | @Override 60 | protected void readRequest(ByteQueue queue) { 61 | // no op 62 | } 63 | 64 | @Override 65 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 66 | return new ReportSlaveIdResponse(slaveId); 67 | } 68 | 69 | @Override 70 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 71 | return new ReportSlaveIdResponse(slaveId, processImage.getReportSlaveIdData()); 72 | } 73 | 74 | /** {@inheritDoc} */ 75 | @Override 76 | public byte getFunctionCode() { 77 | return FunctionCode.REPORT_SLAVE_ID; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/msg/ReportSlaveIdResponse.java: -------------------------------------------------------------------------------- 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, seeReportSlaveIdResponse class.
30 | * 31 | * @author Matthew Lohbihler 32 | * @version 5.0.0 33 | */ 34 | public class ReportSlaveIdResponse extends ModbusResponse { 35 | private byte[] data; 36 | 37 | ReportSlaveIdResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | ReportSlaveIdResponse(int slaveId, byte[] data) throws ModbusTransportException { 42 | super(slaveId); 43 | this.data = data; 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | @Override 48 | public byte getFunctionCode() { 49 | return FunctionCode.REPORT_SLAVE_ID; 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | protected void readResponse(ByteQueue queue) { 55 | int numberOfBytes = ModbusUtils.popUnsignedByte(queue); 56 | if (queue.size() < numberOfBytes) 57 | throw new ArrayIndexOutOfBoundsException(); 58 | 59 | data = new byte[numberOfBytes]; 60 | queue.pop(data); 61 | } 62 | 63 | /** {@inheritDoc} */ 64 | @Override 65 | protected void writeResponse(ByteQueue queue) { 66 | ModbusUtils.pushByte(queue, data.length); 67 | queue.push(data); 68 | } 69 | 70 | /** 71 | *Getter for the field data
.
WriteCoilResponse class.
30 | * 31 | * @author Matthew Lohbihler 32 | * @version 5.0.0 33 | */ 34 | public class WriteCoilResponse extends ModbusResponse { 35 | private int writeOffset; 36 | private boolean writeValue; 37 | 38 | /** {@inheritDoc} */ 39 | @Override 40 | public byte getFunctionCode() { 41 | return FunctionCode.WRITE_COIL; 42 | } 43 | 44 | WriteCoilResponse(int slaveId) throws ModbusTransportException { 45 | super(slaveId); 46 | } 47 | 48 | WriteCoilResponse(int slaveId, int writeOffset, boolean writeValue) throws ModbusTransportException { 49 | super(slaveId); 50 | this.writeOffset = writeOffset; 51 | this.writeValue = writeValue; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override 56 | protected void writeResponse(ByteQueue queue) { 57 | ModbusUtils.pushShort(queue, writeOffset); 58 | ModbusUtils.pushShort(queue, writeValue ? 0xff00 : 0); 59 | } 60 | 61 | /** {@inheritDoc} */ 62 | @Override 63 | protected void readResponse(ByteQueue queue) { 64 | writeOffset = ModbusUtils.popUnsignedShort(queue); 65 | writeValue = ModbusUtils.popUnsignedShort(queue) == 0xff00; 66 | } 67 | 68 | /** 69 | *Getter for the field writeOffset
.
isWriteValue.
79 | * 80 | * @return a boolean. 81 | */ 82 | public boolean isWriteValue() { 83 | return writeValue; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/SerialMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.serial; 2 | 3 | import com.serotonin.modbus4j.msg.ModbusMessage; 4 | 5 | /** 6 | *Abstract SerialMessage class.
7 | * 8 | * @author Matthew Lohbihler 9 | * @version 5.0.0 10 | */ 11 | abstract public class SerialMessage { 12 | protected final ModbusMessage modbusMessage; 13 | 14 | /** 15 | *Constructor for SerialMessage.
16 | * 17 | * @param modbusMessage a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 18 | */ 19 | public SerialMessage(ModbusMessage modbusMessage) { 20 | this.modbusMessage = modbusMessage; 21 | } 22 | 23 | /** 24 | *Getter for the field modbusMessage
.
open.
27 | * 28 | * @throws java.lang.Exception if any. 29 | */ 30 | void open() throws Exception; 31 | 32 | /** 33 | * 34 | * Return the input stream for an open port 35 | * 36 | * @return a {@link java.io.InputStream} object. 37 | */ 38 | InputStream getInputStream(); 39 | 40 | /** 41 | * Return the output stream for an open port 42 | * 43 | * @return a {@link java.io.OutputStream} object. 44 | */ 45 | OutputStream getOutputStream(); 46 | 47 | /** 48 | *getBaudRate.
49 | * 50 | * @return a int. 51 | */ 52 | int getBaudRate(); 53 | 54 | /** 55 | *getDataBits.
56 | * 57 | * @return a int. 58 | */ 59 | int getDataBits(); 60 | 61 | /** 62 | *getStopBits.
63 | * 64 | * @return a int. 65 | */ 66 | int getStopBits(); 67 | 68 | /** 69 | *getParity.
70 | * 71 | * @return a int. 72 | */ 73 | int getParity(); 74 | 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/SerialSlave.java: -------------------------------------------------------------------------------- 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, seeAbstract SerialSlave class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | abstract public class SerialSlave extends ModbusSlaveSet { 37 | 38 | private final Log LOG = LogFactory.getLog(SerialSlave.class); 39 | 40 | // Runtime fields 41 | private SerialPortWrapper wrapper; 42 | protected StreamTransport transport; 43 | 44 | /** 45 | *Constructor for SerialSlave.
46 | * 47 | * @param wrapper a {@link com.serotonin.modbus4j.serial.SerialPortWrapper} object. 48 | */ 49 | public SerialSlave(SerialPortWrapper wrapper) { 50 | this.wrapper = wrapper; 51 | } 52 | 53 | /** {@inheritDoc} */ 54 | @Override 55 | public void start() throws ModbusInitException { 56 | try { 57 | 58 | wrapper.open(); 59 | 60 | transport = new StreamTransport(wrapper.getInputStream(), wrapper.getOutputStream()); 61 | } 62 | catch (Exception e) { 63 | throw new ModbusInitException(e); 64 | } 65 | } 66 | 67 | /** {@inheritDoc} */ 68 | @Override 69 | public void stop() { 70 | try { 71 | wrapper.close(); 72 | } catch (Exception e) { 73 | LOG.error(e.getMessage(),e); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/SerialWaitingRoomKeyFactory.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.serial; 2 | 3 | import com.serotonin.modbus4j.sero.messaging.IncomingResponseMessage; 4 | import com.serotonin.modbus4j.sero.messaging.OutgoingRequestMessage; 5 | import com.serotonin.modbus4j.sero.messaging.WaitingRoomKey; 6 | import com.serotonin.modbus4j.sero.messaging.WaitingRoomKeyFactory; 7 | 8 | /** 9 | *SerialWaitingRoomKeyFactory class.
10 | * 11 | * @author Matthew Lohbihler 12 | * @version 5.0.0 13 | */ 14 | public class SerialWaitingRoomKeyFactory implements WaitingRoomKeyFactory { 15 | private static final Sync sync = new Sync(); 16 | 17 | /** {@inheritDoc} */ 18 | @Override 19 | public WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request) { 20 | return sync; 21 | } 22 | 23 | /** {@inheritDoc} */ 24 | @Override 25 | public WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response) { 26 | return sync; 27 | } 28 | 29 | static class Sync implements WaitingRoomKey { 30 | @Override 31 | public int hashCode() { 32 | return 1; 33 | } 34 | 35 | @Override 36 | public boolean equals(Object obj) { 37 | if (this == obj) 38 | return true; 39 | if (obj == null) 40 | return false; 41 | if (getClass() != obj.getClass()) 42 | return false; 43 | return true; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/ascii/AsciiMessageParser.java: -------------------------------------------------------------------------------- 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, seeAsciiMessageParser class.
29 | * 30 | * @author Matthew Lohbihler 31 | * @version 5.0.0 32 | */ 33 | public class AsciiMessageParser extends BaseMessageParser { 34 | /** 35 | *Constructor for AsciiMessageParser.
36 | * 37 | * @param master a boolean. 38 | */ 39 | public AsciiMessageParser(boolean master) { 40 | super(master); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 46 | if (master) 47 | return AsciiMessageResponse.createAsciiMessageResponse(queue); 48 | return AsciiMessageRequest.createAsciiMessageRequest(queue); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/ascii/AsciiMessageRequest.java: -------------------------------------------------------------------------------- 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, seeAsciiMessageRequest class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class AsciiMessageRequest extends AsciiMessage implements OutgoingRequestMessage, IncomingRequestMessage { 37 | static AsciiMessageRequest createAsciiMessageRequest(ByteQueue queue) throws ModbusTransportException { 38 | ByteQueue msgQueue = getUnasciiMessage(queue); 39 | ModbusRequest request = ModbusRequest.createModbusRequest(msgQueue); 40 | AsciiMessageRequest asciiRequest = new AsciiMessageRequest(request); 41 | 42 | // Return the data. 43 | return asciiRequest; 44 | } 45 | 46 | /** 47 | *Constructor for AsciiMessageRequest.
48 | * 49 | * @param modbusMessage a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 50 | */ 51 | public AsciiMessageRequest(ModbusMessage modbusMessage) { 52 | super(modbusMessage); 53 | } 54 | 55 | /** {@inheritDoc} */ 56 | @Override 57 | public boolean expectsResponse() { 58 | return modbusMessage.getSlaveId() != 0; 59 | } 60 | 61 | /** 62 | *getModbusRequest.
63 | * 64 | * @return a {@link com.serotonin.modbus4j.msg.ModbusRequest} object. 65 | */ 66 | public ModbusRequest getModbusRequest() { 67 | return (ModbusRequest) modbusMessage; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/ascii/AsciiMessageResponse.java: -------------------------------------------------------------------------------- 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, seeAsciiMessageResponse class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class AsciiMessageResponse extends AsciiMessage implements OutgoingResponseMessage, IncomingResponseMessage { 37 | static AsciiMessageResponse createAsciiMessageResponse(ByteQueue queue) throws ModbusTransportException { 38 | ByteQueue msgQueue = getUnasciiMessage(queue); 39 | ModbusResponse response = ModbusResponse.createModbusResponse(msgQueue); 40 | AsciiMessageResponse asciiResponse = new AsciiMessageResponse(response); 41 | 42 | // Return the data. 43 | return asciiResponse; 44 | } 45 | 46 | /** 47 | *Constructor for AsciiMessageResponse.
48 | * 49 | * @param modbusMessage a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 50 | */ 51 | public AsciiMessageResponse(ModbusMessage modbusMessage) { 52 | super(modbusMessage); 53 | } 54 | 55 | /** 56 | *getModbusResponse.
57 | * 58 | * @return a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 59 | */ 60 | public ModbusResponse getModbusResponse() { 61 | return (ModbusResponse) modbusMessage; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/ascii/AsciiRequestHandler.java: -------------------------------------------------------------------------------- 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, seeAsciiRequestHandler class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class AsciiRequestHandler extends BaseRequestHandler { 37 | /** 38 | *Constructor for AsciiRequestHandler.
39 | * 40 | * @param slave a {@link com.serotonin.modbus4j.ModbusSlaveSet} object. 41 | */ 42 | public AsciiRequestHandler(ModbusSlaveSet slave) { 43 | super(slave); 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 48 | AsciiMessageRequest asciiRequest = (AsciiMessageRequest) req; 49 | ModbusRequest request = asciiRequest.getModbusRequest(); 50 | ModbusResponse response = handleRequestImpl(request); 51 | if (response == null) 52 | return null; 53 | return new AsciiMessageResponse(response); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/ascii/AsciiSlave.java: -------------------------------------------------------------------------------- 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, seeAsciiSlave class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class AsciiSlave extends SerialSlave { 37 | private MessageControl conn; 38 | 39 | /** 40 | *Constructor for AsciiSlave.
41 | * 42 | * @param wrapper a {@link com.serotonin.modbus4j.serial.SerialPortWrapper} object. 43 | */ 44 | public AsciiSlave(SerialPortWrapper wrapper) { 45 | super(wrapper); 46 | } 47 | 48 | /** {@inheritDoc} */ 49 | @Override 50 | public void start() throws ModbusInitException { 51 | super.start(); 52 | 53 | AsciiMessageParser asciiMessageParser = new AsciiMessageParser(false); 54 | AsciiRequestHandler asciiRequestHandler = new AsciiRequestHandler(this); 55 | 56 | conn = new MessageControl(); 57 | conn.setExceptionHandler(getExceptionHandler()); 58 | 59 | try { 60 | conn.start(transport, asciiMessageParser, asciiRequestHandler, null); 61 | transport.start("Modbus ASCII slave"); 62 | } 63 | catch (IOException e) { 64 | throw new ModbusInitException(e); 65 | } 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public void stop() { 71 | conn.close(); 72 | super.stop(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/rtu/RtuMessage.java: -------------------------------------------------------------------------------- 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, seeConstructor for RtuMessage.
38 | * 39 | * @param modbusMessage a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 40 | */ 41 | public RtuMessage(ModbusMessage modbusMessage) { 42 | super(modbusMessage); 43 | } 44 | 45 | /** 46 | *getMessageData.
47 | * 48 | * @return an array of {@link byte} objects. 49 | */ 50 | public byte[] getMessageData() { 51 | ByteQueue queue = new ByteQueue(); 52 | 53 | // Write the particular message. 54 | modbusMessage.write(queue); 55 | 56 | // 如果启用的CRC校验,才拼crc数据 57 | if (ModbusConfig.isEnableRtuCrc()) { 58 | // Write the CRC 59 | ModbusUtils.pushShort(queue, ModbusUtils.calculateCRC(modbusMessage)); 60 | } 61 | 62 | // Return the data. 63 | return queue.popAll(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/rtu/RtuMessageParser.java: -------------------------------------------------------------------------------- 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, seeConstructor for RtuMessageParser.
37 | * 38 | * @param master a boolean. 39 | */ 40 | public RtuMessageParser(boolean master) { 41 | super(master); 42 | } 43 | 44 | /** {@inheritDoc} */ 45 | @Override 46 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 47 | if (master) 48 | return RtuMessageResponse.createRtuMessageResponse(queue); 49 | return RtuMessageRequest.createRtuMessageRequest(queue); 50 | } 51 | // 52 | // public static void main(String[] args) throws Exception { 53 | // ByteQueue queue = new ByteQueue(new byte[] { 5, 3, 2, 0, (byte) 0xdc, (byte) 0x48, (byte) 0x1d, 0 }); 54 | // RtuMessageParser p = new RtuMessageParser(false); 55 | // System.out.println(p.parseResponse(queue)); 56 | // } 57 | } 58 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/rtu/RtuMessageRequest.java: -------------------------------------------------------------------------------- 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, seeConstructor for RtuMessageRequest.
50 | * 51 | * @param modbusRequest a {@link com.serotonin.modbus4j.msg.ModbusRequest} object. 52 | */ 53 | public RtuMessageRequest(ModbusRequest modbusRequest) { 54 | super(modbusRequest); 55 | } 56 | 57 | /** {@inheritDoc} */ 58 | @Override 59 | public boolean expectsResponse() { 60 | return modbusMessage.getSlaveId() != 0; 61 | } 62 | 63 | /** 64 | *getModbusRequest.
65 | * 66 | * @return a {@link com.serotonin.modbus4j.msg.ModbusRequest} object. 67 | */ 68 | public ModbusRequest getModbusRequest() { 69 | return (ModbusRequest) modbusMessage; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/rtu/RtuMessageResponse.java: -------------------------------------------------------------------------------- 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, seeConstructor for RtuMessageResponse.
54 | * 55 | * @param modbusResponse a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 56 | */ 57 | public RtuMessageResponse(ModbusResponse modbusResponse) { 58 | super(modbusResponse); 59 | } 60 | 61 | /** 62 | *getModbusResponse.
63 | * 64 | * @return a {@link com.serotonin.modbus4j.msg.ModbusResponse} object. 65 | */ 66 | public ModbusResponse getModbusResponse() { 67 | return (ModbusResponse) modbusMessage; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/rtu/RtuRequestHandler.java: -------------------------------------------------------------------------------- 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, seeRtuRequestHandler class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class RtuRequestHandler extends BaseRequestHandler { 37 | /** 38 | *Constructor for RtuRequestHandler.
39 | * 40 | * @param slave a {@link com.serotonin.modbus4j.ModbusSlaveSet} object. 41 | */ 42 | public RtuRequestHandler(ModbusSlaveSet slave) { 43 | super(slave); 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 48 | RtuMessageRequest rtuRequest = (RtuMessageRequest) req; 49 | ModbusRequest request = rtuRequest.getModbusRequest(); 50 | ModbusResponse response = handleRequestImpl(request); 51 | if (response == null) 52 | return null; 53 | return new RtuMessageResponse(response); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/serial/rtu/RtuSlave.java: -------------------------------------------------------------------------------- 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, seeRtuSlave class.
32 | * 33 | * @author Matthew Lohbihler 34 | * @version 5.0.0 35 | */ 36 | public class RtuSlave extends SerialSlave { 37 | // Runtime fields 38 | private MessageControl conn; 39 | 40 | /** 41 | *Constructor for RtuSlave.
42 | * 43 | * @param wrapper a {@link com.serotonin.modbus4j.serial.SerialPortWrapper} object. 44 | */ 45 | public RtuSlave(SerialPortWrapper wrapper) { 46 | super(wrapper); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public void start() throws ModbusInitException { 52 | super.start(); 53 | 54 | RtuMessageParser rtuMessageParser = new RtuMessageParser(false); 55 | RtuRequestHandler rtuRequestHandler = new RtuRequestHandler(this); 56 | 57 | conn = new MessageControl(); 58 | conn.setExceptionHandler(getExceptionHandler()); 59 | 60 | try { 61 | conn.start(transport, rtuMessageParser, rtuRequestHandler, null); 62 | transport.start("Modbus RTU slave"); 63 | } 64 | catch (IOException e) { 65 | throw new ModbusInitException(e); 66 | } 67 | } 68 | 69 | /** {@inheritDoc} */ 70 | @Override 71 | public void stop() { 72 | conn.close(); 73 | super.stop(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/NotImplementedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.modbus4j.sero; 6 | 7 | /** 8 | *NotImplementedException class.
9 | * 10 | * @author Matthew Lohbihler 11 | * @version 5.0.0 12 | */ 13 | public class NotImplementedException extends RuntimeException { 14 | static final long serialVersionUID = -1; 15 | 16 | /** 17 | *Constructor for NotImplementedException.
18 | */ 19 | public NotImplementedException() { 20 | super(); 21 | } 22 | 23 | /** 24 | *Constructor for NotImplementedException.
25 | * 26 | * @param message a {@link java.lang.String} object. 27 | */ 28 | public NotImplementedException(String message) { 29 | super(message); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/ShouldNeverHappenException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.modbus4j.sero; 6 | 7 | /** 8 | *ShouldNeverHappenException class.
9 | * 10 | * @author Matthew Lohbihler 11 | * @version 5.0.0 12 | */ 13 | public class ShouldNeverHappenException extends RuntimeException { 14 | private static final long serialVersionUID = -1; 15 | 16 | /** 17 | *Constructor for ShouldNeverHappenException.
18 | * 19 | * @param message a {@link java.lang.String} object. 20 | */ 21 | public ShouldNeverHappenException(String message) { 22 | super(message); 23 | } 24 | 25 | /** 26 | *Constructor for ShouldNeverHappenException.
27 | * 28 | * @param cause a {@link java.lang.Throwable} object. 29 | */ 30 | public ShouldNeverHappenException(Throwable cause) { 31 | super(cause); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/epoll/InputStreamEPollWrapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2015 Infinite Automation Software. All rights reserved. 3 | * @author Terry Packer 4 | */ 5 | package com.serotonin.modbus4j.sero.epoll; 6 | 7 | import java.io.InputStream; 8 | 9 | /** 10 | *InputStreamEPollWrapper interface.
11 | * 12 | * @author Terry Packer 13 | * @version 5.0.0 14 | */ 15 | public interface InputStreamEPollWrapper { 16 | 17 | /** 18 | *add.
19 | * 20 | * @param in a {@link java.io.InputStream} object. 21 | * @param inputStreamCallback a {@link com.serotonin.modbus4j.sero.epoll.Modbus4JInputStreamCallback} object. 22 | */ 23 | void add(InputStream in, Modbus4JInputStreamCallback inputStreamCallback); 24 | 25 | /** 26 | *remove.
27 | * 28 | * @param in a {@link java.io.InputStream} object. 29 | */ 30 | void remove(InputStream in); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/epoll/Modbus4JInputStreamCallback.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.epoll; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * A callback interface for input streams. 7 | * 8 | * NOTE: if the InputStreamEPoll instance is terminated, any running processes will be destroyed without any 9 | * notification to this callback. 10 | * 11 | * @author Matthew Lohbihler 12 | * @version 5.0.0 13 | */ 14 | public interface Modbus4JInputStreamCallback { 15 | /** 16 | * Called when content is read from the input stream. 17 | * 18 | * @param buf 19 | * the content that was read. This is a shared byte array. Contents can be manipulated within this call, 20 | * but the array itself should not be stored beyond the call since the contents will be changed. 21 | * @param len 22 | * the length of content that was read. 23 | */ 24 | void input(byte[] buf, int len); 25 | 26 | /** 27 | * Called when the closure of the input stream is detected. 28 | */ 29 | void closed(); 30 | 31 | /** 32 | * Called if there is an {@link java.io.IOException} while reading input stream. 33 | * 34 | * @param e 35 | * the exception that was received 36 | */ 37 | void ioException(IOException e); 38 | 39 | /** 40 | * Called if the InputStreamEPoll instance was terminated while the input stream was still registered. 41 | */ 42 | void terminated(); 43 | } 44 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/io/LineHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2009 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.modbus4j.sero.io; 6 | 7 | /** 8 | *LineHandler interface.
9 | * 10 | * @author Matthew Lohbihler 11 | * @version 5.0.0 12 | */ 13 | public interface LineHandler { 14 | /** 15 | *handleLine.
16 | * 17 | * @param line a {@link java.lang.String} object. 18 | */ 19 | public void handleLine(String line); 20 | 21 | /** 22 | *done.
23 | */ 24 | public void done(); 25 | } 26 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/io/NullWriter.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.io; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | 6 | /** 7 | *NullWriter class.
8 | * 9 | * @author Matthew Lohbihler 10 | * @version 5.0.0 11 | */ 12 | public class NullWriter extends Writer { 13 | /** {@inheritDoc} */ 14 | @Override 15 | public void write(char[] cbuf, int off, int len) throws IOException { 16 | // no op 17 | } 18 | 19 | /** {@inheritDoc} */ 20 | @Override 21 | public void flush() throws IOException { 22 | // no op 23 | } 24 | 25 | /** {@inheritDoc} */ 26 | @Override 27 | public void close() throws IOException { 28 | // no op 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/log/IOLog.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Infinite Automation Software and Serotonin Software. All rights reserved. 3 | * @author Terry Packer, Matthew Lohbihler 4 | */ 5 | package com.serotonin.modbus4j.sero.log; 6 | 7 | import java.io.File; 8 | 9 | /** 10 | *IOLog class.
11 | * 12 | * @author Matthew Lohbihler 13 | * @version 5.0.0 14 | */ 15 | public class IOLog extends BaseIOLog{ 16 | //private static final Log LOG = LogFactory.getLog(IOLog.class); 17 | private static final int MAX_FILESIZE = 1000000; 18 | // private static final int MAX_FILESIZE = 1000; 19 | private final File backupFile; 20 | 21 | /** 22 | *Constructor for IOLog.
23 | * 24 | * @param filename a {@link java.lang.String} object. 25 | */ 26 | public IOLog(String filename) { 27 | super(new File(filename)); 28 | backupFile = new File(filename + ".1"); 29 | } 30 | 31 | 32 | /** {@inheritDoc} */ 33 | @Override 34 | protected void sizeCheck() { 35 | // Check if the file should be rolled. 36 | if (file.length() > MAX_FILESIZE) { 37 | out.close(); 38 | 39 | if (backupFile.exists()) 40 | backupFile.delete(); 41 | file.renameTo(backupFile); 42 | createOut(); 43 | } 44 | } 45 | // 46 | // public static void main(String[] args) { 47 | // byte[] b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 48 | // 49 | // IOLog log = new IOLog("iotest"); 50 | // log.log("test"); 51 | // log.log("testtest"); 52 | // 53 | // log.input(b); 54 | // log.output(b); 55 | // log.input(b); 56 | // log.output(b); 57 | // log.input(b); 58 | // log.output(b); 59 | // log.input(b); 60 | // log.output(b); 61 | // log.input(b); 62 | // log.output(b); 63 | // log.input(b); 64 | // log.output(b); 65 | // log.input(b); 66 | // log.output(b); 67 | // log.input(b); 68 | // log.output(b); 69 | // log.input(b); 70 | // log.output(b); 71 | // log.input(b); 72 | // log.output(b); 73 | // log.input(b); 74 | // log.output(b); 75 | // log.input(b); 76 | // log.output(b); 77 | // 78 | // log.log("testtesttesttesttesttesttesttesttesttest"); 79 | // } 80 | } 81 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/log/SimpleLog.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.log; 2 | 3 | import java.io.PrintWriter; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | 7 | /** 8 | *SimpleLog class.
9 | * 10 | * @author Matthew Lohbihler 11 | * @version 5.0.0 12 | */ 13 | public class SimpleLog { 14 | private final PrintWriter out; 15 | private final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm:ss.SSS"); 16 | private final StringBuilder sb = new StringBuilder(); 17 | private final Date date = new Date(); 18 | 19 | /** 20 | *Constructor for SimpleLog.
21 | */ 22 | public SimpleLog() { 23 | this(new PrintWriter(System.out)); 24 | } 25 | 26 | /** 27 | *Constructor for SimpleLog.
28 | * 29 | * @param out a {@link java.io.PrintWriter} object. 30 | */ 31 | public SimpleLog(PrintWriter out) { 32 | this.out = out; 33 | } 34 | 35 | /** 36 | *out.
37 | * 38 | * @param message a {@link java.lang.String} object. 39 | */ 40 | public void out(String message) { 41 | out(message, null); 42 | } 43 | 44 | /** 45 | *out.
46 | * 47 | * @param t a {@link java.lang.Throwable} object. 48 | */ 49 | public void out(Throwable t) { 50 | out(null, t); 51 | } 52 | 53 | /** 54 | *out.
55 | * 56 | * @param o a {@link java.lang.Object} object. 57 | */ 58 | public void out(Object o) { 59 | if (o instanceof Throwable) 60 | out(null, (Throwable) o); 61 | else if (o == null) 62 | out(null, null); 63 | else 64 | out(o.toString(), null); 65 | } 66 | 67 | /** 68 | *close.
69 | */ 70 | public void close() { 71 | out.close(); 72 | } 73 | 74 | /** 75 | *out.
76 | * 77 | * @param message a {@link java.lang.String} object. 78 | * @param t a {@link java.lang.Throwable} object. 79 | */ 80 | public synchronized void out(String message, Throwable t) { 81 | sb.delete(0, sb.length()); 82 | date.setTime(System.currentTimeMillis()); 83 | sb.append(sdf.format(date)).append(" "); 84 | if (message != null) 85 | sb.append(message); 86 | if (t != null) { 87 | if (t.getMessage() != null) 88 | sb.append(" - ").append(t.getMessage()); 89 | out.println(sb.toString()); 90 | t.printStackTrace(out); 91 | } 92 | else 93 | out.println(sb.toString()); 94 | out.flush(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/DataConsumer.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | *DataConsumer interface.
7 | * 8 | * @author Matthew Lohbihler 9 | * @version 5.0.0 10 | */ 11 | public interface DataConsumer { 12 | /** 13 | * Notifies the consumer that new data is available 14 | * 15 | * @param b 16 | * array of bytes representing the incoming information 17 | * @param len 18 | * length of the data 19 | */ 20 | public void data(byte[] b, int len); 21 | 22 | /** 23 | *handleIOException.
24 | * 25 | * @param e a {@link java.io.IOException} object. 26 | */ 27 | public void handleIOException(IOException e); 28 | } 29 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/DefaultMessagingExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | /** 4 | *DefaultMessagingExceptionHandler class.
5 | * 6 | * @author Matthew Lohbihler 7 | * @version 5.0.0 8 | */ 9 | public class DefaultMessagingExceptionHandler implements MessagingExceptionHandler { 10 | /** {@inheritDoc} */ 11 | public void receivedException(Exception e) { 12 | e.printStackTrace(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/EpollStreamTransport.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | 7 | import com.serotonin.modbus4j.sero.epoll.InputStreamEPollWrapper; 8 | import com.serotonin.modbus4j.sero.epoll.Modbus4JInputStreamCallback; 9 | 10 | /** 11 | * First, instatiate with the streams and epoll. Then add a data consumer, or create a message control and pass this as 12 | * the transport (which will make the message control the data consumer). Stop the transport by stopping the message 13 | * control). 14 | * 15 | * @author Matthew Lohbihler 16 | * @version 5.0.0 17 | */ 18 | public class EpollStreamTransport implements Transport { 19 | private final OutputStream out; 20 | private final InputStream in; 21 | private final InputStreamEPollWrapper epoll; 22 | 23 | /** 24 | *Constructor for EpollStreamTransport.
25 | * 26 | * @param in a {@link java.io.InputStream} object. 27 | * @param out a {@link java.io.OutputStream} object. 28 | * @param epoll a {@link com.serotonin.modbus4j.sero.epoll.InputStreamEPollWrapper} object. 29 | */ 30 | public EpollStreamTransport(InputStream in, OutputStream out, InputStreamEPollWrapper epoll) { 31 | this.out = out; 32 | this.in = in; 33 | this.epoll = epoll; 34 | } 35 | 36 | /** {@inheritDoc} */ 37 | @Override 38 | public void setConsumer(final DataConsumer consumer) { 39 | epoll.add(in, new Modbus4JInputStreamCallback() { 40 | @Override 41 | public void terminated() { 42 | removeConsumer(); 43 | } 44 | 45 | @Override 46 | public void ioException(IOException e) { 47 | consumer.handleIOException(e); 48 | } 49 | 50 | @Override 51 | public void input(byte[] buf, int len) { 52 | consumer.data(buf, len); 53 | } 54 | 55 | @Override 56 | public void closed() { 57 | removeConsumer(); 58 | } 59 | }); 60 | } 61 | 62 | /** 63 | *removeConsumer.
64 | */ 65 | @Override 66 | public void removeConsumer() { 67 | epoll.remove(in); 68 | } 69 | 70 | /** 71 | *write.
72 | * 73 | * @param data an array of {@link byte} objects. 74 | * @throws java.io.IOException if any. 75 | */ 76 | @Override 77 | public void write(byte[] data) throws IOException { 78 | out.write(data); 79 | out.flush(); 80 | } 81 | 82 | /** {@inheritDoc} */ 83 | @Override 84 | public void write(byte[] data, int len) throws IOException { 85 | out.write(data, 0, len); 86 | out.flush(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/EpollStreamTransportCharSpaced.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Infinite Automation Software. All rights reserved. 3 | * @author Terry Packer 4 | */ 5 | package com.serotonin.modbus4j.sero.messaging; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.io.OutputStream; 10 | 11 | import com.serotonin.modbus4j.sero.epoll.InputStreamEPollWrapper; 12 | 13 | /** 14 | *EpollStreamTransportCharSpaced class.
15 | * 16 | * @author Terry Packer 17 | * @version 5.0.0 18 | */ 19 | public class EpollStreamTransportCharSpaced extends EpollStreamTransport{ 20 | 21 | private final long charSpacing; //Spacing for chars in nanoseconds 22 | private final OutputStream out; //Since the subclass has private members 23 | 24 | /** 25 | *Constructor for EpollStreamTransportCharSpaced.
26 | * 27 | * @param in a {@link java.io.InputStream} object. 28 | * @param out a {@link java.io.OutputStream} object. 29 | * @param epoll a {@link com.serotonin.modbus4j.sero.epoll.InputStreamEPollWrapper} object. 30 | * @param charSpacing a long. 31 | */ 32 | public EpollStreamTransportCharSpaced(InputStream in, OutputStream out, 33 | InputStreamEPollWrapper epoll, long charSpacing) { 34 | super(in, out, epoll); 35 | this.out = out; 36 | this.charSpacing = charSpacing; 37 | } 38 | 39 | /** 40 | * {@inheritDoc} 41 | * 42 | * Perform a write, ensure space between chars 43 | */ 44 | @Override 45 | public void write(byte[] data) throws IOException { 46 | 47 | try{ 48 | long waited = 0,writeStart,writeEnd, waitRemaining; 49 | for(byte b : data){ 50 | writeStart = System.nanoTime(); 51 | out.write(b); 52 | writeEnd = System.nanoTime(); 53 | waited = writeEnd - writeStart; 54 | if(waited < this.charSpacing){ 55 | waitRemaining = this.charSpacing - waited; 56 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000)); 57 | } 58 | 59 | } 60 | }catch(Exception e){ 61 | throw new IOException(e); 62 | } 63 | out.flush(); 64 | } 65 | 66 | /** {@inheritDoc} */ 67 | public void write(byte[] data, int len) throws IOException { 68 | try{ 69 | long waited = 0,writeStart,writeEnd, waitRemaining; 70 | for(int i=0; i< len; i++){ 71 | writeStart = System.nanoTime(); 72 | out.write(data[i]); 73 | writeEnd = System.nanoTime(); 74 | waited = writeEnd - writeStart; 75 | if(waited < this.charSpacing){ 76 | waitRemaining = this.charSpacing - waited; 77 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000)); 78 | } 79 | 80 | } 81 | }catch(Exception e){ 82 | throw new IOException(e); 83 | } 84 | out.flush(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/IncomingMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | /** 4 | *IncomingMessage interface.
5 | * 6 | * @author Matthew Lohbihler 7 | * @version 5.0.0 8 | */ 9 | public interface IncomingMessage { 10 | // A marker interface 11 | } 12 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/IncomingRequestMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | 4 | /** 5 | *IncomingRequestMessage interface.
6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface IncomingRequestMessage extends IncomingMessage { 11 | // A marker interface. 12 | } 13 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/IncomingResponseMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | 4 | /** 5 | *IncomingResponseMessage interface.
6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface IncomingResponseMessage extends IncomingMessage { 11 | // A marker interface 12 | } 13 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/MessageParser.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 4 | 5 | /** 6 | * Interface defining methods that are called when data arrives in the connection. 7 | * 8 | * @author Matthew Lohbihler 9 | * @version 5.0.0 10 | */ 11 | public interface MessageParser { 12 | /** 13 | * Attempt to parse a message out of the queue. Data in the queue may be discarded if it is unusable (i.e. a start 14 | * indicator is not found), but otherwise if a message is not found due to the data being incomplete, the method 15 | * should return null. As additional data arrives, it will be appended to the queue and this method will be called 16 | * again. 17 | * 18 | * Implementations should not modify the queue unless it is safe to do so. No copy of the data is made before 19 | * calling this method. 20 | * 21 | * @param queue 22 | * the queue from which to access data for the creation of the message 23 | * @return the message if one was able to be created, or null otherwise. 24 | * @throws java.lang.Exception 25 | * if the data in the queue is sufficient to construct a message, but the message data is invalid, this 26 | * method must throw an exception, or it will keep getting the same data. 27 | */ 28 | IncomingMessage parseMessage(ByteQueue queue) throws Exception; 29 | } 30 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/MessagingExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | /** 4 | *MessagingExceptionHandler interface.
5 | * 6 | * @author Matthew Lohbihler 7 | * @version 5.0.0 8 | */ 9 | public interface MessagingExceptionHandler { 10 | /** 11 | *receivedException.
12 | * 13 | * @param e a {@link java.lang.Exception} object. 14 | */ 15 | public void receivedException(Exception e); 16 | } 17 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/OutgoingMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | /** 4 | *OutgoingMessage interface.
5 | * 6 | * @author Matthew Lohbihler 7 | * @version 5.0.0 8 | */ 9 | public interface OutgoingMessage { 10 | /** 11 | * Return the byte array representing the serialization of the request. 12 | * 13 | * @return byte array representing the serialization of the request 14 | */ 15 | byte[] getMessageData(); 16 | } 17 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/OutgoingRequestMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | 4 | /** 5 | *OutgoingRequestMessage interface.
6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface OutgoingRequestMessage extends OutgoingMessage { 11 | /** 12 | * Whether the request is expecting a response or not. 13 | * 14 | * @return true if a response is expected, false otherwise. 15 | */ 16 | boolean expectsResponse(); 17 | } 18 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/OutgoingResponseMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | 4 | /** 5 | *OutgoingResponseMessage interface.
6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface OutgoingResponseMessage extends OutgoingMessage { 11 | // A marker interface. 12 | } 13 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/RequestHandler.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | 4 | /** 5 | *RequestHandler interface.
6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface RequestHandler { 11 | /** 12 | * Handle the request and return the appropriate response object. 13 | * 14 | * @param request 15 | * the request to handle 16 | * @return the response object or null if no response is to be sent. null may also be returned if the request is 17 | * handled asynchronously. 18 | * @throws java.lang.Exception if necessary 19 | */ 20 | OutgoingResponseMessage handleRequest(IncomingRequestMessage request) throws Exception; 21 | } 22 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/StreamTransport.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | 7 | /** 8 | * First, instatiate with the streams. Then add a data consumer, or create a message control and pass this as the 9 | * transport (which will make the message control the data consumer). Change the read delay if desired. This class 10 | * supports running in its own thread (start) or an external one (run), say from a thread pool. Both approaches are 11 | * delegated to the stream listener. In either case, stop the transport with the stop method (or just stop the message 12 | * control). 13 | * 14 | * @author Matthew Lohbihler 15 | * @version 5.0.0 16 | */ 17 | public class StreamTransport implements Transport, Runnable { 18 | protected OutputStream out; 19 | protected InputStream in; 20 | private InputStreamListener listener; 21 | 22 | /** 23 | *Constructor for StreamTransport.
24 | * 25 | * @param in a {@link java.io.InputStream} object. 26 | * @param out a {@link java.io.OutputStream} object. 27 | */ 28 | public StreamTransport(InputStream in, OutputStream out) { 29 | this.out = out; 30 | this.in = in; 31 | } 32 | 33 | /** 34 | *setReadDelay.
35 | * 36 | * @param readDelay a int. 37 | */ 38 | public void setReadDelay(int readDelay) { 39 | if (listener != null) 40 | listener.setReadDelay(readDelay); 41 | } 42 | 43 | /** 44 | *start.
45 | * 46 | * @param threadName a {@link java.lang.String} object. 47 | */ 48 | public void start(String threadName) { 49 | listener.start(threadName); 50 | } 51 | 52 | /** 53 | *stop.
54 | */ 55 | public void stop() { 56 | listener.stop(); 57 | } 58 | 59 | /** 60 | *run.
61 | */ 62 | public void run() { 63 | listener.run(); 64 | } 65 | 66 | /** {@inheritDoc} */ 67 | public void setConsumer(DataConsumer consumer) { 68 | listener = new InputStreamListener(in, consumer); 69 | } 70 | 71 | /** 72 | *removeConsumer.
73 | */ 74 | public void removeConsumer() { 75 | if (listener != null) { 76 | listener.stop(); 77 | } 78 | listener = null; 79 | } 80 | 81 | /** 82 | *write.
83 | * 84 | * @param data an array of {@link byte} objects. 85 | * @throws java.io.IOException if any. 86 | */ 87 | public void write(byte[] data) throws IOException { 88 | out.write(data); 89 | out.flush(); 90 | } 91 | 92 | /** {@inheritDoc} */ 93 | public void write(byte[] data, int len) throws IOException { 94 | out.write(data, 0, len); 95 | out.flush(); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/StreamTransportCharSpaced.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Infinite Automation Software. All rights reserved. 3 | * @author Terry Packer 4 | */ 5 | package com.serotonin.modbus4j.sero.messaging; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.io.OutputStream; 10 | 11 | /** 12 | *StreamTransportCharSpaced class.
13 | * 14 | * @author Terry Packer 15 | * @version 5.0.0 16 | */ 17 | public class StreamTransportCharSpaced extends StreamTransport{ 18 | 19 | private final long charSpacing; 20 | 21 | /** 22 | *Constructor for StreamTransportCharSpaced.
23 | * 24 | * @param in a {@link java.io.InputStream} object. 25 | * @param out a {@link java.io.OutputStream} object. 26 | * @param charSpacing a long. 27 | */ 28 | public StreamTransportCharSpaced(InputStream in, OutputStream out, long charSpacing) { 29 | super(in, out); 30 | this.charSpacing = charSpacing; 31 | } 32 | 33 | /** 34 | * {@inheritDoc} 35 | * 36 | * Perform a write, ensure space between chars 37 | */ 38 | @Override 39 | public void write(byte[] data) throws IOException { 40 | 41 | try{ 42 | long waited = 0,writeStart,writeEnd, waitRemaining; 43 | for(byte b : data){ 44 | writeStart = System.nanoTime(); 45 | out.write(b); 46 | writeEnd = System.nanoTime(); 47 | waited = writeEnd - writeStart; 48 | if(waited < this.charSpacing){ 49 | waitRemaining = this.charSpacing - waited; 50 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000)); 51 | } 52 | 53 | } 54 | }catch(Exception e){ 55 | throw new IOException(e); 56 | } 57 | out.flush(); 58 | } 59 | 60 | /** {@inheritDoc} */ 61 | public void write(byte[] data, int len) throws IOException { 62 | try{ 63 | long waited = 0,writeStart,writeEnd, waitRemaining; 64 | for(int i=0; i< len; i++){ 65 | writeStart = System.nanoTime(); 66 | out.write(data[i]); 67 | writeEnd = System.nanoTime(); 68 | waited = writeEnd - writeStart; 69 | if(waited < this.charSpacing){ 70 | waitRemaining = this.charSpacing - waited; 71 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000)); 72 | } 73 | 74 | } 75 | }catch(Exception e){ 76 | throw new IOException(e); 77 | } 78 | out.flush(); 79 | } 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/TestableTransport.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.OutputStream; 7 | 8 | /** 9 | * Provides synchronization on the input stream read by wrapping it. 10 | * 11 | * @author Matthew Lohbihler 12 | * @version 5.0.0 13 | */ 14 | public class TestableTransport extends StreamTransport { 15 | /** 16 | *Constructor for TestableTransport.
17 | * 18 | * @param in a {@link java.io.InputStream} object. 19 | * @param out a {@link java.io.OutputStream} object. 20 | */ 21 | public TestableTransport(InputStream in, OutputStream out) { 22 | super(new TestableBufferedInputStream(in), out); 23 | } 24 | 25 | /** 26 | *testInputStream.
27 | * 28 | * @throws java.io.IOException if any. 29 | */ 30 | public void testInputStream() throws IOException { 31 | ((TestableBufferedInputStream) in).test(); 32 | } 33 | 34 | static class TestableBufferedInputStream extends BufferedInputStream { 35 | public TestableBufferedInputStream(InputStream in) { 36 | super(in); 37 | } 38 | 39 | @Override 40 | public synchronized int read(byte[] buf) throws IOException { 41 | return super.read(buf); 42 | } 43 | 44 | public synchronized void test() throws IOException { 45 | mark(1); 46 | int i = read(); 47 | if (i == -1) 48 | throw new IOException("Stream closed"); 49 | reset(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/TimeoutException.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | *TimeoutException class.
7 | * 8 | * @author Matthew Lohbihler 9 | * @version 5.0.0 10 | */ 11 | public class TimeoutException extends IOException { 12 | private static final long serialVersionUID = 1L; 13 | 14 | /** 15 | *Constructor for TimeoutException.
16 | * 17 | * @param message a {@link java.lang.String} object. 18 | */ 19 | public TimeoutException(String message) { 20 | super(message); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/Transport.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * A transport is a wrapper around the means by which data is transferred. So, there could be transports for serial 7 | * ports, sockets, UDP, email, etc. 8 | * 9 | * @author Matthew Lohbihler 10 | * @version 5.0.0 11 | */ 12 | public interface Transport { 13 | /** 14 | *setConsumer.
15 | * 16 | * @param consumer a {@link com.serotonin.modbus4j.sero.messaging.DataConsumer} object. 17 | * @throws java.io.IOException if any. 18 | */ 19 | abstract void setConsumer(DataConsumer consumer) throws IOException; 20 | 21 | /** 22 | *removeConsumer.
23 | */ 24 | abstract void removeConsumer(); 25 | 26 | /** 27 | *write.
28 | * 29 | * @param data an array of {@link byte} objects. 30 | * @throws java.io.IOException if any. 31 | */ 32 | abstract void write(byte[] data) throws IOException; 33 | 34 | /** 35 | *write.
36 | * 37 | * @param data an array of {@link byte} objects. 38 | * @param len a int. 39 | * @throws java.io.IOException if any. 40 | */ 41 | abstract void write(byte[] data, int len) throws IOException; 42 | } 43 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/WaitingRoomException.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | *WaitingRoomException class.
7 | * 8 | * @author Matthew Lohbihler 9 | * @version 5.0.0 10 | */ 11 | public class WaitingRoomException extends IOException { 12 | private static final long serialVersionUID = 1L; 13 | 14 | /** 15 | *Constructor for WaitingRoomException.
16 | * 17 | * @param message a {@link java.lang.String} object. 18 | */ 19 | public WaitingRoomException(String message) { 20 | super(message); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/WaitingRoomKey.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | /** 4 | * Waiting room keys are used to match requests with responses. Implementation need to have hashcode and equals 5 | * definitions. 6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface WaitingRoomKey { 11 | // Implementation needs to have hashcode and equals implementations. 12 | } 13 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/messaging/WaitingRoomKeyFactory.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.messaging; 2 | 3 | 4 | /** 5 | *WaitingRoomKeyFactory interface.
6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface WaitingRoomKeyFactory { 11 | /** 12 | *createWaitingRoomKey.
13 | * 14 | * @param request a {@link com.serotonin.modbus4j.sero.messaging.OutgoingRequestMessage} object. 15 | * @return a {@link com.serotonin.modbus4j.sero.messaging.WaitingRoomKey} object. 16 | */ 17 | WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request); 18 | 19 | /** 20 | *createWaitingRoomKey.
21 | * 22 | * @param response a {@link com.serotonin.modbus4j.sero.messaging.IncomingResponseMessage} object. 23 | * @return a {@link com.serotonin.modbus4j.sero.messaging.WaitingRoomKey} object. 24 | */ 25 | WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response); 26 | } 27 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/timer/SystemTimeSource.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.timer; 2 | 3 | /** 4 | * An implementation of TimeSource that returns the host time via System. 5 | * 6 | * @author Matthew Lohbihler 7 | * @version 5.0.0 8 | */ 9 | public class SystemTimeSource implements TimeSource { 10 | /** 11 | *currentTimeMillis.
12 | * 13 | * @return a long. 14 | */ 15 | public long currentTimeMillis() { 16 | return System.currentTimeMillis(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/timer/TimeSource.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.sero.timer; 2 | 3 | /** 4 | * An interface to abstract the source of current time away from System. This allows code to run in simulations where 5 | * the time is controlled explicitly. 6 | * 7 | * @author Matthew Lohbihler 8 | * @version 5.0.0 9 | */ 10 | public interface TimeSource { 11 | /** 12 | *currentTimeMillis.
13 | * 14 | * @return a long. 15 | */ 16 | long currentTimeMillis(); 17 | } 18 | -------------------------------------------------------------------------------- /modbus4j/src/main/java/com/serotonin/modbus4j/sero/util/ProgressiveTaskListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of version 2 of the GNU General Public License as 6 | published by the Free Software Foundation and additional terms as 7 | specified by Serotonin Software Technologies Inc. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software Foundation, Inc., 16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 17 | 18 | @author Matthew Lohbihler 19 | */ 20 | 21 | package com.serotonin.modbus4j.sero.util; 22 | 23 | /** 24 | *ProgressiveTaskListener interface.
25 | * 26 | * @author Matthew Lohbihler 27 | * @version 5.0.0 28 | */ 29 | public interface ProgressiveTaskListener { 30 | /** 31 | * Optionally called occasionally by the task to declare the progress that has been made. 32 | * 33 | * @param progress float between 0 and 1 where 0 is no progress and 1 is completed. 34 | */ 35 | void progressUpdate(float progress); 36 | 37 | /** 38 | * Notification that the task has been cancelled. Should only be called once for the task. 39 | */ 40 | void taskCancelled(); 41 | 42 | /** 43 | * Notification that the task has been completed. Should only be called once for the task. 44 | */ 45 | void taskCompleted(); 46 | } 47 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':modbus4android' 2 | //include ':modbus4j' 3 | --------------------------------------------------------------------------------