├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── README.md ├── README.txt ├── RELEASE-NOTES ├── build-cdc.xml ├── build.xml ├── lib-opt ├── ant-contrib-1.0b3.jar └── maven-ant-tasks-2.1.3.jar ├── license.txt ├── pom.xml ├── release └── 2.0.2 │ ├── modbus4j-2.0.2-doc.zip │ ├── modbus4j-2.0.2-src.zip │ └── modbus4j-2.0.2.jar ├── src-opt └── com │ └── serotonin │ └── modbus4j │ └── logs │ └── LogReader.java ├── src └── com │ └── serotonin │ └── modbus4j │ ├── BasicProcessImage.java │ ├── BatchRead.java │ ├── BatchResults.java │ ├── ExceptionResult.java │ ├── Modbus.java │ ├── ModbusFactory.java │ ├── ModbusMaster.java │ ├── ModbusSlaveSet.java │ ├── NodeScanListener.java │ ├── ProcessImage.java │ ├── ProcessImageListener.java │ ├── base │ ├── BaseMessageParser.java │ ├── BaseRequestHandler.java │ ├── KeyedModbusLocator.java │ ├── ModbusUtils.java │ ├── RangeAndOffset.java │ ├── ReadFunctionGroup.java │ ├── SlaveAndRange.java │ └── SlaveProfile.java │ ├── code │ ├── DataType.java │ ├── ExceptionCode.java │ ├── FunctionCode.java │ └── RegisterRange.java │ ├── exception │ ├── ErrorResponseException.java │ ├── IllegalDataAddressException.java │ ├── IllegalDataTypeException.java │ ├── IllegalFunctionException.java │ ├── IllegalSlaveIdException.java │ ├── InvalidDataConversionException.java │ ├── ModbusIdException.java │ ├── ModbusInitException.java │ ├── ModbusTransportException.java │ └── SlaveIdNotEqual.java │ ├── ip │ ├── IpMessage.java │ ├── IpMessageResponse.java │ ├── IpParameters.java │ ├── encap │ │ ├── EncapMessage.java │ │ ├── EncapMessageParser.java │ │ ├── EncapMessageRequest.java │ │ ├── EncapMessageResponse.java │ │ ├── EncapRequestHandler.java │ │ └── EncapWaitingRoomKeyFactory.java │ ├── listener │ │ └── TcpListener.java │ ├── tcp │ │ ├── TcpMaster.java │ │ └── TcpSlave.java │ ├── udp │ │ ├── UdpMaster.java │ │ └── UdpSlave.java │ └── xa │ │ ├── XaMessage.java │ │ ├── XaMessageParser.java │ │ ├── XaMessageRequest.java │ │ ├── XaMessageResponse.java │ │ ├── XaRequestHandler.java │ │ └── XaWaitingRoomKeyFactory.java │ ├── locator │ ├── BaseLocator.java │ ├── BinaryLocator.java │ ├── NumericLocator.java │ └── StringLocator.java │ ├── msg │ ├── ExceptionRequest.java │ ├── ExceptionResponse.java │ ├── ModbusMessage.java │ ├── ModbusRequest.java │ ├── ModbusResponse.java │ ├── ReadBinaryRequest.java │ ├── ReadCoilsRequest.java │ ├── ReadCoilsResponse.java │ ├── ReadDiscreteInputsRequest.java │ ├── ReadDiscreteInputsResponse.java │ ├── ReadExceptionStatusRequest.java │ ├── ReadExceptionStatusResponse.java │ ├── ReadHoldingRegistersRequest.java │ ├── ReadHoldingRegistersResponse.java │ ├── ReadInputRegistersRequest.java │ ├── ReadInputRegistersResponse.java │ ├── ReadNumericRequest.java │ ├── ReadResponse.java │ ├── ReportSlaveIdRequest.java │ ├── ReportSlaveIdResponse.java │ ├── WriteCoilRequest.java │ ├── WriteCoilResponse.java │ ├── WriteCoilsRequest.java │ ├── WriteCoilsResponse.java │ ├── WriteMaskRegisterRequest.java │ ├── WriteMaskRegisterResponse.java │ ├── WriteRegisterRequest.java │ ├── WriteRegisterResponse.java │ ├── WriteRegistersRequest.java │ └── WriteRegistersResponse.java │ ├── serial │ ├── SerialMaster.java │ ├── SerialMessage.java │ ├── SerialPortWrapper.java │ ├── SerialSlave.java │ ├── SerialWaitingRoomKeyFactory.java │ ├── ascii │ │ ├── AsciiMaster.java │ │ ├── AsciiMessage.java │ │ ├── AsciiMessageParser.java │ │ ├── AsciiMessageRequest.java │ │ ├── AsciiMessageResponse.java │ │ ├── AsciiRequestHandler.java │ │ └── AsciiSlave.java │ └── rtu │ │ ├── RtuMaster.java │ │ ├── RtuMessage.java │ │ ├── RtuMessageParser.java │ │ ├── RtuMessageRequest.java │ │ ├── RtuMessageResponse.java │ │ ├── RtuRequestHandler.java │ │ └── RtuSlave.java │ ├── sero │ ├── NotImplementedException.java │ ├── ShouldNeverHappenException.java │ ├── epoll │ │ ├── InputStreamEPollWrapper.java │ │ └── Modbus4JInputStreamCallback.java │ ├── io │ │ ├── LineHandler.java │ │ ├── NullWriter.java │ │ └── StreamUtils.java │ ├── log │ │ ├── BaseIOLog.java │ │ ├── IOLog.java │ │ ├── RollingIOLog.java │ │ └── SimpleLog.java │ ├── messaging │ │ ├── DataConsumer.java │ │ ├── DefaultMessagingExceptionHandler.java │ │ ├── EpollStreamTransport.java │ │ ├── EpollStreamTransportCharSpaced.java │ │ ├── IncomingMessage.java │ │ ├── IncomingRequestMessage.java │ │ ├── IncomingResponseMessage.java │ │ ├── InputStreamListener.java │ │ ├── MessageControl.java │ │ ├── MessageParser.java │ │ ├── MessagingExceptionHandler.java │ │ ├── OutgoingMessage.java │ │ ├── OutgoingRequestMessage.java │ │ ├── OutgoingResponseMessage.java │ │ ├── RequestHandler.java │ │ ├── StreamTransport.java │ │ ├── StreamTransportCharSpaced.java │ │ ├── TestableTransport.java │ │ ├── TimeoutException.java │ │ ├── Transport.java │ │ ├── WaitingRoom.java │ │ ├── WaitingRoomException.java │ │ ├── WaitingRoomKey.java │ │ └── WaitingRoomKeyFactory.java │ ├── timer │ │ ├── SystemTimeSource.java │ │ └── TimeSource.java │ └── util │ │ ├── ArrayUtils.java │ │ ├── ProgressiveTask.java │ │ ├── ProgressiveTaskListener.java │ │ └── queue │ │ └── ByteQueue.java │ └── value │ └── ModbusValue.java ├── src_cdc ├── Changes to Modbus4J for Java 1.4 compliance.txt ├── com │ └── serotonin │ │ └── cdc │ │ ├── NotImplementedException.java │ │ ├── ShouldNeverHappenException.java │ │ ├── io │ │ ├── StreamUtils.java │ │ └── serial │ │ │ ├── SerialParameters.java │ │ │ ├── SerialPortException.java │ │ │ └── SerialUtils.java │ │ ├── messaging │ │ ├── DataConsumer.java │ │ ├── DefaultMessagingExceptionHandler.java │ │ ├── IncomingMessage.java │ │ ├── IncomingRequestMessage.java │ │ ├── IncomingResponseMessage.java │ │ ├── InputStreamListener.java │ │ ├── MessageControl.java │ │ ├── MessageParser.java │ │ ├── MessagingExceptionHandler.java │ │ ├── OutgoingMessage.java │ │ ├── OutgoingRequestMessage.java │ │ ├── OutgoingResponseMessage.java │ │ ├── RequestHandler.java │ │ ├── StreamTransport.java │ │ ├── TestableTransport.java │ │ ├── TimeoutException.java │ │ ├── Transport.java │ │ ├── WaitingRoom.java │ │ ├── WaitingRoomException.java │ │ ├── WaitingRoomKey.java │ │ └── WaitingRoomKeyFactory.java │ │ ├── modbus4j │ │ ├── BasicProcessImage.java │ │ ├── BatchRead.java │ │ ├── BatchResults.java │ │ ├── ExceptionResult.java │ │ ├── Modbus.java │ │ ├── ModbusFactory.java │ │ ├── ModbusMaster.java │ │ ├── ModbusSlaveSet.java │ │ ├── NodeScanListener.java │ │ ├── ProcessImage.java │ │ ├── ProcessImageListener.java │ │ ├── base │ │ │ ├── BaseMessageParser.java │ │ │ ├── BaseRequestHandler.java │ │ │ ├── KeyedModbusLocator.java │ │ │ ├── ModbusUtils.java │ │ │ ├── RangeAndOffset.java │ │ │ ├── ReadFunctionGroup.java │ │ │ ├── SlaveAndRange.java │ │ │ └── SlaveProfile.java │ │ ├── code │ │ │ ├── DataType.java │ │ │ ├── ExceptionCode.java │ │ │ ├── FunctionCode.java │ │ │ └── RegisterRange.java │ │ ├── exception │ │ │ ├── ErrorResponseException.java │ │ │ ├── IllegalDataAddressException.java │ │ │ ├── IllegalDataTypeException.java │ │ │ ├── IllegalFunctionException.java │ │ │ ├── IllegalSlaveIdException.java │ │ │ ├── InvalidDataConversionException.java │ │ │ ├── ModbusIdException.java │ │ │ ├── ModbusInitException.java │ │ │ └── ModbusTransportException.java │ │ ├── ip │ │ │ ├── IpMessage.java │ │ │ ├── IpMessageResponse.java │ │ │ ├── IpParameters.java │ │ │ ├── encap │ │ │ │ ├── EncapMessage.java │ │ │ │ ├── EncapMessageParser.java │ │ │ │ ├── EncapMessageRequest.java │ │ │ │ ├── EncapMessageResponse.java │ │ │ │ ├── EncapRequestHandler.java │ │ │ │ └── EncapWaitingRoomKeyFactory.java │ │ │ ├── tcp │ │ │ │ ├── TcpMaster.java │ │ │ │ └── TcpSlave.java │ │ │ ├── udp │ │ │ │ ├── UdpMaster.java │ │ │ │ └── UdpSlave.java │ │ │ └── xa │ │ │ │ ├── XaMessage.java │ │ │ │ ├── XaMessageParser.java │ │ │ │ ├── XaMessageRequest.java │ │ │ │ ├── XaMessageResponse.java │ │ │ │ ├── XaRequestHandler.java │ │ │ │ └── XaWaitingRoomKeyFactory.java │ │ ├── locator │ │ │ ├── BaseLocator.java │ │ │ ├── BinaryLocator.java │ │ │ ├── NumericLocator.java │ │ │ └── StringLocator.java │ │ ├── msg │ │ │ ├── ExceptionRequest.java │ │ │ ├── ExceptionResponse.java │ │ │ ├── ModbusMessage.java │ │ │ ├── ModbusRequest.java │ │ │ ├── ModbusResponse.java │ │ │ ├── ReadBinaryRequest.java │ │ │ ├── ReadCoilsRequest.java │ │ │ ├── ReadCoilsResponse.java │ │ │ ├── ReadDiscreteInputsRequest.java │ │ │ ├── ReadDiscreteInputsResponse.java │ │ │ ├── ReadExceptionStatusRequest.java │ │ │ ├── ReadExceptionStatusResponse.java │ │ │ ├── ReadHoldingRegistersRequest.java │ │ │ ├── ReadHoldingRegistersResponse.java │ │ │ ├── ReadInputRegistersRequest.java │ │ │ ├── ReadInputRegistersResponse.java │ │ │ ├── ReadNumericRequest.java │ │ │ ├── ReadResponse.java │ │ │ ├── ReportSlaveIdRequest.java │ │ │ ├── ReportSlaveIdResponse.java │ │ │ ├── WriteCoilRequest.java │ │ │ ├── WriteCoilResponse.java │ │ │ ├── WriteCoilsRequest.java │ │ │ ├── WriteCoilsResponse.java │ │ │ ├── WriteMaskRegisterRequest.java │ │ │ ├── WriteMaskRegisterResponse.java │ │ │ ├── WriteRegisterRequest.java │ │ │ ├── WriteRegisterResponse.java │ │ │ ├── WriteRegistersRequest.java │ │ │ └── WriteRegistersResponse.java │ │ ├── serial │ │ │ ├── SerialMaster.java │ │ │ ├── SerialMessage.java │ │ │ ├── SerialSlave.java │ │ │ ├── SerialWaitingRoomKeyFactory.java │ │ │ ├── ascii │ │ │ │ ├── AsciiMaster.java │ │ │ │ ├── AsciiMessage.java │ │ │ │ ├── AsciiMessageParser.java │ │ │ │ ├── AsciiMessageRequest.java │ │ │ │ ├── AsciiMessageResponse.java │ │ │ │ ├── AsciiRequestHandler.java │ │ │ │ └── AsciiSlave.java │ │ │ └── rtu │ │ │ │ ├── RtuMaster.java │ │ │ │ ├── RtuMessage.java │ │ │ │ ├── RtuMessageParser.java │ │ │ │ ├── RtuMessageRequest.java │ │ │ │ ├── RtuMessageResponse.java │ │ │ │ ├── RtuRequestHandler.java │ │ │ │ └── RtuSlave.java │ │ └── value │ │ │ └── ModbusValue.java │ │ └── util │ │ ├── ArrayUtils.java │ │ ├── ProgressiveTask.java │ │ ├── ProgressiveTaskListener.java │ │ ├── StringUtils.java │ │ └── queue │ │ └── ByteQueue.java └── gnu │ └── io │ └── RXTXHack.java └── src_test ├── Test.java └── com └── serotonin └── modbus4j └── test ├── AsciiDecodingTest.java ├── BatchTest.java ├── DecodingTest.java ├── ListenerTest.java ├── ListenerTest2.java ├── MasterTest.java ├── MasterTest2.java ├── MaxRegisterTest.java ├── ReadSerialTest.java ├── ReadTest.java ├── Test.java ├── Test2.java ├── Test3.java ├── Test4.java └── TestSerialPortWrapper.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Modbus4J 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | encoding/src=UTF-8 4 | encoding/src_test=UTF-8 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.release=disabled 6 | org.eclipse.jdt.core.compiler.source=1.8 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | modbus4j 2 | ======== 3 | 4 | A high-performance and ease-of-use implementation of the Modbus protocol written in Java by Infinite Automation Systems and Serotonin Software. Supports ASCII, RTU, TCP, and UDP transports as slave or master, automatic request partitioning and response data type parsing. 5 | 6 | For support and general help please see our [Forum](https://forum.infiniteautomation.com/category/11/modbus4j-general-discussion) 7 | 8 | **Commercial licenses are available by contacting: sales@radixiot.com** 9 | 10 | A public Maven Repository is now available with the latest builds add this to your pom.xml 11 | 12 | ```xml 13 | 14 | 15 | 16 | false 17 | 18 | 19 | true 20 | 21 | ias-snapshots 22 | Infinite Automation Snapshot Repository 23 | https://maven.mangoautomation.net/repository/ias-snapshot/ 24 | 25 | 26 | 27 | true 28 | 29 | 30 | false 31 | 32 | ias-releases 33 | Infinite Automation Release Repository 34 | https://maven.mangoautomation.net/repository/ias-release/ 35 | 36 | 37 | ``` 38 | 39 | The dependency information is: 40 | 41 | ```xml 42 | 43 | com.infiniteautomation 44 | modbus4j 45 | 3.0.3 46 | 47 | ``` 48 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | Build Via the Ant Targets, maven will automatically download and install libraries from web and modbus4j-maven-local repository included in project. 2 | 3 | modbus4J.jar requires: (See pom.xml) 4 | 5 | A discussion forum for this package can be found at http://mango.serotoninsoftware.com/forum/forums/show/11.page. 6 | 7 | There is a public maven repository for downloading Modbus4J as a dependency in your project. Just add this: 8 | 9 | 10 | 11 | 12 | true 13 | ignore 14 | 15 | 16 | false 17 | 18 | modbus-data-source 19 | Modbus Dependencies 20 | https://maven.mangoautomation.net/repository/ias-release/ 21 | 22 | 23 | 24 | Use this as the dependency info: 25 | 26 | com.infiniteautomation 27 | modbus4j 28 | 3.0.3 29 | -------------------------------------------------------------------------------- /build-cdc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /lib-opt/ant-contrib-1.0b3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoAutomation/modbus4j/79c6b74fee82613555798115b66175a5e08ef7b5/lib-opt/ant-contrib-1.0b3.jar -------------------------------------------------------------------------------- /lib-opt/maven-ant-tasks-2.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoAutomation/modbus4j/79c6b74fee82613555798115b66175a5e08ef7b5/lib-opt/maven-ant-tasks-2.1.3.jar -------------------------------------------------------------------------------- /release/2.0.2/modbus4j-2.0.2-doc.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoAutomation/modbus4j/79c6b74fee82613555798115b66175a5e08ef7b5/release/2.0.2/modbus4j-2.0.2-doc.zip -------------------------------------------------------------------------------- /release/2.0.2/modbus4j-2.0.2-src.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoAutomation/modbus4j/79c6b74fee82613555798115b66175a5e08ef7b5/release/2.0.2/modbus4j-2.0.2-src.zip -------------------------------------------------------------------------------- /release/2.0.2/modbus4j-2.0.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoAutomation/modbus4j/79c6b74fee82613555798115b66175a5e08ef7b5/release/2.0.2/modbus4j-2.0.2.jar -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ExceptionResult.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, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import com.serotonin.modbus4j.code.ExceptionCode; 24 | 25 | /** 26 | *

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.

47 | * 48 | * @return a byte. 49 | */ 50 | public byte getExceptionCode() { 51 | return exceptionCode; 52 | } 53 | 54 | /** 55 | *

Getter for the field exceptionMessage.

56 | * 57 | * @return a {@link java.lang.String} object. 58 | */ 59 | public String getExceptionMessage() { 60 | return exceptionMessage; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/NodeScanListener.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, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import com.serotonin.modbus4j.sero.util.ProgressiveTaskListener; 24 | 25 | /** 26 | *

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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | /** 24 | *

ProcessImageListener 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 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/SlaveProfile.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, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | /** 24 | * Class for maintaining the profile of a slave device on the master side. Initially, we assume that the device is fully 25 | * featured, and then we note function failures so that we know how requests should subsequently be sent. 26 | * 27 | * @author mlohbihler 28 | * @version 5.0.0 29 | */ 30 | public class SlaveProfile { 31 | private boolean writeMaskRegister = true; 32 | 33 | /** 34 | *

Setter for the field writeMaskRegister.

35 | * 36 | * @param writeMaskRegister a boolean. 37 | */ 38 | public void setWriteMaskRegister(boolean writeMaskRegister) { 39 | this.writeMaskRegister = writeMaskRegister; 40 | } 41 | 42 | /** 43 | *

Getter for the field writeMaskRegister.

44 | * 45 | * @return a boolean. 46 | */ 47 | public boolean getWriteMaskRegister() { 48 | return writeMaskRegister; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/IllegalDataAddressException.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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | /** 24 | *

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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | /** 24 | *

IllegalDataTypeException 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | /** 24 | *

IllegalFunctionException 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.

47 | * 48 | * @return a byte. 49 | */ 50 | public byte getFunctionCode() { 51 | return functionCode; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/IllegalSlaveIdException.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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | /** 24 | *

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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | /** 24 | *

InvalidDataConversionException 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | /** 24 | *

ModbusIdException 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | /** 24 | *

ModbusInitException 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class SlaveIdNotEqual extends ModbusTransportException { 24 | private static final long serialVersionUID = -1; 25 | 26 | /** 27 | * Exception to show that the requested slave id is not what was received 28 | * 29 | * @param requestSlaveId - slave id requested 30 | * @param responseSlaveId - slave id of response 31 | */ 32 | public SlaveIdNotEqual(int requestSlaveId, int responseSlaveId) { 33 | super("Response slave id different from requested id", requestSlaveId); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/IpMessage.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, see . 20 | */ 21 | package com.serotonin.modbus4j.ip; 22 | 23 | import com.serotonin.modbus4j.msg.ModbusMessage; 24 | 25 | /** 26 | *

Abstract 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.

45 | * 46 | * @return a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 47 | */ 48 | public ModbusMessage getModbusMessage() { 49 | return modbusMessage; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/IpMessageResponse.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, see . 20 | */ 21 | package com.serotonin.modbus4j.ip; 22 | 23 | import com.serotonin.modbus4j.msg.ModbusResponse; 24 | import com.serotonin.modbus4j.sero.messaging.IncomingResponseMessage; 25 | import com.serotonin.modbus4j.sero.messaging.OutgoingResponseMessage; 26 | 27 | /** 28 | *

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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.ip.IpMessage; 25 | import com.serotonin.modbus4j.msg.ModbusMessage; 26 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 27 | 28 | /** 29 | *

EncapMessage 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.modbus4j.base.BaseMessageParser; 24 | import com.serotonin.modbus4j.sero.messaging.IncomingMessage; 25 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 26 | 27 | /** 28 | *

EncapMessageParser 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.modbus4j.ModbusSlaveSet; 24 | import com.serotonin.modbus4j.base.BaseRequestHandler; 25 | import com.serotonin.modbus4j.msg.ModbusRequest; 26 | import com.serotonin.modbus4j.msg.ModbusResponse; 27 | import com.serotonin.modbus4j.sero.messaging.IncomingRequestMessage; 28 | import com.serotonin.modbus4j.sero.messaging.OutgoingResponseMessage; 29 | 30 | /** 31 | *

EncapRequestHandler 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.modbus4j.base.BaseMessageParser; 24 | import com.serotonin.modbus4j.sero.messaging.IncomingMessage; 25 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 26 | 27 | /** 28 | *

XaMessageParser 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.modbus4j.ModbusSlaveSet; 24 | import com.serotonin.modbus4j.base.BaseRequestHandler; 25 | import com.serotonin.modbus4j.msg.ModbusRequest; 26 | import com.serotonin.modbus4j.msg.ModbusResponse; 27 | import com.serotonin.modbus4j.sero.messaging.IncomingRequestMessage; 28 | import com.serotonin.modbus4j.sero.messaging.OutgoingResponseMessage; 29 | 30 | /** 31 | *

XaRequestHandler 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.exception.ModbusTransportException; 24 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 25 | 26 | /** 27 | *

ExceptionResponse 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | /** 27 | *

ReadCoilsResponse 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | /** 27 | *

ReadDiscreteInputsResponse 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 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadHoldingRegistersResponse.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, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | /** 27 | *

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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | /** 27 | *

ReadInputRegistersResponse 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 | -------------------------------------------------------------------------------- /src/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.

25 | * 26 | * @return a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 27 | */ 28 | public ModbusMessage getModbusMessage() { 29 | return modbusMessage; 30 | } 31 | 32 | /** {@inheritDoc} */ 33 | @Override 34 | public String toString() { 35 | return "SerialMessage [modbusMessage=" + modbusMessage + "]"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/serial/SerialPortWrapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2015 Infinite Automation Software. All rights reserved. 3 | * @author Terry Packer 4 | */ 5 | package com.serotonin.modbus4j.serial; 6 | 7 | import java.io.InputStream; 8 | import java.io.OutputStream; 9 | 10 | /** 11 | * Wrapper to further aid in abstracting Modbus4J from a serial port implementation 12 | * 13 | * @author Terry Packer 14 | * @version 5.0.0 15 | */ 16 | public interface SerialPortWrapper { 17 | 18 | /** 19 | * Close the Serial Port 20 | * 21 | * @throws java.lang.Exception if any. 22 | */ 23 | void close() throws Exception; 24 | 25 | /** 26 | *

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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.serial.ascii; 22 | 23 | import com.serotonin.modbus4j.base.BaseMessageParser; 24 | import com.serotonin.modbus4j.sero.messaging.IncomingMessage; 25 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 26 | 27 | /** 28 | *

AsciiMessageParser 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 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.serial.rtu; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.msg.ModbusMessage; 25 | import com.serotonin.modbus4j.serial.SerialMessage; 26 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 27 | 28 | /** 29 | * Convenience superclass primarily for calculating CRC values. 30 | * 31 | * @author mlohbihler 32 | * @version 5.0.0 33 | */ 34 | public class RtuMessage extends SerialMessage { 35 | /** 36 | *

Constructor for RtuMessage.

37 | * 38 | * @param modbusMessage a {@link com.serotonin.modbus4j.msg.ModbusMessage} object. 39 | */ 40 | public RtuMessage(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 queue = new ByteQueue(); 51 | 52 | // Write the particular message. 53 | modbusMessage.write(queue); 54 | 55 | // Write the CRC 56 | ModbusUtils.pushShort(queue, ModbusUtils.calculateCRC(modbusMessage)); 57 | 58 | // Return the data. 59 | return queue.popAll(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/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, see . 20 | */ 21 | package com.serotonin.modbus4j.serial.rtu; 22 | 23 | import com.serotonin.modbus4j.ModbusSlaveSet; 24 | import com.serotonin.modbus4j.base.BaseRequestHandler; 25 | import com.serotonin.modbus4j.msg.ModbusRequest; 26 | import com.serotonin.modbus4j.msg.ModbusResponse; 27 | import com.serotonin.modbus4j.sero.messaging.IncomingRequestMessage; 28 | import com.serotonin.modbus4j.sero.messaging.OutgoingResponseMessage; 29 | 30 | /** 31 | *

RtuRequestHandler 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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src/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 | -------------------------------------------------------------------------------- /src_cdc/Changes to Modbus4J for Java 1.4 compliance.txt: -------------------------------------------------------------------------------- 1 | NOTE: March 4 2014 the Serotonin classes need to be updated to reflect new changes. 2 | 3 | Contributed by Richard Jayawant 4 | 5 | 6 | Changes to Modbus4J source to make it compilable under Java 1.4 7 | --------------------------------------------------------------- 8 | 9 | Original sousrce code taken from modbus4j.cvs.sourceforge.net 10 | 11 | 12 | 13 | 14 | Annotations 15 | ----------- 16 | 17 | Removed all annotations outside of JavaDoc comments, mainly @Override and @SuppressWarnings 18 | 19 | 20 | Generics 21 | -------- 22 | 23 | Removed use of generics from all classes. Replaced generic return type with java.lang.Object type. 24 | 25 | 26 | 27 | Autoboxing 28 | ---------- 29 | 30 | Added cast expressions and new ObjType() wrapper expressions where neccesary to replace autoboxing behaviour not found in Java 1.4 31 | 32 | 33 | 34 | Enhanced for-statement 35 | ---------------------- 36 | 37 | Removed all instances of the enhanced for statement and replaced it with a conventional for statement and the use of an iterator object. -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/NotImplementedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.cdc; 6 | 7 | /** 8 | * @author Matthew Lohbihler 9 | */ 10 | public class NotImplementedException extends RuntimeException { 11 | static final long serialVersionUID = -1; 12 | 13 | public NotImplementedException() { 14 | super(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/ShouldNeverHappenException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.cdc; 6 | 7 | /** 8 | * @author Matthew Lohbihler 9 | */ 10 | public class ShouldNeverHappenException extends RuntimeException { 11 | private static final long serialVersionUID = -1; 12 | 13 | public ShouldNeverHappenException(String message) { 14 | super(message); 15 | } 16 | 17 | public ShouldNeverHappenException(Throwable cause) { 18 | super(cause); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/io/StreamUtils.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.io; 2 | 3 | public class StreamUtils { 4 | public static String dumpMessage(byte[] b) { 5 | return dumpMessage(b, 0, b.length); 6 | } 7 | 8 | public static String dumpMessage(byte[] b, int pos, int len) { 9 | StringBuilder sb = new StringBuilder(); 10 | sb.append('['); 11 | for (int i = pos; i < len; i++) { 12 | switch (b[i]) { 13 | case 2: 14 | sb.append("&STX;"); 15 | break; 16 | case 3: 17 | sb.append("&ETX;"); 18 | break; 19 | case 27: 20 | sb.append("&ESC;"); 21 | break; 22 | default: 23 | sb.append((char) b[i]); 24 | } 25 | } 26 | sb.append(']'); 27 | return sb.toString(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/io/serial/SerialParameters.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.io.serial; 2 | 3 | import gnu.io.SerialPort; 4 | 5 | /** 6 | * @author Matthew Lohbihler 7 | * 8 | */ 9 | public class SerialParameters { 10 | private String commPortId; 11 | private String portOwnerName; 12 | private int baudRate = -1; 13 | private int flowControlIn = SerialPort.FLOWCONTROL_NONE; 14 | private int flowControlOut = SerialPort.FLOWCONTROL_NONE; 15 | private int dataBits = SerialPort.DATABITS_8; 16 | private int stopBits = SerialPort.STOPBITS_1; 17 | private int parity = SerialPort.PARITY_NONE; 18 | 19 | public int getBaudRate() { 20 | return baudRate; 21 | } 22 | public void setBaudRate(int baudRate) { 23 | this.baudRate = baudRate; 24 | } 25 | public String getCommPortId() { 26 | return commPortId; 27 | } 28 | public void setCommPortId(String commPortId) { 29 | this.commPortId = commPortId; 30 | } 31 | public int getDataBits() { 32 | return dataBits; 33 | } 34 | public void setDataBits(int dataBits) { 35 | this.dataBits = dataBits; 36 | } 37 | public int getFlowControlIn() { 38 | return flowControlIn; 39 | } 40 | public void setFlowControlIn(int flowControlIn) { 41 | this.flowControlIn = flowControlIn; 42 | } 43 | public int getFlowControlOut() { 44 | return flowControlOut; 45 | } 46 | public void setFlowControlOut(int flowControlOut) { 47 | this.flowControlOut = flowControlOut; 48 | } 49 | public int getParity() { 50 | return parity; 51 | } 52 | public void setParity(int parity) { 53 | this.parity = parity; 54 | } 55 | public int getStopBits() { 56 | return stopBits; 57 | } 58 | public void setStopBits(int stopBits) { 59 | this.stopBits = stopBits; 60 | } 61 | public String getPortOwnerName() { 62 | return portOwnerName; 63 | } 64 | public void setPortOwnerName(String portOwnerName) { 65 | this.portOwnerName = portOwnerName; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/io/serial/SerialPortException.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.cdc.io.serial; 22 | 23 | /** 24 | * @author Matthew Lohbihler 25 | * 26 | */ 27 | public class SerialPortException extends Exception { 28 | private static final long serialVersionUID = -1; 29 | 30 | public SerialPortException(String message) { 31 | super(message); 32 | } 33 | 34 | public SerialPortException(Throwable cause) { 35 | super(cause); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/DataConsumer.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | import java.io.IOException; 4 | 5 | public interface DataConsumer { 6 | /** 7 | * Notifies the consumer that new data is available 8 | * 9 | * @param b 10 | * array of bytes representing the incoming information 11 | * @param len 12 | * length of the data 13 | */ 14 | public void data(byte[] b, int len); 15 | 16 | public void handleIOException(IOException e); 17 | } 18 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/DefaultMessagingExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public class DefaultMessagingExceptionHandler implements MessagingExceptionHandler { 4 | public void receivedException(Exception e) { 5 | e.printStackTrace(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/IncomingMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface IncomingMessage { 4 | // A marker interface 5 | } 6 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/IncomingRequestMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface IncomingRequestMessage extends IncomingMessage { 4 | // A marker interface. 5 | } 6 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/IncomingResponseMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface IncomingResponseMessage extends IncomingMessage { 4 | // A marker interface 5 | } 6 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/MessageParser.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | import com.serotonin.cdc.util.queue.ByteQueue; 4 | 5 | /** 6 | * Interface defining methods that are called when data arrives in the connection. 7 | * 8 | * @author Matthew Lohbihler 9 | */ 10 | public interface MessageParser { 11 | /** 12 | * Attempt to parse a message out of the queue. Data in the queue may be discarded if it is unusable (i.e. a start 13 | * indicator is not found), but otherwise if a message is not found due to the data being incomplete, the method 14 | * should return null. As additional data arrives, it will be appended to the queue and this method will be called 15 | * again. 16 | * 17 | * Implementations should not modify the queue unless it is safe to do so. No copy of the data is made before 18 | * calling this method. 19 | * 20 | * @param queue 21 | * the queue from which to access data for the creation of the message 22 | * @return the message if one was able to be created, or null otherwise. 23 | * @throws Exception 24 | * if the data in the queue is sufficient to construct a message, but the message data is invalid, this 25 | * method must throw an exception, or it will keep getting the same data. 26 | */ 27 | IncomingMessage parseMessage(ByteQueue queue) throws Exception; 28 | } 29 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/MessagingExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface MessagingExceptionHandler { 4 | public void receivedException(Exception e); 5 | } 6 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/OutgoingMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface OutgoingMessage { 4 | /** 5 | * Return the byte array representing the serialization of the request. 6 | * 7 | * @return byte array representing the serialization of the request 8 | */ 9 | byte[] getMessageData(); 10 | } 11 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/OutgoingRequestMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface OutgoingRequestMessage extends OutgoingMessage { 4 | /** 5 | * Whether the request is expecting a response or not. 6 | * 7 | * @return true if a response is expected, false otherwise. 8 | */ 9 | boolean expectsResponse(); 10 | } 11 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/OutgoingResponseMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface OutgoingResponseMessage extends OutgoingMessage { 4 | // A marker interface. 5 | } 6 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/RequestHandler.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface RequestHandler { 4 | /** 5 | * Handle the request and return the appropriate response object. 6 | * 7 | * @param request 8 | * the request to handle 9 | * @return the response object or null if no response is to be sent. null may also be returned if the request is 10 | * handled asynchronously. 11 | * @throws Exception 12 | */ 13 | OutgoingResponseMessage handleRequest(IncomingRequestMessage request) throws Exception; 14 | } 15 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/StreamTransport.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.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 | */ 16 | public class StreamTransport implements Transport, Runnable { 17 | protected OutputStream out; 18 | protected InputStream in; 19 | private InputStreamListener listener; 20 | 21 | public StreamTransport(InputStream in, OutputStream out) { 22 | this.out = out; 23 | this.in = in; 24 | } 25 | 26 | public void setReadDelay(int readDelay) { 27 | if (listener != null) 28 | listener.setReadDelay(readDelay); 29 | } 30 | 31 | public void start(String threadName) { 32 | listener.start(threadName); 33 | } 34 | 35 | public void stop() { 36 | listener.stop(); 37 | } 38 | 39 | public void run() { 40 | listener.run(); 41 | } 42 | 43 | public void setConsumer(DataConsumer consumer) { 44 | listener = new InputStreamListener(in, consumer); 45 | } 46 | 47 | public void removeConsumer() { 48 | listener.stop(); 49 | listener = null; 50 | } 51 | 52 | public void write(byte[] data) throws IOException { 53 | out.write(data); 54 | out.flush(); 55 | } 56 | 57 | public void write(byte[] data, int len) throws IOException { 58 | out.write(data, 0, len); 59 | out.flush(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/TestableTransport.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.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 | */ 13 | public class TestableTransport extends StreamTransport { 14 | public TestableTransport(InputStream in, OutputStream out) { 15 | super(new TestableBufferedInputStream(in), out); 16 | } 17 | 18 | public void testInputStream() throws IOException { 19 | ((TestableBufferedInputStream) in).test(); 20 | } 21 | 22 | static class TestableBufferedInputStream extends BufferedInputStream { 23 | public TestableBufferedInputStream(InputStream in) { 24 | super(in); 25 | } 26 | 27 | public synchronized int read(byte[] buf) throws IOException { 28 | return super.read(buf); 29 | } 30 | 31 | public synchronized void test() throws IOException { 32 | mark(1); 33 | int i = read(); 34 | if (i == -1) 35 | throw new IOException("Stream closed"); 36 | reset(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/TimeoutException.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | import java.io.IOException; 4 | 5 | public class TimeoutException extends IOException { 6 | private static final long serialVersionUID = 1L; 7 | 8 | public TimeoutException(String message) { 9 | super(message); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/Transport.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.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 | */ 11 | public interface Transport { 12 | abstract void setConsumer(DataConsumer consumer) throws IOException; 13 | 14 | abstract void removeConsumer(); 15 | 16 | abstract void write(byte[] data) throws IOException; 17 | 18 | abstract void write(byte[] data, int len) throws IOException; 19 | } 20 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/WaitingRoomException.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | import java.io.IOException; 4 | 5 | public class WaitingRoomException extends IOException { 6 | private static final long serialVersionUID = 1L; 7 | 8 | public WaitingRoomException(String message) { 9 | super(message); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/WaitingRoomKey.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.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 | */ 9 | public interface WaitingRoomKey { 10 | // Implementation needs to have hashcode and equals implementations. 11 | } 12 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/messaging/WaitingRoomKeyFactory.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.messaging; 2 | 3 | public interface WaitingRoomKeyFactory { 4 | WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request); 5 | 6 | WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response); 7 | } 8 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/BatchResults.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j; 22 | 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | public class BatchResults { 27 | private final Map data = new HashMap(); 28 | 29 | public void addResult(Object key, Object value) { 30 | data.put(key, value); 31 | } 32 | 33 | public Object getValue(Object key) { 34 | return data.get(key); 35 | } 36 | 37 | //Override 38 | public String toString() { 39 | return data.toString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/ExceptionResult.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j; 22 | 23 | import com.serotonin.cdc.modbus4j.code.ExceptionCode; 24 | 25 | /** 26 | * @author Matthew Lohbihler 27 | */ 28 | public class ExceptionResult { 29 | private final byte exceptionCode; 30 | private final String exceptionMessage; 31 | 32 | public ExceptionResult(byte exceptionCode) { 33 | this.exceptionCode = exceptionCode; 34 | exceptionMessage = ExceptionCode.getExceptionMessage(exceptionCode); 35 | } 36 | 37 | public byte getExceptionCode() { 38 | return exceptionCode; 39 | } 40 | 41 | public String getExceptionMessage() { 42 | return exceptionMessage; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/ModbusSlaveSet.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j; 22 | 23 | import java.util.Collection; 24 | import java.util.LinkedHashMap; 25 | 26 | import com.serotonin.cdc.modbus4j.exception.ModbusInitException; 27 | 28 | abstract public class ModbusSlaveSet extends Modbus { 29 | protected LinkedHashMap processImages = new LinkedHashMap(); 30 | 31 | public void addProcessImage(ProcessImage processImage) { 32 | processImages.put(new Integer(processImage.getSlaveId()), processImage); 33 | } 34 | 35 | public ProcessImage getProcessImage(int slaveId) { 36 | return (ProcessImage) processImages.get(new Integer(slaveId)); 37 | } 38 | 39 | public Collection getProcessImages() { 40 | return processImages.values(); 41 | } 42 | 43 | /** 44 | * Starts the slave. If an exception is not thrown, this method does not return, but uses the thread to execute the 45 | * listening. 46 | * 47 | * @throws ModbusInitException 48 | */ 49 | abstract public void start() throws ModbusInitException; 50 | 51 | abstract public void stop(); 52 | } 53 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/NodeScanListener.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j; 22 | 23 | import com.serotonin.cdc.util.ProgressiveTaskListener; 24 | 25 | /** 26 | * @author Matthew Lohbihler 27 | */ 28 | public interface NodeScanListener extends ProgressiveTaskListener { 29 | void nodeFound(int nodeNumber); 30 | } 31 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j; 22 | 23 | public interface ProcessImageListener { 24 | public void coilWrite(int offset, boolean oldValue, boolean newValue); 25 | 26 | public void holdingRegisterWrite(int offset, short oldValue, short newValue); 27 | } 28 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.base; 22 | 23 | import com.serotonin.cdc.messaging.IncomingMessage; 24 | import com.serotonin.cdc.messaging.MessageParser; 25 | import com.serotonin.cdc.util.queue.ByteQueue; 26 | 27 | abstract public class BaseMessageParser implements MessageParser { 28 | protected final boolean master; 29 | 30 | public BaseMessageParser(boolean master) { 31 | this.master = master; 32 | } 33 | 34 | //Override 35 | public IncomingMessage parseMessage(ByteQueue queue) throws Exception { 36 | try { 37 | return parseMessageImpl(queue); 38 | } 39 | catch (ArrayIndexOutOfBoundsException e) { 40 | // Means that we ran out of data trying to read the message. Just return null. 41 | return null; 42 | } 43 | } 44 | 45 | abstract protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception; 46 | } 47 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/base/SlaveAndRange.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.base; 22 | 23 | public class SlaveAndRange { 24 | private final int slaveId; 25 | private final int range; 26 | 27 | public SlaveAndRange(int slaveId, int range) { 28 | ModbusUtils.validateSlaveId(slaveId, true); 29 | 30 | this.slaveId = slaveId; 31 | this.range = range; 32 | } 33 | 34 | public int getRange() { 35 | return range; 36 | } 37 | 38 | public int getSlaveId() { 39 | return slaveId; 40 | } 41 | 42 | //Override 43 | public int hashCode() { 44 | final int prime = 31; 45 | int result = 1; 46 | result = prime * result + range; 47 | result = prime * result + slaveId; 48 | return result; 49 | } 50 | 51 | //Override 52 | public boolean equals(Object obj) { 53 | if (this == obj) 54 | return true; 55 | if (obj == null) 56 | return false; 57 | if (getClass() != obj.getClass()) 58 | return false; 59 | final SlaveAndRange other = (SlaveAndRange) obj; 60 | if (range != other.range) 61 | return false; 62 | if (slaveId != other.slaveId) 63 | return false; 64 | return true; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/base/SlaveProfile.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.base; 22 | 23 | /** 24 | * Class for maintaining the profile of a slave device on the master side. Initially, we assume that the device is fully 25 | * featured, and then we note function failures so that we know how requests should subsequently be sent. 26 | * 27 | * @author mlohbihler 28 | */ 29 | public class SlaveProfile { 30 | private boolean writeMaskRegister = true; 31 | 32 | public void setWriteMaskRegister(boolean writeMaskRegister) { 33 | this.writeMaskRegister = writeMaskRegister; 34 | } 35 | 36 | public boolean getWriteMaskRegister() { 37 | return writeMaskRegister; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/code/FunctionCode.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.code; 22 | 23 | /** 24 | * @author Matthew Lohbihler 25 | */ 26 | public class FunctionCode { 27 | public static final byte READ_COILS = 1; 28 | public static final byte READ_DISCRETE_INPUTS = 2; 29 | public static final byte READ_HOLDING_REGISTERS = 3; 30 | public static final byte READ_INPUT_REGISTERS = 4; 31 | public static final byte WRITE_COIL = 5; 32 | public static final byte WRITE_REGISTER = 6; 33 | public static final byte READ_EXCEPTION_STATUS = 7; 34 | public static final byte WRITE_COILS = 15; 35 | public static final byte WRITE_REGISTERS = 16; 36 | public static final byte REPORT_SLAVE_ID = 17; 37 | public static final byte WRITE_MASK_REGISTER = 22; 38 | 39 | public static String toString(byte code) { 40 | return Integer.toString(code & 0xff); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | import com.serotonin.cdc.modbus4j.msg.ModbusRequest; 24 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 25 | 26 | public class ErrorResponseException extends Exception { 27 | private static final long serialVersionUID = -1; 28 | 29 | private final ModbusRequest originalRequest; 30 | private final ModbusResponse errorResponse; 31 | 32 | public ErrorResponseException(ModbusRequest originalRequest, ModbusResponse errorResponse) { 33 | this.originalRequest = originalRequest; 34 | this.errorResponse = errorResponse; 35 | } 36 | 37 | public ModbusResponse getErrorResponse() { 38 | return errorResponse; 39 | } 40 | 41 | public ModbusRequest getOriginalRequest() { 42 | return originalRequest; 43 | } 44 | 45 | //Override 46 | public String getMessage() { 47 | return errorResponse.getExceptionMessage(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/exception/IllegalDataAddressException.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class IllegalDataAddressException extends ModbusTransportException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public IllegalDataAddressException() { 27 | super(); 28 | } 29 | 30 | public IllegalDataAddressException(int slaveId) { 31 | super(slaveId); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class IllegalDataTypeException extends ModbusIdException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public IllegalDataTypeException(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class IllegalFunctionException extends ModbusTransportException { 24 | private static final long serialVersionUID = -1; 25 | 26 | private final byte functionCode; 27 | 28 | public IllegalFunctionException(byte functionCode, int slaveId) { 29 | super("Function code: 0x" + Integer.toHexString(functionCode & 0xff), slaveId); 30 | this.functionCode = functionCode; 31 | } 32 | 33 | public byte getFunctionCode() { 34 | return functionCode; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/exception/IllegalSlaveIdException.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class IllegalSlaveIdException extends ModbusIdException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public IllegalSlaveIdException(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class InvalidDataConversionException extends RuntimeException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public InvalidDataConversionException(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class ModbusIdException extends RuntimeException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public ModbusIdException(String message) { 27 | super(message); 28 | } 29 | 30 | public ModbusIdException(Throwable cause) { 31 | super(cause); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class ModbusInitException extends Exception { 24 | private static final long serialVersionUID = -1; 25 | 26 | public ModbusInitException() { 27 | super(); 28 | } 29 | 30 | public ModbusInitException(String message, Throwable cause) { 31 | super(message, cause); 32 | } 33 | 34 | public ModbusInitException(String message) { 35 | super(message); 36 | } 37 | 38 | public ModbusInitException(Throwable cause) { 39 | super(cause); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/exception/ModbusTransportException.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.exception; 22 | 23 | public class ModbusTransportException extends Exception { 24 | private static final long serialVersionUID = -1; 25 | 26 | private final int slaveId; 27 | 28 | public ModbusTransportException() { 29 | this.slaveId = -1; 30 | } 31 | 32 | public ModbusTransportException(int slaveId) { 33 | this.slaveId = slaveId; 34 | } 35 | 36 | public ModbusTransportException(String message, Throwable cause, int slaveId) { 37 | super(message, cause); 38 | this.slaveId = slaveId; 39 | } 40 | 41 | public ModbusTransportException(String message, int slaveId) { 42 | super(message); 43 | this.slaveId = slaveId; 44 | } 45 | 46 | public ModbusTransportException(String message) { 47 | super(message); 48 | this.slaveId = -1; 49 | } 50 | 51 | public ModbusTransportException(Throwable cause) { 52 | super(cause); 53 | this.slaveId = -1; 54 | } 55 | 56 | public ModbusTransportException(Throwable cause, int slaveId) { 57 | super(cause); 58 | this.slaveId = slaveId; 59 | } 60 | 61 | public int getSlaveId() { 62 | return slaveId; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/ip/IpMessage.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip; 22 | 23 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 24 | 25 | abstract public class IpMessage { 26 | protected final ModbusMessage modbusMessage; 27 | 28 | public IpMessage(ModbusMessage modbusMessage) { 29 | this.modbusMessage = modbusMessage; 30 | } 31 | 32 | public ModbusMessage getModbusMessage() { 33 | return modbusMessage; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/ip/IpMessageResponse.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip; 22 | 23 | import com.serotonin.cdc.messaging.IncomingResponseMessage; 24 | import com.serotonin.cdc.messaging.OutgoingResponseMessage; 25 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 26 | 27 | public interface IpMessageResponse extends OutgoingResponseMessage, IncomingResponseMessage { 28 | ModbusResponse getModbusResponse(); 29 | } 30 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/ip/IpParameters.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip; 22 | 23 | import com.serotonin.cdc.modbus4j.base.ModbusUtils; 24 | 25 | public class IpParameters { 26 | private String host; 27 | private int port = ModbusUtils.TCP_PORT; 28 | private boolean encapsulated; 29 | 30 | public String getHost() { 31 | return host; 32 | } 33 | 34 | public void setHost(String host) { 35 | this.host = host; 36 | } 37 | 38 | public int getPort() { 39 | return port; 40 | } 41 | 42 | public void setPort(int port) { 43 | this.port = port; 44 | } 45 | 46 | public boolean isEncapsulated() { 47 | return encapsulated; 48 | } 49 | 50 | public void setEncapsulated(boolean encapsulated) { 51 | this.encapsulated = encapsulated; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip.encap; 22 | 23 | import com.serotonin.cdc.modbus4j.base.ModbusUtils; 24 | import com.serotonin.cdc.modbus4j.ip.IpMessage; 25 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 26 | import com.serotonin.cdc.util.queue.ByteQueue; 27 | 28 | public class EncapMessage extends IpMessage { 29 | public EncapMessage(ModbusMessage modbusMessage) { 30 | super(modbusMessage); 31 | } 32 | 33 | public byte[] getMessageData() { 34 | ByteQueue msgQueue = new ByteQueue(); 35 | 36 | // Write the particular message. 37 | modbusMessage.write(msgQueue); 38 | 39 | // Write the CRC 40 | ModbusUtils.pushShort(msgQueue, ModbusUtils.calculateCRC(modbusMessage)); 41 | 42 | // Return the data. 43 | return msgQueue.popAll(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip.encap; 22 | 23 | import com.serotonin.cdc.messaging.IncomingMessage; 24 | import com.serotonin.cdc.modbus4j.base.BaseMessageParser; 25 | import com.serotonin.cdc.util.queue.ByteQueue; 26 | 27 | public class EncapMessageParser extends BaseMessageParser { 28 | public EncapMessageParser(boolean master) { 29 | super(master); 30 | } 31 | 32 | //Override 33 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 34 | if (master) 35 | return EncapMessageResponse.createEncapMessageResponse(queue); 36 | return EncapMessageRequest.createEncapMessageRequest(queue); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip.encap; 22 | 23 | import com.serotonin.cdc.modbus4j.base.ModbusUtils; 24 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 25 | import com.serotonin.cdc.modbus4j.ip.IpMessageResponse; 26 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 27 | import com.serotonin.cdc.util.queue.ByteQueue; 28 | 29 | public class EncapMessageResponse extends EncapMessage implements IpMessageResponse { 30 | static EncapMessageResponse createEncapMessageResponse(ByteQueue queue) throws ModbusTransportException { 31 | // Create the modbus response. 32 | ModbusResponse response = ModbusResponse.createModbusResponse(queue); 33 | EncapMessageResponse encapResponse = new EncapMessageResponse(response); 34 | 35 | // Check the CRC 36 | ModbusUtils.checkCRC(encapResponse.modbusMessage, queue); 37 | 38 | return encapResponse; 39 | } 40 | 41 | public EncapMessageResponse(ModbusResponse modbusResponse) { 42 | super(modbusResponse); 43 | } 44 | 45 | public ModbusResponse getModbusResponse() { 46 | return (ModbusResponse) modbusMessage; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip.encap; 22 | 23 | import com.serotonin.cdc.messaging.IncomingRequestMessage; 24 | import com.serotonin.cdc.messaging.OutgoingResponseMessage; 25 | import com.serotonin.cdc.modbus4j.ModbusSlaveSet; 26 | import com.serotonin.cdc.modbus4j.base.BaseRequestHandler; 27 | import com.serotonin.cdc.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 29 | 30 | public class EncapRequestHandler extends BaseRequestHandler { 31 | public EncapRequestHandler(ModbusSlaveSet slave) { 32 | super(slave); 33 | } 34 | 35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 36 | EncapMessageRequest tcpRequest = (EncapMessageRequest) req; 37 | ModbusRequest request = tcpRequest.getModbusRequest(); 38 | ModbusResponse response = handleRequestImpl(request); 39 | if (response == null) 40 | return null; 41 | return new EncapMessageResponse(response); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/ip/encap/EncapWaitingRoomKeyFactory.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.modbus4j.ip.encap; 2 | 3 | import com.serotonin.cdc.messaging.IncomingResponseMessage; 4 | import com.serotonin.cdc.messaging.OutgoingRequestMessage; 5 | import com.serotonin.cdc.messaging.WaitingRoomKey; 6 | import com.serotonin.cdc.messaging.WaitingRoomKeyFactory; 7 | import com.serotonin.cdc.modbus4j.ip.IpMessage; 8 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 9 | 10 | public class EncapWaitingRoomKeyFactory implements WaitingRoomKeyFactory { 11 | //Override 12 | public WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request) { 13 | return createWaitingRoomKey(((IpMessage) request).getModbusMessage()); 14 | } 15 | 16 | //Override 17 | public WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response) { 18 | return createWaitingRoomKey(((IpMessage) response).getModbusMessage()); 19 | } 20 | 21 | public WaitingRoomKey createWaitingRoomKey(ModbusMessage msg) { 22 | return new EncapWaitingRoomKey(msg.getSlaveId(), msg.getFunctionCode()); 23 | } 24 | 25 | class EncapWaitingRoomKey implements WaitingRoomKey { 26 | private final int slaveId; 27 | private final byte functionCode; 28 | 29 | public EncapWaitingRoomKey(int slaveId, byte functionCode) { 30 | this.slaveId = slaveId; 31 | this.functionCode = functionCode; 32 | } 33 | 34 | //Override 35 | public int hashCode() { 36 | final int prime = 31; 37 | int result = 1; 38 | result = prime * result + functionCode; 39 | result = prime * result + slaveId; 40 | return result; 41 | } 42 | 43 | //Override 44 | public boolean equals(Object obj) { 45 | if (this == obj) 46 | return true; 47 | if (obj == null) 48 | return false; 49 | if (getClass() != obj.getClass()) 50 | return false; 51 | EncapWaitingRoomKey other = (EncapWaitingRoomKey) obj; 52 | if (functionCode != other.functionCode) 53 | return false; 54 | if (slaveId != other.slaveId) 55 | return false; 56 | return true; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/ip/xa/XaMessage.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip.xa; 22 | 23 | import com.serotonin.cdc.modbus4j.base.ModbusUtils; 24 | import com.serotonin.cdc.modbus4j.ip.IpMessage; 25 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 26 | import com.serotonin.cdc.util.queue.ByteQueue; 27 | 28 | public class XaMessage extends IpMessage { 29 | protected final int transactionId; 30 | 31 | public XaMessage(ModbusMessage modbusMessage, int transactionId) { 32 | super(modbusMessage); 33 | this.transactionId = transactionId; 34 | } 35 | 36 | public byte[] getMessageData() { 37 | ByteQueue msgQueue = new ByteQueue(); 38 | 39 | // Write the particular message. 40 | modbusMessage.write(msgQueue); 41 | 42 | // Create the XA message 43 | ByteQueue xaQueue = new ByteQueue(); 44 | ModbusUtils.pushShort(xaQueue, transactionId); 45 | ModbusUtils.pushShort(xaQueue, ModbusUtils.IP_PROTOCOL_ID); 46 | ModbusUtils.pushShort(xaQueue, msgQueue.size()); 47 | xaQueue.push(msgQueue); 48 | 49 | // Return the data. 50 | return xaQueue.popAll(); 51 | } 52 | 53 | public int getTransactionId() { 54 | return transactionId; 55 | } 56 | 57 | public ModbusMessage getModbusMessage() { 58 | return modbusMessage; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip.xa; 22 | 23 | import com.serotonin.cdc.messaging.IncomingMessage; 24 | import com.serotonin.cdc.modbus4j.base.BaseMessageParser; 25 | import com.serotonin.cdc.util.queue.ByteQueue; 26 | 27 | public class XaMessageParser extends BaseMessageParser { 28 | public XaMessageParser(boolean master) { 29 | super(master); 30 | } 31 | 32 | //Override 33 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 34 | if (master) 35 | return XaMessageResponse.createXaMessageResponse(queue); 36 | return XaMessageRequest.createXaMessageRequest(queue); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.ip.xa; 22 | 23 | import com.serotonin.cdc.messaging.IncomingRequestMessage; 24 | import com.serotonin.cdc.messaging.OutgoingResponseMessage; 25 | import com.serotonin.cdc.modbus4j.ModbusSlaveSet; 26 | import com.serotonin.cdc.modbus4j.base.BaseRequestHandler; 27 | import com.serotonin.cdc.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 29 | 30 | public class XaRequestHandler extends BaseRequestHandler { 31 | public XaRequestHandler(ModbusSlaveSet slave) { 32 | super(slave); 33 | } 34 | 35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 36 | XaMessageRequest tcpRequest = (XaMessageRequest) req; 37 | ModbusRequest request = tcpRequest.getModbusRequest(); 38 | ModbusResponse response = handleRequestImpl(request); 39 | if (response == null) 40 | return null; 41 | return new XaMessageResponse(response, tcpRequest.transactionId); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 24 | import com.serotonin.cdc.util.queue.ByteQueue; 25 | 26 | /** 27 | * @author Matthew Lohbihler 28 | */ 29 | public class ExceptionResponse extends ModbusResponse { 30 | private final byte functionCode; 31 | 32 | public ExceptionResponse(int slaveId, byte functionCode, byte exceptionCode) throws ModbusTransportException { 33 | super(slaveId); 34 | this.functionCode = functionCode; 35 | setException(exceptionCode); 36 | } 37 | 38 | //Override 39 | public byte getFunctionCode() { 40 | return functionCode; 41 | } 42 | 43 | //Override 44 | protected void readResponse(ByteQueue queue) { 45 | // no op 46 | } 47 | 48 | //Override 49 | protected void writeResponse(ByteQueue queue) { 50 | // no op 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/msg/ReadCoilsRequest.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.ProcessImage; 24 | import com.serotonin.cdc.modbus4j.code.FunctionCode; 25 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 26 | 27 | public class ReadCoilsRequest extends ReadBinaryRequest { 28 | public ReadCoilsRequest(int slaveId, int startOffset, int numberOfBits) throws ModbusTransportException { 29 | super(slaveId, startOffset, numberOfBits); 30 | } 31 | 32 | ReadCoilsRequest(int slaveId) throws ModbusTransportException { 33 | super(slaveId); 34 | } 35 | 36 | //Override 37 | public byte getFunctionCode() { 38 | return FunctionCode.READ_COILS; 39 | } 40 | 41 | //Override 42 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 43 | return new ReadCoilsResponse(slaveId, getData(processImage)); 44 | } 45 | 46 | //Override 47 | protected boolean getBinary(ProcessImage processImage, int index) throws ModbusTransportException { 48 | return processImage.getCoil(index); 49 | } 50 | 51 | //Override 52 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 53 | return new ReadCoilsResponse(slaveId); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.code.FunctionCode; 24 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadCoilsResponse extends ReadResponse { 27 | ReadCoilsResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadCoilsResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | //Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_COILS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.ProcessImage; 24 | import com.serotonin.cdc.modbus4j.code.FunctionCode; 25 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 26 | 27 | public class ReadDiscreteInputsRequest extends ReadBinaryRequest { 28 | public ReadDiscreteInputsRequest(int slaveId, int startOffset, int numberOfBits) throws ModbusTransportException { 29 | super(slaveId, startOffset, numberOfBits); 30 | } 31 | 32 | ReadDiscreteInputsRequest(int slaveId) throws ModbusTransportException { 33 | super(slaveId); 34 | } 35 | 36 | //Override 37 | public byte getFunctionCode() { 38 | return FunctionCode.READ_DISCRETE_INPUTS; 39 | } 40 | 41 | //Override 42 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 43 | return new ReadDiscreteInputsResponse(slaveId, getData(processImage)); 44 | } 45 | 46 | //Override 47 | protected boolean getBinary(ProcessImage processImage, int index) throws ModbusTransportException { 48 | return processImage.getInput(index); 49 | } 50 | 51 | //Override 52 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 53 | return new ReadDiscreteInputsResponse(slaveId); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.code.FunctionCode; 24 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadDiscreteInputsResponse extends ReadResponse { 27 | ReadDiscreteInputsResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadDiscreteInputsResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | //Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_DISCRETE_INPUTS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.code.FunctionCode; 24 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 25 | import com.serotonin.cdc.util.queue.ByteQueue; 26 | 27 | public class ReadExceptionStatusResponse extends ModbusResponse { 28 | private byte exceptionStatus; 29 | 30 | ReadExceptionStatusResponse(int slaveId) throws ModbusTransportException { 31 | super(slaveId); 32 | } 33 | 34 | ReadExceptionStatusResponse(int slaveId, byte exceptionStatus) throws ModbusTransportException { 35 | super(slaveId); 36 | this.exceptionStatus = exceptionStatus; 37 | } 38 | 39 | //Override 40 | public byte getFunctionCode() { 41 | return FunctionCode.READ_EXCEPTION_STATUS; 42 | } 43 | 44 | //Override 45 | protected void readResponse(ByteQueue queue) { 46 | exceptionStatus = queue.pop(); 47 | } 48 | 49 | //Override 50 | protected void writeResponse(ByteQueue queue) { 51 | queue.push(exceptionStatus); 52 | } 53 | 54 | public byte getExceptionStatus() { 55 | return exceptionStatus; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/msg/ReadHoldingRegistersResponse.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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.code.FunctionCode; 24 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadHoldingRegistersResponse extends ReadResponse { 27 | ReadHoldingRegistersResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadHoldingRegistersResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | //Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_HOLDING_REGISTERS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.msg; 22 | 23 | import com.serotonin.cdc.modbus4j.code.FunctionCode; 24 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadInputRegistersResponse extends ReadResponse { 27 | ReadInputRegistersResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadInputRegistersResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | //Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_INPUT_REGISTERS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/modbus4j/serial/SerialMessage.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.modbus4j.serial; 2 | 3 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 4 | 5 | abstract public class SerialMessage { 6 | protected final ModbusMessage modbusMessage; 7 | 8 | public SerialMessage(ModbusMessage modbusMessage) { 9 | this.modbusMessage = modbusMessage; 10 | } 11 | 12 | public ModbusMessage getModbusMessage() { 13 | return modbusMessage; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial; 22 | 23 | import gnu.io.SerialPort; 24 | 25 | import com.serotonin.cdc.io.serial.SerialParameters; 26 | import com.serotonin.cdc.io.serial.SerialUtils; 27 | import com.serotonin.cdc.messaging.StreamTransport; 28 | import com.serotonin.cdc.modbus4j.ModbusSlaveSet; 29 | import com.serotonin.cdc.modbus4j.exception.ModbusInitException; 30 | 31 | abstract public class SerialSlave extends ModbusSlaveSet { 32 | // Configuration fields. 33 | private final SerialParameters serialParameters; 34 | 35 | // Runtime fields 36 | private SerialPort serialPort; 37 | protected StreamTransport transport; 38 | 39 | public SerialSlave(SerialParameters params) { 40 | serialParameters = params; 41 | } 42 | 43 | //Override 44 | public void start() throws ModbusInitException { 45 | try { 46 | serialPort = SerialUtils.openSerialPort(serialParameters); 47 | transport = new StreamTransport(serialPort.getInputStream(), serialPort.getOutputStream()); 48 | } 49 | catch (Exception e) { 50 | throw new ModbusInitException(e); 51 | } 52 | } 53 | 54 | //Override 55 | public void stop() { 56 | SerialUtils.close(serialPort); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.ascii; 22 | 23 | import com.serotonin.cdc.messaging.IncomingMessage; 24 | import com.serotonin.cdc.modbus4j.base.BaseMessageParser; 25 | import com.serotonin.cdc.util.queue.ByteQueue; 26 | 27 | public class AsciiMessageParser extends BaseMessageParser { 28 | public AsciiMessageParser(boolean master) { 29 | super(master); 30 | } 31 | 32 | //Override 33 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 34 | if (master) 35 | return AsciiMessageResponse.createAsciiMessageResponse(queue); 36 | return AsciiMessageRequest.createAsciiMessageRequest(queue); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.ascii; 22 | 23 | import com.serotonin.cdc.messaging.IncomingRequestMessage; 24 | import com.serotonin.cdc.messaging.OutgoingRequestMessage; 25 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 27 | import com.serotonin.cdc.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.cdc.util.queue.ByteQueue; 29 | 30 | public class AsciiMessageRequest extends AsciiMessage implements OutgoingRequestMessage, IncomingRequestMessage { 31 | static AsciiMessageRequest createAsciiMessageRequest(ByteQueue queue) throws ModbusTransportException { 32 | ByteQueue msgQueue = getUnasciiMessage(queue); 33 | ModbusRequest request = ModbusRequest.createModbusRequest(msgQueue); 34 | AsciiMessageRequest asciiRequest = new AsciiMessageRequest(request); 35 | 36 | // Return the data. 37 | return asciiRequest; 38 | } 39 | 40 | public AsciiMessageRequest(ModbusMessage modbusMessage) { 41 | super(modbusMessage); 42 | } 43 | 44 | //Override 45 | public boolean expectsResponse() { 46 | return modbusMessage.getSlaveId() != 0; 47 | } 48 | 49 | public ModbusRequest getModbusRequest() { 50 | return (ModbusRequest) modbusMessage; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.ascii; 22 | 23 | import com.serotonin.cdc.messaging.IncomingResponseMessage; 24 | import com.serotonin.cdc.messaging.OutgoingResponseMessage; 25 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 27 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 28 | import com.serotonin.cdc.util.queue.ByteQueue; 29 | 30 | public class AsciiMessageResponse extends AsciiMessage implements OutgoingResponseMessage, IncomingResponseMessage { 31 | static AsciiMessageResponse createAsciiMessageResponse(ByteQueue queue) throws ModbusTransportException { 32 | ByteQueue msgQueue = getUnasciiMessage(queue); 33 | ModbusResponse response = ModbusResponse.createModbusResponse(msgQueue); 34 | AsciiMessageResponse asciiResponse = new AsciiMessageResponse(response); 35 | 36 | // Return the data. 37 | return asciiResponse; 38 | } 39 | 40 | public AsciiMessageResponse(ModbusMessage modbusMessage) { 41 | super(modbusMessage); 42 | } 43 | 44 | public ModbusResponse getModbusResponse() { 45 | return (ModbusResponse) modbusMessage; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.ascii; 22 | 23 | import com.serotonin.cdc.messaging.IncomingRequestMessage; 24 | import com.serotonin.cdc.messaging.OutgoingResponseMessage; 25 | import com.serotonin.cdc.modbus4j.ModbusSlaveSet; 26 | import com.serotonin.cdc.modbus4j.base.BaseRequestHandler; 27 | import com.serotonin.cdc.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 29 | 30 | public class AsciiRequestHandler extends BaseRequestHandler { 31 | public AsciiRequestHandler(ModbusSlaveSet slave) { 32 | super(slave); 33 | } 34 | 35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 36 | AsciiMessageRequest asciiRequest = (AsciiMessageRequest) req; 37 | ModbusRequest request = asciiRequest.getModbusRequest(); 38 | ModbusResponse response = handleRequestImpl(request); 39 | if (response == null) 40 | return null; 41 | return new AsciiMessageResponse(response); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.ascii; 22 | 23 | import java.io.IOException; 24 | 25 | import com.serotonin.cdc.io.serial.SerialParameters; 26 | import com.serotonin.cdc.messaging.MessageControl; 27 | import com.serotonin.cdc.modbus4j.exception.ModbusInitException; 28 | import com.serotonin.cdc.modbus4j.serial.SerialSlave; 29 | 30 | public class AsciiSlave extends SerialSlave { 31 | private MessageControl conn; 32 | 33 | public AsciiSlave(SerialParameters params) { 34 | super(params); 35 | } 36 | 37 | //Override 38 | public void start() throws ModbusInitException { 39 | super.start(); 40 | 41 | AsciiMessageParser asciiMessageParser = new AsciiMessageParser(false); 42 | AsciiRequestHandler asciiRequestHandler = new AsciiRequestHandler(this); 43 | 44 | conn = new MessageControl(); 45 | conn.setExceptionHandler(getExceptionHandler()); 46 | 47 | try { 48 | conn.start(transport, asciiMessageParser, asciiRequestHandler, null); 49 | transport.start("Modbus ASCII slave"); 50 | } 51 | catch (IOException e) { 52 | throw new ModbusInitException(e); 53 | } 54 | } 55 | 56 | //Override 57 | public void stop() { 58 | conn.close(); 59 | super.stop(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.rtu; 22 | 23 | import com.serotonin.cdc.modbus4j.base.ModbusUtils; 24 | import com.serotonin.cdc.modbus4j.msg.ModbusMessage; 25 | import com.serotonin.cdc.modbus4j.serial.SerialMessage; 26 | import com.serotonin.cdc.util.queue.ByteQueue; 27 | 28 | /** 29 | * Convenience superclass primarily for calculating CRC values. 30 | * 31 | * @author mlohbihler 32 | */ 33 | public class RtuMessage extends SerialMessage { 34 | public RtuMessage(ModbusMessage modbusMessage) { 35 | super(modbusMessage); 36 | } 37 | 38 | public byte[] getMessageData() { 39 | ByteQueue queue = new ByteQueue(); 40 | 41 | // Write the particular message. 42 | modbusMessage.write(queue); 43 | 44 | // Write the CRC 45 | ModbusUtils.pushShort(queue, ModbusUtils.calculateCRC(modbusMessage)); 46 | 47 | // Return the data. 48 | return queue.popAll(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.rtu; 22 | 23 | import com.serotonin.cdc.messaging.IncomingMessage; 24 | import com.serotonin.cdc.modbus4j.base.BaseMessageParser; 25 | import com.serotonin.cdc.util.queue.ByteQueue; 26 | 27 | /** 28 | * Message parser implementation for RTU encoding. Primary reference for the ordering of CRC bytes. Also provides 29 | * handling of incomplete messages. 30 | * 31 | * @author mlohbihler 32 | */ 33 | public class RtuMessageParser extends BaseMessageParser { 34 | public RtuMessageParser(boolean master) { 35 | super(master); 36 | } 37 | 38 | //Override 39 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 40 | if (master) 41 | return RtuMessageResponse.createRtuMessageResponse(queue); 42 | return RtuMessageRequest.createRtuMessageRequest(queue); 43 | } 44 | // 45 | // public static void main(String[] args) throws Exception { 46 | // ByteQueue queue = new ByteQueue(new byte[] { 5, 3, 2, 0, (byte) 0xdc, (byte) 0x48, (byte) 0x1d, 0 }); 47 | // RtuMessageParser p = new RtuMessageParser(false); 48 | // System.out.println(p.parseResponse(queue)); 49 | // } 50 | } 51 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.rtu; 22 | 23 | import com.serotonin.cdc.messaging.IncomingResponseMessage; 24 | import com.serotonin.cdc.messaging.OutgoingResponseMessage; 25 | import com.serotonin.cdc.modbus4j.base.ModbusUtils; 26 | import com.serotonin.cdc.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 28 | import com.serotonin.cdc.util.queue.ByteQueue; 29 | 30 | /** 31 | * Handles the RTU enveloping of modbus responses. 32 | * 33 | * @author mlohbihler 34 | */ 35 | public class RtuMessageResponse extends RtuMessage implements OutgoingResponseMessage, IncomingResponseMessage { 36 | static RtuMessageResponse createRtuMessageResponse(ByteQueue queue) throws ModbusTransportException { 37 | ModbusResponse response = ModbusResponse.createModbusResponse(queue); 38 | RtuMessageResponse rtuResponse = new RtuMessageResponse(response); 39 | 40 | // Check the CRC 41 | ModbusUtils.checkCRC(rtuResponse.modbusMessage, queue); 42 | 43 | // Return the data. 44 | return rtuResponse; 45 | } 46 | 47 | public RtuMessageResponse(ModbusResponse modbusResponse) { 48 | super(modbusResponse); 49 | } 50 | 51 | public ModbusResponse getModbusResponse() { 52 | return (ModbusResponse) modbusMessage; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.rtu; 22 | 23 | import com.serotonin.cdc.messaging.IncomingRequestMessage; 24 | import com.serotonin.cdc.messaging.OutgoingResponseMessage; 25 | import com.serotonin.cdc.modbus4j.ModbusSlaveSet; 26 | import com.serotonin.cdc.modbus4j.base.BaseRequestHandler; 27 | import com.serotonin.cdc.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.cdc.modbus4j.msg.ModbusResponse; 29 | 30 | public class RtuRequestHandler extends BaseRequestHandler { 31 | public RtuRequestHandler(ModbusSlaveSet slave) { 32 | super(slave); 33 | } 34 | 35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 36 | RtuMessageRequest rtuRequest = (RtuMessageRequest) req; 37 | ModbusRequest request = rtuRequest.getModbusRequest(); 38 | ModbusResponse response = handleRequestImpl(request); 39 | if (response == null) 40 | return null; 41 | return new RtuMessageResponse(response); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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, see . 20 | */ 21 | package com.serotonin.cdc.modbus4j.serial.rtu; 22 | 23 | import java.io.IOException; 24 | 25 | import com.serotonin.cdc.io.serial.SerialParameters; 26 | import com.serotonin.cdc.messaging.MessageControl; 27 | import com.serotonin.cdc.modbus4j.exception.ModbusInitException; 28 | import com.serotonin.cdc.modbus4j.serial.SerialSlave; 29 | 30 | public class RtuSlave extends SerialSlave { 31 | // Runtime fields 32 | private MessageControl conn; 33 | 34 | public RtuSlave(SerialParameters params) { 35 | super(params); 36 | } 37 | 38 | //Override 39 | public void start() throws ModbusInitException { 40 | super.start(); 41 | 42 | RtuMessageParser rtuMessageParser = new RtuMessageParser(false); 43 | RtuRequestHandler rtuRequestHandler = new RtuRequestHandler(this); 44 | 45 | conn = new MessageControl(); 46 | conn.setExceptionHandler(getExceptionHandler()); 47 | 48 | try { 49 | conn.start(transport, rtuMessageParser, rtuRequestHandler, null); 50 | transport.start("Modbus RTU slave"); 51 | } 52 | catch (IOException e) { 53 | throw new ModbusInitException(e); 54 | } 55 | } 56 | 57 | //Override 58 | public void stop() { 59 | conn.close(); 60 | super.stop(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/util/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.util; 2 | 3 | public class ArrayUtils { 4 | public static String toHexString(byte[] bytes) { 5 | return toHexString(bytes, 0, bytes.length); 6 | } 7 | 8 | public static String toHexString(byte[] bytes, int start, int len) { 9 | if (len == 0) 10 | return "[]"; 11 | 12 | StringBuffer sb = new StringBuffer(); 13 | sb.append('['); 14 | sb.append(Integer.toHexString(bytes[start] & 0xff)); 15 | for (int i = 1; i < len; i++) 16 | sb.append(',').append(Integer.toHexString(bytes[start + i] & 0xff)); 17 | sb.append("]"); 18 | 19 | return sb.toString(); 20 | } 21 | 22 | public static boolean contains(int[] values, int value) { 23 | if (values == null) 24 | return false; 25 | 26 | for (int i = 0; i < values.length; i++) { 27 | if (values[i] == value) 28 | return true; 29 | } 30 | 31 | return false; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/util/ProgressiveTask.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.util; 2 | 3 | /** 4 | * @author Matthew Lohbihler 5 | */ 6 | abstract public class ProgressiveTask implements Runnable { 7 | private boolean cancelled = false; 8 | protected boolean completed = false; 9 | private ProgressiveTaskListener listener; 10 | 11 | public ProgressiveTask() { 12 | // no op 13 | } 14 | 15 | public ProgressiveTask(ProgressiveTaskListener l) { 16 | listener = l; 17 | } 18 | 19 | public void cancel() { 20 | cancelled = true; 21 | } 22 | 23 | public boolean isCancelled() { 24 | return cancelled; 25 | } 26 | 27 | public boolean isCompleted() { 28 | return completed; 29 | } 30 | 31 | public final void run() { 32 | while (true) { 33 | if (isCancelled()) { 34 | declareFinished(true); 35 | break; 36 | } 37 | 38 | runImpl(); 39 | 40 | if (isCompleted()) { 41 | declareFinished(false); 42 | break; 43 | } 44 | } 45 | completed = true; 46 | } 47 | 48 | protected void declareProgress(float progress) { 49 | ProgressiveTaskListener l = listener; 50 | if (l != null) 51 | l.progressUpdate(progress); 52 | } 53 | 54 | private void declareFinished(boolean cancelled) { 55 | ProgressiveTaskListener l = listener; 56 | if (l != null) { 57 | if (cancelled) 58 | l.taskCancelled(); 59 | else 60 | l.taskCompleted(); 61 | } 62 | } 63 | 64 | /** 65 | * Implementers of this method MUST return from it occasionally so that the cancelled status can be checked. Each 66 | * return must leave the class and thread state with the expectation that runImpl will not be called again, while 67 | * acknowledging the possibility that it will. 68 | * 69 | * Implementations SHOULD call the declareProgress method with each runImpl execution such that the listener can be 70 | * notified. 71 | * 72 | * Implementations MUST set the completed field to true when the task is finished. 73 | */ 74 | abstract protected void runImpl(); 75 | } 76 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/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.cdc.util; 22 | 23 | /** 24 | * @author Matthew Lohbihler 25 | * 26 | */ 27 | public interface ProgressiveTaskListener { 28 | /** 29 | * Optionally called occasionally by the task to declare the progress that has been made. 30 | * @param progress float between 0 and 1 where 0 is no progress and 1 is completed. 31 | */ 32 | void progressUpdate(float progress); 33 | 34 | /** 35 | * Notification that the task has been cancelled. Should only be called once for the task. 36 | */ 37 | void taskCancelled(); 38 | 39 | /** 40 | * Notification that the task has been completed. Should only be called once for the task. 41 | */ 42 | void taskCompleted(); 43 | } 44 | -------------------------------------------------------------------------------- /src_cdc/com/serotonin/cdc/util/StringUtils.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.cdc.util; 2 | 3 | public class StringUtils { 4 | public static boolean equals(Object o1, Object o2) { 5 | if (o1 == null && o2 == null) 6 | return true; 7 | if (o1 == null) 8 | return false; 9 | if (o2 == null) 10 | return false; 11 | return o1.equals(o2); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src_cdc/gnu/io/RXTXHack.java: -------------------------------------------------------------------------------- 1 | package gnu.io; 2 | 3 | public class RXTXHack { 4 | private RXTXHack() { 5 | // Singleton 6 | } 7 | 8 | public static void closeRxtxPort(RXTXPort port) { 9 | try { 10 | port.IOLocked = 0; 11 | } 12 | catch (IllegalAccessError e) { 13 | System.out.println(e.getMessage()); 14 | } 15 | port.close(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src_test/com/serotonin/modbus4j/test/AsciiDecodingTest.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.test; 2 | 3 | import com.serotonin.modbus4j.serial.ascii.AsciiMessageParser; 4 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 5 | 6 | public class AsciiDecodingTest { 7 | public static void main(String[] args) throws Exception { 8 | decodeRequest(":010100000002FC\r\n"); 9 | decodeResponse(":01010101FC\r\n"); 10 | decodeRequest(":010300000008F4\r\n"); 11 | decodeResponse(":010310009100000000000000000000000000005B\r\n"); 12 | } 13 | 14 | public static void decodeRequest(String s) throws Exception { 15 | ByteQueue queue = new ByteQueue(toBytes(s)); 16 | new AsciiMessageParser(false).parseMessage(queue); 17 | } 18 | 19 | public static void decodeResponse(String s) throws Exception { 20 | ByteQueue queue = new ByteQueue(toBytes(s)); 21 | new AsciiMessageParser(true).parseMessage(queue); 22 | } 23 | 24 | public static byte[] toBytes(String s) throws Exception { 25 | return s.getBytes("ASCII"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src_test/com/serotonin/modbus4j/test/DecodingTest.java: -------------------------------------------------------------------------------- 1 | package com.serotonin.modbus4j.test; 2 | 3 | import com.serotonin.modbus4j.serial.rtu.RtuMessageParser; 4 | import com.serotonin.modbus4j.sero.util.queue.ByteQueue; 5 | 6 | public class DecodingTest { 7 | public static void main(String[] args) throws Exception { 8 | // decodeRequest("0A0300770001356B"); 9 | // decodeResponse("0A030200001D85"); 10 | // decodeRequest("0A0600770001F96B"); 11 | 12 | // decodeRequest("011000a80002043535353530f4"); 13 | // decodeResponse("011000a80002042993"); 14 | decodeRequest("010300010001D5CA"); 15 | } 16 | 17 | public static void decodeRequest(String s) throws Exception { 18 | ByteQueue queue = new ByteQueue(toBytes(s)); 19 | new RtuMessageParser(false).parseMessage(queue); 20 | } 21 | 22 | public static void decodeResponse(String s) throws Exception { 23 | ByteQueue queue = new ByteQueue(toBytes(s)); 24 | new RtuMessageParser(true).parseMessage(queue); 25 | } 26 | 27 | public static byte[] toBytes(String s) { 28 | if (s.startsWith("[")) 29 | s = s.substring(1); 30 | if (s.endsWith("]")) 31 | s = s.substring(0, s.length() - 1); 32 | String[] parts = s.split(","); 33 | if (parts.length == 1) 34 | parts = s.split("\\|"); 35 | if (parts.length == 1) 36 | parts = s.split(" "); 37 | if (parts.length == 1) { 38 | parts = new String[s.length() / 2]; 39 | for (int i = 0; i < parts.length; i++) 40 | parts[i] = s.substring(i * 2, i * 2 + 2); 41 | } 42 | 43 | byte[] bytes = new byte[parts.length]; 44 | 45 | for (int i = 0; i < bytes.length; i++) 46 | bytes[i] = (byte) Integer.parseInt(parts[i].trim(), 16); 47 | 48 | return bytes; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src_test/com/serotonin/modbus4j/test/ReadSerialTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.modbus4j.test; 6 | 7 | import com.serotonin.modbus4j.ModbusFactory; 8 | import com.serotonin.modbus4j.ModbusMaster; 9 | 10 | /** 11 | * @author Matthew Lohbihler 12 | */ 13 | public class ReadSerialTest { 14 | public static void main(String[] args) throws Exception { 15 | 16 | String commPortId = "COM1"; 17 | int baudRate = 9600; 18 | int flowControlIn = 0; 19 | int flowControlOut = 0; 20 | int dataBits = 8; 21 | int stopBits = 2; 22 | int parity = 0; 23 | 24 | TestSerialPortWrapper wrapper = new TestSerialPortWrapper(commPortId, baudRate, flowControlIn, flowControlOut, dataBits, stopBits, parity); 25 | ModbusMaster master = new ModbusFactory().createRtuMaster(wrapper); 26 | master.setTimeout(200); 27 | master.setRetries(1); 28 | master.init(); 29 | 30 | for (int i = 1; i < 5; i++) { 31 | long start = System.currentTimeMillis(); 32 | System.out.print("Testing " + i + "... "); 33 | System.out.println(master.testSlaveNode(i)); 34 | System.out.println("Time: " + (System.currentTimeMillis() - start)); 35 | } 36 | 37 | // try { 38 | // System.out.println(master.send(new ReadHoldingRegistersRequest(1, 0, 1))); 39 | // } 40 | // catch (Exception e) { 41 | // e.printStackTrace(); 42 | // } 43 | 44 | // try { 45 | // // ReadCoilsRequest request = new ReadCoilsRequest(2, 65534, 1); 46 | // ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master 47 | // .send(new ReadHoldingRegistersRequest(2, 0, 1)); 48 | // System.out.println(response); 49 | // } 50 | // catch (Exception e) { 51 | // e.printStackTrace(); 52 | // } 53 | 54 | // System.out.println(master.scanForSlaveNodes()); 55 | 56 | master.destroy(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src_test/com/serotonin/modbus4j/test/Test2.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.modbus4j.test; 6 | 7 | import com.serotonin.modbus4j.ModbusFactory; 8 | import com.serotonin.modbus4j.ModbusMaster; 9 | import com.serotonin.modbus4j.code.DataType; 10 | import com.serotonin.modbus4j.ip.IpParameters; 11 | import com.serotonin.modbus4j.locator.BaseLocator; 12 | 13 | /** 14 | * @author Matthew Lohbihler 15 | */ 16 | public class Test2 { 17 | public static void main(String[] args) throws Exception { 18 | IpParameters params = new IpParameters(); 19 | params.setHost("localhost"); 20 | params.setPort(502); 21 | 22 | ModbusMaster master = new ModbusFactory().createTcpMaster(params, false); 23 | master.init(); 24 | 25 | System.out.println(master.testSlaveNode(5)); 26 | 27 | // Define the point locator. 28 | BaseLocator loc = BaseLocator.holdingRegister(1, 0, DataType.TWO_BYTE_INT_UNSIGNED); 29 | 30 | // Set the point value 31 | master.setValue(loc, 1800); 32 | 33 | // Get the point value 34 | System.out.println(master.getValue(loc)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src_test/com/serotonin/modbus4j/test/Test3.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc. 3 | @author Matthew Lohbihler 4 | */ 5 | package com.serotonin.modbus4j.test; 6 | 7 | import com.serotonin.modbus4j.ModbusFactory; 8 | import com.serotonin.modbus4j.ModbusMaster; 9 | import com.serotonin.modbus4j.code.DataType; 10 | import com.serotonin.modbus4j.locator.BaseLocator; 11 | 12 | /** 13 | * @author Matthew Lohbihler 14 | */ 15 | public class Test3 { 16 | public static void main(String[] args) throws Exception { 17 | String commPortId = "COM1"; 18 | int baudRate = 9600; 19 | int flowControlIn = 0; 20 | int flowControlOut = 0; 21 | int dataBits = 8; 22 | int stopBits = 2; 23 | int parity = 0; 24 | 25 | TestSerialPortWrapper wrapper = new TestSerialPortWrapper(commPortId, baudRate, flowControlIn, flowControlOut, dataBits, stopBits, parity); 26 | 27 | ModbusMaster master = new ModbusFactory().createRtuMaster(wrapper); 28 | master.init(); 29 | 30 | System.out.println(master.testSlaveNode(5)); 31 | 32 | // Define the point locator. 33 | BaseLocator loc = BaseLocator.holdingRegister(1, 0, DataType.TWO_BYTE_INT_UNSIGNED); 34 | 35 | // Set the point value 36 | master.setValue(loc, 1800); 37 | 38 | // Get the point value 39 | System.out.println(master.getValue(loc)); 40 | } 41 | } 42 | --------------------------------------------------------------------------------