├── .classpath ├── .gitignore ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── AndroidManifest.xml ├── README.md ├── bin ├── ModbusDroid.apk └── classes.dex ├── project.properties ├── res ├── _pre-production_src-files-drawables │ ├── Connect.svg │ ├── QR_code.png │ ├── QR_code_med.png │ ├── disconnect.svg │ ├── edit.png │ ├── edit.svg │ ├── edit_svg.png │ ├── edit_svg_96x96.png │ ├── edit_svg_96x96.xcf │ ├── ic_dialog_edit.png │ ├── ic_launcher.zip │ ├── ic_menu_connect.zip │ ├── ic_menu_connect_144x144.png │ ├── ic_menu_connect_144x144_2.png │ ├── ic_menu_disconnect.zip │ ├── ic_menu_disconnect_144x144.png │ ├── ic_menu_disconnect_144x144_2.png │ ├── modbusdroid_icon_2.0.svg │ ├── modbusdroid_icon_48x48.png │ ├── modbusdroid_icon_864x864.png │ ├── modbusdroid_promo_180x120.png │ ├── modbusdroid_promo_512x512.png │ ├── path5622.png │ └── screenshots │ │ ├── connected.png │ │ ├── settings-0.9.1.png │ │ ├── settings.png │ │ └── write_dialog.png ├── drawable-hdpi │ ├── ic_dialog_edit.png │ ├── ic_launcher_modbusdroid.png │ ├── ic_menu_connect.png │ ├── ic_menu_disconnect.png │ └── icon.png ├── drawable-ldpi │ ├── ic_dialog_edit.png │ ├── ic_launcher_modbusdroid.png │ ├── ic_menu_connect.png │ ├── ic_menu_disconnect.png │ └── icon.png ├── drawable-mdpi │ ├── ic_dialog_edit.png │ ├── ic_launcher.png │ ├── ic_launcher_modbusdroid.png │ ├── ic_menu_connect.png │ ├── ic_menu_disconnect.png │ └── icon.png ├── drawable-xhdpi │ ├── ic_launcher_modbusdroid.png │ ├── ic_menu_connect.png │ └── ic_menu_disconnect.png ├── layout-land │ └── main.xml ├── layout-port │ └── main.xml ├── layout │ ├── company_info_layout.xml │ ├── modbus_value_row.xml │ └── write_value_numeric.xml ├── values │ ├── arrays.xml │ └── strings.xml └── xml │ └── preferences.xml └── src └── com ├── bencatlin └── modbusdroid │ ├── OldVersion │ ├── CustomDialogPreference.java │ ├── MbDroidMsgExceptionHandler.java │ ├── ModbusDroid.java │ ├── ModbusListView.java │ ├── ModbusMultiLocator.java │ ├── ModbusTCPFactory.java │ ├── ModbusTCPMaster.java │ ├── PollModbus.java │ └── connectionSettings.java │ ├── StartupActivity.java │ ├── activities │ └── ModbusDroidActivity.java │ ├── fragments │ └── ModbusParametersFragment.java │ └── utils │ ├── FroyoSharedPreferenceSaver.java │ ├── GingerbreadSharedPreferenceSaver.java │ ├── HoneycombStrictMode.java │ ├── InteractiveResumeBackupAgent.java │ ├── LegacySharedPreferenceSaver.java │ └── base │ ├── IStrictMode.java │ └── SharedPreferenceSaver.java └── serotonin └── modbus4j ├── BasicProcessImage.java ├── BatchRead.java ├── BatchResults.java ├── ExceptionResult.java ├── Modbus.java ├── ModbusFactory.java ├── ModbusLocator.java ├── ModbusMaster.java ├── ModbusSlaveSet.java ├── NodeScanListener.java ├── ProcessImage.java ├── ProcessImageListener.java ├── base ├── BaseMessageParser.java ├── BaseRequestHandler.java ├── KeyedModbusLocator.java ├── ModbusUtils.java ├── RangeAndOffset.java ├── ReadFunctionGroup.java ├── SlaveAndRange.java └── SlaveProfile.java ├── code ├── DataType.java ├── ExceptionCode.java ├── FunctionCode.java └── RegisterRange.java ├── exception ├── ErrorResponseException.java ├── IllegalDataAddressException.java ├── IllegalDataTypeException.java ├── IllegalFunctionException.java ├── IllegalSlaveIdException.java ├── InvalidDataConversionException.java ├── ModbusIdException.java ├── ModbusInitException.java └── ModbusTransportException.java ├── ip ├── IpMessage.java ├── IpMessageResponse.java ├── IpParameters.java ├── encap │ ├── EncapMessage.java │ ├── EncapMessageParser.java │ ├── EncapMessageRequest.java │ ├── EncapMessageResponse.java │ ├── EncapRequestHandler.java │ └── EncapWaitingRoomKey.java ├── tcp │ ├── TcpMaster.java │ └── TcpSlave.java ├── udp │ ├── UdpMaster.java │ └── UdpSlave.java └── xa │ ├── XaMessage.java │ ├── XaMessageParser.java │ ├── XaMessageRequest.java │ ├── XaMessageResponse.java │ ├── XaRequestHandler.java │ └── XaWaitingRoomKey.java ├── msg ├── ExceptionRequest.java ├── ExceptionResponse.java ├── ModbusMessage.java ├── ModbusRequest.java ├── ModbusResponse.java ├── ReadBinaryRequest.java ├── ReadCoilsRequest.java ├── ReadCoilsResponse.java ├── ReadDiscreteInputsRequest.java ├── ReadDiscreteInputsResponse.java ├── ReadExceptionStatusRequest.java ├── ReadExceptionStatusResponse.java ├── ReadHoldingRegistersRequest.java ├── ReadHoldingRegistersResponse.java ├── ReadInputRegistersRequest.java ├── ReadInputRegistersResponse.java ├── ReadNumericRequest.java ├── ReadResponse.java ├── ReportSlaveIdRequest.java ├── ReportSlaveIdResponse.java ├── WriteCoilRequest.java ├── WriteCoilResponse.java ├── WriteCoilsRequest.java ├── WriteCoilsResponse.java ├── WriteMaskRegisterRequest.java ├── WriteMaskRegisterResponse.java ├── WriteRegisterRequest.java ├── WriteRegisterResponse.java ├── WriteRegistersRequest.java └── WriteRegistersResponse.java └── value └── ModbusValue.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | /bin 15 | 16 | # Local configuration file (sdk path, etc) 17 | local.properties 18 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ModbusDroid 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Mon Apr 19 14:51:48 CDT 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.5 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.source=1.5 13 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Modbus-Droid](http://www.bencatlin.com/sofware-projects/modbus-droid/)](http://www.bencatlin.com/sofware-projects/modbus-droid/) 2 | 3 | ## Intro 4 | 5 | I'm a Electrical Engineer working in the industrial controls industry with a software engineer / general computer nerd bent. A while back I wanted to get into some phone development, and I found that a lot of times on site with a customer it would be really handy to not have to have my laptop to troubleshoot a signal. Well, since where I works deals predominantly in ModbusTCP I thought I could marry the two together. 6 | 7 | I found a library that looked good (modbus4j), and sank my teeth into my first Android app 8 | 9 | ## How-to 10 | 11 | 12 | ## Resources 13 | 14 | ## Credits 15 | 16 | Original Author: Ben Catlin, ben@bencatlin.com 17 | 18 | 19 | -------------------------------------------------------------------------------- /bin/ModbusDroid.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/bin/ModbusDroid.apk -------------------------------------------------------------------------------- /bin/classes.dex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/bin/classes.dex -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system use, 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | 10 | # Indicates whether an apk should be generated for each density. 11 | split.density=false 12 | # Project target. 13 | target=android-15 14 | -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/QR_code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/QR_code.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/QR_code_med.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/QR_code_med.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/edit.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/edit.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 21 | 39 | 41 | 43 | 47 | 51 | 52 | 54 | 58 | 62 | 63 | 64 | 66 | 67 | 69 | image/svg+xml 70 | 72 | 73 | 74 | 75 | 76 | 80 | 87 | 88 | 93 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/edit_svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/edit_svg.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/edit_svg_96x96.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/edit_svg_96x96.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/edit_svg_96x96.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/edit_svg_96x96.xcf -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_dialog_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_dialog_edit.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_launcher.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_launcher.zip -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_menu_connect.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_menu_connect.zip -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_menu_connect_144x144.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_menu_connect_144x144.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_menu_connect_144x144_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_menu_connect_144x144_2.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_menu_disconnect.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_menu_disconnect.zip -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_menu_disconnect_144x144.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_menu_disconnect_144x144.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/ic_menu_disconnect_144x144_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/ic_menu_disconnect_144x144_2.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/modbusdroid_icon_48x48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/modbusdroid_icon_48x48.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/modbusdroid_icon_864x864.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/modbusdroid_icon_864x864.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/modbusdroid_promo_180x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/modbusdroid_promo_180x120.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/modbusdroid_promo_512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/modbusdroid_promo_512x512.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/path5622.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/path5622.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/screenshots/connected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/screenshots/connected.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/screenshots/settings-0.9.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/screenshots/settings-0.9.1.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/screenshots/settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/screenshots/settings.png -------------------------------------------------------------------------------- /res/_pre-production_src-files-drawables/screenshots/write_dialog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/_pre-production_src-files-drawables/screenshots/write_dialog.png -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_dialog_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-hdpi/ic_dialog_edit.png -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher_modbusdroid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-hdpi/ic_launcher_modbusdroid.png -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_menu_connect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-hdpi/ic_menu_connect.png -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_menu_disconnect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-hdpi/ic_menu_disconnect.png -------------------------------------------------------------------------------- /res/drawable-hdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-hdpi/icon.png -------------------------------------------------------------------------------- /res/drawable-ldpi/ic_dialog_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-ldpi/ic_dialog_edit.png -------------------------------------------------------------------------------- /res/drawable-ldpi/ic_launcher_modbusdroid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-ldpi/ic_launcher_modbusdroid.png -------------------------------------------------------------------------------- /res/drawable-ldpi/ic_menu_connect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-ldpi/ic_menu_connect.png -------------------------------------------------------------------------------- /res/drawable-ldpi/ic_menu_disconnect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-ldpi/ic_menu_disconnect.png -------------------------------------------------------------------------------- /res/drawable-ldpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-ldpi/icon.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_dialog_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-mdpi/ic_dialog_edit.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher_modbusdroid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-mdpi/ic_launcher_modbusdroid.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_menu_connect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-mdpi/ic_menu_connect.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_menu_disconnect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-mdpi/ic_menu_disconnect.png -------------------------------------------------------------------------------- /res/drawable-mdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-mdpi/icon.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher_modbusdroid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-xhdpi/ic_launcher_modbusdroid.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_menu_connect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-xhdpi/ic_menu_connect.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_menu_disconnect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigcat/ModbusDroid/2a44a7daa5a413791d173507d70c9b18791b50af/res/drawable-xhdpi/ic_menu_disconnect.png -------------------------------------------------------------------------------- /res/layout-land/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 16 | 24 | 31 | 32 | 33 | 45 | 58 | 59 | 60 | 61 | 70 | 71 | 72 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /res/layout-port/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 23 | 30 | 38 | 39 | 40 | 52 | 64 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /res/layout/company_info_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | -------------------------------------------------------------------------------- /res/layout/modbus_value_row.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /res/layout/write_value_numeric.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 21 | 22 | -------------------------------------------------------------------------------- /res/values/arrays.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Holding Coils 5 | Discrete Inputs 6 | Holding Registers 7 | Input Registers 8 | 9 | 10 | 11 | Binary 12 | Unsigned Integer (16bit) 13 | Signed Integer (16bit) 14 | Unsigned Integer (32bit) 15 | Signed Integer (32bit) 16 | Unsigned Int (32bit word-swapped) 17 | Signed Int (32bit word-swapped) 18 | Float (32bit) 19 | Float (32bit word-swapped) 20 | Unsigned Integer (64bit) 21 | Signed Integer (64bit) 22 | Unsigned Integer (64bit word-swapped) 23 | Signed Integer (64bit word-swapped) 24 | Float (64bit) 25 | Float (64bit word-swapped) 26 | BCD (16bit) 27 | BCD (32bit) 28 | 29 | 30 | 31 | False 32 | True 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Modbus-Droid 4 | Select a Modbus Point Type 5 | Connection Settings 6 | ModbusDroidSharedPrefs 7 | 8 | Brought to you by BigCat Inc.
10 |

For more information about the app and development updates check out BenCatlin.com.

11 |

This software is based on the Modbus library Modbus4J, created by Seretonin software.

12 |

This app is also open source, licensed under the GPL version 3, and its source code is hosted and available at Launchpad.net/modbusdroid.

]]> 13 |
14 | 15 |
16 | -------------------------------------------------------------------------------- /res/xml/preferences.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 14 | 23 | 24 | 25 | 35 | 44 | 45 | 46 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/OldVersion/CustomDialogPreference.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid.OldVersion; 2 | 3 | import com.bencatlin.modbusdroid.R; 4 | 5 | import android.content.Context; 6 | import android.preference.DialogPreference; 7 | import android.text.Html; 8 | import android.util.AttributeSet; 9 | import android.view.View; 10 | import android.widget.TextView; 11 | 12 | public class CustomDialogPreference extends DialogPreference { 13 | 14 | private TextView companyInfoTextBox; 15 | private Context mContext; 16 | 17 | public CustomDialogPreference(Context context, AttributeSet attrs) { 18 | super(context, attrs); 19 | setDialogLayoutResource(R.layout.company_info_layout); 20 | mContext = context; 21 | } 22 | 23 | @Override 24 | protected void onBindDialogView (View view) { 25 | super.onBindDialogView(view); 26 | 27 | companyInfoTextBox = (TextView) view.findViewById(R.id.company_info_textview); 28 | //This sets the dialog text to a string resource formatted as HTML 29 | companyInfoTextBox.setText(Html.fromHtml(mContext.getString(R.string.company_info_text) ) ); 30 | /*companyInfoTextBox.setText(Html.fromHtml("

Brought to you by BigCat Inc.


" + 31 | "

For more information please check out BenCatlin.com/software-projects/modbus-droid.


This software is based on the Modbus library Modbus4J, created by Seretonin software.


" + 32 | "

This app is also open source, licensed under the GPL version 3, and its source code is hosted and available at Launchpad.net/modbusdroid. Source code hosting will eventually be available on github. Watch our main site for the announcement of when that takes place.


") );*/ 33 | } 34 | } -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/OldVersion/MbDroidMsgExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid.OldVersion; 2 | 3 | import android.os.Handler; 4 | import android.os.Message; 5 | import android.util.Log; 6 | import com.serotonin.messaging.MessagingExceptionHandler; 7 | 8 | 9 | 10 | public class MbDroidMsgExceptionHandler implements MessagingExceptionHandler { 11 | 12 | private Handler mainThreadHandler; 13 | private Message m; 14 | 15 | public MbDroidMsgExceptionHandler (Handler h) { 16 | this.mainThreadHandler = h; 17 | } 18 | 19 | 20 | public void receivedException(Exception genericException) { 21 | // TODO Auto-generated method stub 22 | Log.e(getClass().getSimpleName(), genericException.getMessage() ); 23 | m.arg1 = -1; 24 | m.obj = genericException.getMessage(); 25 | mainThreadHandler.sendMessage(m); 26 | } 27 | 28 | public void receivedMessageMismatchException(Exception mismatchException) { 29 | // TODO Auto-generated method stub 30 | Log.e(getClass().getSimpleName(), mismatchException.getMessage() ); 31 | m.arg1 = -1; 32 | m.obj = mismatchException.getMessage(); 33 | mainThreadHandler.sendMessage(m); 34 | } 35 | 36 | public void receivedResponseException(Exception responseException) { 37 | // TODO Auto-generated method stub 38 | Log.e(getClass().getSimpleName(), responseException.getMessage() ); 39 | m.arg1 = -1; 40 | m.obj = responseException.getMessage(); 41 | mainThreadHandler.sendMessage(m); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/OldVersion/ModbusTCPFactory.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid.OldVersion; 2 | 3 | 4 | import com.serotonin.modbus4j.ModbusFactory; 5 | import com.serotonin.modbus4j.ip.IpParameters; 6 | 7 | 8 | 9 | public class ModbusTCPFactory extends ModbusFactory { 10 | 11 | public ModbusTCPMaster createModbusTCPMaster(IpParameters params, boolean keepAlive) { 12 | return new ModbusTCPMaster(params, keepAlive); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/OldVersion/connectionSettings.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid.OldVersion; 2 | 3 | import com.bencatlin.modbusdroid.R; 4 | 5 | import android.content.SharedPreferences; 6 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 7 | import android.os.Bundle; 8 | import android.preference.EditTextPreference; 9 | import android.preference.PreferenceActivity; 10 | 11 | public class connectionSettings extends PreferenceActivity implements OnSharedPreferenceChangeListener{ 12 | 13 | public static final String IP_ADDRESS_PREFERENCE = "IpAddress"; 14 | public static final String PORT_PREFERENCE = "PortSetting"; 15 | public static final String POLL_TIME_PREFERENCE = "PollTime"; 16 | public static final String SLAVE_ID = "SlaveAddress"; 17 | 18 | private EditTextPreference IPAddressPreference; 19 | private EditTextPreference PortPreference; 20 | private EditTextPreference PollTimePreference; 21 | private EditTextPreference SlaveIdPreference; 22 | 23 | 24 | @Override 25 | public void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | //setContentView(R.layout.connection_settings); 28 | addPreferencesFromResource(R.xml.preferences); 29 | 30 | IPAddressPreference = (EditTextPreference)getPreferenceScreen().findPreference(IP_ADDRESS_PREFERENCE); 31 | IPAddressPreference.setSummary(getPreferenceScreen().getSharedPreferences().getString(IP_ADDRESS_PREFERENCE, "Set a new IP address")); 32 | 33 | PortPreference = (EditTextPreference)getPreferenceScreen().findPreference(PORT_PREFERENCE); 34 | PortPreference.setSummary(getPreferenceScreen().getSharedPreferences().getString(PORT_PREFERENCE, "Set a destination Port (default = 502)")); 35 | 36 | PollTimePreference = (EditTextPreference)getPreferenceScreen().findPreference(POLL_TIME_PREFERENCE); 37 | PollTimePreference.setSummary(getPreferenceScreen().getSharedPreferences().getString(POLL_TIME_PREFERENCE, "Set Poll Time in ms (0 = Poll Once)") + "ms"); 38 | 39 | SlaveIdPreference = (EditTextPreference)getPreferenceScreen().findPreference(SLAVE_ID); 40 | 41 | /*final EditText IP_address_entry = (EditText) findViewById(R.id.IP_address_entry); 42 | final Button Cancel = (Button) findViewById(R.id.Cancel); 43 | final Button Save = (Button) findViewById(R.id.Save); 44 | final Button SaveConnect = (Button) findViewById(R.id.SaveConnect); 45 | final EditText Port_Entry = (EditText) findViewById(R.id.Port_entry); 46 | final EditText Poll_Entry = (EditText) findViewById(R.id.Poll_entry); 47 | 48 | SharedPreferences settings = getSharedPreferences(this.getString(R.string.modbus_prefs), 0); 49 | String IpAddress = settings.getString("IpAddress", "10.0.2.2"); 50 | int PortSetting = settings.getInt("PortEntry", 502); 51 | int PollSetting = settings.getInt("PollTime", 30); 52 | IP_address_entry.setText(IpAddress); 53 | Port_Entry.setText(Integer.toString(PortSetting) ); 54 | Poll_Entry.setText(Integer.toString(PollSetting)); 55 | */ 56 | } 57 | 58 | @Override 59 | protected void onResume() { 60 | super.onResume(); 61 | // Set up a listener whenever a key changes 62 | getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); 63 | } 64 | 65 | @Override 66 | protected void onPause() { 67 | super.onPause(); 68 | // Unregister the listener whenever a key changes 69 | getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); 70 | 71 | } 72 | 73 | 74 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 75 | // Let's do something when my value changes 76 | if (key.equals(IP_ADDRESS_PREFERENCE)) 77 | { 78 | IPAddressPreference.setSummary(sharedPreferences.getString(key, "Set an new IP address")); 79 | } 80 | else if (key.equals(PORT_PREFERENCE)) 81 | { 82 | PortPreference.setSummary(sharedPreferences.getString(key, "Set a destination Port (default = 502)")); 83 | } 84 | else if (key.equals(POLL_TIME_PREFERENCE)) 85 | { 86 | PollTimePreference.setSummary(sharedPreferences.getString(key, "Set Poll Time in ms (0 = Poll Once)") + "ms"); 87 | } 88 | else if (key.equals(SLAVE_ID)) { 89 | 90 | } 91 | } 92 | 93 | } -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/StartupActivity.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid; 2 | 3 | import com.bencatlin.modbusdroid.OldVersion.ModbusDroid; 4 | import com.bencatlin.modbusdroid.activities.ModbusDroidActivity; 5 | import android.app.Activity; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | 9 | public class StartupActivity extends Activity { 10 | 11 | //Check for android 3.0 or higher and a large screen format, 12 | private final boolean isHoneyCombOrNewer = android.os.Build.VERSION.SDK_INT 13 | >= android.os.Build.VERSION_CODES.HONEYCOMB; 14 | 15 | /** Called when the activity is first created. 16 | * 17 | * */ 18 | @Override 19 | public void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | 22 | 23 | // Start the appropriate activity for the version running 24 | // this is for legacy support - the old version is for < 3.0. 25 | // If we want, later we can add a separate activity for phones > 3.0 26 | // if there is a reason for that 27 | Intent startActivityIntent = null; 28 | if ( isHoneyCombOrNewer /*& extraLargeScreen*/ ) { 29 | startActivityIntent = new Intent(this, ModbusDroidActivity.class); 30 | } 31 | else { 32 | // Old Version 33 | startActivityIntent = new Intent(this, ModbusDroid.class); 34 | } 35 | startActivity(startActivityIntent); 36 | finish(); 37 | } 38 | } -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/activities/ModbusDroidActivity.java: -------------------------------------------------------------------------------- 1 | /***************************** 2 | * 3 | * ModbusDroid Activity for the Android 3.2+ version of the original app 4 | * 5 | * 6 | * @author Ben Catlin 7 | * 2012 8 | * 9 | *****************************/ 10 | 11 | package com.bencatlin.modbusdroid.activities; 12 | 13 | import android.app.ActionBar; 14 | import android.app.Activity; 15 | import android.os.Bundle; 16 | import android.preference.PreferenceManager; 17 | 18 | public class ModbusDroidActivity extends Activity { 19 | 20 | 21 | private ActionBar actionBar; 22 | 23 | /*********** Activity lifecycle ****************/ 24 | 25 | /* 26 | * (non-Javadoc) 27 | * @see android.app.Activity#onCreate(android.os.Bundle) 28 | */ 29 | @Override 30 | public void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | 33 | actionBar = getActionBar(); 34 | 35 | } 36 | 37 | 38 | /* 39 | * (non-Javadoc) 40 | * @see android.app.Activity#onResume() 41 | */ 42 | @Override 43 | public void onResume() { 44 | super.onResume(); 45 | 46 | /* if (settings == null) { 47 | settings = PreferenceManager.getDefaultSharedPreferences(this); 48 | } 49 | getSharedSettings(); 50 | */ 51 | } 52 | 53 | /* 54 | * (non-Javadoc) 55 | * @see android.app.Activity#onStop() 56 | * We are overriding this because we need to explicitly 57 | * disconnect if we are connected when the app stops 58 | * 59 | * Consider using onPause/onResume and a service to allow a hidden app 60 | * to continue polling and then re-display results. 61 | */ 62 | @Override 63 | public void onStop () { 64 | super.onStop(); 65 | /*if (isFinishing() && mb.isConnected()) { 66 | mb.disconnect(); 67 | }*/ 68 | } 69 | 70 | /** 71 | * 72 | */ 73 | @Override 74 | public void onDestroy() { 75 | super.onDestroy(); 76 | /*if (isFinishing() && mb.isConnected()) { 77 | mb.disconnect(); 78 | }*/ 79 | } 80 | 81 | @Override 82 | protected void onSaveInstanceState(Bundle outState) { 83 | super.onSaveInstanceState(outState); 84 | /*if (mb.isConnected()) { 85 | outState.putBoolean("Connected", true); 86 | } 87 | else { 88 | outState.putBoolean("Connected", false); 89 | }*/ 90 | } 91 | 92 | @Override 93 | public Object onRetainNonConfigurationInstance() { 94 | //final PollModbus data = mb; 95 | //return mb; 96 | return 1; 97 | } 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/fragments/ModbusParametersFragment.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.bencatlin.modbusdroid.fragments; 5 | 6 | import android.app.Fragment; 7 | 8 | /** 9 | * @author bcatlin 10 | * 11 | */ 12 | public class ModbusParametersFragment extends Fragment { 13 | 14 | /** 15 | * 16 | */ 17 | public ModbusParametersFragment() { 18 | // TODO Auto-generated constructor stub 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/utils/FroyoSharedPreferenceSaver.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid.utils; 2 | 3 | /* 4 | * Copyright 2011 Google Inc. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import android.app.backup.BackupManager; 20 | import android.content.Context; 21 | import android.content.SharedPreferences; 22 | import android.content.SharedPreferences.Editor; 23 | 24 | /** 25 | * Save {@link SharedPreferences} and provide the option to notify 26 | * the BackupManager to initiate a backup. 27 | */ 28 | public class FroyoSharedPreferenceSaver extends LegacySharedPreferenceSaver { 29 | 30 | protected BackupManager backupManager; 31 | 32 | public FroyoSharedPreferenceSaver(Context context) { 33 | super(context); 34 | backupManager = new BackupManager(context); 35 | } 36 | 37 | /** 38 | * {@inheritDoc} 39 | */ 40 | @Override 41 | public void savePreferences(Editor editor, boolean backup) { 42 | editor.commit(); 43 | backupManager.dataChanged(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/utils/GingerbreadSharedPreferenceSaver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.bencatlin.modbusdroid.utils; 17 | 18 | import android.content.Context; 19 | import android.content.SharedPreferences; 20 | 21 | /** 22 | * Save {@link SharedPreferences} using the asynchronous apply method available 23 | * in Gingerbread, and provide the option to notify the BackupManager to 24 | * initiate a backup. 25 | */ 26 | public class GingerbreadSharedPreferenceSaver extends FroyoSharedPreferenceSaver { 27 | 28 | public GingerbreadSharedPreferenceSaver(Context context) { 29 | super(context); 30 | } 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | @Override 36 | public void savePreferences(SharedPreferences.Editor editor, boolean backup) { 37 | editor.apply(); 38 | backupManager.dataChanged(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/utils/HoneycombStrictMode.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid.utils; 2 | /* 3 | * Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | import android.os.StrictMode; 19 | import com.bencatlin.modbusdroid.utils.base.IStrictMode; 20 | 21 | /** 22 | * Implementation that supports the Strict Mode functionality 23 | * available Honeycomb. 24 | */ 25 | public class HoneycombStrictMode implements IStrictMode { 26 | protected static String TAG = "HoneycombStrictMode"; 27 | 28 | /** 29 | * Enable {@link StrictMode} 30 | * TODO Set your preferred Strict Mode features. 31 | */ 32 | public void enableStrictMode() { 33 | StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 34 | .detectDiskReads() 35 | .detectDiskWrites() 36 | .detectNetwork() 37 | .penaltyLog() 38 | .penaltyFlashScreen() 39 | .build()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/utils/InteractiveResumeBackupAgent.java: -------------------------------------------------------------------------------- 1 | package com.bencatlin.modbusdroid.utils; 2 | 3 | import android.app.backup.BackupAgentHelper; 4 | import android.app.backup.SharedPreferencesBackupHelper; 5 | 6 | public class InteractiveResumeBackupAgent extends BackupAgentHelper { 7 | @Override 8 | public void onCreate() { 9 | SharedPreferencesBackupHelper helper = new 10 | SharedPreferencesBackupHelper(this, "SHARED_PREFERENCE_FILE"); 11 | //addHelper(PlacesConstants.SP_KEY_FOLLOW_LOCATION_CHANGES, helper); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/utils/LegacySharedPreferenceSaver.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright 2011 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.bencatlin.modbusdroid.utils; 19 | 20 | import com.bencatlin.modbusdroid.utils.base.SharedPreferenceSaver; 21 | import android.content.Context; 22 | import android.content.SharedPreferences; 23 | import android.content.SharedPreferences.Editor; 24 | 25 | 26 | 27 | /** 28 | * Save {@link SharedPreferences} in a way compatible with Android 1.6. 29 | */ 30 | public class LegacySharedPreferenceSaver extends SharedPreferenceSaver { 31 | 32 | public LegacySharedPreferenceSaver(Context context) { 33 | super(context); 34 | } 35 | 36 | /** 37 | * {@inheritDoc} 38 | */ 39 | @Override 40 | public void savePreferences(Editor editor, boolean backup) { 41 | editor.commit(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/utils/base/IStrictMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bencatlin.modbusdroid.utils.base; 18 | 19 | /** 20 | * This Interface definition allows you to create OS version-specific 21 | * implementations that offer the full Strict Mode functionality 22 | * available in each platform release. 23 | */ 24 | public interface IStrictMode { 25 | /** 26 | * Enable {@link StrictMode} using whichever platform-specific flags you wish. 27 | */ 28 | public void enableStrictMode(); 29 | } 30 | -------------------------------------------------------------------------------- /src/com/bencatlin/modbusdroid/utils/base/SharedPreferenceSaver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bencatlin.modbusdroid.utils.base; 18 | 19 | import android.content.Context; 20 | import android.content.SharedPreferences; 21 | 22 | /** 23 | * Abstract base class that can be extended to provide classes that save 24 | * {@link SharedPreferences} in the most efficient way possible. 25 | * Decendent classes can optionally choose to backup some {@link SharedPreferences} 26 | * to the Google {@link BackupService} on platforms where this is available. 27 | * 28 | * BPC - I ganked this from Retro Mier's app for google IO 2011 - 29 | * I believe it is apache licensed, and the source is available on google code. 30 | * 31 | */ 32 | public abstract class SharedPreferenceSaver { 33 | 34 | protected Context context; 35 | 36 | protected SharedPreferenceSaver(Context context) { 37 | this.context = context; 38 | } 39 | 40 | /** 41 | * Save the Shared Preferences modified through the Editor object. 42 | * @param editor Shared Preferences Editor to commit. 43 | * @param backup Backup to the cloud if possible. 44 | */ 45 | public void savePreferences(SharedPreferences.Editor editor, boolean backup) {} 46 | } 47 | 48 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/BatchResults.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | public class BatchResults { 27 | private final Map data = new HashMap(); 28 | 29 | public void addResult(K key, Object value) { 30 | data.put(key, value); 31 | } 32 | 33 | public Object getValue(K key) { 34 | return data.get(key); 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return data.toString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ExceptionResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import com.serotonin.modbus4j.code.ExceptionCode; 24 | 25 | /** 26 | * @author Matthew Lohbihler 27 | */ 28 | public class ExceptionResult { 29 | private final byte exceptionCode; 30 | private final String exceptionMessage; 31 | 32 | public ExceptionResult(byte exceptionCode) { 33 | this.exceptionCode = exceptionCode; 34 | exceptionMessage = ExceptionCode.getExceptionMessage(exceptionCode); 35 | } 36 | 37 | public byte getExceptionCode() { 38 | return exceptionCode; 39 | } 40 | 41 | public String getExceptionMessage() { 42 | return exceptionMessage; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/Modbus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import com.serotonin.messaging.DefaultMessagingExceptionHandler; 24 | import com.serotonin.messaging.MessagingExceptionHandler; 25 | 26 | /** 27 | * Base level for masters and slaves/listeners 28 | * 29 | * TODO: - handle echoing in RS485 30 | * 31 | * @author mlohbihler 32 | */ 33 | public class Modbus { 34 | private MessagingExceptionHandler exceptionHandler = new DefaultMessagingExceptionHandler(); 35 | 36 | public void setExceptionHandler(MessagingExceptionHandler exceptionHandler) { 37 | if (exceptionHandler == null) 38 | this.exceptionHandler = new DefaultMessagingExceptionHandler(); 39 | else 40 | this.exceptionHandler = exceptionHandler; 41 | } 42 | 43 | public MessagingExceptionHandler getExceptionHandler() { 44 | return exceptionHandler; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ModbusFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import com.serotonin.modbus4j.ip.IpParameters; 24 | import com.serotonin.modbus4j.ip.tcp.TcpMaster; 25 | import com.serotonin.modbus4j.ip.tcp.TcpSlave; 26 | import com.serotonin.modbus4j.ip.udp.UdpMaster; 27 | import com.serotonin.modbus4j.ip.udp.UdpSlave; 28 | 29 | public class ModbusFactory { 30 | // 31 | // Modbus masters 32 | // 33 | public ModbusMaster createTcpMaster(IpParameters params, boolean keepAlive) { 34 | return new TcpMaster(params, keepAlive); 35 | } 36 | 37 | public ModbusMaster createUdpMaster(IpParameters params) { 38 | return new UdpMaster(params); 39 | } 40 | 41 | // 42 | // Modbus slaves 43 | // 44 | public ModbusSlaveSet createTcpSlave(boolean encapsulated) { 45 | return new TcpSlave(encapsulated); 46 | } 47 | 48 | public ModbusSlaveSet createUdpSlave(boolean encapsulated) { 49 | return new UdpSlave(encapsulated); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ModbusSlaveSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import java.util.Collection; 24 | import java.util.LinkedHashMap; 25 | 26 | import com.serotonin.modbus4j.exception.ModbusInitException; 27 | 28 | abstract public class ModbusSlaveSet extends Modbus { 29 | protected LinkedHashMap processImages = new LinkedHashMap(); 30 | 31 | public void addProcessImage(ProcessImage processImage) { 32 | processImages.put(processImage.getSlaveId(), processImage); 33 | } 34 | 35 | public ProcessImage getProcessImage(int slaveId) { 36 | return processImages.get(slaveId); 37 | } 38 | 39 | public Collection getProcessImages() { 40 | return processImages.values(); 41 | } 42 | 43 | /** 44 | * Starts the slave. If an exception is not thrown, this method does not return, but uses the thread to execute the 45 | * listening. 46 | * 47 | * @throws ModbusInitException 48 | */ 49 | abstract public void start() throws ModbusInitException; 50 | 51 | abstract public void stop(); 52 | } 53 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/NodeScanListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | import com.serotonin.util.ProgressiveTaskListener; 24 | 25 | /** 26 | * @author Matthew Lohbihler 27 | */ 28 | public interface NodeScanListener extends ProgressiveTaskListener { 29 | void nodeFound(int nodeNumber); 30 | } 31 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ProcessImageListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j; 22 | 23 | public interface ProcessImageListener { 24 | public void coilWrite(int offset, boolean oldValue, boolean newValue); 25 | 26 | public void holdingRegisterWrite(int offset, short oldValue, short newValue); 27 | } 28 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/BaseMessageParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | import com.serotonin.messaging.IncomingMessage; 24 | import com.serotonin.messaging.MessageParser; 25 | import com.serotonin.util.queue.ByteQueue; 26 | 27 | abstract public class BaseMessageParser implements MessageParser { 28 | protected final boolean master; 29 | 30 | public BaseMessageParser(boolean master) { 31 | this.master = master; 32 | } 33 | 34 | //@Override 35 | public IncomingMessage parseMessage(ByteQueue queue) throws Exception { 36 | try { 37 | return parseMessageImpl(queue); 38 | } 39 | catch (ArrayIndexOutOfBoundsException e) { 40 | // Means that we ran out of data trying to read the message. Just return null. 41 | return null; 42 | } 43 | } 44 | 45 | abstract protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception; 46 | } 47 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/BaseRequestHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | import com.serotonin.messaging.RequestHandler; 24 | import com.serotonin.modbus4j.ModbusSlaveSet; 25 | import com.serotonin.modbus4j.ProcessImage; 26 | import com.serotonin.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.modbus4j.msg.ModbusResponse; 29 | 30 | abstract public class BaseRequestHandler implements RequestHandler { 31 | protected ModbusSlaveSet slave; 32 | 33 | public BaseRequestHandler(ModbusSlaveSet slave) { 34 | this.slave = slave; 35 | } 36 | 37 | protected ModbusResponse handleRequestImpl(ModbusRequest request) throws ModbusTransportException { 38 | int slaveId = request.getSlaveId(); 39 | 40 | // Check the slave id. 41 | if (slaveId == 0) { 42 | // Broadcast message. Send to all process images. 43 | for (ProcessImage processImage : slave.getProcessImages()) 44 | request.handle(processImage); 45 | return null; 46 | } 47 | 48 | // Find the process image to which to send. 49 | ProcessImage processImage = slave.getProcessImage(slaveId); 50 | if (processImage == null) 51 | return null; 52 | 53 | return request.handle(processImage); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/KeyedModbusLocator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | import com.serotonin.modbus4j.ExceptionResult; 24 | import com.serotonin.modbus4j.ModbusLocator; 25 | import com.serotonin.modbus4j.code.ExceptionCode; 26 | 27 | public class KeyedModbusLocator { 28 | private final K key; 29 | private final ModbusLocator locator; 30 | 31 | public KeyedModbusLocator(K key, ModbusLocator locator) { 32 | this.key = key; 33 | this.locator = locator; 34 | } 35 | 36 | public KeyedModbusLocator(K key, int slaveId, int range, int offset, int dataType) { 37 | this.key = key; 38 | locator = new ModbusLocator(slaveId, range, offset, dataType); 39 | } 40 | 41 | public KeyedModbusLocator(K key, int slaveId, int range, int offset, byte bit) { 42 | this.key = key; 43 | locator = new ModbusLocator(slaveId, range, offset, bit); 44 | } 45 | 46 | public KeyedModbusLocator(K key, SlaveAndRange slaveAndRange, int offset, int dataType) { 47 | this.key = key; 48 | locator = new ModbusLocator(slaveAndRange, offset, dataType); 49 | } 50 | 51 | public K getKey() { 52 | return key; 53 | } 54 | 55 | public ModbusLocator getLocator() { 56 | return locator; 57 | } 58 | 59 | // 60 | /// 61 | /// Delegation. 62 | /// 63 | // 64 | public int getDataType() { 65 | return locator.getDataType(); 66 | } 67 | 68 | public int getOffset() { 69 | return locator.getOffset(); 70 | } 71 | 72 | public SlaveAndRange getSlaveAndRange() { 73 | return locator.getSlaveAndRange(); 74 | } 75 | 76 | public int getEndOffset() { 77 | return locator.getEndOffset(); 78 | } 79 | 80 | public int getLength() { 81 | return locator.getLength(); 82 | } 83 | 84 | public byte getBit() { 85 | return locator.getBit(); 86 | } 87 | 88 | public Object bytesToValue(byte[] data, int requestOffset) { 89 | try { 90 | return locator.bytesToValue(data, requestOffset); 91 | } 92 | catch (ArrayIndexOutOfBoundsException e) { 93 | // Some equipment will not return data lengths that we expect, which causes AIOOBEs. Catch them and convert 94 | // them into illegal data address exceptions. 95 | return new ExceptionResult(ExceptionCode.ILLEGAL_DATA_ADDRESS); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/RangeAndOffset.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | import com.serotonin.modbus4j.code.RegisterRange; 24 | 25 | public class RangeAndOffset { 26 | private int range; 27 | private int offset; 28 | 29 | public RangeAndOffset(int range, int offset) { 30 | this.range = range; 31 | this.offset = offset; 32 | } 33 | 34 | /** 35 | * This constructor provides a best guess at the function and offset the user wants, with the assumption that the 36 | * offset will never go over 9999. 37 | */ 38 | public RangeAndOffset(int registerId) { 39 | if (registerId < 10000) { 40 | this.range = RegisterRange.COIL_STATUS; 41 | this.offset = registerId - 1; 42 | } 43 | else if (registerId < 20000) { 44 | this.range = RegisterRange.INPUT_STATUS; 45 | this.offset = registerId - 10001; 46 | } 47 | else if (registerId < 40000) { 48 | this.range = RegisterRange.INPUT_REGISTER; 49 | this.offset = registerId - 30001; 50 | } 51 | else { 52 | this.range = RegisterRange.HOLDING_REGISTER; 53 | this.offset = registerId - 40001; 54 | } 55 | } 56 | 57 | public int getRange() { 58 | return range; 59 | } 60 | 61 | public int getOffset() { 62 | return offset; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/ReadFunctionGroup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import com.serotonin.modbus4j.code.RegisterRange; 27 | 28 | public class ReadFunctionGroup { 29 | private final SlaveAndRange slaveAndRange; 30 | private final int functionCode; 31 | private final List> locators = new ArrayList>(); 32 | private int startOffset = 65536; 33 | private int length = 0; 34 | 35 | public ReadFunctionGroup(KeyedModbusLocator locator) { 36 | slaveAndRange = locator.getSlaveAndRange(); 37 | functionCode = RegisterRange.getReadFunctionCode(slaveAndRange.getRange()); 38 | add(locator); 39 | } 40 | 41 | public void add(KeyedModbusLocator locator) { 42 | if (startOffset > locator.getOffset()) 43 | startOffset = locator.getOffset(); 44 | if (length < locator.getEndOffset() - startOffset + 1) 45 | length = locator.getEndOffset() - startOffset + 1; 46 | locators.add(locator); 47 | } 48 | 49 | public int getStartOffset() { 50 | return startOffset; 51 | } 52 | 53 | public int getEndOffset() { 54 | return startOffset + length - 1; 55 | } 56 | 57 | public SlaveAndRange getSlaveAndRange() { 58 | return slaveAndRange; 59 | } 60 | 61 | public int getLength() { 62 | return length; 63 | } 64 | 65 | public int getFunctionCode() { 66 | return functionCode; 67 | } 68 | 69 | public List> getLocators() { 70 | return locators; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/SlaveAndRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | public class SlaveAndRange { 24 | private final int slaveId; 25 | private final int range; 26 | 27 | public SlaveAndRange(int slaveId, int range) { 28 | ModbusUtils.validateSlaveId(slaveId, true); 29 | 30 | this.slaveId = slaveId; 31 | this.range = range; 32 | } 33 | 34 | public int getRange() { 35 | return range; 36 | } 37 | 38 | public int getSlaveId() { 39 | return slaveId; 40 | } 41 | 42 | @Override 43 | public int hashCode() { 44 | final int prime = 31; 45 | int result = 1; 46 | result = prime * result + range; 47 | result = prime * result + slaveId; 48 | return result; 49 | } 50 | 51 | @Override 52 | public boolean equals(Object obj) { 53 | if (this == obj) 54 | return true; 55 | if (obj == null) 56 | return false; 57 | if (getClass() != obj.getClass()) 58 | return false; 59 | final SlaveAndRange other = (SlaveAndRange) obj; 60 | if (range != other.range) 61 | return false; 62 | if (slaveId != other.slaveId) 63 | return false; 64 | return true; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/base/SlaveProfile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.base; 22 | 23 | /** 24 | * Class for maintaining the profile of a slave device on the master side. Initially, we assume that the device is fully 25 | * featured, and then we note function failures so that we know how requests should subsequently be sent. 26 | * 27 | * @author mlohbihler 28 | */ 29 | public class SlaveProfile { 30 | private boolean writeMaskRegister = true; 31 | 32 | public void setWriteMaskRegister(boolean writeMaskRegister) { 33 | this.writeMaskRegister = writeMaskRegister; 34 | } 35 | 36 | public boolean getWriteMaskRegister() { 37 | return writeMaskRegister; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/code/ExceptionCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.code; 22 | 23 | /** 24 | * @author Matthew Lohbihler 25 | */ 26 | public class ExceptionCode { 27 | public static final byte ILLEGAL_FUNCTION = 0x1; 28 | public static final byte ILLEGAL_DATA_ADDRESS = 0x2; 29 | // public static final byte ILLEGAL_DATA_VALUE = 0x3; 30 | public static final byte SLAVE_DEVICE_FAILURE = 0x4; 31 | 32 | // public static final byte ACKNOWLEDGE = 0x5; 33 | // public static final byte SLAVE_DEVICE_BUSY = 0x6; 34 | // public static final byte MEMORY_PARITY_ERROR = 0x8; 35 | // public static final byte GATEWAY_PATH_UNAVAILABLE = 0xa; 36 | // public static final byte GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND = 0xb; 37 | 38 | public static String getExceptionMessage(byte id) { 39 | switch (id) { 40 | case ILLEGAL_FUNCTION: 41 | return "Illegal function"; 42 | case ILLEGAL_DATA_ADDRESS: 43 | return "Illegal data address"; 44 | // case ILLEGAL_DATA_VALUE : 45 | // return "Illegal data value"; 46 | case SLAVE_DEVICE_FAILURE: 47 | return "Slave device failure"; 48 | // case ACKNOWLEDGE : 49 | // return "Acknowledge"; 50 | // case SLAVE_DEVICE_BUSY : 51 | // return "Slave device busy"; 52 | // case MEMORY_PARITY_ERROR : 53 | // return "Memory parity error"; 54 | // case GATEWAY_PATH_UNAVAILABLE : 55 | // return "Gateway path unavailable"; 56 | // case GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND : 57 | // return "Gateway target device failed to respond"; 58 | } 59 | return null; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/code/FunctionCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.code; 22 | 23 | /** 24 | * @author Matthew Lohbihler 25 | */ 26 | public class FunctionCode { 27 | public static final byte READ_COILS = 1; 28 | public static final byte READ_DISCRETE_INPUTS = 2; 29 | public static final byte READ_HOLDING_REGISTERS = 3; 30 | public static final byte READ_INPUT_REGISTERS = 4; 31 | public static final byte WRITE_COIL = 5; 32 | public static final byte WRITE_REGISTER = 6; 33 | public static final byte READ_EXCEPTION_STATUS = 7; 34 | public static final byte WRITE_COILS = 15; 35 | public static final byte WRITE_REGISTERS = 16; 36 | public static final byte REPORT_SLAVE_ID = 17; 37 | public static final byte WRITE_MASK_REGISTER = 22; 38 | 39 | public static String toString(byte code) { 40 | return Integer.toString(code & 0xff); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/code/RegisterRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.code; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | 25 | /** 26 | * @author Matthew Lohbihler 27 | */ 28 | public class RegisterRange { 29 | public static final int COIL_STATUS = 1; 30 | public static final int INPUT_STATUS = 2; 31 | public static final int HOLDING_REGISTER = 3; 32 | public static final int INPUT_REGISTER = 4; 33 | 34 | public static int getFrom(int id) { 35 | switch (id) { 36 | case COIL_STATUS: 37 | return 0; 38 | case INPUT_STATUS: 39 | return 0x10000; 40 | case HOLDING_REGISTER: 41 | return 0x40000; 42 | case INPUT_REGISTER: 43 | return 0x30000; 44 | } 45 | return -1; 46 | } 47 | 48 | public static int getTo(int id) { 49 | switch (id) { 50 | case COIL_STATUS: 51 | return 0xffff; 52 | case INPUT_STATUS: 53 | return 0x1ffff; 54 | case HOLDING_REGISTER: 55 | return 0x4ffff; 56 | case INPUT_REGISTER: 57 | return 0x3ffff; 58 | } 59 | return -1; 60 | } 61 | 62 | public static int getReadFunctionCode(int id) { 63 | switch (id) { 64 | case COIL_STATUS: 65 | return FunctionCode.READ_COILS; 66 | case INPUT_STATUS: 67 | return FunctionCode.READ_DISCRETE_INPUTS; 68 | case HOLDING_REGISTER: 69 | return FunctionCode.READ_HOLDING_REGISTERS; 70 | case INPUT_REGISTER: 71 | return FunctionCode.READ_INPUT_REGISTERS; 72 | } 73 | return -1; 74 | } 75 | 76 | public static int getMaxReadCount(int id) { 77 | switch (id) { 78 | case COIL_STATUS: 79 | case INPUT_STATUS: 80 | return ModbusUtils.MAX_READ_BIT_COUNT; 81 | case HOLDING_REGISTER: 82 | case INPUT_REGISTER: 83 | return ModbusUtils.MAX_READ_REGISTER_COUNT; 84 | } 85 | return -1; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/ErrorResponseException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | import com.serotonin.modbus4j.msg.ModbusRequest; 24 | import com.serotonin.modbus4j.msg.ModbusResponse; 25 | 26 | public class ErrorResponseException extends Exception { 27 | private static final long serialVersionUID = -1; 28 | 29 | private final ModbusRequest originalRequest; 30 | private final ModbusResponse errorResponse; 31 | 32 | public ErrorResponseException(ModbusRequest originalRequest, ModbusResponse errorResponse) { 33 | this.originalRequest = originalRequest; 34 | this.errorResponse = errorResponse; 35 | } 36 | 37 | public ModbusResponse getErrorResponse() { 38 | return errorResponse; 39 | } 40 | 41 | public ModbusRequest getOriginalRequest() { 42 | return originalRequest; 43 | } 44 | 45 | @Override 46 | public String getMessage() { 47 | return errorResponse.getExceptionMessage(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/IllegalDataAddressException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class IllegalDataAddressException extends ModbusTransportException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public IllegalDataAddressException() { 27 | super(); 28 | } 29 | 30 | public IllegalDataAddressException(int slaveId) { 31 | super(slaveId); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/IllegalDataTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class IllegalDataTypeException extends ModbusIdException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public IllegalDataTypeException(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/IllegalFunctionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class IllegalFunctionException extends ModbusTransportException { 24 | private static final long serialVersionUID = -1; 25 | 26 | private final byte functionCode; 27 | 28 | public IllegalFunctionException(byte functionCode, int slaveId) { 29 | super("Function code: 0x" + Integer.toHexString(functionCode & 0xff), slaveId); 30 | this.functionCode = functionCode; 31 | } 32 | 33 | public byte getFunctionCode() { 34 | return functionCode; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/IllegalSlaveIdException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class IllegalSlaveIdException extends ModbusIdException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public IllegalSlaveIdException(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/InvalidDataConversionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class InvalidDataConversionException extends RuntimeException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public InvalidDataConversionException(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/ModbusIdException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class ModbusIdException extends RuntimeException { 24 | private static final long serialVersionUID = -1; 25 | 26 | public ModbusIdException(String message) { 27 | super(message); 28 | } 29 | 30 | public ModbusIdException(Throwable cause) { 31 | super(cause); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/ModbusInitException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class ModbusInitException extends Exception { 24 | private static final long serialVersionUID = -1; 25 | 26 | public ModbusInitException() { 27 | super(); 28 | } 29 | 30 | public ModbusInitException(String message, Throwable cause) { 31 | super(message, cause); 32 | } 33 | 34 | public ModbusInitException(String message) { 35 | super(message); 36 | } 37 | 38 | public ModbusInitException(Throwable cause) { 39 | super(cause); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/exception/ModbusTransportException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.exception; 22 | 23 | public class ModbusTransportException extends Exception { 24 | private static final long serialVersionUID = -1; 25 | 26 | private final int slaveId; 27 | 28 | public ModbusTransportException() { 29 | this.slaveId = -1; 30 | } 31 | 32 | public ModbusTransportException(int slaveId) { 33 | this.slaveId = slaveId; 34 | } 35 | 36 | public ModbusTransportException(String message, Throwable cause, int slaveId) { 37 | super(message, cause); 38 | this.slaveId = slaveId; 39 | } 40 | 41 | public ModbusTransportException(String message, int slaveId) { 42 | super(message); 43 | this.slaveId = slaveId; 44 | } 45 | 46 | public ModbusTransportException(String message) { 47 | super(message); 48 | this.slaveId = -1; 49 | } 50 | 51 | public ModbusTransportException(Throwable cause) { 52 | super(cause); 53 | this.slaveId = -1; 54 | } 55 | 56 | public ModbusTransportException(Throwable cause, int slaveId) { 57 | super(cause); 58 | this.slaveId = slaveId; 59 | } 60 | 61 | public int getSlaveId() { 62 | return slaveId; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/IpMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip; 22 | 23 | import com.serotonin.modbus4j.msg.ModbusMessage; 24 | 25 | abstract public class IpMessage { 26 | protected ModbusMessage modbusMessage; 27 | 28 | public IpMessage(ModbusMessage modbusMessage) { 29 | this.modbusMessage = modbusMessage; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/IpMessageResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip; 22 | 23 | import com.serotonin.messaging.IncomingResponseMessage; 24 | import com.serotonin.messaging.OutgoingResponseMessage; 25 | import com.serotonin.modbus4j.msg.ModbusResponse; 26 | 27 | public interface IpMessageResponse extends OutgoingResponseMessage, IncomingResponseMessage { 28 | ModbusResponse getModbusResponse(); 29 | } 30 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/IpParameters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | 25 | public class IpParameters { 26 | private String host; 27 | private int port = ModbusUtils.TCP_PORT; 28 | private boolean encapsulated; 29 | 30 | public String getHost() { 31 | return host; 32 | } 33 | 34 | public void setHost(String host) { 35 | this.host = host; 36 | } 37 | 38 | public int getPort() { 39 | return port; 40 | } 41 | 42 | public void setPort(int port) { 43 | this.port = port; 44 | } 45 | 46 | public boolean isEncapsulated() { 47 | return encapsulated; 48 | } 49 | 50 | public void setEncapsulated(boolean encapsulated) { 51 | this.encapsulated = encapsulated; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/encap/EncapMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.messaging.WaitingRoomKey; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.ip.IpMessage; 26 | import com.serotonin.modbus4j.msg.ModbusMessage; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class EncapMessage extends IpMessage { 30 | public EncapMessage(ModbusMessage modbusMessage) { 31 | super(modbusMessage); 32 | } 33 | 34 | public byte[] getMessageData() { 35 | ByteQueue msgQueue = new ByteQueue(); 36 | 37 | // Write the particular message. 38 | modbusMessage.write(msgQueue); 39 | 40 | // Write the CRC 41 | ModbusUtils.pushShort(msgQueue, ModbusUtils.calculateCRC(modbusMessage)); 42 | 43 | // Return the data. 44 | return msgQueue.popAll(); 45 | } 46 | 47 | public WaitingRoomKey getWaitingRoomKey() { 48 | return new EncapWaitingRoomKey(modbusMessage.getSlaveId(), modbusMessage.getFunctionCode()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/encap/EncapMessageParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.messaging.IncomingMessage; 24 | import com.serotonin.modbus4j.base.BaseMessageParser; 25 | import com.serotonin.util.queue.ByteQueue; 26 | 27 | public class EncapMessageParser extends BaseMessageParser { 28 | public EncapMessageParser(boolean master) { 29 | super(master); 30 | } 31 | 32 | @Override 33 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 34 | if (master) 35 | return EncapMessageResponse.createEncapMessageResponse(queue); 36 | return EncapMessageRequest.createEncapMessageRequest(queue); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/encap/EncapMessageRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.messaging.IncomingRequestMessage; 24 | import com.serotonin.messaging.OutgoingRequestMessage; 25 | import com.serotonin.modbus4j.base.ModbusUtils; 26 | import com.serotonin.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.util.queue.ByteQueue; 29 | 30 | public class EncapMessageRequest extends EncapMessage implements OutgoingRequestMessage, IncomingRequestMessage { 31 | static EncapMessageRequest createEncapMessageRequest(ByteQueue queue) throws ModbusTransportException { 32 | // Create the modbus response. 33 | ModbusRequest request = ModbusRequest.createModbusRequest(queue); 34 | EncapMessageRequest encapRequest = new EncapMessageRequest(request); 35 | 36 | // Check the CRC 37 | ModbusUtils.checkCRC(encapRequest.modbusMessage, queue); 38 | 39 | return encapRequest; 40 | } 41 | 42 | public EncapMessageRequest(ModbusRequest modbusRequest) { 43 | super(modbusRequest); 44 | } 45 | 46 | //@Override 47 | public boolean expectsResponse() { 48 | return modbusMessage.getSlaveId() != 0; 49 | } 50 | 51 | public ModbusRequest getModbusRequest() { 52 | return (ModbusRequest) modbusMessage; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/encap/EncapMessageResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | import com.serotonin.modbus4j.ip.IpMessageResponse; 26 | import com.serotonin.modbus4j.msg.ModbusResponse; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class EncapMessageResponse extends EncapMessage implements IpMessageResponse { 30 | static EncapMessageResponse createEncapMessageResponse(ByteQueue queue) throws ModbusTransportException { 31 | // Create the modbus response. 32 | ModbusResponse response = ModbusResponse.createModbusResponse(queue); 33 | EncapMessageResponse encapResponse = new EncapMessageResponse(response); 34 | 35 | // Check the CRC 36 | ModbusUtils.checkCRC(encapResponse.modbusMessage, queue); 37 | 38 | return encapResponse; 39 | } 40 | 41 | public EncapMessageResponse(ModbusResponse modbusResponse) { 42 | super(modbusResponse); 43 | } 44 | 45 | public ModbusResponse getModbusResponse() { 46 | return (ModbusResponse) modbusMessage; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/encap/EncapRequestHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.messaging.IncomingRequestMessage; 24 | import com.serotonin.messaging.OutgoingResponseMessage; 25 | import com.serotonin.modbus4j.ModbusSlaveSet; 26 | import com.serotonin.modbus4j.base.BaseRequestHandler; 27 | import com.serotonin.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.modbus4j.msg.ModbusResponse; 29 | 30 | public class EncapRequestHandler extends BaseRequestHandler { 31 | public EncapRequestHandler(ModbusSlaveSet slave) { 32 | super(slave); 33 | } 34 | 35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 36 | EncapMessageRequest tcpRequest = (EncapMessageRequest) req; 37 | ModbusRequest request = tcpRequest.getModbusRequest(); 38 | ModbusResponse response = handleRequestImpl(request); 39 | if (response == null) 40 | return null; 41 | return new EncapMessageResponse(response); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/encap/EncapWaitingRoomKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.encap; 22 | 23 | import com.serotonin.messaging.WaitingRoomKey; 24 | 25 | public class EncapWaitingRoomKey implements WaitingRoomKey { 26 | private final int slaveId; 27 | private final byte functionCode; 28 | 29 | public EncapWaitingRoomKey(int slaveId, byte functionCode) { 30 | this.slaveId = slaveId; 31 | this.functionCode = functionCode; 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | final int prime = 31; 37 | int result = 1; 38 | result = prime * result + functionCode; 39 | result = prime * result + slaveId; 40 | return result; 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | if (this == obj) 46 | return true; 47 | if (obj == null) 48 | return false; 49 | if (getClass() != obj.getClass()) 50 | return false; 51 | EncapWaitingRoomKey other = (EncapWaitingRoomKey) obj; 52 | if (functionCode != other.functionCode) 53 | return false; 54 | if (slaveId != other.slaveId) 55 | return false; 56 | return true; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/xa/XaMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.messaging.WaitingRoomKey; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.ip.IpMessage; 26 | import com.serotonin.modbus4j.msg.ModbusMessage; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class XaMessage extends IpMessage { 30 | protected int transactionId; 31 | 32 | public XaMessage(ModbusMessage modbusMessage, int transactionId) { 33 | super(modbusMessage); 34 | this.transactionId = transactionId; 35 | } 36 | 37 | public byte[] getMessageData() { 38 | ByteQueue msgQueue = new ByteQueue(); 39 | 40 | // Write the particular message. 41 | modbusMessage.write(msgQueue); 42 | 43 | // Create the XA message 44 | ByteQueue xaQueue = new ByteQueue(); 45 | ModbusUtils.pushShort(xaQueue, transactionId); 46 | ModbusUtils.pushShort(xaQueue, ModbusUtils.IP_PROTOCOL_ID); 47 | ModbusUtils.pushShort(xaQueue, msgQueue.size()); 48 | xaQueue.push(msgQueue); 49 | 50 | // Return the data. 51 | return xaQueue.popAll(); 52 | } 53 | 54 | public WaitingRoomKey getWaitingRoomKey() { 55 | return new XaWaitingRoomKey(transactionId, modbusMessage.getSlaveId(), modbusMessage.getFunctionCode()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/xa/XaMessageParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.messaging.IncomingMessage; 24 | import com.serotonin.modbus4j.base.BaseMessageParser; 25 | import com.serotonin.util.queue.ByteQueue; 26 | 27 | public class XaMessageParser extends BaseMessageParser { 28 | public XaMessageParser(boolean master) { 29 | super(master); 30 | } 31 | 32 | @Override 33 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception { 34 | if (master) 35 | return XaMessageResponse.createXaMessageResponse(queue); 36 | return XaMessageRequest.createXaMessageRequest(queue); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/xa/XaMessageRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.messaging.IncomingRequestMessage; 24 | import com.serotonin.messaging.OutgoingRequestMessage; 25 | import com.serotonin.modbus4j.base.ModbusUtils; 26 | import com.serotonin.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.util.queue.ByteQueue; 29 | 30 | public class XaMessageRequest extends XaMessage implements OutgoingRequestMessage, IncomingRequestMessage { 31 | static XaMessageRequest createXaMessageRequest(ByteQueue queue) throws ModbusTransportException { 32 | // Remove the XA header 33 | int transactionId = ModbusUtils.popShort(queue); 34 | int protocolId = ModbusUtils.popShort(queue); 35 | if (protocolId != ModbusUtils.IP_PROTOCOL_ID) 36 | throw new ModbusTransportException("Unsupported IP protocol id: " + protocolId); 37 | ModbusUtils.popShort(queue); // Length, which we don't care about. 38 | 39 | // Create the modbus response. 40 | ModbusRequest request = ModbusRequest.createModbusRequest(queue); 41 | return new XaMessageRequest(request, transactionId); 42 | } 43 | 44 | public XaMessageRequest(ModbusRequest modbusRequest, int transactionId) { 45 | super(modbusRequest, transactionId); 46 | } 47 | 48 | //@Override 49 | public boolean expectsResponse() { 50 | return modbusMessage.getSlaveId() != 0; 51 | } 52 | 53 | public ModbusRequest getModbusRequest() { 54 | return (ModbusRequest) modbusMessage; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/xa/XaMessageResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | import com.serotonin.modbus4j.ip.IpMessageResponse; 26 | import com.serotonin.modbus4j.msg.ModbusResponse; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class XaMessageResponse extends XaMessage implements IpMessageResponse { 30 | static XaMessageResponse createXaMessageResponse(ByteQueue queue) throws ModbusTransportException { 31 | // Remove the XA header 32 | int transactionId = ModbusUtils.popShort(queue); 33 | int protocolId = ModbusUtils.popShort(queue); 34 | if (protocolId != ModbusUtils.IP_PROTOCOL_ID) 35 | throw new ModbusTransportException("Unsupported IP protocol id: " + protocolId); 36 | ModbusUtils.popShort(queue); // Length, which we don't care about. 37 | 38 | // Create the modbus response. 39 | ModbusResponse response = ModbusResponse.createModbusResponse(queue); 40 | return new XaMessageResponse(response, transactionId); 41 | } 42 | 43 | public XaMessageResponse(ModbusResponse modbusResponse, int transactionId) { 44 | super(modbusResponse, transactionId); 45 | } 46 | 47 | public ModbusResponse getModbusResponse() { 48 | return (ModbusResponse) modbusMessage; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/xa/XaRequestHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.messaging.IncomingRequestMessage; 24 | import com.serotonin.messaging.OutgoingResponseMessage; 25 | import com.serotonin.modbus4j.ModbusSlaveSet; 26 | import com.serotonin.modbus4j.base.BaseRequestHandler; 27 | import com.serotonin.modbus4j.msg.ModbusRequest; 28 | import com.serotonin.modbus4j.msg.ModbusResponse; 29 | 30 | public class XaRequestHandler extends BaseRequestHandler { 31 | public XaRequestHandler(ModbusSlaveSet slave) { 32 | super(slave); 33 | } 34 | 35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception { 36 | XaMessageRequest tcpRequest = (XaMessageRequest) req; 37 | ModbusRequest request = tcpRequest.getModbusRequest(); 38 | ModbusResponse response = handleRequestImpl(request); 39 | if (response == null) 40 | return null; 41 | return new XaMessageResponse(response, tcpRequest.transactionId); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/ip/xa/XaWaitingRoomKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.ip.xa; 22 | 23 | import com.serotonin.messaging.WaitingRoomKey; 24 | 25 | public class XaWaitingRoomKey implements WaitingRoomKey { 26 | private final int transactionId; 27 | private final int slaveId; 28 | private final byte functionCode; 29 | 30 | public XaWaitingRoomKey(int transactionId, int slaveId, byte functionCode) { 31 | this.transactionId = transactionId; 32 | this.slaveId = slaveId; 33 | this.functionCode = functionCode; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | final int prime = 31; 39 | int result = 1; 40 | result = prime * result + functionCode; 41 | result = prime * result + slaveId; 42 | result = prime * result + transactionId; 43 | return result; 44 | } 45 | 46 | @Override 47 | public boolean equals(Object obj) { 48 | if (this == obj) 49 | return true; 50 | if (obj == null) 51 | return false; 52 | if (getClass() != obj.getClass()) 53 | return false; 54 | XaWaitingRoomKey other = (XaWaitingRoomKey) obj; 55 | if (functionCode != other.functionCode) 56 | return false; 57 | if (slaveId != other.slaveId) 58 | return false; 59 | if (transactionId != other.transactionId) 60 | return false; 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ExceptionRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.ShouldNeverHappenException; 24 | import com.serotonin.modbus4j.ProcessImage; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | /** 29 | * @author Matthew Lohbihler 30 | */ 31 | public class ExceptionRequest extends ModbusRequest { 32 | private final byte functionCode; 33 | private final byte exceptionCode; 34 | 35 | public ExceptionRequest(int slaveId, byte functionCode, byte exceptionCode) throws ModbusTransportException { 36 | super(slaveId); 37 | this.functionCode = functionCode; 38 | this.exceptionCode = exceptionCode; 39 | } 40 | 41 | @Override 42 | protected void writeRequest(ByteQueue queue) { 43 | throw new ShouldNeverHappenException("wha"); 44 | } 45 | 46 | @Override 47 | protected void readRequest(ByteQueue queue) { 48 | queue.clear(); 49 | } 50 | 51 | @Override 52 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 53 | return new ExceptionResponse(slaveId, functionCode, exceptionCode); 54 | } 55 | 56 | @Override 57 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 58 | return getResponseInstance(slaveId); 59 | } 60 | 61 | @Override 62 | public byte getFunctionCode() { 63 | return functionCode; 64 | } 65 | 66 | public byte getExceptionCode() { 67 | return exceptionCode; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ExceptionResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.exception.ModbusTransportException; 24 | import com.serotonin.util.queue.ByteQueue; 25 | 26 | /** 27 | * @author Matthew Lohbihler 28 | */ 29 | public class ExceptionResponse extends ModbusResponse { 30 | private final byte functionCode; 31 | 32 | public ExceptionResponse(int slaveId, byte functionCode, byte exceptionCode) throws ModbusTransportException { 33 | super(slaveId); 34 | this.functionCode = functionCode; 35 | setException(exceptionCode); 36 | } 37 | 38 | @Override 39 | public byte getFunctionCode() { 40 | return functionCode; 41 | } 42 | 43 | @Override 44 | protected void readResponse(ByteQueue queue) { 45 | // no op 46 | } 47 | 48 | @Override 49 | protected void writeResponse(ByteQueue queue) { 50 | // no op 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ModbusMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | import com.serotonin.util.queue.ByteQueue; 26 | 27 | abstract public class ModbusMessage { 28 | protected int slaveId; 29 | 30 | public ModbusMessage(int slaveId) throws ModbusTransportException { 31 | // Validate the node id. Note that a 0 slave id is a broadcast message. 32 | if (slaveId < 0 || slaveId > 247) 33 | throw new ModbusTransportException("Invalid slave id", slaveId); 34 | 35 | this.slaveId = slaveId; 36 | } 37 | 38 | public int getSlaveId() { 39 | return slaveId; 40 | } 41 | 42 | abstract public byte getFunctionCode(); 43 | 44 | final public void write(ByteQueue queue) { 45 | ModbusUtils.pushByte(queue, slaveId); 46 | writeImpl(queue); 47 | } 48 | 49 | abstract protected void writeImpl(ByteQueue queue); 50 | 51 | protected byte[] convertToBytes(boolean[] bdata) { 52 | int byteCount = (bdata.length + 7) / 8; 53 | byte[] data = new byte[byteCount]; 54 | for (int i = 0; i < bdata.length; i++) 55 | data[i / 8] |= (bdata[i] ? 1 : 0) << (i % 8); 56 | return data; 57 | } 58 | 59 | protected byte[] convertToBytes(short[] sdata) { 60 | int byteCount = sdata.length * 2; 61 | byte[] data = new byte[byteCount]; 62 | for (int i = 0; i < sdata.length; i++) { 63 | data[i * 2] = (byte) (0xff & (sdata[i] >> 8)); 64 | data[i * 2 + 1] = (byte) (0xff & sdata[i]); 65 | } 66 | return data; 67 | } 68 | 69 | protected boolean[] convertToBooleans(byte[] data) { 70 | boolean[] bdata = new boolean[data.length * 8]; 71 | for (int i = 0; i < bdata.length; i++) 72 | bdata[i] = ((data[i / 8] >> (i % 8)) & 0x1) == 1; 73 | return bdata; 74 | } 75 | 76 | protected short[] convertToShorts(byte[] data) { 77 | short[] sdata = new short[data.length / 2]; 78 | for (int i = 0; i < sdata.length; i++) 79 | sdata[i] = ModbusUtils.toShort(data[i * 2], data[i * 2 + 1]); 80 | return sdata; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadBinaryRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | abstract public class ReadBinaryRequest extends ModbusRequest { 29 | private int startOffset; 30 | private int numberOfBits; 31 | 32 | public ReadBinaryRequest(int slaveId, int startOffset, int numberOfBits) throws ModbusTransportException { 33 | super(slaveId); 34 | 35 | // Do some validation of the data provided. 36 | ModbusUtils.validateOffset(startOffset); 37 | ModbusUtils.validateNumberOfBits(numberOfBits); 38 | ModbusUtils.validateEndOffset(startOffset + numberOfBits); 39 | 40 | this.startOffset = startOffset; 41 | this.numberOfBits = numberOfBits; 42 | } 43 | 44 | ReadBinaryRequest(int slaveId) throws ModbusTransportException { 45 | super(slaveId); 46 | } 47 | 48 | @Override 49 | protected void writeRequest(ByteQueue queue) { 50 | ModbusUtils.pushShort(queue, startOffset); 51 | ModbusUtils.pushShort(queue, numberOfBits); 52 | } 53 | 54 | @Override 55 | protected void readRequest(ByteQueue queue) { 56 | startOffset = ModbusUtils.popUnsignedShort(queue); 57 | numberOfBits = ModbusUtils.popUnsignedShort(queue); 58 | } 59 | 60 | protected byte[] getData(ProcessImage processImage) throws ModbusTransportException { 61 | boolean[] data = new boolean[numberOfBits]; 62 | 63 | // Get the data from the process image. 64 | for (int i = 0; i < numberOfBits; i++) 65 | data[i] = getBinary(processImage, i + startOffset); 66 | 67 | // Convert the boolean array into an array of bytes. 68 | return convertToBytes(data); 69 | } 70 | 71 | abstract protected boolean getBinary(ProcessImage processImage, int index) throws ModbusTransportException; 72 | } 73 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadCoilsRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | 27 | public class ReadCoilsRequest extends ReadBinaryRequest { 28 | public ReadCoilsRequest(int slaveId, int startOffset, int numberOfBits) throws ModbusTransportException { 29 | super(slaveId, startOffset, numberOfBits); 30 | } 31 | 32 | ReadCoilsRequest(int slaveId) throws ModbusTransportException { 33 | super(slaveId); 34 | } 35 | 36 | @Override 37 | public byte getFunctionCode() { 38 | return FunctionCode.READ_COILS; 39 | } 40 | 41 | @Override 42 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 43 | return new ReadCoilsResponse(slaveId, getData(processImage)); 44 | } 45 | 46 | @Override 47 | protected boolean getBinary(ProcessImage processImage, int index) throws ModbusTransportException { 48 | return processImage.getCoil(index); 49 | } 50 | 51 | @Override 52 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 53 | return new ReadCoilsResponse(slaveId); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadCoilsResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadCoilsResponse extends ReadResponse { 27 | ReadCoilsResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadCoilsResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | @Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_COILS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadDiscreteInputsRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | 27 | public class ReadDiscreteInputsRequest extends ReadBinaryRequest { 28 | public ReadDiscreteInputsRequest(int slaveId, int startOffset, int numberOfBits) throws ModbusTransportException { 29 | super(slaveId, startOffset, numberOfBits); 30 | } 31 | 32 | ReadDiscreteInputsRequest(int slaveId) throws ModbusTransportException { 33 | super(slaveId); 34 | } 35 | 36 | @Override 37 | public byte getFunctionCode() { 38 | return FunctionCode.READ_DISCRETE_INPUTS; 39 | } 40 | 41 | @Override 42 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 43 | return new ReadDiscreteInputsResponse(slaveId, getData(processImage)); 44 | } 45 | 46 | @Override 47 | protected boolean getBinary(ProcessImage processImage, int index) throws ModbusTransportException { 48 | return processImage.getInput(index); 49 | } 50 | 51 | @Override 52 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 53 | return new ReadDiscreteInputsResponse(slaveId); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadDiscreteInputsResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadDiscreteInputsResponse extends ReadResponse { 27 | ReadDiscreteInputsResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadDiscreteInputsResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | @Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_DISCRETE_INPUTS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadExceptionStatusRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class ReadExceptionStatusRequest extends ModbusRequest { 29 | public ReadExceptionStatusRequest(int slaveId) throws ModbusTransportException { 30 | super(slaveId); 31 | } 32 | 33 | @Override 34 | protected void writeRequest(ByteQueue queue) { 35 | // no op 36 | } 37 | 38 | @Override 39 | protected void readRequest(ByteQueue queue) { 40 | // no op 41 | } 42 | 43 | @Override 44 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 45 | return new ReadExceptionStatusResponse(slaveId); 46 | } 47 | 48 | @Override 49 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 50 | return new ReadExceptionStatusResponse(slaveId, processImage.getExceptionStatus()); 51 | } 52 | 53 | @Override 54 | public byte getFunctionCode() { 55 | return FunctionCode.READ_EXCEPTION_STATUS; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadExceptionStatusResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | import com.serotonin.util.queue.ByteQueue; 26 | 27 | public class ReadExceptionStatusResponse extends ModbusResponse { 28 | private byte exceptionStatus; 29 | 30 | ReadExceptionStatusResponse(int slaveId) throws ModbusTransportException { 31 | super(slaveId); 32 | } 33 | 34 | ReadExceptionStatusResponse(int slaveId, byte exceptionStatus) throws ModbusTransportException { 35 | super(slaveId); 36 | this.exceptionStatus = exceptionStatus; 37 | } 38 | 39 | @Override 40 | public byte getFunctionCode() { 41 | return FunctionCode.READ_EXCEPTION_STATUS; 42 | } 43 | 44 | @Override 45 | protected void readResponse(ByteQueue queue) { 46 | exceptionStatus = queue.pop(); 47 | } 48 | 49 | @Override 50 | protected void writeResponse(ByteQueue queue) { 51 | queue.push(exceptionStatus); 52 | } 53 | 54 | public byte getExceptionStatus() { 55 | return exceptionStatus; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadHoldingRegistersRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | 27 | public class ReadHoldingRegistersRequest extends ReadNumericRequest { 28 | public ReadHoldingRegistersRequest(int slaveId, int startOffset, int numberOfRegisters) 29 | throws ModbusTransportException { 30 | super(slaveId, startOffset, numberOfRegisters); 31 | } 32 | 33 | ReadHoldingRegistersRequest(int slaveId) throws ModbusTransportException { 34 | super(slaveId); 35 | } 36 | 37 | @Override 38 | public byte getFunctionCode() { 39 | return FunctionCode.READ_HOLDING_REGISTERS; 40 | } 41 | 42 | @Override 43 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 44 | return new ReadHoldingRegistersResponse(slaveId, getData(processImage)); 45 | } 46 | 47 | @Override 48 | protected short getNumeric(ProcessImage processImage, int index) throws ModbusTransportException { 49 | return processImage.getHoldingRegister(index); 50 | } 51 | 52 | @Override 53 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 54 | return new ReadHoldingRegistersResponse(slaveId); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadHoldingRegistersResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadHoldingRegistersResponse extends ReadResponse { 27 | ReadHoldingRegistersResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadHoldingRegistersResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | @Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_HOLDING_REGISTERS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadInputRegistersRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | 27 | public class ReadInputRegistersRequest extends ReadNumericRequest { 28 | public ReadInputRegistersRequest(int slaveId, int startOffset, int numberOfRegisters) 29 | throws ModbusTransportException { 30 | super(slaveId, startOffset, numberOfRegisters); 31 | } 32 | 33 | ReadInputRegistersRequest(int slaveId) throws ModbusTransportException { 34 | super(slaveId); 35 | } 36 | 37 | @Override 38 | public byte getFunctionCode() { 39 | return FunctionCode.READ_INPUT_REGISTERS; 40 | } 41 | 42 | @Override 43 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 44 | return new ReadInputRegistersResponse(slaveId, getData(processImage)); 45 | } 46 | 47 | @Override 48 | protected short getNumeric(ProcessImage processImage, int index) throws ModbusTransportException { 49 | return processImage.getInputRegister(index); 50 | } 51 | 52 | @Override 53 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 54 | return new ReadInputRegistersResponse(slaveId); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadInputRegistersResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.code.FunctionCode; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | 26 | public class ReadInputRegistersResponse extends ReadResponse { 27 | ReadInputRegistersResponse(int slaveId, byte[] data) throws ModbusTransportException { 28 | super(slaveId, data); 29 | } 30 | 31 | ReadInputRegistersResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | @Override 36 | public byte getFunctionCode() { 37 | return FunctionCode.READ_INPUT_REGISTERS; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadNumericRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | abstract public class ReadNumericRequest extends ModbusRequest { 29 | private int startOffset; 30 | private int numberOfRegisters; 31 | 32 | public ReadNumericRequest(int slaveId, int startOffset, int numberOfRegisters) throws ModbusTransportException { 33 | super(slaveId); 34 | 35 | // Do some validation of the data provided. 36 | ModbusUtils.validateOffset(startOffset); 37 | ModbusUtils.validateNumberOfRegisters(numberOfRegisters); 38 | ModbusUtils.validateEndOffset(startOffset + numberOfRegisters); 39 | 40 | this.startOffset = startOffset; 41 | this.numberOfRegisters = numberOfRegisters; 42 | } 43 | 44 | ReadNumericRequest(int slaveId) throws ModbusTransportException { 45 | super(slaveId); 46 | } 47 | 48 | @Override 49 | protected void writeRequest(ByteQueue queue) { 50 | ModbusUtils.pushShort(queue, startOffset); 51 | ModbusUtils.pushShort(queue, numberOfRegisters); 52 | } 53 | 54 | @Override 55 | protected void readRequest(ByteQueue queue) { 56 | startOffset = ModbusUtils.popUnsignedShort(queue); 57 | numberOfRegisters = ModbusUtils.popUnsignedShort(queue); 58 | } 59 | 60 | protected byte[] getData(ProcessImage processImage) throws ModbusTransportException { 61 | short[] data = new short[numberOfRegisters]; 62 | 63 | // Get the data from the process image. 64 | for (int i = 0; i < numberOfRegisters; i++) 65 | data[i] = getNumeric(processImage, i + startOffset); 66 | 67 | return convertToBytes(data); 68 | } 69 | 70 | abstract protected short getNumeric(ProcessImage processImage, int index) throws ModbusTransportException; 71 | } 72 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReadResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.exception.ModbusTransportException; 25 | import com.serotonin.util.queue.ByteQueue; 26 | 27 | abstract public class ReadResponse extends ModbusResponse { 28 | private byte[] data; 29 | 30 | ReadResponse(int slaveId) throws ModbusTransportException { 31 | super(slaveId); 32 | } 33 | 34 | ReadResponse(int slaveId, byte[] data) throws ModbusTransportException { 35 | super(slaveId); 36 | this.data = data; 37 | } 38 | 39 | @Override 40 | protected void readResponse(ByteQueue queue) { 41 | int numberOfBytes = ModbusUtils.popUnsignedByte(queue); 42 | if (queue.size() < numberOfBytes) 43 | throw new ArrayIndexOutOfBoundsException(); 44 | 45 | data = new byte[numberOfBytes]; 46 | queue.pop(data); 47 | } 48 | 49 | @Override 50 | protected void writeResponse(ByteQueue queue) { 51 | ModbusUtils.pushByte(queue, data.length); 52 | queue.push(data); 53 | } 54 | 55 | public byte[] getData() { 56 | return data; 57 | } 58 | 59 | public short[] getShortData() { 60 | return convertToShorts(data); 61 | } 62 | 63 | public boolean[] getBooleanData() { 64 | return convertToBooleans(data); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReportSlaveIdRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class ReportSlaveIdRequest extends ModbusRequest { 29 | public ReportSlaveIdRequest(int slaveId) throws ModbusTransportException { 30 | super(slaveId); 31 | } 32 | 33 | @Override 34 | protected void writeRequest(ByteQueue queue) { 35 | // no op 36 | } 37 | 38 | @Override 39 | protected void readRequest(ByteQueue queue) { 40 | // no op 41 | } 42 | 43 | @Override 44 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 45 | return new ReportSlaveIdResponse(slaveId); 46 | } 47 | 48 | @Override 49 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 50 | return new ReportSlaveIdResponse(slaveId, processImage.getReportSlaveIdData()); 51 | } 52 | 53 | @Override 54 | public byte getFunctionCode() { 55 | return FunctionCode.REPORT_SLAVE_ID; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/ReportSlaveIdResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class ReportSlaveIdResponse extends ModbusResponse { 29 | private byte[] data; 30 | 31 | ReportSlaveIdResponse(int slaveId) throws ModbusTransportException { 32 | super(slaveId); 33 | } 34 | 35 | ReportSlaveIdResponse(int slaveId, byte[] data) throws ModbusTransportException { 36 | super(slaveId); 37 | this.data = data; 38 | } 39 | 40 | @Override 41 | public byte getFunctionCode() { 42 | return FunctionCode.REPORT_SLAVE_ID; 43 | } 44 | 45 | @Override 46 | protected void readResponse(ByteQueue queue) { 47 | int numberOfBytes = ModbusUtils.popUnsignedByte(queue); 48 | if (queue.size() < numberOfBytes) 49 | throw new ArrayIndexOutOfBoundsException(); 50 | 51 | data = new byte[numberOfBytes]; 52 | queue.pop(data); 53 | } 54 | 55 | @Override 56 | protected void writeResponse(ByteQueue queue) { 57 | ModbusUtils.pushByte(queue, data.length); 58 | queue.push(data); 59 | } 60 | 61 | public byte[] getData() { 62 | return data; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteCoilRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.code.FunctionCode; 26 | import com.serotonin.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class WriteCoilRequest extends ModbusRequest { 30 | private int writeOffset; 31 | private boolean writeValue; 32 | 33 | public WriteCoilRequest(int slaveId, int writeOffset, boolean writeValue) throws ModbusTransportException { 34 | super(slaveId); 35 | 36 | // Do some validation of the data provided. 37 | ModbusUtils.validateOffset(writeOffset); 38 | 39 | this.writeOffset = writeOffset; 40 | this.writeValue = writeValue; 41 | } 42 | 43 | WriteCoilRequest(int slaveId) throws ModbusTransportException { 44 | super(slaveId); 45 | } 46 | 47 | @Override 48 | protected void writeRequest(ByteQueue queue) { 49 | ModbusUtils.pushShort(queue, writeOffset); 50 | ModbusUtils.pushShort(queue, writeValue ? 0xff00 : 0); 51 | } 52 | 53 | @Override 54 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 55 | processImage.writeCoil(writeOffset, writeValue); 56 | return new WriteCoilResponse(slaveId, writeOffset, writeValue); 57 | } 58 | 59 | @Override 60 | public byte getFunctionCode() { 61 | return FunctionCode.WRITE_COIL; 62 | } 63 | 64 | @Override 65 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 66 | return new WriteCoilResponse(slaveId); 67 | } 68 | 69 | @Override 70 | protected void readRequest(ByteQueue queue) { 71 | writeOffset = ModbusUtils.popUnsignedShort(queue); 72 | writeValue = ModbusUtils.popUnsignedShort(queue) == 0xff00; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteCoilResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class WriteCoilResponse extends ModbusResponse { 29 | private int writeOffset; 30 | private boolean writeValue; 31 | 32 | @Override 33 | public byte getFunctionCode() { 34 | return FunctionCode.WRITE_COIL; 35 | } 36 | 37 | WriteCoilResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | WriteCoilResponse(int slaveId, int writeOffset, boolean writeValue) throws ModbusTransportException { 42 | super(slaveId); 43 | this.writeOffset = writeOffset; 44 | this.writeValue = writeValue; 45 | } 46 | 47 | @Override 48 | protected void writeResponse(ByteQueue queue) { 49 | ModbusUtils.pushShort(queue, writeOffset); 50 | ModbusUtils.pushShort(queue, writeValue ? 0xff00 : 0); 51 | } 52 | 53 | @Override 54 | protected void readResponse(ByteQueue queue) { 55 | writeOffset = ModbusUtils.popUnsignedShort(queue); 56 | writeValue = ModbusUtils.popUnsignedShort(queue) == 0xff00; 57 | } 58 | 59 | public int getWriteOffset() { 60 | return writeOffset; 61 | } 62 | 63 | public boolean isWriteValue() { 64 | return writeValue; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteCoilsRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.code.FunctionCode; 26 | import com.serotonin.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class WriteCoilsRequest extends ModbusRequest { 30 | private int startOffset; 31 | private int numberOfBits; 32 | private byte[] data; 33 | 34 | public WriteCoilsRequest(int slaveId, int startOffset, boolean[] bdata) throws ModbusTransportException { 35 | super(slaveId); 36 | 37 | ModbusUtils.validateOffset(startOffset); 38 | ModbusUtils.validateNumberOfBits(bdata.length); 39 | ModbusUtils.validateEndOffset(startOffset + bdata.length); 40 | 41 | this.startOffset = startOffset; 42 | numberOfBits = bdata.length; 43 | data = convertToBytes(bdata); 44 | } 45 | 46 | WriteCoilsRequest(int slaveId) throws ModbusTransportException { 47 | super(slaveId); 48 | } 49 | 50 | @Override 51 | protected void writeRequest(ByteQueue queue) { 52 | ModbusUtils.pushShort(queue, startOffset); 53 | ModbusUtils.pushShort(queue, numberOfBits); 54 | ModbusUtils.pushByte(queue, data.length); 55 | queue.push(data); 56 | } 57 | 58 | @Override 59 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 60 | boolean[] bdata = convertToBooleans(data); 61 | for (int i = 0; i < numberOfBits; i++) 62 | processImage.writeCoil(startOffset + i, bdata[i]); 63 | return new WriteCoilsResponse(slaveId, startOffset, numberOfBits); 64 | } 65 | 66 | @Override 67 | public byte getFunctionCode() { 68 | return FunctionCode.WRITE_COILS; 69 | } 70 | 71 | @Override 72 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 73 | return new WriteCoilsResponse(slaveId); 74 | } 75 | 76 | @Override 77 | protected void readRequest(ByteQueue queue) { 78 | startOffset = ModbusUtils.popUnsignedShort(queue); 79 | numberOfBits = ModbusUtils.popUnsignedShort(queue); 80 | data = new byte[ModbusUtils.popUnsignedByte(queue)]; 81 | queue.pop(data); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteCoilsResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class WriteCoilsResponse extends ModbusResponse { 29 | private int startOffset; 30 | private int numberOfBits; 31 | 32 | @Override 33 | public byte getFunctionCode() { 34 | return FunctionCode.WRITE_COILS; 35 | } 36 | 37 | WriteCoilsResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | WriteCoilsResponse(int slaveId, int startOffset, int numberOfBits) throws ModbusTransportException { 42 | super(slaveId); 43 | this.startOffset = startOffset; 44 | this.numberOfBits = numberOfBits; 45 | } 46 | 47 | @Override 48 | protected void writeResponse(ByteQueue queue) { 49 | ModbusUtils.pushShort(queue, startOffset); 50 | ModbusUtils.pushShort(queue, numberOfBits); 51 | } 52 | 53 | @Override 54 | protected void readResponse(ByteQueue queue) { 55 | startOffset = ModbusUtils.popUnsignedShort(queue); 56 | numberOfBits = ModbusUtils.popUnsignedShort(queue); 57 | } 58 | 59 | public int getStartOffset() { 60 | return startOffset; 61 | } 62 | 63 | public int getNumberOfBits() { 64 | return numberOfBits; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteMaskRegisterResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class WriteMaskRegisterResponse extends ModbusResponse { 29 | private int writeOffset; 30 | private int andMask; 31 | private int orMask; 32 | 33 | @Override 34 | public byte getFunctionCode() { 35 | return FunctionCode.WRITE_MASK_REGISTER; 36 | } 37 | 38 | WriteMaskRegisterResponse(int slaveId) throws ModbusTransportException { 39 | super(slaveId); 40 | } 41 | 42 | WriteMaskRegisterResponse(int slaveId, int writeOffset, int andMask, int orMask) throws ModbusTransportException { 43 | super(slaveId); 44 | this.writeOffset = writeOffset; 45 | this.andMask = andMask; 46 | this.orMask = orMask; 47 | } 48 | 49 | @Override 50 | protected void writeResponse(ByteQueue queue) { 51 | ModbusUtils.pushShort(queue, writeOffset); 52 | ModbusUtils.pushShort(queue, andMask); 53 | ModbusUtils.pushShort(queue, orMask); 54 | } 55 | 56 | @Override 57 | protected void readResponse(ByteQueue queue) { 58 | writeOffset = ModbusUtils.popUnsignedShort(queue); 59 | andMask = ModbusUtils.popUnsignedShort(queue); 60 | orMask = ModbusUtils.popUnsignedShort(queue); 61 | } 62 | 63 | public int getWriteOffset() { 64 | return writeOffset; 65 | } 66 | 67 | public int getAndMask() { 68 | return andMask; 69 | } 70 | 71 | public int getOrMask() { 72 | return orMask; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteRegisterRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.code.FunctionCode; 26 | import com.serotonin.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class WriteRegisterRequest extends ModbusRequest { 30 | private int writeOffset; 31 | private int writeValue; 32 | 33 | public WriteRegisterRequest(int slaveId, int writeOffset, int writeValue) throws ModbusTransportException { 34 | super(slaveId); 35 | 36 | // Do some validation of the data provided. 37 | ModbusUtils.validateOffset(writeOffset); 38 | 39 | this.writeOffset = writeOffset; 40 | this.writeValue = writeValue; 41 | } 42 | 43 | WriteRegisterRequest(int slaveId) throws ModbusTransportException { 44 | super(slaveId); 45 | } 46 | 47 | @Override 48 | protected void writeRequest(ByteQueue queue) { 49 | ModbusUtils.pushShort(queue, writeOffset); 50 | ModbusUtils.pushShort(queue, writeValue); 51 | } 52 | 53 | @Override 54 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 55 | processImage.writeHoldingRegister(writeOffset, (short) writeValue); 56 | return new WriteRegisterResponse(slaveId, writeOffset, writeValue); 57 | } 58 | 59 | @Override 60 | public byte getFunctionCode() { 61 | return FunctionCode.WRITE_REGISTER; 62 | } 63 | 64 | @Override 65 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 66 | return new WriteRegisterResponse(slaveId); 67 | } 68 | 69 | @Override 70 | protected void readRequest(ByteQueue queue) { 71 | writeOffset = ModbusUtils.popUnsignedShort(queue); 72 | writeValue = ModbusUtils.popUnsignedShort(queue); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteRegisterResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class WriteRegisterResponse extends ModbusResponse { 29 | private int writeOffset; 30 | private int writeValue; 31 | 32 | @Override 33 | public byte getFunctionCode() { 34 | return FunctionCode.WRITE_REGISTER; 35 | } 36 | 37 | WriteRegisterResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | WriteRegisterResponse(int slaveId, int writeOffset, int writeValue) throws ModbusTransportException { 42 | super(slaveId); 43 | this.writeOffset = writeOffset; 44 | this.writeValue = writeValue; 45 | } 46 | 47 | @Override 48 | protected void writeResponse(ByteQueue queue) { 49 | ModbusUtils.pushShort(queue, writeOffset); 50 | ModbusUtils.pushShort(queue, writeValue); 51 | } 52 | 53 | @Override 54 | protected void readResponse(ByteQueue queue) { 55 | writeOffset = ModbusUtils.popUnsignedShort(queue); 56 | writeValue = ModbusUtils.popUnsignedShort(queue); 57 | } 58 | 59 | public int getWriteOffset() { 60 | return writeOffset; 61 | } 62 | 63 | public int getWriteValue() { 64 | return writeValue; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteRegistersRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.ProcessImage; 24 | import com.serotonin.modbus4j.base.ModbusUtils; 25 | import com.serotonin.modbus4j.code.FunctionCode; 26 | import com.serotonin.modbus4j.exception.ModbusTransportException; 27 | import com.serotonin.util.queue.ByteQueue; 28 | 29 | public class WriteRegistersRequest extends ModbusRequest { 30 | private int startOffset; 31 | private byte[] data; 32 | 33 | public WriteRegistersRequest(int slaveId, int startOffset, short[] sdata) throws ModbusTransportException { 34 | super(slaveId); 35 | 36 | ModbusUtils.validateOffset(startOffset); 37 | if (sdata.length < 1 || sdata.length > ModbusUtils.MAX_WRITE_REGISTER_COUNT) 38 | throw new ModbusTransportException("Invalid number of registers: " + sdata.length, slaveId); 39 | ModbusUtils.validateEndOffset(startOffset + sdata.length); 40 | 41 | this.startOffset = startOffset; 42 | data = convertToBytes(sdata); 43 | } 44 | 45 | WriteRegistersRequest(int slaveId) throws ModbusTransportException { 46 | super(slaveId); 47 | } 48 | 49 | @Override 50 | protected void writeRequest(ByteQueue queue) { 51 | ModbusUtils.pushShort(queue, startOffset); 52 | ModbusUtils.pushShort(queue, data.length / 2); 53 | ModbusUtils.pushByte(queue, data.length); 54 | queue.push(data); 55 | } 56 | 57 | @Override 58 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException { 59 | short[] sdata = convertToShorts(data); 60 | for (int i = 0; i < sdata.length; i++) 61 | processImage.writeHoldingRegister(startOffset + i, sdata[i]); 62 | return new WriteRegistersResponse(slaveId, startOffset, sdata.length); 63 | } 64 | 65 | @Override 66 | public byte getFunctionCode() { 67 | return FunctionCode.WRITE_REGISTERS; 68 | } 69 | 70 | @Override 71 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException { 72 | return new WriteRegistersResponse(slaveId); 73 | } 74 | 75 | @Override 76 | protected void readRequest(ByteQueue queue) { 77 | startOffset = ModbusUtils.popUnsignedShort(queue); 78 | ModbusUtils.popUnsignedShort(queue); // register count not needed. 79 | data = new byte[ModbusUtils.popUnsignedByte(queue)]; 80 | queue.pop(data); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/msg/WriteRegistersResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.msg; 22 | 23 | import com.serotonin.modbus4j.base.ModbusUtils; 24 | import com.serotonin.modbus4j.code.FunctionCode; 25 | import com.serotonin.modbus4j.exception.ModbusTransportException; 26 | import com.serotonin.util.queue.ByteQueue; 27 | 28 | public class WriteRegistersResponse extends ModbusResponse { 29 | private int startOffset; 30 | private int numberOfRegisters; 31 | 32 | @Override 33 | public byte getFunctionCode() { 34 | return FunctionCode.WRITE_REGISTERS; 35 | } 36 | 37 | WriteRegistersResponse(int slaveId) throws ModbusTransportException { 38 | super(slaveId); 39 | } 40 | 41 | WriteRegistersResponse(int slaveId, int startOffset, int numberOfRegisters) throws ModbusTransportException { 42 | super(slaveId); 43 | this.startOffset = startOffset; 44 | this.numberOfRegisters = numberOfRegisters; 45 | } 46 | 47 | @Override 48 | protected void writeResponse(ByteQueue queue) { 49 | ModbusUtils.pushShort(queue, startOffset); 50 | ModbusUtils.pushShort(queue, numberOfRegisters); 51 | } 52 | 53 | @Override 54 | protected void readResponse(ByteQueue queue) { 55 | startOffset = ModbusUtils.popUnsignedShort(queue); 56 | numberOfRegisters = ModbusUtils.popUnsignedShort(queue); 57 | } 58 | 59 | public int getStartOffset() { 60 | return startOffset; 61 | } 62 | 63 | public int getNumberOfRegisters() { 64 | return numberOfRegisters; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/serotonin/modbus4j/value/ModbusValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ============================================================================ 3 | * GNU General Public License 4 | * ============================================================================ 5 | * 6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com 7 | * @author Matthew Lohbihler 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | package com.serotonin.modbus4j.value; 22 | 23 | import java.math.BigInteger; 24 | 25 | import com.serotonin.modbus4j.code.DataType; 26 | import com.serotonin.modbus4j.exception.InvalidDataConversionException; 27 | 28 | abstract public class ModbusValue { 29 | private final DataType type; 30 | private final Object value; 31 | 32 | public ModbusValue(DataType type, Object value) { 33 | this.type = type; 34 | this.value = value; 35 | } 36 | 37 | public DataType getType() { 38 | return type; 39 | } 40 | 41 | public Object getValue() { 42 | return value; 43 | } 44 | 45 | public boolean booleanValue() { 46 | if (value instanceof Boolean) 47 | return ((Boolean) value).booleanValue(); 48 | throw new InvalidDataConversionException("Can't convert " + value.getClass() + " to boolean"); 49 | } 50 | 51 | public int intValue() { 52 | if (value instanceof Integer) 53 | return ((Integer) value).intValue(); 54 | if (value instanceof Short) 55 | return ((Short) value).shortValue() & 0xffff; 56 | throw new InvalidDataConversionException("Can't convert " + value.getClass() + " to int"); 57 | } 58 | 59 | public long longValue() { 60 | if (value instanceof Long) 61 | return ((Long) value).longValue(); 62 | if (value instanceof Integer) 63 | return ((Integer) value).intValue() & 0xffffffff; 64 | if (value instanceof Short) 65 | return ((Short) value).shortValue() & 0xffff; 66 | throw new InvalidDataConversionException("Can't convert " + value.getClass() + " to long"); 67 | } 68 | 69 | public BigInteger bigIntegerValue() { 70 | if (value instanceof BigInteger) 71 | return (BigInteger) value; 72 | if (value instanceof Long) 73 | return BigInteger.valueOf(((Long) value).longValue()); 74 | if (value instanceof Integer) 75 | return BigInteger.valueOf(((Integer) value).intValue() & 0xffffffff); 76 | if (value instanceof Short) 77 | return BigInteger.valueOf(((Short) value).shortValue() & 0xffff); 78 | throw new InvalidDataConversionException("Can't convert " + value.getClass() + " to BigInteger"); 79 | } 80 | 81 | public float floatValue() { 82 | if (value instanceof Float) 83 | return ((Float) value).floatValue(); 84 | throw new InvalidDataConversionException("Can't convert " + value.getClass() + " to float"); 85 | } 86 | 87 | public double doubleValue() { 88 | if (value instanceof Double) 89 | return ((Double) value).doubleValue(); 90 | if (value instanceof Float) 91 | return ((Float) value).doubleValue(); 92 | throw new InvalidDataConversionException("Can't convert " + value.getClass() + " to float"); 93 | } 94 | } 95 | --------------------------------------------------------------------------------