├── .gitignore ├── LICENSE ├── README ├── build.copyright ├── build.number ├── build.properties ├── build.version ├── build.xml ├── lib ├── RXTXcomm.jar ├── androidthings-1.0.jar ├── jssc-2.8.0.jar └── purejavacomm.jar ├── pom.xml └── src └── com └── intelligt └── modbus ├── examples ├── ExampleRTU.java ├── ExampleRTUOverTCP.java ├── ExampleTCP.java ├── ModbusTest.java ├── SimpleMasterRTU.java ├── SimpleMasterTCP.java └── SimpleSlaveTCP.java └── jlibmodbus ├── Modbus.java ├── Version.java ├── data ├── CommStatus.java ├── DataHolder.java ├── DataHolderBuilder.java ├── ExceptionStatus.java ├── FifoQueue.java ├── ModbusCoils.java ├── ModbusFile.java ├── ModbusHoldingRegisters.java ├── ModbusValues.java ├── SimpleDataHolderBuilder.java ├── SimpleExceptionStatus.java ├── SimpleFifoQueue.java ├── SimpleModbusFile.java ├── SimpleSlaveId.java ├── SlaveId.java ├── comm │ ├── ModbusCommEvent.java │ ├── ModbusCommEventEnterListenOnlyMode.java │ ├── ModbusCommEventInitiatedCommunicationRestart.java │ ├── ModbusCommEventReceive.java │ └── ModbusCommEventSend.java └── mei │ └── ReadDeviceIdentificationInterface.java ├── exception ├── AcknowledgeException.java ├── GatewayPathUnavailableException.java ├── GatewayTargetDeviceFailedToRespondException.java ├── IllegalDataAddressException.java ├── IllegalDataValueException.java ├── IllegalFunctionException.java ├── IllegalRangeException.java ├── MemoryParityErrorException.java ├── ModbusChecksumException.java ├── ModbusIOException.java ├── ModbusMasterException.java ├── ModbusNumberException.java ├── ModbusProtocolException.java ├── ModbusSlaveException.java ├── SlaveDeviceBusyException.java └── SlaveDeviceFailureException.java ├── master ├── ModbusMaster.java ├── ModbusMasterASCII.java ├── ModbusMasterFactory.java ├── ModbusMasterRTU.java ├── ModbusMasterSerial.java └── ModbusMasterTCP.java ├── msg ├── ModbusMessageFactory.java ├── ModbusRequestBuilder.java ├── ModbusRequestFactory.java ├── ModbusResponseFactory.java ├── base │ ├── AbstractDataRequest.java │ ├── AbstractMultipleRequest.java │ ├── AbstractReadResponse.java │ ├── AbstractWriteMultipleRequest.java │ ├── AbstractWriteResponse.java │ ├── ModbusFileRecord.java │ ├── ModbusMessage.java │ ├── ModbusRequest.java │ ├── ModbusResponse.java │ └── mei │ │ ├── CANopenGeneralReferencePDU.java │ │ ├── ConformityLevel.java │ │ ├── MEIFactory.java │ │ ├── MEIReadDeviceIdentification.java │ │ ├── ModbusEncapsulatedInterface.java │ │ └── ReadDeviceIdentificationCode.java ├── request │ ├── DiagnosticsRequest.java │ ├── EncapsulatedInterfaceTransportRequest.java │ ├── GetCommEventCounterRequest.java │ ├── GetCommEventLogRequest.java │ ├── IllegalFunctionRequest.java │ ├── MaskWriteRegisterRequest.java │ ├── ReadCoilsRequest.java │ ├── ReadDeviceIdentificationRequest.java │ ├── ReadDiscreteInputsRequest.java │ ├── ReadExceptionStatusRequest.java │ ├── ReadFifoQueueRequest.java │ ├── ReadFileRecordRequest.java │ ├── ReadHoldingRegistersRequest.java │ ├── ReadInputRegistersRequest.java │ ├── ReadWriteMultipleRegistersRequest.java │ ├── ReportSlaveIdRequest.java │ ├── WriteFileRecordRequest.java │ ├── WriteMultipleCoilsRequest.java │ ├── WriteMultipleRegistersRequest.java │ ├── WriteSingleCoilRequest.java │ └── WriteSingleRegisterRequest.java └── response │ ├── BroadcastResponse.java │ ├── DiagnosticsResponse.java │ ├── EncapsulatedInterfaceTransportResponse.java │ ├── GetCommEventCounterResponse.java │ ├── GetCommEventLogResponse.java │ ├── IllegalFunctionResponse.java │ ├── MaskWriteRegisterResponse.java │ ├── ReadCoilsResponse.java │ ├── ReadDiscreteInputsResponse.java │ ├── ReadExceptionStatusResponse.java │ ├── ReadFifoQueueResponse.java │ ├── ReadFileRecordResponse.java │ ├── ReadHoldingRegistersResponse.java │ ├── ReadInputRegistersResponse.java │ ├── ReadWriteMultipleRegistersResponse.java │ ├── ReportSlaveIdResponse.java │ ├── WriteFileRecordResponse.java │ ├── WriteMultipleCoilsResponse.java │ ├── WriteMultipleRegistersResponse.java │ ├── WriteSingleCoilResponse.java │ └── WriteSingleRegisterResponse.java ├── net ├── ModbusConnection.java ├── ModbusConnectionASCII.java ├── ModbusConnectionFactory.java ├── ModbusConnectionRTU.java ├── ModbusConnectionSerial.java ├── ModbusMasterConnectionTCP.java ├── ModbusSlaveConnectionTCP.java ├── stream │ ├── InputStreamASCII.java │ ├── InputStreamRTU.java │ ├── InputStreamSerial.java │ ├── InputStreamTCP.java │ ├── OutputStreamASCII.java │ ├── OutputStreamRTU.java │ ├── OutputStreamSerial.java │ ├── OutputStreamTCP.java │ └── base │ │ ├── LoggingInputStream.java │ │ ├── LoggingOutputStream.java │ │ ├── ModbusInputStream.java │ │ └── ModbusOutputStream.java └── transport │ ├── ModbusTransport.java │ ├── ModbusTransportASCII.java │ ├── ModbusTransportFactory.java │ ├── ModbusTransportRTU.java │ ├── ModbusTransportSerial.java │ └── ModbusTransportTCP.java ├── serial ├── SerialParameters.java ├── SerialPort.java ├── SerialPortAT.java ├── SerialPortAbstractFactory.java ├── SerialPortException.java ├── SerialPortFactoryAT.java ├── SerialPortFactoryJSSC.java ├── SerialPortFactoryJSerialComm.java ├── SerialPortFactoryLoopback.java ├── SerialPortFactoryPJC.java ├── SerialPortFactoryRXTX.java ├── SerialPortFactoryTcpClient.java ├── SerialPortFactoryTcpServer.java ├── SerialPortJSSC.java ├── SerialPortJSerialComm.java ├── SerialPortLoopback.java ├── SerialPortPJC.java ├── SerialPortRXTX.java ├── SerialUtils.java └── ValidatorSerialPortFactory.java ├── slave ├── ModbusSlave.java ├── ModbusSlaveASCII.java ├── ModbusSlaveFactory.java ├── ModbusSlaveRTU.java ├── ModbusSlaveSerial.java ├── ModbusSlaveTCP.java ├── RequestHandler.java ├── RequestHandlerSerial.java └── RequestHandlerTCP.java ├── tcp ├── TcpAduHeader.java └── TcpParameters.java └── utils ├── ByteFifo.java ├── CRC16.java ├── DataUtils.java ├── DiagnosticsSubFunctionCode.java ├── FrameEvent.java ├── FrameEventListener.java ├── FrameEventListenerList.java ├── FrameEventListenerListImpl.java ├── LRC.java ├── MEITypeCode.java ├── ModbusExceptionCode.java ├── ModbusFunctionCode.java ├── ModbusSlaveSerialObserver.java ├── ModbusSlaveTcpObserver.java ├── SerialPortInfo.java └── TcpClientInfo.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse 2 | .classpath 3 | .project 4 | .settings/ 5 | 6 | #Apache ant 7 | build/ 8 | dist/ 9 | 10 | # Built application files and Maven 11 | target/ 12 | pom.xml.tag 13 | pom.xml.releaseBackup 14 | pom.xml.versionsBackup 15 | pom.xml.next 16 | release.properties 17 | dependency-reduced-pom.xml 18 | buildNumber.properties 19 | .mvn/timing.properties 20 | 21 | # Compiled class files 22 | *.class 23 | 24 | # Log Files 25 | *.log 26 | 27 | # About IntelliJ 28 | *.iml 29 | *.iws 30 | *.ids 31 | *.ipr 32 | *.db 33 | .idea/ 34 | idea/ 35 | out/ 36 | 37 | # BlueJ files 38 | *.ctxt 39 | 40 | # Mobile Tools for Java (J2ME) 41 | .mtj.tmp/ 42 | 43 | # macOS 44 | .DS_Store 45 | 46 | # Package Files 47 | *.jar 48 | *.war 49 | *.ear 50 | *.zip 51 | *.tar.gz 52 | *.rar 53 | 54 | # CMake 55 | cmake-build-debug/ 56 | 57 | # File-based project format 58 | *.iws 59 | 60 | # mpeltonen/sbt-idea plugin 61 | .idea_modules/ 62 | 63 | # JIRA plugin 64 | atlassian-ide-plugin.xml 65 | 66 | # Crashlytics plugin (for Android Studio and IntelliJ) 67 | com_crashlytics_export_strings.xml 68 | crashlytics.properties 69 | crashlytics-build.properties 70 | fabric.properties 71 | 72 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 73 | hs_err_pid* -------------------------------------------------------------------------------- /build.copyright: -------------------------------------------------------------------------------- 1 | build.copyright=Copyright (c) 2015-2016 JSC Invertor -------------------------------------------------------------------------------- /build.number: -------------------------------------------------------------------------------- 1 | #Build Number for ANT. Do not edit! 2 | #Tue Mar 16 15:39:35 YEKT 2024 3 | build.number=11 4 | -------------------------------------------------------------------------------- /build.properties: -------------------------------------------------------------------------------- 1 | build.deprecation=on 2 | build.optimize=on 3 | build.debug=true 4 | build.debuglevel=lines,vars 5 | -------------------------------------------------------------------------------- /build.version: -------------------------------------------------------------------------------- 1 | build.version=1.2.9 -------------------------------------------------------------------------------- /lib/RXTXcomm.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kochedykov/jlibmodbus/619478713ddd996fb61c8e2a47b83833c8755242/lib/RXTXcomm.jar -------------------------------------------------------------------------------- /lib/androidthings-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kochedykov/jlibmodbus/619478713ddd996fb61c8e2a47b83833c8755242/lib/androidthings-1.0.jar -------------------------------------------------------------------------------- /lib/jssc-2.8.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kochedykov/jlibmodbus/619478713ddd996fb61c8e2a47b83833c8755242/lib/jssc-2.8.0.jar -------------------------------------------------------------------------------- /lib/purejavacomm.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kochedykov/jlibmodbus/619478713ddd996fb61c8e2a47b83833c8755242/lib/purejavacomm.jar -------------------------------------------------------------------------------- /src/com/intelligt/modbus/jlibmodbus/Version.java: -------------------------------------------------------------------------------- 1 | package com.intelligt.modbus.jlibmodbus; 2 | /* 3 | * Copyright (C) 2016 "Invertor" Factory", JSC 4 | * [http://www.sbp-invertor.ru] 5 | * 6 | * This file is part of JLibModbus. 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | * 20 | * Authors: Vladislav Y. Kochedykov, software engineer. 21 | * email: vladislav.kochedykov@gmail.com 22 | */ 23 | /** 24 | * A class to keep the current version in. 25 | * It's an auto generated class, which used by 26 | * Modbus.getVersion(). 27 | * 28 | * @since 1.2.3 29 | */ 30 | class Version { 31 | 32 | private static final String version = "1.2.9.11"; 33 | /** 34 | * returns the current version of the JLibModbus library 35 | * 36 | * @return a string representing the version 37 | */ 38 | public static String getVersion() { 39 | return version; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/intelligt/modbus/jlibmodbus/data/DataHolderBuilder.java: -------------------------------------------------------------------------------- 1 | package com.intelligt.modbus.jlibmodbus.data; 2 | 3 | /* 4 | * Copyright (C) 2016 "Invertor" Factory", JSC 5 | * [http://www.sbp-invertor.ru] 6 | * 7 | * This file is part of JLibModbus. 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * Authors: Vladislav Y. Kochedykov, software engineer. 22 | * email: vladislav.kochedykov@gmail.com 23 | */ 24 | abstract public class DataHolderBuilder { 25 | protected DataHolder dataHolder; 26 | 27 | protected DataHolderBuilder() { 28 | createNewDataHolder(); 29 | } 30 | 31 | public DataHolder getDataHolder() { 32 | return dataHolder; 33 | } 34 | 35 | final public void createNewDataHolder() { 36 | dataHolder = new DataHolder(); 37 | } 38 | 39 | public abstract void buildCoils(); 40 | 41 | public abstract void buildDiscreteInputs(); 42 | 43 | public abstract void buildHoldingRegisters(); 44 | 45 | public abstract void buildInputRegisters(); 46 | 47 | public abstract void buildSlaveId(); 48 | 49 | public abstract void buildExceptionStatus(); 50 | 51 | public abstract void buildFifoQueue(); 52 | 53 | public abstract void readDeviceIdentificationInterface(); 54 | 55 | final public DataHolder build() { 56 | createNewDataHolder(); 57 | buildCoils(); 58 | buildDiscreteInputs(); 59 | buildHoldingRegisters(); 60 | buildInputRegisters(); 61 | buildSlaveId(); 62 | buildExceptionStatus(); 63 | buildFifoQueue(); 64 | readDeviceIdentificationInterface(); 65 | return getDataHolder(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/com/intelligt/modbus/jlibmodbus/data/ExceptionStatus.java: -------------------------------------------------------------------------------- 1 | package com.intelligt.modbus.jlibmodbus.data; 2 | 3 | /* 4 | * Copyright (C) 2016 "Invertor" Factory", JSC 5 | * [http://www.sbp-invertor.ru] 6 | * 7 | * This file is part of JLibModbus. 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * Authors: Vladislav Y. Kochedykov, software engineer. 22 | * email: vladislav.kochedykov@gmail.com 23 | */ 24 | public interface ExceptionStatus { 25 | int get(); 26 | 27 | void set(int exceptionStatus); 28 | } 29 | -------------------------------------------------------------------------------- /src/com/intelligt/modbus/jlibmodbus/data/FifoQueue.java: -------------------------------------------------------------------------------- 1 | package com.intelligt.modbus.jlibmodbus.data; 2 | 3 | import com.intelligt.modbus.jlibmodbus.exception.IllegalDataValueException; 4 | 5 | /* 6 | * Copyright (C) 2016 "Invertor" Factory", JSC 7 | * [http://www.sbp-invertor.ru] 8 | * 9 | * This file is part of JLibModbus. 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | * 23 | * Authors: Vladislav Y. Kochedykov, software engineer. 24 | * email: vladislav.kochedykov@gmail.com 25 | */ 26 | abstract public class FifoQueue { 27 | 28 | final private int capacity; 29 | 30 | public FifoQueue(int capacity) { 31 | this.capacity = capacity; 32 | } 33 | 34 | public abstract int size(); 35 | 36 | abstract protected int[] peekImpl(); 37 | 38 | abstract protected void addImpl(int register); 39 | 40 | abstract protected void pollImpl(); 41 | 42 | synchronized final public void poll() { 43 | if (size() != 0) 44 | pollImpl(); 45 | } 46 | 47 | synchronized final public void add(int register) { 48 | if (size() < capacity) 49 | addImpl(register); 50 | } 51 | 52 | synchronized final public int[] get() throws IllegalDataValueException { 53 | if (size() > 31 || size() == 0) { 54 | throw new IllegalDataValueException(); 55 | } 56 | return peekImpl(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/com/intelligt/modbus/jlibmodbus/data/ModbusFile.java: -------------------------------------------------------------------------------- 1 | package com.intelligt.modbus.jlibmodbus.data; 2 | 3 | /* 4 | * Copyright (C) 2016 "Invertor" Factory", JSC 5 | * [http://www.sbp-invertor.ru] 6 | * 7 | * This file is part of JLibModbus. 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * Authors: Vladislav Y. Kochedykov, software engineer. 22 | * email: vladislav.kochedykov@gmail.com 23 | */ 24 | 25 | import com.intelligt.modbus.jlibmodbus.exception.IllegalDataAddressException; 26 | import com.intelligt.modbus.jlibmodbus.exception.IllegalDataValueException; 27 | 28 | import java.util.Observable; 29 | 30 | /** 31 | * quote from MODBUS Application Protocol Specification V1.1b 32 | *
33 | * "A file is an organization of records. Each file contains 10000 records, addressed 0000 to 34 | * 9999 decimal or 0X0000 to 0X270F. For example, record 12 is addressed as 12. 35 | * ... 36 | * The quantity of registers to be read, combined with all other fields in the expected response, 37 | * must not exceed the allowable length of the MODBUS PDU : 253 bytes." 38 | *
39 | * so the length of file record must not exceed 250 bytes(253 - function_code - resp_data_len - sub_req_resp_len).
40 | */
41 | abstract public class ModbusFile extends Observable {
42 | private final int number;
43 |
44 | public ModbusFile(int number) {
45 | this.number = number;
46 | }
47 |
48 | /**
49 | * read modbus file record
50 | *
51 | * @param recordNumber number of a record
52 | * @param recordLength read register count
53 | * @return record data
54 | * @throws IllegalDataAddressException record with number recordNumber not exist or recordLength bytes not allowable.
55 | */
56 | abstract public int[] read(int recordNumber, int recordLength) throws IllegalDataAddressException;
57 |
58 | public void write(int recordNumber, int[] buffer) throws IllegalDataAddressException, IllegalDataValueException {
59 | notifyObservers(new int[]{recordNumber, buffer.length});
60 | }
61 |
62 | public int getNumber() {
63 | return number;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/data/ModbusValues.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.data;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.IllegalDataAddressException;
5 | import com.intelligt.modbus.jlibmodbus.exception.IllegalDataValueException;
6 |
7 | import java.util.ConcurrentModificationException;
8 | import java.util.Iterator;
9 | import java.util.NoSuchElementException;
10 | import java.util.Observable;
11 |
12 | abstract public class ModbusValues
30 | * "Specialized use in conjunction with programming
31 | * commands.
32 | * The server (or slave) has accepted the request
33 | * and is processing it, but a long duration of time
34 | * will be required to do so. This response is
35 | * returned to prevent a timeout error from occurring
36 | * in the client (or master). The client (or master)
37 | * can next issue a Poll Program Complete message
38 | * to determine if processing is completed."
39 | */
40 | public class AcknowledgeException extends ModbusProtocolException {
41 | public AcknowledgeException() {
42 | super(ModbusExceptionCode.ACKNOWLEDGE);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/GatewayPathUnavailableException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "Specialized use in conjunction with gateways,
31 | * indicates that the gateway was unable to allocate
32 | * an internal communication path from the input
33 | * port to the output port for processing the request.
34 | * Usually means that the gateway is misconfigured
35 | * or overloaded."
36 | */
37 | public class GatewayPathUnavailableException extends ModbusProtocolException {
38 | public GatewayPathUnavailableException() {
39 | super(ModbusExceptionCode.GATEWAY_PATH_UNAVAILABLE);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/GatewayTargetDeviceFailedToRespondException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "Specialized use in conjunction with gateways,
31 | * indicates that no response was obtained from the
32 | * target device. Usually means that the device is
33 | * not present on the network."
34 | */
35 | public class GatewayTargetDeviceFailedToRespondException extends ModbusProtocolException {
36 | public GatewayTargetDeviceFailedToRespondException() {
37 | super(ModbusExceptionCode.GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/IllegalDataAddressException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "The data address received in the query is not an
31 | * allowable address for the server (or slave). More
32 | * specifically, the combination of reference number
33 | * and transfer length is invalid. For a controller with
34 | * 100 registers, the PDU addresses the first
35 | * register as 0, and the last one as 99. If a request
36 | * is submitted with a starting register address of 96
37 | * and a quantity of registers of 4, then this request
38 | * will successfully operate (address-wise at least)
39 | * on registers 96, 97, 98, 99. If a request is
40 | * submitted with a starting register address of 96
41 | * and a quantity of registers of 5, then this request
42 | * will fail with Exception Code 0x02 “Illegal Data
43 | * Address” since it attempts to operate on registers
44 | * 96, 97, 98, 99 and 100, and there is no register
45 | * with address 100."
46 | */
47 | public class IllegalDataAddressException extends ModbusProtocolException {
48 | final private int dataAddress;
49 |
50 | public IllegalDataAddressException(int dataAddress) {
51 | super(ModbusExceptionCode.ILLEGAL_DATA_ADDRESS);
52 | this.dataAddress = dataAddress;
53 | }
54 |
55 | public int getDataAddress() {
56 | return dataAddress;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/IllegalDataValueException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "A value contained in the query data field is not an
31 | * allowable value for server (or slave). This
32 | * indicates a fault in the structure of the remainder
33 | * of a complex request, such as that the implied
34 | * length is incorrect. It specifically does NOT mean
35 | * that a data item submitted for storage in a register
36 | * has a value outside the expectation of the
37 | * application program, since the MODBUS protocol
38 | * is unaware of the significance of any particular
39 | * value of any particular register."
40 | */
41 | public class IllegalDataValueException extends ModbusProtocolException {
42 |
43 | public IllegalDataValueException() {
44 | super(ModbusExceptionCode.ILLEGAL_DATA_VALUE);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/IllegalFunctionException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "The function code received in the query is not an
31 | * allowable action for the server (or slave). This
32 | * may be because the function code is only
33 | * applicable to newer devices, and was not
34 | * implemented in the unit selected. It could also
35 | * indicate that the server (or slave) is in the wrong
36 | * state to process a request of this type, for
37 | * example because it is unconfigured and is being
38 | * asked to return register values."
39 | */
40 | public class IllegalFunctionException extends ModbusProtocolException {
41 |
42 | final private int functionCode;
43 |
44 | public IllegalFunctionException(int functionCode) {
45 | super(ModbusExceptionCode.ILLEGAL_FUNCTION);
46 | this.functionCode = functionCode;
47 | }
48 |
49 | public int getFunctionCode() {
50 | return functionCode;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/IllegalRangeException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2024 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "A value contained in the query data field is not an
31 | * allowable value for server (or slave). This
32 | * indicates a fault in the structure of the remainder
33 | * of a complex request, such as that the implied
34 | * length is incorrect. It specifically does NOT mean
35 | * that a data item submitted for storage in a register
36 | * has a value outside the expectation of the
37 | * application program, since the MODBUS protocol
38 | * is unaware of the significance of any particular
39 | * value of any particular register."
40 | */
41 | public class IllegalRangeException extends ModbusProtocolException {
42 |
43 | public IllegalRangeException() {
44 | super(ModbusExceptionCode.UNKNOWN_EXCEPTION);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/MemoryParityErrorException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "Specialized use in conjunction with function codes
31 | * 20 and 21 and reference type 6, to indicate that
32 | * the extended file area failed to pass a consistency
33 | * checkFrame.
34 | * The server (or slave) attempted to read record
35 | * file, but detected a parity error in the memory.
36 | * The client (or master) can retry the request, but
37 | * service may be required on the server (or slave)
38 | * device."
39 | */
40 | public class MemoryParityErrorException extends ModbusProtocolException {
41 | public MemoryParityErrorException() {
42 | super(ModbusExceptionCode.MEMORY_PARITY_ERROR);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/ModbusChecksumException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public class ModbusChecksumException extends ModbusIOException {
25 |
26 | public static final String CHECKSUM_ERROR_STRING = "checksum error: received %d, calculated %d";
27 |
28 | public ModbusChecksumException(int recv, int calc) {
29 | super(String.format(CHECKSUM_ERROR_STRING, recv, calc));
30 | }
31 |
32 | public ModbusChecksumException(int recv, int calc, Throwable cause) {
33 | super(String.format(CHECKSUM_ERROR_STRING, recv, calc), cause);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/ModbusIOException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public class ModbusIOException extends Exception {
25 |
26 | public ModbusIOException(String message) {
27 | super(message);
28 | }
29 |
30 | public ModbusIOException(String message, Throwable cause) {
31 | super(message, cause);
32 | }
33 |
34 | public ModbusIOException(Throwable cause) {
35 | super(cause);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/ModbusMasterException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public class ModbusMasterException extends Exception {
25 |
26 | public ModbusMasterException(String message) {
27 | super(message);
28 | }
29 |
30 | public ModbusMasterException(Throwable cause) {
31 | super(cause);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/ModbusNumberException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public class ModbusNumberException extends Exception {
25 | public ModbusNumberException(String message, int number) {
26 | super(message + " : " + number);
27 | }
28 |
29 | public ModbusNumberException(String message) {
30 | super(message);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/ModbusProtocolException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | public class ModbusProtocolException extends Exception {
28 |
29 | private final ModbusExceptionCode exception;
30 |
31 | public ModbusProtocolException(ModbusExceptionCode exception) {
32 | super(exception.toString());
33 | this.exception = exception;
34 | }
35 |
36 | public ModbusExceptionCode getException() {
37 | return exception;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/ModbusSlaveException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public class ModbusSlaveException extends Exception {
25 |
26 | public ModbusSlaveException(String message) {
27 | super(message);
28 | }
29 |
30 | public ModbusSlaveException(Throwable cause) {
31 | super(cause);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/SlaveDeviceBusyException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "Specialized use in conjunction with programming
31 | * commands.
32 | * The server (or slave) is engaged in processing a
33 | * long–duration program command. The client (or
34 | * master) should retransmit the message later when
35 | * the server (or slave) is free."
36 | */
37 | public class SlaveDeviceBusyException extends ModbusProtocolException {
38 | public SlaveDeviceBusyException() {
39 | super(ModbusExceptionCode.SLAVE_DEVICE_BUSY);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/exception/SlaveDeviceFailureException.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.exception;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusExceptionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 |
27 | /**
28 | * quote from MODBUS Application Protocol Specification V1.1b
29 | *
30 | * "An unrecoverable error occurred while the server
31 | * (or slave) was attempting to perform the
32 | * requested action."
33 | */
34 | public class SlaveDeviceFailureException extends ModbusProtocolException {
35 | public SlaveDeviceFailureException() {
36 | super(ModbusExceptionCode.SLAVE_DEVICE_FAILURE);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/master/ModbusMasterASCII.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.master;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.ModbusConnectionFactory;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialParameters;
5 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
6 | import com.intelligt.modbus.jlibmodbus.serial.SerialPortException;
7 | import com.intelligt.modbus.jlibmodbus.serial.SerialUtils;
8 |
9 | /*
10 | * Copyright (C) 2016 "Invertor" Factory", JSC
11 | * [http://www.sbp-invertor.ru]
12 | *
13 | * This file is part of JLibModbus.
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | *
27 | * Authors: Vladislav Y. Kochedykov, software engineer.
28 | * email: vladislav.kochedykov@gmail.com
29 | */
30 |
31 | final public class ModbusMasterASCII extends ModbusMasterSerial {
32 |
33 | public ModbusMasterASCII(SerialParameters parameters) throws SerialPortException {
34 | super(ModbusConnectionFactory.getASCII(SerialUtils.createSerial(parameters)));
35 | }
36 |
37 | public ModbusMasterASCII(String device, SerialPort.BaudRate baudRate, SerialPort.Parity parity) throws SerialPortException {
38 | this(new SerialParameters(device, baudRate, 7, parity == SerialPort.Parity.NONE ? 2 : 1, parity));
39 | }
40 |
41 | public ModbusMasterASCII(String device, SerialPort.BaudRate baudRate) throws SerialPortException {
42 | this(device, baudRate, SerialPort.Parity.EVEN);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/master/ModbusMasterFactory.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.master;
2 |
3 | import com.intelligt.modbus.jlibmodbus.serial.SerialParameters;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
5 | import com.intelligt.modbus.jlibmodbus.serial.SerialPortException;
6 | import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
7 |
8 | /*
9 | * Copyright (C) 2016 "Invertor" Factory", JSC
10 | * [http://www.sbp-invertor.ru]
11 | *
12 | * This file is part of JLibModbus.
13 | *
14 | * Licensed under the Apache License, Version 2.0 (the "License");
15 | * you may not use this file except in compliance with the License.
16 | * You may obtain a copy of the License at
17 | *
18 | * http://www.apache.org/licenses/LICENSE-2.0
19 | *
20 | * Unless required by applicable law or agreed to in writing, software
21 | * distributed under the License is distributed on an "AS IS" BASIS,
22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 | * See the License for the specific language governing permissions and
24 | * limitations under the License.
25 | *
26 | * Authors: Vladislav Y. Kochedykov, software engineer.
27 | * email: vladislav.kochedykov@gmail.com
28 | */
29 |
30 | final public class ModbusMasterFactory {
31 |
32 | private ModbusMasterFactory() {
33 |
34 | }
35 |
36 | /**
37 | * Creates a ModbusMasterRTU instance.
38 | *
39 | * @param sp parameters of the serial port you would like to use.
40 | * @return the newly created rtu-master
41 | * @see SerialPort.Parity
42 | * @see SerialPort.BaudRate
43 | * @see ModbusMaster
44 | * @see ModbusMasterRTU
45 | * @see SerialParameters
46 | */
47 | static public ModbusMaster createModbusMasterRTU(SerialParameters sp) throws SerialPortException {
48 | return new ModbusMasterRTU(sp);
49 | }
50 |
51 | /**
52 | * Creates a ModbusMasterASCII instance.
53 | *
54 | * @param sp parameters of the serial port you would like to use.
55 | * @return the newly created ascii-master
56 | * @see SerialPort.Parity
57 | * @see SerialPort.BaudRate
58 | * @see ModbusMaster
59 | * @see ModbusMasterASCII
60 | */
61 | static public ModbusMaster createModbusMasterASCII(SerialParameters sp) throws SerialPortException {
62 | return new ModbusMasterASCII(sp);
63 | }
64 |
65 | /**
66 | * Creates ModbusMasterTCP instance.
67 | *
68 | * @param tcpParameters - a TcpParameters instance
69 | * @return the newly created tcp-master
70 | * @see ModbusMaster
71 | * @see ModbusMasterTCP
72 | * @see TcpParameters
73 | */
74 | static public ModbusMaster createModbusMasterTCP(TcpParameters tcpParameters) {
75 | return new ModbusMasterTCP(tcpParameters);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/master/ModbusMasterRTU.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.master;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.ModbusConnectionFactory;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialParameters;
5 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
6 | import com.intelligt.modbus.jlibmodbus.serial.SerialPortException;
7 | import com.intelligt.modbus.jlibmodbus.serial.SerialUtils;
8 |
9 | /*
10 | * Copyright (C) 2016 "Invertor" Factory", JSC
11 | * [http://www.sbp-invertor.ru]
12 | *
13 | * This file is part of JLibModbus.
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | *
27 | * Authors: Vladislav Y. Kochedykov, software engineer.
28 | * email: vladislav.kochedykov@gmail.com
29 | */
30 |
31 | final public class ModbusMasterRTU extends ModbusMasterSerial {
32 |
33 | public ModbusMasterRTU(SerialParameters parameters) throws SerialPortException {
34 | super(ModbusConnectionFactory.getRTU(SerialUtils.createSerial(parameters)));
35 | }
36 |
37 | public ModbusMasterRTU(String device, SerialPort.BaudRate baudRate, int dataBits, int stopBits, SerialPort.Parity parity) throws SerialPortException {
38 | this(new SerialParameters(device, baudRate, dataBits, stopBits, parity));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/ModbusMessageFactory.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg;
2 |
3 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusMessage;
4 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusRequest;
5 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | public interface ModbusMessageFactory {
29 | /**
30 | * This method creates a #ModbusMessage instance from #functionCode.
31 | * The returned value can be either an instance of ModbusRequest or ModbusResponse.
32 | *
33 | * @param functionCode a number representing a modbus function
34 | * @return an instance of a specific ModbusMessage
35 | * @see ModbusRequest
36 | * @see ModbusResponse
37 | * @see ModbusMessageFactory
38 | * @see ModbusResponseFactory
39 | */
40 | ModbusMessage createMessage(int functionCode);
41 | }
42 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/AbstractDataRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | abstract public class AbstractDataRequest extends ModbusRequest {
32 |
33 | private int startAddress;
34 |
35 | protected AbstractDataRequest() {
36 | super();
37 | }
38 |
39 | abstract protected void writeData(ModbusOutputStream fifo) throws IOException;
40 |
41 | abstract protected void readData(ModbusInputStream fifo) throws IOException, ModbusNumberException;
42 |
43 | @Override
44 | final public void readPDU(ModbusInputStream fifo) throws ModbusNumberException, IOException {
45 | setStartAddress(fifo.readShortBE());
46 | readData(fifo);
47 | }
48 |
49 | @Override
50 | public void writeRequest(ModbusOutputStream fifo) throws IOException {
51 | fifo.writeShortBE(getStartAddress());
52 | writeData(fifo);
53 | }
54 |
55 | public int getStartAddress() {
56 | return startAddress;
57 | }
58 |
59 | public void setStartAddress(int startAddress) throws ModbusNumberException {
60 | if (!Modbus.checkStartAddress(startAddress)) {
61 | throw new ModbusNumberException("Error in start address", startAddress);
62 | }
63 | this.startAddress = startAddress;
64 | }
65 |
66 | @Override
67 | final public int requestSize() {
68 | return 2 + dataSize();
69 | }
70 |
71 | abstract protected int dataSize();
72 | }
73 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/AbstractMultipleRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
6 |
7 | import java.io.IOException;
8 |
9 | /*
10 | * Copyright (C) 2016 "Invertor" Factory", JSC
11 | * [http://www.sbp-invertor.ru]
12 | *
13 | * This file is part of JLibModbus.
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | *
27 | * Authors: Vladislav Y. Kochedykov, software engineer.
28 | * email: vladislav.kochedykov@gmail.com
29 | */
30 | abstract public class AbstractMultipleRequest extends AbstractDataRequest {
31 | private int quantity;
32 |
33 | protected AbstractMultipleRequest() {
34 | super();
35 | }
36 |
37 | @Override
38 | protected void readData(ModbusInputStream fifo) throws IOException, ModbusNumberException {
39 | setQuantity(fifo.readShortBE());
40 | }
41 |
42 | @Override
43 | protected void writeData(ModbusOutputStream fifo) throws IOException {
44 | fifo.writeShortBE(quantity);
45 | }
46 |
47 | @Override
48 | protected int dataSize() {
49 | return 2;
50 | }
51 |
52 | public int getQuantity() {
53 | return quantity;
54 | }
55 |
56 | public void setQuantity(int quantity) throws ModbusNumberException {
57 | if (!checkAddressRange(getStartAddress(), quantity))
58 | throw new ModbusNumberException("End address out of bounds", getStartAddress() + quantity);
59 | this.quantity = quantity;
60 | }
61 |
62 | protected abstract boolean checkAddressRange(int startAddress, int quantity);
63 | }
64 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/AbstractReadResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | abstract public class AbstractReadResponse extends ModbusResponse {
32 |
33 | private int byteCount = 0;
34 |
35 | protected AbstractReadResponse() {
36 | super();
37 | }
38 |
39 | public int getByteCount() {
40 | return byteCount;
41 | }
42 |
43 | protected void setByteCount(int byteCount) throws ModbusNumberException {
44 | if (byteCount > (Modbus.MAX_PDU_LENGTH - 2))
45 | throw new ModbusNumberException("Byte count greater than max allowable");
46 | this.byteCount = byteCount;
47 | }
48 |
49 | @Override
50 | final public void readResponse(ModbusInputStream fifo) throws IOException, ModbusNumberException {
51 | setByteCount(fifo.read());
52 | readData(fifo);
53 | }
54 |
55 | @Override
56 | final public void writeResponse(ModbusOutputStream fifo) throws IOException {
57 | fifo.write(getByteCount());
58 | writeData(fifo);
59 | }
60 |
61 | abstract protected void readData(ModbusInputStream fifo) throws IOException, ModbusNumberException;
62 |
63 | abstract protected void writeData(ModbusOutputStream fifo) throws IOException;
64 |
65 | @Override
66 | final protected int responseSize() {
67 | return 1 + getByteCount();
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/AbstractWriteMultipleRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | abstract public class AbstractWriteMultipleRequest extends AbstractMultipleRequest {
32 |
33 | private byte[] values;
34 | private int byteCount;
35 |
36 | protected AbstractWriteMultipleRequest() {
37 | super();
38 | }
39 |
40 | @Override
41 | public void writeData(ModbusOutputStream fifo) throws IOException {
42 | super.writeData(fifo);
43 | fifo.write(getByteCount());
44 | fifo.write(getBytes());
45 | }
46 |
47 | @Override
48 | protected void readData(ModbusInputStream fifo) throws IOException, ModbusNumberException {
49 | super.readData(fifo);
50 | setByteCount(fifo.read());
51 | values = new byte[byteCount];
52 | int size;
53 | if ((size = fifo.read(values, 0, getByteCount())) < getByteCount())
54 | Modbus.log().warning(getByteCount() + " bytes expected, but " + size + " received.");
55 | }
56 |
57 | public int getByteCount() {
58 | return byteCount;
59 | }
60 |
61 | public void setByteCount(int byteCount) throws ModbusNumberException {
62 | if (byteCount > Modbus.MAX_WRITE_REGISTER_COUNT * 2) {
63 | //TODO add a description
64 | throw new ModbusNumberException("" + byteCount);
65 | }
66 | this.byteCount = byteCount;
67 | }
68 |
69 | public byte[] getBytes() {
70 | return values;
71 | }
72 |
73 | public void setBytes(byte[] values) throws ModbusNumberException {
74 | this.values = values;
75 | setByteCount(values.length);
76 | }
77 |
78 | @Override
79 | protected int dataSize() {
80 | return super.dataSize() + 1 + getByteCount();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/AbstractWriteResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 |
32 | public abstract class AbstractWriteResponse extends ModbusResponse {
33 | private int startAddress = 0;
34 |
35 | protected AbstractWriteResponse() {
36 | super();
37 | }
38 |
39 | @Override
40 | final protected void readResponse(ModbusInputStream fifo) throws IOException {
41 | try {
42 | setStartAddress(fifo.readShortBE());
43 | } catch (ModbusNumberException e) {
44 | e.printStackTrace();
45 | }
46 | readValue(fifo);
47 | }
48 |
49 | @Override
50 | final public void writeResponse(ModbusOutputStream fifo) throws IOException {
51 | fifo.writeShortBE(getStartAddress());
52 | writeValue(fifo);
53 | }
54 |
55 | abstract protected void readValue(ModbusInputStream fifo) throws IOException;
56 |
57 | abstract protected void writeValue(ModbusOutputStream fifo) throws IOException;
58 |
59 | final public int getStartAddress() {
60 | return startAddress;
61 | }
62 |
63 | public void setStartAddress(int startAddress) throws ModbusNumberException {
64 | if (!(Modbus.checkStartAddress(startAddress)))
65 | throw new ModbusNumberException("Error in start address", startAddress);
66 | this.startAddress = startAddress;
67 | }
68 |
69 | @Override
70 | final protected int responseSize() {
71 | return 4;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/ModbusFileRecord.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.IllegalDataValueException;
4 |
5 | import java.util.Arrays;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | public class ModbusFileRecord {
29 | final static public int REF_TYPE = 0x06;
30 | final private int number;
31 | final private int file;
32 | private int length = 0;
33 | private int[] registers;
34 |
35 | public ModbusFileRecord(int fileNumber, int recordNumber, int registerCount) {
36 | this.file = fileNumber;
37 | this.number = recordNumber;
38 | this.length = registerCount;
39 | }
40 |
41 | public ModbusFileRecord(int file, int number, int[] buffer) {
42 | this.file = file;
43 | this.number = number;
44 | setRegisters(buffer);
45 | }
46 |
47 | public int[] getRegisters() {
48 | return Arrays.copyOf(registers, registers.length);
49 | }
50 |
51 | /**
52 | * setter
53 | *
54 | * @param registers modbus register values
55 | */
56 | protected void setRegisters(int[] registers) {
57 | this.registers = Arrays.copyOf(registers, registers.length);
58 | this.length = registers.length;
59 | }
60 |
61 | public void writeRegisters(int[] registers) throws IllegalDataValueException {
62 | if (registers.length > getRegisters().length)
63 | throw new IllegalDataValueException();
64 | this.length = registers.length;
65 | }
66 |
67 | public int getFileNumber() {
68 | return file;
69 | }
70 |
71 | public int getRecordNumber() {
72 | return number;
73 | }
74 |
75 | public int getRecordLength() {
76 | return length;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/mei/ConformityLevel.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base.mei;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public enum ConformityLevel {
25 | BASIC_STREAM_ONLY(0x01),
26 | REGULAR_STREAM_ONLY(0x02),
27 | EXTENDED_STREAM_ONLY(0x03),
28 | BASIC_STREAM_AND_INDIVIDUAL(0x81),
29 | REGULAR_STREAM_AND_INDIVIDUAL(0x82),
30 | EXTENDED_STREAM_AND_INDIVIDUAL(0x83);
31 |
32 | final private int code;
33 |
34 | ConformityLevel(int code) {
35 | this.code = code;
36 | }
37 |
38 | static public ConformityLevel get(int code) {
39 | for (ConformityLevel c : values()) {
40 | if (c.toInt() == code) {
41 | return c;
42 | }
43 | }
44 | return BASIC_STREAM_ONLY;
45 | }
46 |
47 | public int toInt() {
48 | return code;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/mei/MEIFactory.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base.mei;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.MEITypeCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 | public class MEIFactory {
27 | static public ModbusEncapsulatedInterface getMEI(MEITypeCode meiTypeCode) {
28 | switch (meiTypeCode) {
29 | case READ_DEVICE_IDENTIFICATION:
30 | return new MEIReadDeviceIdentification();
31 | case CAN_OPEN_PDU:
32 | return new CANopenGeneralReferencePDU();
33 | case RESERVED:
34 | default:
35 | return null;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/mei/ModbusEncapsulatedInterface.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base.mei;
2 |
3 | import com.intelligt.modbus.jlibmodbus.data.DataHolder;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 | import com.intelligt.modbus.jlibmodbus.utils.MEITypeCode;
8 |
9 | import java.io.IOException;
10 |
11 | /*
12 | * Copyright (C) 2016 "Invertor" Factory", JSC
13 | * [http://www.sbp-invertor.ru]
14 | *
15 | * This file is part of JLibModbus.
16 | *
17 | * Licensed under the Apache License, Version 2.0 (the "License");
18 | * you may not use this file except in compliance with the License.
19 | * You may obtain a copy of the License at
20 | *
21 | * http://www.apache.org/licenses/LICENSE-2.0
22 | *
23 | * Unless required by applicable law or agreed to in writing, software
24 | * distributed under the License is distributed on an "AS IS" BASIS,
25 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 | * See the License for the specific language governing permissions and
27 | * limitations under the License.
28 | *
29 | * Authors: Vladislav Y. Kochedykov, software engineer.
30 | * email: vladislav.kochedykov@gmail.com
31 | */
32 | public interface ModbusEncapsulatedInterface {
33 | MEITypeCode getTypeCode();
34 |
35 | void writeRequest(ModbusOutputStream fifo) throws IOException;
36 |
37 | void readRequest(ModbusInputStream fifo) throws IOException;
38 |
39 | int getRequestSize();
40 |
41 | void writeResponse(ModbusOutputStream fifo) throws IOException;
42 |
43 | void readResponse(ModbusInputStream fifo) throws IOException, ModbusNumberException;
44 |
45 | int getResponseSize();
46 |
47 | void process(DataHolder dataHolder);
48 | }
49 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/base/mei/ReadDeviceIdentificationCode.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.base.mei;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public enum ReadDeviceIdentificationCode {
25 | BASIC_STREAM_ACCESS(0x01),
26 | REGULAR_STREAM_ACCESS(0x02),
27 | EXTENDED_STREAM_ACCESS(0x03),
28 | ONE_SPECIFIC_INDIVIDUAL_ACCESS(0x04);
29 |
30 | final private int code;
31 |
32 | ReadDeviceIdentificationCode(int code) {
33 | this.code = code;
34 | }
35 |
36 | static public ReadDeviceIdentificationCode get(int code) {
37 | for (ReadDeviceIdentificationCode c : values()) {
38 | if (c.toInt() == code) {
39 | return c;
40 | }
41 | }
42 | return BASIC_STREAM_ACCESS;
43 | }
44 |
45 | public int toInt() {
46 | return code;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/request/GetCommEventCounterRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.request;
2 |
3 | import com.intelligt.modbus.jlibmodbus.data.DataHolder;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusRequest;
6 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
7 | import com.intelligt.modbus.jlibmodbus.msg.response.GetCommEventCounterResponse;
8 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
9 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
10 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
11 |
12 | import java.io.IOException;
13 |
14 | /*
15 | * Copyright (C) 2016 "Invertor" Factory", JSC
16 | * [http://www.sbp-invertor.ru]
17 | *
18 | * This file is part of JLibModbus.
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | *
32 | * Authors: Vladislav Y. Kochedykov, software engineer.
33 | * email: vladislav.kochedykov@gmail.com
34 | */
35 | final public class GetCommEventCounterRequest extends ModbusRequest {
36 |
37 | public GetCommEventCounterRequest() {
38 | super();
39 | }
40 |
41 | @Override
42 | protected Class getResponseClass() {
43 | return GetCommEventCounterResponse.class;
44 | }
45 |
46 | @Override
47 | public void writeRequest(ModbusOutputStream fifo) throws IOException {
48 | //no op
49 | }
50 |
51 | @Override
52 | public int requestSize() {
53 | return 0;
54 | }
55 |
56 | @Override
57 | public ModbusResponse process(DataHolder dataHolder) throws ModbusNumberException {
58 | GetCommEventCounterResponse response = new GetCommEventCounterResponse();
59 | response.setServerAddress(getServerAddress());
60 | response.setEventCount(dataHolder.getCommStatus().getEventCount());
61 | response.setStatus(dataHolder.getCommStatus().getCommStatus());
62 | return response;
63 | }
64 |
65 | @Override
66 | public boolean validateResponseImpl(ModbusResponse response) {
67 | return true;
68 | }
69 |
70 | @Override
71 | public void readPDU(ModbusInputStream fifo) throws ModbusNumberException, IOException {
72 | //no op
73 | }
74 |
75 | @Override
76 | public int getFunction() {
77 | return ModbusFunctionCode.GET_COMM_EVENT_COUNTER.toInt();
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/request/IllegalFunctionRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.request;
2 |
3 | import com.intelligt.modbus.jlibmodbus.data.DataHolder;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusRequest;
6 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
7 | import com.intelligt.modbus.jlibmodbus.msg.response.IllegalFunctionResponse;
8 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
9 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
10 |
11 | import java.io.IOException;
12 |
13 | /*
14 | * Copyright (C) 2016 "Invertor" Factory", JSC
15 | * [http://www.sbp-invertor.ru]
16 | *
17 | * This file is part of JLibModbus.
18 | *
19 | * Licensed under the Apache License, Version 2.0 (the "License");
20 | * you may not use this file except in compliance with the License.
21 | * You may obtain a copy of the License at
22 | *
23 | * http://www.apache.org/licenses/LICENSE-2.0
24 | *
25 | * Unless required by applicable law or agreed to in writing, software
26 | * distributed under the License is distributed on an "AS IS" BASIS,
27 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 | * See the License for the specific language governing permissions and
29 | * limitations under the License.
30 | *
31 | * Authors: Vladislav Y. Kochedykov, software engineer.
32 | * email: vladislav.kochedykov@gmail.com
33 | */
34 | public class IllegalFunctionRequest extends ModbusRequest {
35 |
36 | final private int functionCode;
37 |
38 | public IllegalFunctionRequest(int functionCode) {
39 | super();
40 |
41 | this.functionCode = functionCode;
42 | }
43 |
44 | @Override
45 | protected Class getResponseClass() {
46 | return IllegalFunctionResponse.class;
47 | }
48 |
49 | @Override
50 | public void writeRequest(ModbusOutputStream fifo) throws IOException {
51 | throw new IOException("Can't send Illegal request");
52 | }
53 |
54 | @Override
55 | public int requestSize() {
56 | return 0;
57 | }
58 |
59 | @Override
60 | public ModbusResponse process(DataHolder dataHolder) throws ModbusNumberException {
61 | IllegalFunctionResponse response = (IllegalFunctionResponse) getResponse();
62 | response.setFunctionCode(getFunction());
63 | return response;
64 | }
65 |
66 | @Override
67 | protected boolean validateResponseImpl(ModbusResponse response) {
68 | return response.getFunction() == getFunction();
69 | }
70 |
71 | @Override
72 | public void readPDU(ModbusInputStream fifo) throws ModbusNumberException, IOException {
73 | //no op
74 | }
75 |
76 | @Override
77 | public int getFunction() {
78 | return functionCode;
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/request/ReadDeviceIdentificationRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.request;
2 |
3 | import com.intelligt.modbus.jlibmodbus.msg.base.mei.MEIReadDeviceIdentification;
4 | import com.intelligt.modbus.jlibmodbus.msg.base.mei.ReadDeviceIdentificationCode;
5 | import com.intelligt.modbus.jlibmodbus.utils.MEITypeCode;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | public class ReadDeviceIdentificationRequest extends EncapsulatedInterfaceTransportRequest {
29 |
30 | public ReadDeviceIdentificationRequest() {
31 | super();
32 | setMEIType(MEITypeCode.READ_DEVICE_IDENTIFICATION);
33 | }
34 |
35 | public void setObjectId(int objectId) {
36 | ((MEIReadDeviceIdentification) getMei()).setObjectId(objectId);
37 | }
38 |
39 | public void setReadDeviceId(ReadDeviceIdentificationCode readDeviceId) {
40 | ((MEIReadDeviceIdentification) getMei()).setReadDeviceId(readDeviceId);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/request/ReadDiscreteInputsRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.request;
2 |
3 | import com.intelligt.modbus.jlibmodbus.data.DataHolder;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
6 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
7 | import com.intelligt.modbus.jlibmodbus.msg.response.ReadCoilsResponse;
8 | import com.intelligt.modbus.jlibmodbus.msg.response.ReadDiscreteInputsResponse;
9 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
10 |
11 | /*
12 | * Copyright (C) 2016 "Invertor" Factory", JSC
13 | * [http://www.sbp-invertor.ru]
14 | *
15 | * This file is part of JLibModbus.
16 | *
17 | * Licensed under the Apache License, Version 2.0 (the "License");
18 | * you may not use this file except in compliance with the License.
19 | * You may obtain a copy of the License at
20 | *
21 | * http://www.apache.org/licenses/LICENSE-2.0
22 | *
23 | * Unless required by applicable law or agreed to in writing, software
24 | * distributed under the License is distributed on an "AS IS" BASIS,
25 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 | * See the License for the specific language governing permissions and
27 | * limitations under the License.
28 | *
29 | * Authors: Vladislav Y. Kochedykov, software engineer.
30 | * email: vladislav.kochedykov@gmail.com
31 | */
32 |
33 | final public class ReadDiscreteInputsRequest extends ReadCoilsRequest {
34 |
35 | public ReadDiscreteInputsRequest() {
36 | super();
37 | }
38 |
39 | @Override
40 | protected Class getResponseClass() {
41 | return ReadDiscreteInputsResponse.class;
42 | }
43 |
44 | @Override
45 | public ModbusResponse process(DataHolder dataHolder) throws ModbusNumberException {
46 | ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse) getResponse();
47 | try {
48 | boolean[] range = dataHolder.readDiscreteInputRange(getStartAddress(), getQuantity());
49 | response.setCoils(range);
50 | } catch (ModbusProtocolException e) {
51 | response.setException();
52 | response.setModbusExceptionCode(e.getException().getValue());
53 | }
54 | return response;
55 | }
56 |
57 | @Override
58 | public int getFunction() {
59 | return ModbusFunctionCode.READ_DISCRETE_INPUTS.toInt();
60 | }
61 |
62 | @Override
63 | public boolean validateResponseImpl(ModbusResponse response) {
64 | if (!(response instanceof ReadDiscreteInputsResponse)) {
65 | return false;
66 | }
67 | ReadDiscreteInputsResponse r = (ReadDiscreteInputsResponse) response;
68 | return (r.getByteCount() == ReadCoilsResponse.calcByteCount(getQuantity()));
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/request/ReadInputRegistersRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.request;
2 |
3 | import com.intelligt.modbus.jlibmodbus.data.DataHolder;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
6 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
7 | import com.intelligt.modbus.jlibmodbus.msg.response.ReadInputRegistersResponse;
8 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 |
32 | final public class ReadInputRegistersRequest extends ReadHoldingRegistersRequest {
33 |
34 | public ReadInputRegistersRequest() {
35 | super();
36 | }
37 |
38 | @Override
39 | public ModbusResponse process(DataHolder dataHolder) throws ModbusNumberException {
40 | ReadInputRegistersResponse response = (ReadInputRegistersResponse) getResponse();
41 | response.setServerAddress(getServerAddress());
42 | try {
43 | int[] range = dataHolder.readInputRegisterRange(getStartAddress(), getQuantity());
44 | response.setBuffer(range);
45 | } catch (ModbusProtocolException e) {
46 | response.setException();
47 | response.setModbusExceptionCode(e.getException().getValue());
48 | }
49 | return response;
50 | }
51 |
52 | @Override
53 | public boolean validateResponseImpl(ModbusResponse response) {
54 | if (!(response instanceof ReadInputRegistersResponse)) {
55 | return false;
56 | }
57 | ReadInputRegistersResponse r = (ReadInputRegistersResponse) response;
58 | return r.getByteCount() == getQuantity() * 2;
59 | }
60 |
61 | @Override
62 | public int getFunction() {
63 | return ModbusFunctionCode.READ_INPUT_REGISTERS.toInt();
64 | }
65 |
66 | @Override
67 | protected Class getResponseClass() {
68 | return ReadInputRegistersResponse.class;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/request/WriteSingleCoilRequest.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.request;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.data.DataHolder;
5 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
6 | import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
7 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
8 | import com.intelligt.modbus.jlibmodbus.msg.response.WriteSingleCoilResponse;
9 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
10 |
11 | /*
12 | * Copyright (C) 2016 "Invertor" Factory", JSC
13 | * [http://www.sbp-invertor.ru]
14 | *
15 | * This file is part of JLibModbus.
16 | *
17 | * Licensed under the Apache License, Version 2.0 (the "License");
18 | * you may not use this file except in compliance with the License.
19 | * You may obtain a copy of the License at
20 | *
21 | * http://www.apache.org/licenses/LICENSE-2.0
22 | *
23 | * Unless required by applicable law or agreed to in writing, software
24 | * distributed under the License is distributed on an "AS IS" BASIS,
25 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 | * See the License for the specific language governing permissions and
27 | * limitations under the License.
28 | *
29 | * Authors: Vladislav Y. Kochedykov, software engineer.
30 | * email: vladislav.kochedykov@gmail.com
31 | */
32 | final public class WriteSingleCoilRequest extends WriteSingleRegisterRequest {
33 |
34 | public WriteSingleCoilRequest() {
35 | super();
36 | }
37 |
38 | @Override
39 | protected Class getResponseClass() {
40 | return WriteSingleCoilResponse.class;
41 | }
42 |
43 | @Override
44 | public ModbusResponse process(DataHolder dataHolder) throws ModbusNumberException {
45 | WriteSingleCoilResponse response = (WriteSingleCoilResponse) getResponse();
46 | response.setStartAddress(getStartAddress());
47 | response.setValue(getValue());
48 | try {
49 | dataHolder.writeCoil(getStartAddress(), getCoil());
50 | } catch (ModbusProtocolException e) {
51 | response.setException();
52 | response.setModbusExceptionCode(e.getException().getValue());
53 | }
54 | return response;
55 | }
56 |
57 | @Override
58 | public boolean validateResponseImpl(ModbusResponse response) {
59 | if (!(response instanceof WriteSingleCoilResponse)) {
60 | return false;
61 | }
62 | WriteSingleCoilResponse r = (WriteSingleCoilResponse) response;
63 | return r.getStartAddress() == getStartAddress() && r.getValue() == getValue();
64 | }
65 |
66 | public boolean getCoil() {
67 | return Modbus.toCoil(getValue());
68 | }
69 |
70 | public void setCoil(boolean coil) throws ModbusNumberException {
71 | setValue(Modbus.fromCoil(coil));
72 | }
73 |
74 | @Override
75 | public int getFunction() {
76 | return ModbusFunctionCode.WRITE_SINGLE_COIL.toInt();
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/BroadcastResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
4 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
8 |
9 | import java.io.IOException;
10 |
11 | /*
12 | * Copyright (C) 2017 Vladislav Y. Kochedykov
13 | * [https://github.com/kochedykov/jlibmodbus]
14 | *
15 | * This file is part of JLibModbus.
16 | *
17 | * Licensed under the Apache License, Version 2.0 (the "License");
18 | * you may not use this file except in compliance with the License.
19 | * You may obtain a copy of the License at
20 | *
21 | * http://www.apache.org/licenses/LICENSE-2.0
22 | *
23 | * Unless required by applicable law or agreed to in writing, software
24 | * distributed under the License is distributed on an "AS IS" BASIS,
25 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 | * See the License for the specific language governing permissions and
27 | * limitations under the License.
28 | *
29 | * Authors: Vladislav Y. Kochedykov, software engineer.
30 | * email: vladislav.kochedykov@gmail.com
31 | */
32 | public class BroadcastResponse extends ModbusResponse {
33 |
34 | private int functionCode = ModbusFunctionCode.UNKNOWN.toInt();
35 |
36 | @Override
37 | public int getFunction() {
38 | return functionCode;
39 | }
40 |
41 | public void setFunction(int functionCode) {
42 | this.functionCode = functionCode;
43 | }
44 |
45 | @Override
46 | protected void readResponse(ModbusInputStream fifo) throws IOException, ModbusNumberException {
47 | //do nothing
48 | }
49 |
50 | @Override
51 | protected void writeResponse(ModbusOutputStream fifo) throws IOException {
52 | //do nothing
53 | }
54 |
55 | @Override
56 | protected int responseSize() {
57 | return 0;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/DiagnosticsResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
4 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 | import com.intelligt.modbus.jlibmodbus.utils.DiagnosticsSubFunctionCode;
8 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
9 |
10 | import java.io.IOException;
11 |
12 | /*
13 | * Copyright (C) 2016 "Invertor" Factory", JSC
14 | * [http://www.sbp-invertor.ru]
15 | *
16 | * This file is part of JLibModbus.
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *
22 | * http://www.apache.org/licenses/LICENSE-2.0
23 | *
24 | * Unless required by applicable law or agreed to in writing, software
25 | * distributed under the License is distributed on an "AS IS" BASIS,
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 | * See the License for the specific language governing permissions and
28 | * limitations under the License.
29 | *
30 | * Authors: Vladislav Y. Kochedykov, software engineer.
31 | * email: vladislav.kochedykov@gmail.com
32 | */
33 | public class DiagnosticsResponse extends ModbusResponse {
34 |
35 | private DiagnosticsSubFunctionCode subFunctionCode;
36 | private int subFunctionData = 0;
37 |
38 | public DiagnosticsResponse() {
39 | super();
40 | }
41 |
42 | @Override
43 | protected void readResponse(ModbusInputStream fifo) throws IOException, ModbusNumberException {
44 | setSubFunctionCode(DiagnosticsSubFunctionCode.get(fifo.readShortBE()));
45 | setSubFunctionData(fifo.readShortBE());
46 | }
47 |
48 | @Override
49 | protected void writeResponse(ModbusOutputStream fifo) throws IOException {
50 | fifo.writeShortBE(getSubFunctionCode().toInt());
51 | fifo.writeShortBE(getSubFunctionData());
52 | }
53 |
54 | @Override
55 | protected int responseSize() {
56 | return 4;
57 | }
58 |
59 | @Override
60 | public int getFunction() {
61 | return ModbusFunctionCode.DIAGNOSTICS.toInt();
62 | }
63 |
64 | public DiagnosticsSubFunctionCode getSubFunctionCode() {
65 | return subFunctionCode;
66 | }
67 |
68 | public void setSubFunctionCode(DiagnosticsSubFunctionCode subFunctionCode) {
69 | this.subFunctionCode = subFunctionCode;
70 | }
71 |
72 | public int getSubFunctionData() {
73 | return subFunctionData;
74 | }
75 |
76 | public void setSubFunctionData(int subFunctionData) {
77 | this.subFunctionData = subFunctionData;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/EncapsulatedInterfaceTransportResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
4 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
5 | import com.intelligt.modbus.jlibmodbus.msg.base.mei.MEIFactory;
6 | import com.intelligt.modbus.jlibmodbus.msg.base.mei.ModbusEncapsulatedInterface;
7 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
8 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
9 | import com.intelligt.modbus.jlibmodbus.utils.MEITypeCode;
10 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
11 |
12 | import java.io.IOException;
13 |
14 | /*
15 | * Copyright (C) 2016 "Invertor" Factory", JSC
16 | * [http://www.sbp-invertor.ru]
17 | *
18 | * This file is part of JLibModbus.
19 | *
20 | * Licensed under the Apache License, Version 2.0 (the "License");
21 | * you may not use this file except in compliance with the License.
22 | * You may obtain a copy of the License at
23 | *
24 | * http://www.apache.org/licenses/LICENSE-2.0
25 | *
26 | * Unless required by applicable law or agreed to in writing, software
27 | * distributed under the License is distributed on an "AS IS" BASIS,
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 | * See the License for the specific language governing permissions and
30 | * limitations under the License.
31 | *
32 | * Authors: Vladislav Y. Kochedykov, software engineer.
33 | * email: vladislav.kochedykov@gmail.com
34 | */
35 | public class EncapsulatedInterfaceTransportResponse extends ModbusResponse {
36 |
37 | private ModbusEncapsulatedInterface mei = null;
38 |
39 | public EncapsulatedInterfaceTransportResponse() {
40 | super();
41 | }
42 |
43 | @Override
44 | protected void readResponse(ModbusInputStream fifo) throws IOException, ModbusNumberException {
45 | int meiTypeCode = fifo.read();
46 | setMei(MEIFactory.getMEI(MEITypeCode.get(meiTypeCode)));
47 | if (getMei() == null)
48 | throw new ModbusNumberException("Unknown MEI type", meiTypeCode);
49 | getMei().readResponse(fifo);
50 | }
51 |
52 | @Override
53 | protected void writeResponse(ModbusOutputStream fifo) throws IOException {
54 | fifo.write(getMei().getTypeCode().toInt());
55 | getMei().writeResponse(fifo);
56 | }
57 |
58 | public ModbusEncapsulatedInterface getMei() {
59 | return mei;
60 | }
61 |
62 | public void setMei(ModbusEncapsulatedInterface mei) {
63 | this.mei = mei;
64 | }
65 |
66 | @Override
67 | protected int responseSize() {
68 | return 1 + (mei == null ? 0 : mei.getResponseSize());
69 | }
70 |
71 | public int getFunction() {
72 | return ModbusFunctionCode.ENCAPSULATED_INTERFACE_TRANSPORT.toInt();
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/GetCommEventCounterResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
6 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | final public class GetCommEventCounterResponse extends ModbusResponse {
32 |
33 | private int status;
34 | private int eventCount;
35 |
36 | public GetCommEventCounterResponse() {
37 | super();
38 | }
39 |
40 | @Override
41 | protected void readResponse(ModbusInputStream fifo) throws IOException {
42 | setStatus(fifo.readShortBE());
43 | setEventCount(fifo.readShortBE());
44 | }
45 |
46 | @Override
47 | protected void writeResponse(ModbusOutputStream fifo) throws IOException {
48 | fifo.writeShortBE(getStatus());
49 | fifo.writeShortBE(getEventCount());
50 | }
51 |
52 | @Override
53 | protected int responseSize() {
54 | return 4;
55 | }
56 |
57 | @Override
58 | public int getFunction() {
59 | return ModbusFunctionCode.GET_COMM_EVENT_COUNTER.toInt();
60 | }
61 |
62 | public int getStatus() {
63 | return status;
64 | }
65 |
66 | public void setStatus(int status) {
67 | this.status = status;
68 | }
69 |
70 | public int getEventCount() {
71 | return eventCount;
72 | }
73 |
74 | public void setEventCount(int eventCount) {
75 | this.eventCount = eventCount;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/IllegalFunctionResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
4 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | public class IllegalFunctionResponse extends ModbusResponse {
32 | private int functionCode = 0;
33 |
34 | public IllegalFunctionResponse(int functionCode) {
35 | super();
36 | this.functionCode = functionCode;
37 | setException();
38 | }
39 |
40 | @Override
41 | public int getFunction() {
42 | return functionCode;
43 | }
44 |
45 | public void setFunctionCode(int functionCode) {
46 | this.functionCode = functionCode;
47 | }
48 |
49 | @Override
50 | protected void readResponse(ModbusInputStream fifo) throws IOException, ModbusNumberException {
51 | //no op
52 | }
53 |
54 | @Override
55 | protected void writeResponse(ModbusOutputStream fifo) throws IOException {
56 | //no op
57 | }
58 |
59 | @Override
60 | protected int responseSize() {
61 | return 0;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/MaskWriteRegisterResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.msg.base.AbstractWriteResponse;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
6 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | final public class MaskWriteRegisterResponse extends AbstractWriteResponse {
32 |
33 | private int maskAnd;
34 | private int maskOr;
35 |
36 | public MaskWriteRegisterResponse() {
37 | super();
38 | }
39 |
40 | @Override
41 | protected void readValue(ModbusInputStream fifo) throws IOException {
42 | setMaskAnd(fifo.readShortBE());
43 | setMaskOr(fifo.readShortBE());
44 | }
45 |
46 | @Override
47 | protected void writeValue(ModbusOutputStream fifo) throws IOException {
48 | fifo.writeShortBE(getMaskAnd());
49 | fifo.writeShortBE(getMaskOr());
50 | }
51 |
52 |
53 | @Override
54 | public int getFunction() {
55 | return ModbusFunctionCode.MASK_WRITE_REGISTER.toInt();
56 | }
57 |
58 | public int getMaskAnd() {
59 | return maskAnd;
60 | }
61 |
62 | public void setMaskAnd(int maskAnd) {
63 | this.maskAnd = maskAnd;
64 | }
65 |
66 | public int getMaskOr() {
67 | return maskOr;
68 | }
69 |
70 | public void setMaskOr(int maskOr) {
71 | this.maskOr = maskOr;
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/ReadDiscreteInputsResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 | final public class ReadDiscreteInputsResponse extends ReadCoilsResponse {
27 |
28 | public ReadDiscreteInputsResponse() {
29 | super();
30 | }
31 |
32 | @Override
33 | public int getFunction() {
34 | return ModbusFunctionCode.READ_DISCRETE_INPUTS.toInt();
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/ReadExceptionStatusResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusResponse;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
6 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | final public class ReadExceptionStatusResponse extends ModbusResponse {
32 |
33 | private int exceptionStatus = 0;
34 |
35 | public ReadExceptionStatusResponse() {
36 | super();
37 | }
38 |
39 | public int getExceptionStatus() {
40 | return exceptionStatus;
41 | }
42 |
43 | public void setExceptionStatus(int exceptionStatus) {
44 | this.exceptionStatus = (byte) exceptionStatus & 0xff;
45 | }
46 |
47 | @Override
48 | protected void readResponse(ModbusInputStream fifo) throws IOException {
49 | setExceptionStatus(fifo.read());
50 | }
51 |
52 | @Override
53 | protected void writeResponse(ModbusOutputStream fifo) throws IOException {
54 | fifo.write(getExceptionStatus());
55 | }
56 |
57 | @Override
58 | protected int responseSize() {
59 | return 1;
60 | }
61 |
62 | @Override
63 | public int getFunction() {
64 | return ModbusFunctionCode.READ_EXCEPTION_STATUS.toInt();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/ReadInputRegistersResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 | final public class ReadInputRegistersResponse extends ReadHoldingRegistersResponse {
27 |
28 | public ReadInputRegistersResponse() {
29 | super();
30 | }
31 |
32 | @Override
33 | public int getFunction() {
34 | return ModbusFunctionCode.READ_INPUT_REGISTERS.toInt();
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/ReadWriteMultipleRegistersResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
4 |
5 | /*
6 | * Copyright (C) 2016 "Invertor" Factory", JSC
7 | * [http://www.sbp-invertor.ru]
8 | *
9 | * This file is part of JLibModbus.
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | *
23 | * Authors: Vladislav Y. Kochedykov, software engineer.
24 | * email: vladislav.kochedykov@gmail.com
25 | */
26 | final public class ReadWriteMultipleRegistersResponse extends ReadHoldingRegistersResponse {
27 |
28 | public ReadWriteMultipleRegistersResponse() {
29 | super();
30 | }
31 |
32 | @Override
33 | public int getFunction() {
34 | return ModbusFunctionCode.READ_WRITE_MULTIPLE_REGISTERS.toInt();
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/ReportSlaveIdResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.msg.base.AbstractReadResponse;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
7 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
8 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
9 |
10 | import java.io.IOException;
11 | import java.util.Arrays;
12 |
13 | /*
14 | * Copyright (C) 2016 "Invertor" Factory", JSC
15 | * [http://www.sbp-invertor.ru]
16 | *
17 | * This file is part of JLibModbus.
18 | *
19 | * Licensed under the Apache License, Version 2.0 (the "License");
20 | * you may not use this file except in compliance with the License.
21 | * You may obtain a copy of the License at
22 | *
23 | * http://www.apache.org/licenses/LICENSE-2.0
24 | *
25 | * Unless required by applicable law or agreed to in writing, software
26 | * distributed under the License is distributed on an "AS IS" BASIS,
27 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 | * See the License for the specific language governing permissions and
29 | * limitations under the License.
30 | *
31 | * Authors: Vladislav Y. Kochedykov, software engineer.
32 | * email: vladislav.kochedykov@gmail.com
33 | */
34 | final public class ReportSlaveIdResponse extends AbstractReadResponse {
35 |
36 | private byte[] slaveId;
37 |
38 | public ReportSlaveIdResponse() {
39 | super();
40 | }
41 |
42 | public byte[] getSlaveId() {
43 | return slaveId != null ? Arrays.copyOf(slaveId, slaveId.length) : new byte[0];
44 | }
45 |
46 | public void setSlaveId(byte[] slaveId) throws ModbusNumberException {
47 | if ((slaveId.length + 2) > Modbus.MAX_PDU_LENGTH)
48 | throw new ModbusNumberException("Slave Id greater than max pdu length: ", getByteCount());
49 | setByteCount(slaveId.length);
50 | this.slaveId = Arrays.copyOf(slaveId, slaveId.length);
51 | }
52 |
53 | @Override
54 | protected void readData(ModbusInputStream fifo) throws IOException {
55 | if (Modbus.MAX_PDU_LENGTH < responseSize()) {
56 | throw new IOException("Slave Id greater than max pdu length: " + getByteCount());
57 | }
58 | slaveId = new byte[getByteCount()];
59 | int size;
60 | if ((size = fifo.read(slaveId)) < slaveId.length)
61 | Modbus.log().warning(slaveId.length + " bytes expected, but " + size + " received.");
62 | }
63 |
64 | @Override
65 | protected void writeData(ModbusOutputStream fifo) throws IOException {
66 | fifo.write(slaveId);
67 | }
68 |
69 | @Override
70 | public int getFunction() {
71 | return ModbusFunctionCode.REPORT_SLAVE_ID.toInt();
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/WriteMultipleCoilsResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | final public class WriteMultipleCoilsResponse extends WriteSingleRegisterResponse {
29 |
30 | public WriteMultipleCoilsResponse() {
31 | super();
32 | }
33 |
34 | public int getQuantity() {
35 | return getValue();
36 | }
37 |
38 | public void setQuantity(int quantity) throws ModbusNumberException {
39 | setValue(quantity);
40 | }
41 |
42 | @Override
43 | protected boolean checkValue() {
44 | return Modbus.checkWriteCoilCount(getValue());
45 | }
46 |
47 | @Override
48 | public int getFunction() {
49 | return ModbusFunctionCode.WRITE_MULTIPLE_COILS.toInt();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/WriteMultipleRegistersResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | final public class WriteMultipleRegistersResponse extends WriteSingleRegisterResponse {
29 |
30 | public WriteMultipleRegistersResponse() {
31 | super();
32 | }
33 |
34 | public int getQuantity() {
35 | return getValue();
36 | }
37 |
38 | public void setQuantity(int quantity) throws ModbusNumberException {
39 | setValue(quantity);
40 | }
41 |
42 | @Override
43 | protected boolean checkValue() {
44 | return Modbus.checkWriteRegisterCount(getValue());
45 | }
46 |
47 | @Override
48 | public int getFunction() {
49 | return ModbusFunctionCode.WRITE_MULTIPLE_REGISTERS.toInt();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/WriteSingleCoilResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | final public class WriteSingleCoilResponse extends WriteSingleRegisterResponse {
29 |
30 | public WriteSingleCoilResponse() {
31 | super();
32 | }
33 |
34 | public boolean getCoil() {
35 | return Modbus.toCoil(getValue());
36 | }
37 |
38 | public void setCoil(boolean coil) throws ModbusNumberException {
39 | setValue(Modbus.fromCoil(coil));
40 | }
41 |
42 | @Override
43 | protected boolean checkValue() {
44 | return getValue() == Modbus.COIL_VALUE_ON || getValue() == Modbus.COIL_VALUE_OFF;
45 | }
46 |
47 | @Override
48 | public int getFunction() {
49 | return ModbusFunctionCode.WRITE_SINGLE_COIL.toInt();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/msg/response/WriteSingleRegisterResponse.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.msg.response;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.msg.base.AbstractWriteResponse;
6 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
7 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
8 | import com.intelligt.modbus.jlibmodbus.utils.ModbusFunctionCode;
9 |
10 | import java.io.IOException;
11 |
12 | /*
13 | * Copyright (C) 2016 "Invertor" Factory", JSC
14 | * [http://www.sbp-invertor.ru]
15 | *
16 | * This file is part of JLibModbus.
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *
22 | * http://www.apache.org/licenses/LICENSE-2.0
23 | *
24 | * Unless required by applicable law or agreed to in writing, software
25 | * distributed under the License is distributed on an "AS IS" BASIS,
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 | * See the License for the specific language governing permissions and
28 | * limitations under the License.
29 | *
30 | * Authors: Vladislav Y. Kochedykov, software engineer.
31 | * email: vladislav.kochedykov@gmail.com
32 | */
33 | public class WriteSingleRegisterResponse extends AbstractWriteResponse {
34 |
35 | int value;
36 |
37 | public WriteSingleRegisterResponse() {
38 | super();
39 | }
40 |
41 | @Override
42 | final protected void readValue(ModbusInputStream fifo) throws IOException {
43 | try {
44 | setValue(fifo.readShortBE());
45 | } catch (ModbusNumberException e) {
46 | e.printStackTrace();
47 | }
48 | }
49 |
50 | @Override
51 | final protected void writeValue(ModbusOutputStream fifo) throws IOException {
52 | fifo.writeShortBE(getValue());
53 | }
54 |
55 | protected boolean checkValue() {
56 | return Modbus.checkRegisterValue(getValue());
57 | }
58 |
59 | @Override
60 | public int getFunction() {
61 | return ModbusFunctionCode.WRITE_SINGLE_REGISTER.toInt();
62 | }
63 |
64 | final public int getValue() {
65 | return value;
66 | }
67 |
68 | final public void setValue(int value) throws ModbusNumberException {
69 | this.value = ((short) value) & 0xffff;
70 | if (!checkValue())
71 | throw new ModbusNumberException("Error in register value", value);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/ModbusConnectionASCII.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.transport.ModbusTransportFactory;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
5 |
6 | /*
7 | * Copyright (C) 2016 "Invertor" Factory", JSC
8 | * [http://www.sbp-invertor.ru]
9 | *
10 | * This file is part of JLibModbus.
11 | *
12 | * Licensed under the Apache License, Version 2.0 (the "License");
13 | * you may not use this file except in compliance with the License.
14 | * You may obtain a copy of the License at
15 | *
16 | * http://www.apache.org/licenses/LICENSE-2.0
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the License is distributed on an "AS IS" BASIS,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the License for the specific language governing permissions and
22 | * limitations under the License.
23 | *
24 | * Authors: Vladislav Y. Kochedykov, software engineer.
25 | * email: vladislav.kochedykov@gmail.com
26 | */
27 | class ModbusConnectionASCII extends ModbusConnectionSerial {
28 |
29 | ModbusConnectionASCII(SerialPort serial) {
30 | super(serial, ModbusTransportFactory.createASCII(serial));
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/ModbusConnectionFactory.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
5 | import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
6 |
7 | import java.net.Socket;
8 |
9 | /*
10 | * Copyright (C) 2016 "Invertor" Factory", JSC
11 | * [http://www.sbp-invertor.ru]
12 | *
13 | * This file is part of JLibModbus.
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | *
27 | * Authors: Vladislav Y. Kochedykov, software engineer.
28 | * email: vladislav.kochedykov@gmail.com
29 | */
30 |
31 | public class ModbusConnectionFactory {
32 | static public ModbusConnection getASCII(SerialPort serial) {
33 | return new ModbusConnectionASCII(serial);
34 | }
35 |
36 | static public ModbusConnection getRTU(SerialPort serial) {
37 | return new ModbusConnectionRTU(serial);
38 | }
39 |
40 | static public ModbusConnection getTcpMaster(TcpParameters tcpParameters) {
41 | return new ModbusMasterConnectionTCP(tcpParameters);
42 | }
43 |
44 | static public ModbusConnection getTcpSlave(Socket socket) throws ModbusIOException {
45 | return new ModbusSlaveConnectionTCP(socket);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/ModbusConnectionRTU.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.transport.ModbusTransportFactory;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
5 |
6 | /*
7 | * Copyright (C) 2016 "Invertor" Factory", JSC
8 | * [http://www.sbp-invertor.ru]
9 | *
10 | * This file is part of JLibModbus.
11 | *
12 | * Licensed under the Apache License, Version 2.0 (the "License");
13 | * you may not use this file except in compliance with the License.
14 | * You may obtain a copy of the License at
15 | *
16 | * http://www.apache.org/licenses/LICENSE-2.0
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the License is distributed on an "AS IS" BASIS,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the License for the specific language governing permissions and
22 | * limitations under the License.
23 | *
24 | * Authors: Vladislav Y. Kochedykov, software engineer.
25 | * email: vladislav.kochedykov@gmail.com
26 | */
27 | class ModbusConnectionRTU extends ModbusConnectionSerial {
28 |
29 | ModbusConnectionRTU(SerialPort serial) {
30 | super(serial, ModbusTransportFactory.createRTU(serial));
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/ModbusConnectionSerial.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.LoggingInputStream;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.LoggingOutputStream;
6 | import com.intelligt.modbus.jlibmodbus.net.transport.ModbusTransport;
7 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
8 | import com.intelligt.modbus.jlibmodbus.serial.SerialPortException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 |
32 | /**
33 | * this class is an extension of the ModbusConnection class
34 | * to implement features for all serial port based connections.
35 | *
36 | * @see ModbusConnectionRTU
37 | * @see ModbusConnectionASCII
38 | * @see ModbusConnection
39 | */
40 | abstract class ModbusConnectionSerial extends ModbusConnection {
41 |
42 | /**
43 | * instance of SerialPort class,
44 | * which is a wrapper for concrete serial API implementation.
45 | *
46 | * @see SerialPort
47 | */
48 | final private SerialPort serial;
49 | final private ModbusTransport transport;
50 |
51 | ModbusConnectionSerial(SerialPort serial, ModbusTransport transport) {
52 | this.serial = serial;
53 | this.transport = transport;
54 | }
55 |
56 | @Override
57 | protected void openImpl() throws ModbusIOException {
58 | if (!isOpened()) {
59 | try {
60 | this.serial.open();
61 | } catch (SerialPortException e) {
62 | throw new ModbusIOException(e);
63 | }
64 | }
65 | }
66 |
67 | @Override
68 | protected void closeImpl() {
69 | this.serial.close();
70 | }
71 |
72 | @Override
73 | public LoggingOutputStream getOutputStream() {
74 | return transport.getOutputStream();
75 | }
76 |
77 | @Override
78 | public LoggingInputStream getInputStream() {
79 | return transport.getInputStream();
80 | }
81 |
82 | @Override
83 | public ModbusTransport getTransport() {
84 | return transport;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/InputStreamASCII.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusChecksumException;
5 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
6 | import com.intelligt.modbus.jlibmodbus.utils.DataUtils;
7 |
8 | import java.io.IOException;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | public class InputStreamASCII extends InputStreamSerial {
32 |
33 | private int lrc = 0;
34 |
35 | public InputStreamASCII(SerialPort serial) {
36 | super(serial);
37 | }
38 |
39 | private void lrcAdd(byte b) {
40 | lrc += b;
41 | }
42 |
43 | private int lrcGet() {
44 | return (byte) -lrc;
45 | }
46 |
47 | @Override
48 | public void frameCheck() throws IOException, ModbusChecksumException {
49 | int c_lrc = (byte) lrcGet();
50 | int r_lrc = (byte) read();
51 |
52 | int cr = readRaw();
53 | int lf = readRaw();
54 | // following checks delimiter has read (LF character by default)
55 | if (cr != Modbus.ASCII_CODE_CR || lf != Modbus.getAsciiMsgDelimiter())
56 | Modbus.log().warning("\\r\\n not received.");
57 | if (c_lrc != r_lrc) {
58 | throw new ModbusChecksumException(r_lrc, c_lrc);
59 | }
60 | }
61 |
62 | @Override
63 | public void frameInit() throws IOException {
64 | lrc = 0;
65 | char c = (char) readRaw();
66 | if (c != Modbus.ASCII_CODE_COLON) {
67 | throw new IOException("no bytes read");
68 | }
69 | }
70 |
71 | public int readRaw() throws IOException {
72 | return super.read();
73 | }
74 |
75 | @Override
76 | public int read() throws IOException {
77 | int b;
78 | char c = (char) readRaw();
79 | b = DataUtils.fromAscii(c, (char) readRaw());
80 | lrcAdd((byte) b);
81 | return b;
82 | }
83 |
84 | @Override
85 | public int read(byte[] b, int off, int len) throws IOException {
86 | for (int i = off; i < len; i++) {
87 | b[i] = (byte) read();
88 | }
89 | return len;
90 | }
91 | }
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/InputStreamRTU.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusChecksumException;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
5 | import com.intelligt.modbus.jlibmodbus.utils.CRC16;
6 |
7 | import java.io.IOException;
8 |
9 | /*
10 | * Copyright (C) 2016 "Invertor" Factory", JSC
11 | * [http://www.sbp-invertor.ru]
12 | *
13 | * This file is part of JLibModbus.
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | *
27 | * Authors: Vladislav Y. Kochedykov, software engineer.
28 | * email: vladislav.kochedykov@gmail.com
29 | */
30 | public class InputStreamRTU extends InputStreamSerial {
31 |
32 | private int crc = CRC16.INITIAL_VALUE;
33 |
34 | public InputStreamRTU(SerialPort serial) {
35 | super(serial);
36 | }
37 |
38 | @Override
39 | public void frameCheck() throws IOException, ModbusChecksumException {
40 | int c_crc = getCrc();
41 | int r_crc = readShortLE();
42 |
43 | if (c_crc != r_crc) {
44 | throw new ModbusChecksumException(r_crc, c_crc);
45 | }
46 | }
47 |
48 | @Override
49 | public void frameInit() {
50 | crc = CRC16.INITIAL_VALUE;
51 | }
52 |
53 | @Override
54 | public int read() throws IOException {
55 | int b = super.read();
56 | crc = CRC16.calc(crc, (byte) b);
57 | return b;
58 | }
59 |
60 | @Override
61 | public int read(byte[] b, int off, int len) throws IOException {
62 | int c = super.read(b, off, len);
63 | crc = CRC16.calc(crc, b, off, len);
64 | return c;
65 | }
66 |
67 | private int getCrc() {
68 | return crc;
69 | }
70 | }
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/InputStreamSerial.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusChecksumException;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.LoggingInputStream;
5 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
6 |
7 | import java.io.IOException;
8 |
9 | /*
10 | * Copyright (C) 2016 "Invertor" Factory", JSC
11 | * [http://www.sbp-invertor.ru]
12 | *
13 | * This file is part of JLibModbus.
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | *
27 | * Authors: Vladislav Y. Kochedykov, software engineer.
28 | * email: vladislav.kochedykov@gmail.com
29 | */
30 | public abstract class InputStreamSerial extends LoggingInputStream {
31 |
32 | InputStreamSerial(SerialPort serial) {
33 | super(serial.getInputStream());
34 | }
35 |
36 | /**
37 | * transport invokes it for validation of each frame
38 | *
39 | * @throws IOException when there is any communication trouble
40 | * @throws ModbusChecksumException when invalid frame has received
41 | */
42 | abstract public void frameCheck() throws IOException, ModbusChecksumException;
43 |
44 | /**
45 | * it should be invoked before reading of a frame.
46 | *
47 | * @throws IOException when there is any communication trouble
48 | */
49 | abstract public void frameInit() throws IOException;
50 | }
51 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/InputStreamTCP.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.LoggingInputStream;
5 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusInputStream;
6 |
7 | import java.io.BufferedInputStream;
8 | import java.io.IOException;
9 | import java.net.Socket;
10 | import java.net.SocketException;
11 |
12 | /*
13 | * Copyright (C) 2016 "Invertor" Factory", JSC
14 | * [http://www.sbp-invertor.ru]
15 | *
16 | * This file is part of JLibModbus.
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *
22 | * http://www.apache.org/licenses/LICENSE-2.0
23 | *
24 | * Unless required by applicable law or agreed to in writing, software
25 | * distributed under the License is distributed on an "AS IS" BASIS,
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 | * See the License for the specific language governing permissions and
28 | * limitations under the License.
29 | *
30 | * Authors: Vladislav Y. Kochedykov, software engineer.
31 | * email: vladislav.kochedykov@gmail.com
32 | */
33 | public class InputStreamTCP extends LoggingInputStream {
34 |
35 | public InputStreamTCP(final Socket s) throws IOException {
36 | super(new ModbusInputStream() {
37 |
38 | final private Socket socket = s;
39 | final private BufferedInputStream in = new BufferedInputStream(s.getInputStream());
40 |
41 | @Override
42 | public int read() throws IOException {
43 | int c = in.read();
44 | if (-1 == c) {
45 | throw new IOException("Input stream is closed");
46 | }
47 | return c;
48 | }
49 |
50 | @Override
51 | public int read(byte[] b, int off, int len) throws IOException {
52 | int count = 0;
53 | int k = 0;
54 | while (count < len && k != -1) {
55 | k = in.read(b, off + count, len - count);
56 | if (-1 != k)
57 | count += k;
58 | }
59 | return count;
60 | }
61 |
62 | @Override
63 | public void setReadTimeout(int readTimeout) {
64 | try {
65 | socket.setSoTimeout(readTimeout);
66 | } catch (SocketException e) {
67 | Modbus.log().warning(e.getLocalizedMessage());
68 | }
69 | }
70 |
71 | @Override
72 | public void close() throws IOException {
73 | in.close();
74 | }
75 | });
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/OutputStreamASCII.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
5 | import com.intelligt.modbus.jlibmodbus.utils.DataUtils;
6 |
7 | import java.io.IOException;
8 | import java.nio.charset.Charset;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | public class OutputStreamASCII extends OutputStreamSerial {
32 |
33 | private int lrc = 0;
34 |
35 | public OutputStreamASCII(SerialPort serial) {
36 | super(serial);
37 | reset();
38 | }
39 |
40 | @Override
41 | public void write(byte[] bytes) throws IOException {
42 | write(bytes, 0, bytes.length);
43 | }
44 |
45 | @Override
46 | public void write(byte[] bytes, int offset, int length) throws IOException {
47 | int tail = offset+length;
48 | for (int i = offset; i < tail; i++) {
49 | lrc += bytes[i];
50 | }
51 | byte[] ascii = DataUtils.toAscii(bytes, offset, length).getBytes(Charset.forName("ASCII"));
52 | super.write(ascii);
53 | }
54 |
55 | @Override
56 | public void write(int b) throws IOException {
57 | lrc += (byte) b;
58 | byte[] bytes = DataUtils.toAscii((byte) b).getBytes(Charset.forName("ASCII"));
59 | super.write(bytes);
60 | }
61 |
62 | private void writeChecksum() throws IOException {
63 | byte[] bytes = DataUtils.toAscii((byte) -lrc).getBytes(Charset.forName("ASCII"));
64 | super.write(bytes);
65 | }
66 |
67 | public void writeRaw(int b) throws IOException {
68 | super.write(b);
69 | }
70 |
71 | @Override
72 | public void flush() throws IOException {
73 | writeChecksum();
74 | writeRaw(Modbus.ASCII_CODE_CR);
75 | writeRaw(Modbus.ASCII_CODE_LF);
76 | super.flush();
77 | reset();
78 | }
79 |
80 | public void reset() {
81 | try {
82 | lrc = 0;
83 | writeRaw(Modbus.ASCII_CODE_COLON);
84 | } catch (IOException e) {
85 | e.printStackTrace();
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/OutputStreamRTU.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
4 | import com.intelligt.modbus.jlibmodbus.utils.CRC16;
5 |
6 | import java.io.IOException;
7 |
8 | /*
9 | * Copyright (C) 2016 "Invertor" Factory", JSC
10 | * [http://www.sbp-invertor.ru]
11 | *
12 | * This file is part of JLibModbus.
13 | *
14 | * Licensed under the Apache License, Version 2.0 (the "License");
15 | * you may not use this file except in compliance with the License.
16 | * You may obtain a copy of the License at
17 | *
18 | * http://www.apache.org/licenses/LICENSE-2.0
19 | *
20 | * Unless required by applicable law or agreed to in writing, software
21 | * distributed under the License is distributed on an "AS IS" BASIS,
22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 | * See the License for the specific language governing permissions and
24 | * limitations under the License.
25 | *
26 | * Authors: Vladislav Y. Kochedykov, software engineer.
27 | * email: vladislav.kochedykov@gmail.com
28 | */
29 | public class OutputStreamRTU extends OutputStreamSerial {
30 |
31 | public OutputStreamRTU(SerialPort serial) {
32 | super(serial);
33 | }
34 |
35 | @Override
36 | public void flush() throws IOException {
37 | writeShortLE(CRC16.calc(toByteArray()));
38 | super.flush();
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/OutputStreamSerial.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.stream.base.LoggingOutputStream;
4 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
5 |
6 | /*
7 | * Copyright (C) 2016 "Invertor" Factory", JSC
8 | * [http://www.sbp-invertor.ru]
9 | *
10 | * This file is part of JLibModbus.
11 | *
12 | * Licensed under the Apache License, Version 2.0 (the "License");
13 | * you may not use this file except in compliance with the License.
14 | * You may obtain a copy of the License at
15 | *
16 | * http://www.apache.org/licenses/LICENSE-2.0
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the License is distributed on an "AS IS" BASIS,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the License for the specific language governing permissions and
22 | * limitations under the License.
23 | *
24 | * Authors: Vladislav Y. Kochedykov, software engineer.
25 | * email: vladislav.kochedykov@gmail.com
26 | */
27 | public class OutputStreamSerial extends LoggingOutputStream {
28 |
29 | OutputStreamSerial(SerialPort serial) {
30 | super(serial.getOutputStream());
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/OutputStreamTCP.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.stream.base.LoggingOutputStream;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.base.ModbusOutputStream;
5 |
6 | import java.io.IOException;
7 | import java.net.Socket;
8 |
9 | /*
10 | * Copyright (C) 2016 "Invertor" Factory", JSC
11 | * [http://www.sbp-invertor.ru]
12 | *
13 | * This file is part of JLibModbus.
14 | *
15 | * Licensed under the Apache License, Version 2.0 (the "License");
16 | * you may not use this file except in compliance with the License.
17 | * You may obtain a copy of the License at
18 | *
19 | * http://www.apache.org/licenses/LICENSE-2.0
20 | *
21 | * Unless required by applicable law or agreed to in writing, software
22 | * distributed under the License is distributed on an "AS IS" BASIS,
23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 | * See the License for the specific language governing permissions and
25 | * limitations under the License.
26 | *
27 | * Authors: Vladislav Y. Kochedykov, software engineer.
28 | * email: vladislav.kochedykov@gmail.com
29 | */
30 | public class OutputStreamTCP extends LoggingOutputStream {
31 |
32 | public OutputStreamTCP(final Socket s) throws IOException {
33 | super(new ModbusOutputStream() {
34 | final Socket socket = s;
35 |
36 | @Override
37 | public void flush() throws IOException {
38 | try {
39 | byte[] bytes = toByteArray();
40 | socket.getOutputStream().write(bytes);
41 | socket.getOutputStream().flush();
42 | } catch (Exception e) {
43 | throw new IOException(e);
44 | } finally {
45 | super.flush();
46 | }
47 | }
48 |
49 | @Override
50 | public void close() throws IOException {
51 | socket.getOutputStream().close();
52 | }
53 | });
54 | }
55 | }
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/base/LoggingInputStream.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.utils.*;
5 |
6 | import java.io.IOException;
7 |
8 | /*
9 | * Copyright (C) 2016 "Invertor" Factory", JSC
10 | * [http://www.sbp-invertor.ru]
11 | *
12 | * This file is part of JLibModbus.
13 | *
14 | * Licensed under the Apache License, Version 2.0 (the "License");
15 | * you may not use this file except in compliance with the License.
16 | * You may obtain a copy of the License at
17 | *
18 | * http://www.apache.org/licenses/LICENSE-2.0
19 | *
20 | * Unless required by applicable law or agreed to in writing, software
21 | * distributed under the License is distributed on an "AS IS" BASIS,
22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 | * See the License for the specific language governing permissions and
24 | * limitations under the License.
25 | *
26 | * Authors: Vladislav Y. Kochedykov, software engineer.
27 | * email: vladislav.kochedykov@gmail.com
28 | */
29 |
30 | /**
31 | * this class allows to log a content of the input stream stream.
32 | *
33 | * @author kochedykov
34 | * @since 1.2
35 | */
36 | public class LoggingInputStream extends ModbusInputStream {
37 |
38 | private FrameEventListenerList listenerList = new FrameEventListenerListImpl();
39 | final static private String LOG_MESSAGE_TITLE = "Frame recv: ";
40 | /**
41 | * The input stream to be logged
42 | */
43 | final private ModbusInputStream in;
44 | final private ByteFifo fifo = new ByteFifo(Modbus.MAX_PDU_LENGTH);
45 |
46 | public LoggingInputStream(ModbusInputStream in) {
47 | this.in = in;
48 | }
49 |
50 | @Override
51 | public int read() throws IOException {
52 | int b = in.read();
53 | if (Modbus.isLoggingEnabled()) {
54 | fifo.write(b);
55 | }
56 | return b;
57 | }
58 |
59 | @Override
60 | public int read(byte[] b, int off, int len) throws IOException {
61 | int read = in.read(b, off, len);
62 | if (Modbus.isLoggingEnabled()) {
63 | fifo.write(b, off, read);
64 | }
65 | return read;
66 | }
67 |
68 | @Override
69 | public void setReadTimeout(int readTimeout) {
70 | in.setReadTimeout(readTimeout);
71 | }
72 |
73 | public void log() {
74 | if (Modbus.isLoggingEnabled() && fifo.size() > 0) {
75 | byte[] bytes = fifo.toByteArray();
76 | Modbus.log().info(LOG_MESSAGE_TITLE + DataUtils.toAscii(bytes));
77 | listenerList.fireFrameReceivedEvent(new FrameEvent(bytes));
78 | fifo.reset();
79 | }
80 | }
81 |
82 | public void setListenerList(FrameEventListenerList listenerList) {
83 | this.listenerList = listenerList;
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/stream/base/ModbusOutputStream.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.stream.base;
2 |
3 | import com.intelligt.modbus.jlibmodbus.Modbus;
4 | import com.intelligt.modbus.jlibmodbus.utils.ByteFifo;
5 | import com.intelligt.modbus.jlibmodbus.utils.DataUtils;
6 |
7 | import java.io.IOException;
8 | import java.io.OutputStream;
9 |
10 | /*
11 | * Copyright (C) 2016 "Invertor" Factory", JSC
12 | * [http://www.sbp-invertor.ru]
13 | *
14 | * This file is part of JLibModbus.
15 | *
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | *
28 | * Authors: Vladislav Y. Kochedykov, software engineer.
29 | * email: vladislav.kochedykov@gmail.com
30 | */
31 | abstract public class ModbusOutputStream extends OutputStream {
32 |
33 | private final ByteFifo fifo = new ByteFifo(Modbus.MAX_TCP_ADU_LENGTH);
34 |
35 | @Override
36 | public void write(byte[] b) throws IOException {
37 | fifo.write(b);
38 | }
39 |
40 | @Override
41 | public void write(byte b[], int off, int len) throws IOException {
42 | fifo.write(b, off, len);
43 | }
44 |
45 | @Override
46 | public void write(int b) throws IOException {
47 | fifo.write(b);
48 | }
49 |
50 | /**
51 | * it should have invoked last
52 | */
53 | @Override
54 | public void flush() throws IOException {
55 | fifo.reset();
56 | }
57 |
58 | public void writeShortBE(int s) throws IOException {
59 | write(DataUtils.byteHigh(s));
60 | write(DataUtils.byteLow(s));
61 | }
62 |
63 | public void writeShortLE(int s) throws IOException {
64 | write(DataUtils.byteLow(s));
65 | write(DataUtils.byteHigh(s));
66 | }
67 |
68 | public byte[] toByteArray() {
69 | return fifo.toByteArray();
70 | }
71 |
72 | public ByteFifo getFifo() {
73 | return fifo;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/transport/ModbusTransportASCII.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.transport;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.stream.InputStreamASCII;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.OutputStreamASCII;
5 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | class ModbusTransportASCII extends ModbusTransportSerial {
29 |
30 | ModbusTransportASCII(SerialPort serial) {
31 | super(new InputStreamASCII(serial), new OutputStreamASCII(serial));
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/transport/ModbusTransportFactory.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.transport;
2 |
3 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
4 |
5 | import java.io.IOException;
6 | import java.net.Socket;
7 |
8 | /*
9 | * Copyright (C) 2016 "Invertor" Factory", JSC
10 | * [http://www.sbp-invertor.ru]
11 | *
12 | * This file is part of JLibModbus.
13 | *
14 | * Licensed under the Apache License, Version 2.0 (the "License");
15 | * you may not use this file except in compliance with the License.
16 | * You may obtain a copy of the License at
17 | *
18 | * http://www.apache.org/licenses/LICENSE-2.0
19 | *
20 | * Unless required by applicable law or agreed to in writing, software
21 | * distributed under the License is distributed on an "AS IS" BASIS,
22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 | * See the License for the specific language governing permissions and
24 | * limitations under the License.
25 | *
26 | * Authors: Vladislav Y. Kochedykov, software engineer.
27 | * email: vladislav.kochedykov@gmail.com
28 | */
29 |
30 | public class ModbusTransportFactory {
31 | static public ModbusTransport createRTU(SerialPort serial) {
32 | return new ModbusTransportRTU(serial);
33 | }
34 |
35 | static public ModbusTransport createASCII(SerialPort serial) {
36 | return new ModbusTransportASCII(serial);
37 | }
38 |
39 | static public ModbusTransport createTCP(Socket socket) throws IOException {
40 | return new ModbusTransportTCP(socket);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/transport/ModbusTransportRTU.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.transport;
2 |
3 | import com.intelligt.modbus.jlibmodbus.net.stream.InputStreamRTU;
4 | import com.intelligt.modbus.jlibmodbus.net.stream.OutputStreamRTU;
5 | import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
6 |
7 | /*
8 | * Copyright (C) 2016 "Invertor" Factory", JSC
9 | * [http://www.sbp-invertor.ru]
10 | *
11 | * This file is part of JLibModbus.
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | *
25 | * Authors: Vladislav Y. Kochedykov, software engineer.
26 | * email: vladislav.kochedykov@gmail.com
27 | */
28 | class ModbusTransportRTU extends ModbusTransportSerial {
29 |
30 | ModbusTransportRTU(SerialPort serial) {
31 | super(new InputStreamRTU(serial), new OutputStreamRTU(serial));
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/net/transport/ModbusTransportSerial.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.net.transport;
2 |
3 | import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
4 | import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
5 | import com.intelligt.modbus.jlibmodbus.msg.ModbusMessageFactory;
6 | import com.intelligt.modbus.jlibmodbus.msg.base.ModbusMessage;
7 | import com.intelligt.modbus.jlibmodbus.net.stream.InputStreamSerial;
8 | import com.intelligt.modbus.jlibmodbus.net.stream.OutputStreamSerial;
9 |
10 | import java.io.IOException;
11 |
12 | /*
13 | * Copyright (C) 2016 "Invertor" Factory", JSC
14 | * [http://www.sbp-invertor.ru]
15 | *
16 | * This file is part of JLibModbus.
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *
22 | * http://www.apache.org/licenses/LICENSE-2.0
23 | *
24 | * Unless required by applicable law or agreed to in writing, software
25 | * distributed under the License is distributed on an "AS IS" BASIS,
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 | * See the License for the specific language governing permissions and
28 | * limitations under the License.
29 | *
30 | * Authors: Vladislav Y. Kochedykov, software engineer.
31 | * email: vladislav.kochedykov@gmail.com
32 | */
33 | class ModbusTransportSerial extends ModbusTransport {
34 |
35 | ModbusTransportSerial(InputStreamSerial is, OutputStreamSerial os) {
36 | super(is, os);
37 | }
38 |
39 | @Override
40 | protected ModbusMessage read(ModbusMessageFactory factory) throws ModbusIOException, ModbusNumberException {
41 | if (getInputStream() instanceof InputStreamSerial) {
42 | InputStreamSerial is = (InputStreamSerial) getInputStream();
43 | try {
44 | is.frameInit();
45 | ModbusMessage msg = createMessage(factory);
46 | is.frameCheck();
47 | return msg;
48 | } catch (IOException ioe) {
49 | throw new ModbusIOException(ioe);
50 | }
51 | } else {
52 | throw new ModbusIOException("Can't cast getInputStream() to InputStreamSerial");
53 | }
54 | }
55 |
56 | @Override
57 | protected void sendImpl(ModbusMessage msg) throws ModbusIOException {
58 | msg.write(getOutputStream());
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/serial/SerialParameters.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.serial;
2 |
3 | /*
4 | * Copyright (C) 2016 "Invertor" Factory", JSC
5 | * [http://www.sbp-invertor.ru]
6 | *
7 | * This file is part of JLibModbus.
8 | *
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | *
21 | * Authors: Vladislav Y. Kochedykov, software engineer.
22 | * email: vladislav.kochedykov@gmail.com
23 | */
24 | public class SerialParameters {
25 | private String device = null;
26 | private SerialPort.BaudRate baudRate;
27 | private int dataBits;
28 | private int stopBits;
29 | private SerialPort.Parity parity;
30 |
31 | public SerialParameters() {
32 | setBaudRate(SerialPort.BaudRate.BAUD_RATE_115200);
33 | setDataBits(8);
34 | setStopBits(1);
35 | setParity(SerialPort.Parity.NONE);
36 | }
37 |
38 | /**
39 | * @param device the name(path) of the serial port
40 | * @param baudRate baud rate
41 | * @param dataBits the number of data bits
42 | * @param stopBits the number of stop bits(1,2)
43 | * @param parity parity check (NONE, EVEN, ODD, MARK, SPACE)
44 | */
45 | public SerialParameters(String device, SerialPort.BaudRate baudRate, int dataBits, int stopBits, SerialPort.Parity parity) {
46 | setDevice(device);
47 | setBaudRate(baudRate);
48 | setDataBits(dataBits);
49 | setStopBits(stopBits);
50 | setParity(parity);
51 | }
52 |
53 | public String getDevice() {
54 | return device;
55 | }
56 |
57 | public void setDevice(String device) {
58 | this.device = device;
59 | }
60 |
61 | public int getBaudRate() {
62 | return baudRate.getValue();
63 | }
64 |
65 | public void setBaudRate(SerialPort.BaudRate baudRate) {
66 | this.baudRate = baudRate;
67 | }
68 |
69 | public int getDataBits() {
70 | return dataBits;
71 | }
72 |
73 | public void setDataBits(int dataBits) {
74 | this.dataBits = dataBits;
75 | }
76 |
77 | public int getStopBits() {
78 | return stopBits;
79 | }
80 |
81 | public void setStopBits(int stopBits) {
82 | this.stopBits = stopBits;
83 | }
84 |
85 | public SerialPort.Parity getParity() {
86 | return parity;
87 | }
88 |
89 | public void setParity(SerialPort.Parity parity) {
90 | this.parity = parity;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/com/intelligt/modbus/jlibmodbus/serial/SerialPortAbstractFactory.java:
--------------------------------------------------------------------------------
1 | package com.intelligt.modbus.jlibmodbus.serial;
2 |
3 | /*
4 | * Copyright (C) 2017 Vladislav Y. Kochedykov
5 | *
6 | * [http://jlibmodbus.sourceforge.net]
7 | *
8 | * This file is part of JLibModbus.
9 | *
10 | * Licensed under the Apache License, Version 2.0 (the "License");
11 | * you may not use this file except in compliance with the License.
12 | * You may obtain a copy of the License at
13 | *
14 | * http://www.apache.org/licenses/LICENSE-2.0
15 | *
16 | * Unless required by applicable law or agreed to in writing, software
17 | * distributed under the License is distributed on an "AS IS" BASIS,
18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 | * See the License for the specific language governing permissions and
20 | * limitations under the License.
21 | *
22 | * Authors: Vladislav Y. Kochedykov, software engineer.
23 | * email: vladislav.kochedykov@gmail.com
24 | */
25 |
26 | import java.util.List;
27 |
28 | abstract public class SerialPortAbstractFactory {
29 |
30 | protected SerialPortAbstractFactory() {
31 | }
32 |
33 | final public String getUnavailableString() {
34 | return "Connector is missing";
35 | }
36 |
37 | abstract public SerialPort createSerialImpl(SerialParameters sp) throws SerialPortException;
38 | abstract public List