11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | applicationId "com.zgkxzx.modbustest"
9 | minSdkVersion 21
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:25.2.0'
26 | // compile files('libs/commons-lang3-3.1.jar')
27 | // compile files('libs/commons-logging-1.2.jar')
28 | //compile files('libs/modbus4Android-1.0.jar')
29 | compile files('libs/modbus4Android-1.2.jar')
30 | }
31 |
--------------------------------------------------------------------------------
/app/libs/modbus4Android-1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/app/libs/modbus4Android-1.2.jar
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/zgkxzx/modbustest/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbustest;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
22 |
28 |
29 |
35 |
36 |
42 |
48 |
49 |
55 |
56 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ModbusTest
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/zgkxzx/modbustest/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbustest;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.3'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=http\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/modbus4android/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/modbus4android/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 21
9 | targetSdkVersion 25
10 | versionCode 3
11 | versionName "1.2"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | //Copy类型
25 | task makeJar(type: Copy) {
26 | //删除存在的
27 | delete 'build/libs/modbus4Android.jar'
28 | //设置拷贝的文件
29 | from('build/intermediates/bundles/release/')
30 | //打进jar包后的文件目录
31 | into('build/libs/')
32 | //将classes.jar放入build/libs/目录下
33 | //include ,exclude参数来设置过滤
34 | //(我们只关心classes.jar这个文件)
35 | include('classes.jar')
36 | //重命名
37 | rename ('classes.jar', 'modbus4Android-'+android.defaultConfig.versionName+'.jar')
38 | }
39 |
40 | makeJar.dependsOn(build)
41 | //在终端执行生成JAR包
42 | // gradlew makeJar
43 |
44 | //makeJar.dependsOn(build)
45 | //在终端执行生成JAR包
46 | // gradlew makeJar
47 |
48 | dependencies {
49 | compile fileTree(dir: 'libs', include: ['*.jar'])
50 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
51 | exclude group: 'com.android.support', module: 'support-annotations'
52 | })
53 | compile 'com.android.support:appcompat-v7:25.2.0'
54 | testCompile 'junit:junit:4.12'
55 | compile files('libs/commons-lang3-3.1.jar')
56 | compile files('libs/commons-logging-1.2.jar')
57 | }
58 |
--------------------------------------------------------------------------------
/modbus4android/libs/commons-lang3-3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/modbus4android/libs/commons-lang3-3.1.jar
--------------------------------------------------------------------------------
/modbus4android/libs/commons-logging-1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/modbus4android/libs/commons-logging-1.2.jar
--------------------------------------------------------------------------------
/modbus4android/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/modbus4android/src/androidTest/java/com/zgkxzx/modbus4android/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4android;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.zgkxzx.modbus4android.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/modbus4android/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And;
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 | public Integer getIntValue(K key) {
38 | return (Integer) getValue(key);
39 | }
40 |
41 | public Long getLongValue(K key) {
42 | return (Long) getValue(key);
43 | }
44 |
45 | public Double getDoubleValue(K key) {
46 | return (Double) getValue(key);
47 | }
48 |
49 | public Float getFloatValue(K key) {
50 | return (Float) getValue(key);
51 | }
52 |
53 | @Override
54 | public String toString() {
55 | return data.toString();
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And;
22 |
23 | import com.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And;
22 |
23 | import com.zgkxzx.modbus4And.sero.util.ProgressiveTaskListener;
24 |
25 | /**
26 | * @author Matthew Lohbihler
27 | */
28 | public interface NodeScanListener extends ProgressiveTaskListener {
29 | void nodeFound(int nodeNumber);
30 | }
31 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And;
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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.base;
22 |
23 | import com.zgkxzx.modbus4And.sero.messaging.IncomingMessage;
24 | import com.zgkxzx.modbus4And.sero.messaging.MessageParser;
25 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.base;
22 |
23 | import com.zgkxzx.modbus4And.ModbusSlaveSet;
24 | import com.zgkxzx.modbus4And.ProcessImage;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
27 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
28 | import com.zgkxzx.modbus4And.sero.messaging.RequestHandler;
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 | request.validate(slave);
39 |
40 | int slaveId = request.getSlaveId();
41 |
42 | // Check the slave id.
43 | if (slaveId == 0) {
44 | // Broadcast message. Send to all process images.
45 | for (ProcessImage processImage : slave.getProcessImages())
46 | request.handle(processImage);
47 | return null;
48 | }
49 |
50 | // Find the process image to which to send.
51 | ProcessImage processImage = slave.getProcessImage(slaveId);
52 | if (processImage == null)
53 | return null;
54 |
55 | return request.handle(processImage);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.base;
22 |
23 | import com.zgkxzx.modbus4And.ExceptionResult;
24 | import com.zgkxzx.modbus4And.code.ExceptionCode;
25 | import com.zgkxzx.modbus4And.locator.BaseLocator;
26 |
27 | public class KeyedModbusLocator {
28 | private final K key;
29 | private final BaseLocator> locator;
30 |
31 | public KeyedModbusLocator(K key, BaseLocator> locator) {
32 | this.key = key;
33 | this.locator = locator;
34 | }
35 |
36 | public K getKey() {
37 | return key;
38 | }
39 |
40 | public BaseLocator> getLocator() {
41 | return locator;
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "KeyedModbusLocator(key=" + key + ", locator=" + locator + ")";
47 | }
48 |
49 | //
50 | ///
51 | /// Delegation.
52 | ///
53 | //
54 | public int getDataType() {
55 | return locator.getDataType();
56 | }
57 |
58 | public int getOffset() {
59 | return locator.getOffset();
60 | }
61 |
62 | public SlaveAndRange getSlaveAndRange() {
63 | return new SlaveAndRange(locator.getSlaveId(), locator.getRange());
64 | }
65 |
66 | public int getEndOffset() {
67 | return locator.getEndOffset();
68 | }
69 |
70 | public int getRegisterCount() {
71 | return locator.getRegisterCount();
72 | }
73 |
74 | public Object bytesToValue(byte[] data, int requestOffset) {
75 | try {
76 | return locator.bytesToValue(data, requestOffset);
77 | }
78 | catch (ArrayIndexOutOfBoundsException e) {
79 | // Some equipment will not return data lengths that we expect, which causes AIOOBEs. Catch them and convert
80 | // them into illegal data address exceptions.
81 | return new ExceptionResult(ExceptionCode.ILLEGAL_DATA_ADDRESS);
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.base;
22 |
23 | import com.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.base;
22 |
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | import com.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 | public static final byte ACKNOWLEDGE = 0x5;
32 | public static final byte SLAVE_DEVICE_BUSY = 0x6;
33 | public static final byte MEMORY_PARITY_ERROR = 0x8;
34 | public static final byte GATEWAY_PATH_UNAVAILABLE = 0xa;
35 | public static final byte GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND = 0xb;
36 |
37 | public static String getExceptionMessage(byte id) {
38 | switch (id) {
39 | case ILLEGAL_FUNCTION:
40 | return "Illegal function";
41 | case ILLEGAL_DATA_ADDRESS:
42 | return "Illegal data address";
43 | case ILLEGAL_DATA_VALUE:
44 | return "Illegal data value";
45 | case SLAVE_DEVICE_FAILURE:
46 | return "Slave device failure";
47 | case ACKNOWLEDGE:
48 | return "Acknowledge";
49 | case SLAVE_DEVICE_BUSY:
50 | return "Slave device busy";
51 | case MEMORY_PARITY_ERROR:
52 | return "Memory parity error";
53 | case GATEWAY_PATH_UNAVAILABLE:
54 | return "Gateway path unavailable";
55 | case GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND:
56 | return "Gateway target device failed to respond";
57 | }
58 | return "Unknown exception code: " + id;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.code;
22 |
23 | /**
24 | * @author Matthew Lohbihler
25 | */
26 | public class RegisterRange {
27 | public static final int COIL_STATUS = 1;
28 | public static final int INPUT_STATUS = 2;
29 | public static final int HOLDING_REGISTER = 3;
30 | public static final int INPUT_REGISTER = 4;
31 |
32 | public static int getFrom(int id) {
33 | switch (id) {
34 | case COIL_STATUS:
35 | return 0;
36 | case INPUT_STATUS:
37 | return 0x10000;
38 | case HOLDING_REGISTER:
39 | return 0x40000;
40 | case INPUT_REGISTER:
41 | return 0x30000;
42 | }
43 | return -1;
44 | }
45 |
46 | public static int getTo(int id) {
47 | switch (id) {
48 | case COIL_STATUS:
49 | return 0xffff;
50 | case INPUT_STATUS:
51 | return 0x1ffff;
52 | case HOLDING_REGISTER:
53 | return 0x4ffff;
54 | case INPUT_REGISTER:
55 | return 0x3ffff;
56 | }
57 | return -1;
58 | }
59 |
60 | public static int getReadFunctionCode(int id) {
61 | switch (id) {
62 | case COIL_STATUS:
63 | return FunctionCode.READ_COILS;
64 | case INPUT_STATUS:
65 | return FunctionCode.READ_DISCRETE_INPUTS;
66 | case HOLDING_REGISTER:
67 | return FunctionCode.READ_HOLDING_REGISTERS;
68 | case INPUT_REGISTER:
69 | return FunctionCode.READ_INPUT_REGISTERS;
70 | }
71 | return -1;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.exception;
22 |
23 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
24 | import com.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip;
22 |
23 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
24 |
25 | abstract public class IpMessage {
26 | protected final ModbusMessage modbusMessage;
27 |
28 | public IpMessage(ModbusMessage modbusMessage) {
29 | this.modbusMessage = modbusMessage;
30 | }
31 |
32 | public ModbusMessage getModbusMessage() {
33 | return modbusMessage;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip;
22 |
23 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
24 | import com.zgkxzx.modbus4And.sero.messaging.IncomingResponseMessage;
25 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingResponseMessage;
26 |
27 | public interface IpMessageResponse extends OutgoingResponseMessage, IncomingResponseMessage {
28 | ModbusResponse getModbusResponse();
29 | }
30 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip;
22 |
23 | import com.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.encap;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.ip.IpMessage;
25 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
26 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
27 |
28 | public class EncapMessage extends IpMessage {
29 | public EncapMessage(ModbusMessage modbusMessage) {
30 | super(modbusMessage);
31 | }
32 |
33 | public byte[] getMessageData() {
34 | ByteQueue msgQueue = new ByteQueue();
35 |
36 | // Write the particular message.
37 | modbusMessage.write(msgQueue);
38 |
39 | // Write the CRC
40 | ModbusUtils.pushShort(msgQueue, ModbusUtils.calculateCRC(modbusMessage));
41 |
42 | // Return the data.
43 | return msgQueue.popAll();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.encap;
22 |
23 | import com.zgkxzx.modbus4And.base.BaseMessageParser;
24 | import com.zgkxzx.modbus4And.sero.messaging.IncomingMessage;
25 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.encap;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
27 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.encap;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.ip.IpMessageResponse;
26 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
27 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.encap;
22 |
23 | import com.zgkxzx.modbus4And.ModbusSlaveSet;
24 | import com.zgkxzx.modbus4And.base.BaseRequestHandler;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
27 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingResponseMessage;
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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/ip/encap/EncapWaitingRoomKeyFactory.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.ip.encap;
2 |
3 | import com.zgkxzx.modbus4And.ip.IpMessage;
4 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
5 | import com.zgkxzx.modbus4And.sero.messaging.IncomingResponseMessage;
6 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingRequestMessage;
7 | import com.zgkxzx.modbus4And.sero.messaging.WaitingRoomKey;
8 | import com.zgkxzx.modbus4And.sero.messaging.WaitingRoomKeyFactory;
9 |
10 | public class EncapWaitingRoomKeyFactory implements WaitingRoomKeyFactory {
11 | @Override
12 | public WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request) {
13 | return createWaitingRoomKey(((IpMessage) request).getModbusMessage());
14 | }
15 |
16 | @Override
17 | public WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response) {
18 | return createWaitingRoomKey(((IpMessage) response).getModbusMessage());
19 | }
20 |
21 | public WaitingRoomKey createWaitingRoomKey(ModbusMessage msg) {
22 | return new EncapWaitingRoomKey(msg.getSlaveId(), msg.getFunctionCode());
23 | }
24 |
25 | class EncapWaitingRoomKey implements WaitingRoomKey {
26 | private final int slaveId;
27 | private final byte functionCode;
28 |
29 | public EncapWaitingRoomKey(int slaveId, byte functionCode) {
30 | this.slaveId = slaveId;
31 | this.functionCode = functionCode;
32 | }
33 |
34 | @Override
35 | public int hashCode() {
36 | final int prime = 31;
37 | int result = 1;
38 | result = prime * result + functionCode;
39 | result = prime * result + slaveId;
40 | return result;
41 | }
42 |
43 | @Override
44 | public boolean equals(Object obj) {
45 | if (this == obj)
46 | return true;
47 | if (obj == null)
48 | return false;
49 | if (getClass() != obj.getClass())
50 | return false;
51 | EncapWaitingRoomKey other = (EncapWaitingRoomKey) obj;
52 | if (functionCode != other.functionCode)
53 | return false;
54 | if (slaveId != other.slaveId)
55 | return false;
56 | return true;
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.xa;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.ip.IpMessage;
25 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
26 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
27 |
28 | public class XaMessage extends IpMessage {
29 | protected final int transactionId;
30 |
31 | public XaMessage(ModbusMessage modbusMessage, int transactionId) {
32 | super(modbusMessage);
33 | this.transactionId = transactionId;
34 | }
35 |
36 | public byte[] getMessageData() {
37 | ByteQueue msgQueue = new ByteQueue();
38 |
39 | // Write the particular message.
40 | modbusMessage.write(msgQueue);
41 |
42 | // Create the XA message
43 | ByteQueue xaQueue = new ByteQueue();
44 | ModbusUtils.pushShort(xaQueue, transactionId);
45 | ModbusUtils.pushShort(xaQueue, ModbusUtils.IP_PROTOCOL_ID);
46 | ModbusUtils.pushShort(xaQueue, msgQueue.size());
47 | xaQueue.push(msgQueue);
48 |
49 | // Return the data.
50 | return xaQueue.popAll();
51 | }
52 |
53 | public int getTransactionId() {
54 | return transactionId;
55 | }
56 |
57 | @Override
58 | public ModbusMessage getModbusMessage() {
59 | return modbusMessage;
60 | }
61 |
62 | @Override
63 | public String toString() {
64 | return "XaMessage [transactionId=" + transactionId + ", message=" + modbusMessage + "]";
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.xa;
22 |
23 | import com.zgkxzx.modbus4And.base.BaseMessageParser;
24 | import com.zgkxzx.modbus4And.sero.messaging.IncomingMessage;
25 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.xa;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
27 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.xa;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.ip.IpMessageResponse;
26 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
27 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.ip.xa;
22 |
23 | import com.zgkxzx.modbus4And.ModbusSlaveSet;
24 | import com.zgkxzx.modbus4And.base.BaseRequestHandler;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
27 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingResponseMessage;
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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/ip/xa/XaWaitingRoomKeyFactory.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.ip.xa;
2 |
3 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
4 | import com.zgkxzx.modbus4And.sero.messaging.IncomingResponseMessage;
5 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingRequestMessage;
6 | import com.zgkxzx.modbus4And.sero.messaging.WaitingRoomKey;
7 | import com.zgkxzx.modbus4And.sero.messaging.WaitingRoomKeyFactory;
8 |
9 | public class XaWaitingRoomKeyFactory implements WaitingRoomKeyFactory {
10 | @Override
11 | public WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request) {
12 | return createWaitingRoomKey((XaMessage) request);
13 | }
14 |
15 | @Override
16 | public WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response) {
17 | return createWaitingRoomKey((XaMessage) response);
18 | }
19 |
20 | public WaitingRoomKey createWaitingRoomKey(XaMessage msg) {
21 | return new XaWaitingRoomKey(msg.getTransactionId(), msg.getModbusMessage());
22 | }
23 |
24 | class XaWaitingRoomKey implements WaitingRoomKey {
25 | private final int transactionId;
26 | private final int slaveId;
27 | private final byte functionCode;
28 |
29 | public XaWaitingRoomKey(int transactionId, ModbusMessage msg) {
30 | this.transactionId = transactionId;
31 | this.slaveId = msg.getSlaveId();
32 | this.functionCode = msg.getFunctionCode();
33 | }
34 |
35 | @Override
36 | public int hashCode() {
37 | final int prime = 31;
38 | int result = 1;
39 | result = prime * result + functionCode;
40 | result = prime * result + slaveId;
41 | result = prime * result + transactionId;
42 | return result;
43 | }
44 |
45 | @Override
46 | public boolean equals(Object obj) {
47 | if (this == obj)
48 | return true;
49 | if (obj == null)
50 | return false;
51 | if (getClass() != obj.getClass())
52 | return false;
53 | XaWaitingRoomKey other = (XaWaitingRoomKey) obj;
54 | if (functionCode != other.functionCode)
55 | return false;
56 | if (slaveId != other.slaveId)
57 | return false;
58 | if (transactionId != other.transactionId)
59 | return false;
60 | return true;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/locator/BinaryLocator.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.locator;
2 |
3 | import com.zgkxzx.modbus4And.base.ModbusUtils;
4 | import com.zgkxzx.modbus4And.code.DataType;
5 | import com.zgkxzx.modbus4And.code.RegisterRange;
6 | import com.zgkxzx.modbus4And.exception.ModbusIdException;
7 | import com.zgkxzx.modbus4And.sero.NotImplementedException;
8 |
9 | public class BinaryLocator extends BaseLocator {
10 | private int bit = -1;
11 |
12 | public BinaryLocator(int slaveId, int range, int offset) {
13 | super(slaveId, range, offset);
14 | if (!isBinaryRange(range))
15 | throw new ModbusIdException("Non-bit requests can only be made from coil status and input status ranges");
16 | validate();
17 | }
18 |
19 | public BinaryLocator(int slaveId, int range, int offset, int bit) {
20 | super(slaveId, range, offset);
21 | if (isBinaryRange(range))
22 | throw new ModbusIdException("Bit requests can only be made from holding registers and input registers");
23 | this.bit = bit;
24 | validate();
25 | }
26 |
27 | public static boolean isBinaryRange(int range) {
28 | return range == RegisterRange.COIL_STATUS || range == RegisterRange.INPUT_STATUS;
29 | }
30 |
31 | protected void validate() {
32 | super.validate(1);
33 |
34 | if (!isBinaryRange(range))
35 | ModbusUtils.validateBit(bit);
36 | }
37 |
38 | public int getBit() {
39 | return bit;
40 | }
41 |
42 | @Override
43 | public int getDataType() {
44 | return DataType.BINARY;
45 | }
46 |
47 | @Override
48 | public int getRegisterCount() {
49 | return 1;
50 | }
51 |
52 | @Override
53 | public String toString() {
54 | return "BinaryLocator(slaveId=" + getSlaveId() + ", range=" + range + ", offset=" + offset + ", bit=" + bit
55 | + ")";
56 | }
57 |
58 | @Override
59 | public Boolean bytesToValueRealOffset(byte[] data, int offset) {
60 | // If this is a coil or input, convert to boolean.
61 | if (range == RegisterRange.COIL_STATUS || range == RegisterRange.INPUT_STATUS)
62 | return new Boolean((((data[offset / 8] & 0xff) >> (offset % 8)) & 0x1) == 1);
63 |
64 | // For the rest of the types, we double the normalized offset to account for short to byte.
65 | offset *= 2;
66 |
67 | // We could still be asking for a binary if it's a bit in a register.
68 | return new Boolean((((data[offset + 1 - bit / 8] & 0xff) >> (bit % 8)) & 0x1) == 1);
69 | }
70 |
71 | @Override
72 | public short[] valueToShorts(Boolean value) {
73 | throw new NotImplementedException();
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.Modbus;
24 | import com.zgkxzx.modbus4And.ProcessImage;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.sero.ShouldNeverHappenException;
27 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
28 |
29 | /**
30 | * @author Matthew Lohbihler
31 | */
32 | public class ExceptionRequest extends ModbusRequest {
33 | private final byte functionCode;
34 | private final byte exceptionCode;
35 |
36 | public ExceptionRequest(int slaveId, byte functionCode, byte exceptionCode) throws ModbusTransportException {
37 | super(slaveId);
38 | this.functionCode = functionCode;
39 | this.exceptionCode = exceptionCode;
40 | }
41 |
42 | @Override
43 | public void validate(Modbus modbus) {
44 | // no op
45 | }
46 |
47 | @Override
48 | protected void writeRequest(ByteQueue queue) {
49 | throw new ShouldNeverHappenException("wha");
50 | }
51 |
52 | @Override
53 | protected void readRequest(ByteQueue queue) {
54 | queue.clear();
55 | }
56 |
57 | @Override
58 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException {
59 | return new ExceptionResponse(slaveId, functionCode, exceptionCode);
60 | }
61 |
62 | @Override
63 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException {
64 | return getResponseInstance(slaveId);
65 | }
66 |
67 | @Override
68 | public byte getFunctionCode() {
69 | return functionCode;
70 | }
71 |
72 | public byte getExceptionCode() {
73 | return exceptionCode;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
24 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.ProcessImage;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.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 | @Override
57 | public String toString() {
58 | return "ReadCoilsRequest [slaveId=" + slaveId + ", getFunctionCode()=" + getFunctionCode() + ", toString()="
59 | + super.toString() + "]";
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.code.FunctionCode;
24 | import com.zgkxzx.modbus4And.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 | @Override
41 | public String toString() {
42 | return "ReadCoilsResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId + ", getFunctionCode()="
43 | + getFunctionCode() + ", isException()=" + isException() + ", getExceptionMessage()="
44 | + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode() + ", toString()="
45 | + super.toString(false) + "]";
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.ProcessImage;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.code.FunctionCode;
24 | import com.zgkxzx.modbus4And.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 | @Override
41 | public String toString() {
42 | return "ReadDiscreteInputsResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId
43 | + ", getFunctionCode()=" + getFunctionCode() + ", isException()=" + isException()
44 | + ", getExceptionMessage()=" + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode()
45 | + super.toString(false) + "]";
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.Modbus;
24 | import com.zgkxzx.modbus4And.ProcessImage;
25 | import com.zgkxzx.modbus4And.code.FunctionCode;
26 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
27 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
28 |
29 | public class ReadExceptionStatusRequest extends ModbusRequest {
30 | public ReadExceptionStatusRequest(int slaveId) throws ModbusTransportException {
31 | super(slaveId);
32 | }
33 |
34 | @Override
35 | public void validate(Modbus modbus) {
36 | // no op
37 | }
38 |
39 | @Override
40 | protected void writeRequest(ByteQueue queue) {
41 | // no op
42 | }
43 |
44 | @Override
45 | protected void readRequest(ByteQueue queue) {
46 | // no op
47 | }
48 |
49 | @Override
50 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException {
51 | return new ReadExceptionStatusResponse(slaveId);
52 | }
53 |
54 | @Override
55 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException {
56 | return new ReadExceptionStatusResponse(slaveId, processImage.getExceptionStatus());
57 | }
58 |
59 | @Override
60 | public byte getFunctionCode() {
61 | return FunctionCode.READ_EXCEPTION_STATUS;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.code.FunctionCode;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.ProcessImage;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.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 | @Override
58 | public String toString() {
59 | return "ReadHoldingRegistersRequest [slaveId=" + slaveId + ", getFunctionCode()=" + getFunctionCode()
60 | + ", toString()=" + super.toString() + "]";
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.code.FunctionCode;
24 | import com.zgkxzx.modbus4And.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 | @Override
41 | public String toString() {
42 | return "ReadHoldingRegistersResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId
43 | + ", getFunctionCode()=" + getFunctionCode() + ", isException()=" + isException()
44 | + ", getExceptionMessage()=" + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode()
45 | + ", toString()=" + super.toString(true) + "]";
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.ProcessImage;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.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 | @Override
58 | public String toString() {
59 | return "ReadInputRegistersRequest [slaveId=" + slaveId + ", getFunctionCode()=" + getFunctionCode()
60 | + ", toString()=" + super.toString() + "]";
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.code.FunctionCode;
24 | import com.zgkxzx.modbus4And.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 | @Override
41 | public String toString() {
42 | return "ReadInputRegistersResponse [exceptionCode=" + exceptionCode + ", slaveId=" + slaveId
43 | + ", getFunctionCode()=" + getFunctionCode() + ", isException()=" + isException()
44 | + ", getExceptionMessage()=" + getExceptionMessage() + ", getExceptionCode()=" + getExceptionCode()
45 | + ", toString()=" + super.toString(true) + "]";
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.sero.io.StreamUtils;
26 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
27 |
28 | abstract public class ReadResponse extends ModbusResponse {
29 | private byte[] data;
30 |
31 | ReadResponse(int slaveId) throws ModbusTransportException {
32 | super(slaveId);
33 | }
34 |
35 | ReadResponse(int slaveId, byte[] data) throws ModbusTransportException {
36 | super(slaveId);
37 | this.data = data;
38 | }
39 |
40 | @Override
41 | protected void readResponse(ByteQueue queue) {
42 | int numberOfBytes = ModbusUtils.popUnsignedByte(queue);
43 | if (queue.size() < numberOfBytes)
44 | throw new ArrayIndexOutOfBoundsException();
45 |
46 | data = new byte[numberOfBytes];
47 | queue.pop(data);
48 | }
49 |
50 | @Override
51 | protected void writeResponse(ByteQueue queue) {
52 | ModbusUtils.pushByte(queue, data.length);
53 | queue.push(data);
54 | }
55 |
56 | public byte[] getData() {
57 | return data;
58 | }
59 |
60 | public short[] getShortData() {
61 | return convertToShorts(data);
62 | }
63 |
64 | public boolean[] getBooleanData() {
65 | return convertToBooleans(data);
66 | }
67 |
68 | public String toString(boolean numeric) {
69 | if (data == null)
70 | return "ReadResponse [null]";
71 | return "ReadResponse [len=" + (numeric ? data.length / 2 : data.length * 8) + ", " + StreamUtils.dumpHex(data)
72 | + "]";
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.Modbus;
24 | import com.zgkxzx.modbus4And.ProcessImage;
25 | import com.zgkxzx.modbus4And.code.FunctionCode;
26 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
27 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
28 |
29 | public class ReportSlaveIdRequest extends ModbusRequest {
30 | public ReportSlaveIdRequest(int slaveId) throws ModbusTransportException {
31 | super(slaveId);
32 | }
33 |
34 | @Override
35 | public void validate(Modbus modbus) {
36 | // no op
37 | }
38 |
39 | @Override
40 | protected void writeRequest(ByteQueue queue) {
41 | // no op
42 | }
43 |
44 | @Override
45 | protected void readRequest(ByteQueue queue) {
46 | // no op
47 | }
48 |
49 | @Override
50 | ModbusResponse getResponseInstance(int slaveId) throws ModbusTransportException {
51 | return new ReportSlaveIdResponse(slaveId);
52 | }
53 |
54 | @Override
55 | ModbusResponse handleImpl(ProcessImage processImage) throws ModbusTransportException {
56 | return new ReportSlaveIdResponse(slaveId, processImage.getReportSlaveIdData());
57 | }
58 |
59 | @Override
60 | public byte getFunctionCode() {
61 | return FunctionCode.REPORT_SLAVE_ID;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/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.zgkxzx.modbus4And.msg;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.code.FunctionCode;
25 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
26 | import com.zgkxzx.modbus4And.sero.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 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/requset/ModbusParam.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.requset;
2 |
3 | import com.zgkxzx.modbus4And.base.ModbusUtils;
4 |
5 | /**
6 | * 创建者 zgkxzx
7 | * 创建日期 2017/10/12.
8 | * 功能描述
9 | */
10 |
11 | public class ModbusParam {
12 | public String host;
13 | public int port = ModbusUtils.TCP_PORT;
14 | public boolean encapsulated;
15 |
16 | public int timeout = 500;
17 | public int retries = 2;
18 | public boolean keepAlive = true;
19 |
20 | /**
21 | * If connection is established with slave/slaves
22 | */
23 | public boolean connected = false;
24 |
25 |
26 | public ModbusParam setHost(String host) {
27 | this.host = host;
28 | return this;
29 | }
30 |
31 |
32 |
33 | public ModbusParam setPort(int port) {
34 | this.port = port;
35 | return this;
36 | }
37 |
38 |
39 |
40 | public ModbusParam setEncapsulated(boolean encapsulated) {
41 | this.encapsulated = encapsulated;
42 | return this;
43 | }
44 |
45 |
46 |
47 | public ModbusParam setTimeout(int timeout) {
48 | this.timeout = timeout;
49 | return this;
50 | }
51 |
52 |
53 |
54 | public ModbusParam setRetries(int retries) {
55 | this.retries = retries;
56 | return this;
57 | }
58 |
59 |
60 |
61 | public ModbusParam setConnected(boolean connected) {
62 | this.connected = connected;
63 | return this;
64 | }
65 |
66 |
67 |
68 | public ModbusParam setKeepAlive(boolean keepAlive) {
69 | this.keepAlive = keepAlive;
70 | return this;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/requset/OnRequestBack.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.requset;
2 |
3 | /**
4 | * 创建者 zgkxzx
5 | * 创建日期 2017/6/12.
6 | * 功能描述
7 | */
8 |
9 | public interface OnRequestBack {
10 | void onSuccess(T t);
11 | void onFailed(String msg);
12 | }
13 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/SerialMessage.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.serial;
2 |
3 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
4 |
5 | abstract public class SerialMessage {
6 | protected final ModbusMessage modbusMessage;
7 |
8 | public SerialMessage(ModbusMessage modbusMessage) {
9 | this.modbusMessage = modbusMessage;
10 | }
11 |
12 | public ModbusMessage getModbusMessage() {
13 | return modbusMessage;
14 | }
15 |
16 | @Override
17 | public String toString() {
18 | return "SerialMessage [modbusMessage=" + modbusMessage + "]";
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/SerialPortWrapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Infinite Automation Software. All rights reserved.
3 | * @author Terry Packer
4 | */
5 | package com.zgkxzx.modbus4And.serial;
6 |
7 | import java.io.InputStream;
8 | import java.io.OutputStream;
9 |
10 | /**
11 | * Wrapper to further aid in abstracting Modbus4J from a serial port implementation
12 | *
13 | * @author Terry Packer
14 | *
15 | */
16 | public interface SerialPortWrapper {
17 |
18 | /**
19 | * Close the Serial Port
20 | */
21 | void close() throws Exception;
22 |
23 | /**
24 | *
25 | */
26 | void open() throws Exception;
27 |
28 | /**
29 | *
30 | * Return the input stream for an open port
31 | * @return
32 | */
33 | InputStream getInputStream();
34 |
35 | /**
36 | * Return the output stream for an open port
37 | * @return
38 | */
39 | OutputStream getOutputStream();
40 |
41 | /**
42 | * @return
43 | */
44 | int getBaudRate();
45 |
46 | /**
47 | *
48 | * @return
49 | */
50 | int getFlowControlIn();
51 |
52 | /**
53 | *
54 | * @return
55 | */
56 | int getFlowControlOut();
57 |
58 | /**
59 | * @return
60 | */
61 | int getDataBits();
62 |
63 | /**
64 | * @return
65 | */
66 | int getStopBits();
67 |
68 | /**
69 | * @return
70 | */
71 | int getParity();
72 |
73 |
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/SerialSlave.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial;
22 |
23 | import org.apache.commons.logging.Log;
24 | import org.apache.commons.logging.LogFactory;
25 |
26 | import com.zgkxzx.modbus4And.ModbusSlaveSet;
27 | import com.zgkxzx.modbus4And.exception.ModbusInitException;
28 | import com.zgkxzx.modbus4And.sero.messaging.StreamTransport;
29 |
30 | abstract public class SerialSlave extends ModbusSlaveSet {
31 |
32 | private final Log LOG = LogFactory.getLog(SerialSlave.class);
33 |
34 | // Runtime fields
35 | private SerialPortWrapper wrapper;
36 | protected StreamTransport transport;
37 |
38 | public SerialSlave(SerialPortWrapper wrapper) {
39 | this.wrapper = wrapper;
40 | }
41 |
42 | @Override
43 | public void start() throws ModbusInitException {
44 | try {
45 |
46 | wrapper.open();
47 |
48 | transport = new StreamTransport(wrapper.getInputStream(), wrapper.getOutputStream());
49 | }
50 | catch (Exception e) {
51 | throw new ModbusInitException(e);
52 | }
53 | }
54 |
55 | @Override
56 | public void stop() {
57 | try {
58 | wrapper.close();
59 | } catch (Exception e) {
60 | LOG.error(e.getMessage(),e);
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/SerialWaitingRoomKeyFactory.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.serial;
2 |
3 | import com.zgkxzx.modbus4And.sero.messaging.IncomingResponseMessage;
4 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingRequestMessage;
5 | import com.zgkxzx.modbus4And.sero.messaging.WaitingRoomKey;
6 | import com.zgkxzx.modbus4And.sero.messaging.WaitingRoomKeyFactory;
7 |
8 | public class SerialWaitingRoomKeyFactory implements WaitingRoomKeyFactory {
9 | private static final Sync sync = new Sync();
10 |
11 | @Override
12 | public WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request) {
13 | return sync;
14 | }
15 |
16 | @Override
17 | public WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response) {
18 | return sync;
19 | }
20 |
21 | static class Sync implements WaitingRoomKey {
22 | @Override
23 | public int hashCode() {
24 | return 1;
25 | }
26 |
27 | @Override
28 | public boolean equals(Object obj) {
29 | if (this == obj)
30 | return true;
31 | if (obj == null)
32 | return false;
33 | if (getClass() != obj.getClass())
34 | return false;
35 | return true;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/ascii/AsciiMessageParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.ascii;
22 |
23 | import com.zgkxzx.modbus4And.base.BaseMessageParser;
24 | import com.zgkxzx.modbus4And.sero.messaging.IncomingMessage;
25 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
26 |
27 | public class AsciiMessageParser extends BaseMessageParser {
28 | public AsciiMessageParser(boolean master) {
29 | super(master);
30 | }
31 |
32 | @Override
33 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception {
34 | if (master)
35 | return AsciiMessageResponse.createAsciiMessageResponse(queue);
36 | return AsciiMessageRequest.createAsciiMessageRequest(queue);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/ascii/AsciiMessageRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.ascii;
22 |
23 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
24 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
27 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
29 |
30 | public class AsciiMessageRequest extends AsciiMessage implements OutgoingRequestMessage, IncomingRequestMessage {
31 | static AsciiMessageRequest createAsciiMessageRequest(ByteQueue queue) throws ModbusTransportException {
32 | ByteQueue msgQueue = getUnasciiMessage(queue);
33 | ModbusRequest request = ModbusRequest.createModbusRequest(msgQueue);
34 | AsciiMessageRequest asciiRequest = new AsciiMessageRequest(request);
35 |
36 | // Return the data.
37 | return asciiRequest;
38 | }
39 |
40 | public AsciiMessageRequest(ModbusMessage modbusMessage) {
41 | super(modbusMessage);
42 | }
43 |
44 | @Override
45 | public boolean expectsResponse() {
46 | return modbusMessage.getSlaveId() != 0;
47 | }
48 |
49 | public ModbusRequest getModbusRequest() {
50 | return (ModbusRequest) modbusMessage;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/ascii/AsciiMessageResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.ascii;
22 |
23 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
24 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
25 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
26 | import com.zgkxzx.modbus4And.sero.messaging.IncomingResponseMessage;
27 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingResponseMessage;
28 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
29 |
30 | public class AsciiMessageResponse extends AsciiMessage implements OutgoingResponseMessage, IncomingResponseMessage {
31 | static AsciiMessageResponse createAsciiMessageResponse(ByteQueue queue) throws ModbusTransportException {
32 | ByteQueue msgQueue = getUnasciiMessage(queue);
33 | ModbusResponse response = ModbusResponse.createModbusResponse(msgQueue);
34 | AsciiMessageResponse asciiResponse = new AsciiMessageResponse(response);
35 |
36 | // Return the data.
37 | return asciiResponse;
38 | }
39 |
40 | public AsciiMessageResponse(ModbusMessage modbusMessage) {
41 | super(modbusMessage);
42 | }
43 |
44 | public ModbusResponse getModbusResponse() {
45 | return (ModbusResponse) modbusMessage;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/ascii/AsciiRequestHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.ascii;
22 |
23 | import com.zgkxzx.modbus4And.ModbusSlaveSet;
24 | import com.zgkxzx.modbus4And.base.BaseRequestHandler;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
27 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingResponseMessage;
29 |
30 | public class AsciiRequestHandler extends BaseRequestHandler {
31 | public AsciiRequestHandler(ModbusSlaveSet slave) {
32 | super(slave);
33 | }
34 |
35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception {
36 | AsciiMessageRequest asciiRequest = (AsciiMessageRequest) req;
37 | ModbusRequest request = asciiRequest.getModbusRequest();
38 | ModbusResponse response = handleRequestImpl(request);
39 | if (response == null)
40 | return null;
41 | return new AsciiMessageResponse(response);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/ascii/AsciiSlave.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.ascii;
22 |
23 | import java.io.IOException;
24 |
25 | import com.zgkxzx.modbus4And.exception.ModbusInitException;
26 | import com.zgkxzx.modbus4And.serial.SerialPortWrapper;
27 | import com.zgkxzx.modbus4And.serial.SerialSlave;
28 | import com.zgkxzx.modbus4And.sero.messaging.MessageControl;
29 |
30 | public class AsciiSlave extends SerialSlave {
31 | private MessageControl conn;
32 |
33 | public AsciiSlave(SerialPortWrapper wrapper) {
34 | super(wrapper);
35 | }
36 |
37 | @Override
38 | public void start() throws ModbusInitException {
39 | super.start();
40 |
41 | AsciiMessageParser asciiMessageParser = new AsciiMessageParser(false);
42 | AsciiRequestHandler asciiRequestHandler = new AsciiRequestHandler(this);
43 |
44 | conn = new MessageControl();
45 | conn.setExceptionHandler(getExceptionHandler());
46 |
47 | try {
48 | conn.start(transport, asciiMessageParser, asciiRequestHandler, null);
49 | transport.start("Modbus ASCII slave");
50 | }
51 | catch (IOException e) {
52 | throw new ModbusInitException(e);
53 | }
54 | }
55 |
56 | @Override
57 | public void stop() {
58 | conn.close();
59 | super.stop();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/rtu/RtuMessage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.rtu;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.msg.ModbusMessage;
25 | import com.zgkxzx.modbus4And.serial.SerialMessage;
26 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
27 |
28 | /**
29 | * Convenience superclass primarily for calculating CRC values.
30 | *
31 | * @author mlohbihler
32 | */
33 | public class RtuMessage extends SerialMessage {
34 | public RtuMessage(ModbusMessage modbusMessage) {
35 | super(modbusMessage);
36 | }
37 |
38 | public byte[] getMessageData() {
39 | ByteQueue queue = new ByteQueue();
40 |
41 | // Write the particular message.
42 | modbusMessage.write(queue);
43 |
44 | // Write the CRC
45 | ModbusUtils.pushShort(queue, ModbusUtils.calculateCRC(modbusMessage));
46 |
47 | // Return the data.
48 | return queue.popAll();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/rtu/RtuMessageParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.rtu;
22 |
23 | import com.zgkxzx.modbus4And.base.BaseMessageParser;
24 | import com.zgkxzx.modbus4And.sero.messaging.IncomingMessage;
25 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
26 |
27 | /**
28 | * Message parser implementation for RTU encoding. Primary reference for the ordering of CRC bytes. Also provides
29 | * handling of incomplete messages.
30 | *
31 | * @author mlohbihler
32 | */
33 | public class RtuMessageParser extends BaseMessageParser {
34 | public RtuMessageParser(boolean master) {
35 | super(master);
36 | }
37 |
38 | @Override
39 | protected IncomingMessage parseMessageImpl(ByteQueue queue) throws Exception {
40 | if (master)
41 | return RtuMessageResponse.createRtuMessageResponse(queue);
42 | return RtuMessageRequest.createRtuMessageRequest(queue);
43 | }
44 | //
45 | // public static void main(String[] args) throws Exception {
46 | // ByteQueue queue = new ByteQueue(new byte[] { 5, 3, 2, 0, (byte) 0xdc, (byte) 0x48, (byte) 0x1d, 0 });
47 | // RtuMessageParser p = new RtuMessageParser(false);
48 | // System.out.println(p.parseResponse(queue));
49 | // }
50 | }
51 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/rtu/RtuMessageRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.rtu;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
27 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
29 |
30 | /**
31 | * Handles the RTU enveloping of modbus requests.
32 | *
33 | * @author mlohbihler
34 | */
35 | public class RtuMessageRequest extends RtuMessage implements OutgoingRequestMessage, IncomingRequestMessage {
36 | static RtuMessageRequest createRtuMessageRequest(ByteQueue queue) throws ModbusTransportException {
37 | ModbusRequest request = ModbusRequest.createModbusRequest(queue);
38 | RtuMessageRequest rtuRequest = new RtuMessageRequest(request);
39 |
40 | // Check the CRC
41 | ModbusUtils.checkCRC(rtuRequest.modbusMessage, queue);
42 |
43 | // Return the data.
44 | return rtuRequest;
45 | }
46 |
47 | public RtuMessageRequest(ModbusRequest modbusRequest) {
48 | super(modbusRequest);
49 | }
50 |
51 | @Override
52 | public boolean expectsResponse() {
53 | return modbusMessage.getSlaveId() != 0;
54 | }
55 |
56 | public ModbusRequest getModbusRequest() {
57 | return (ModbusRequest) modbusMessage;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/rtu/RtuMessageResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.rtu;
22 |
23 | import com.zgkxzx.modbus4And.base.ModbusUtils;
24 | import com.zgkxzx.modbus4And.exception.ModbusTransportException;
25 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
26 | import com.zgkxzx.modbus4And.sero.messaging.IncomingResponseMessage;
27 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingResponseMessage;
28 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
29 |
30 | /**
31 | * Handles the RTU enveloping of modbus responses.
32 | *
33 | * @author mlohbihler
34 | */
35 | public class RtuMessageResponse extends RtuMessage implements OutgoingResponseMessage, IncomingResponseMessage {
36 | static RtuMessageResponse createRtuMessageResponse(ByteQueue queue) throws ModbusTransportException {
37 | ModbusResponse response = ModbusResponse.createModbusResponse(queue);
38 | RtuMessageResponse rtuResponse = new RtuMessageResponse(response);
39 |
40 | // Check the CRC
41 | ModbusUtils.checkCRC(rtuResponse.modbusMessage, queue);
42 |
43 | // Return the data.
44 | return rtuResponse;
45 | }
46 |
47 | public RtuMessageResponse(ModbusResponse modbusResponse) {
48 | super(modbusResponse);
49 | }
50 |
51 | public ModbusResponse getModbusResponse() {
52 | return (ModbusResponse) modbusMessage;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/rtu/RtuRequestHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.rtu;
22 |
23 | import com.zgkxzx.modbus4And.ModbusSlaveSet;
24 | import com.zgkxzx.modbus4And.base.BaseRequestHandler;
25 | import com.zgkxzx.modbus4And.msg.ModbusRequest;
26 | import com.zgkxzx.modbus4And.msg.ModbusResponse;
27 | import com.zgkxzx.modbus4And.sero.messaging.IncomingRequestMessage;
28 | import com.zgkxzx.modbus4And.sero.messaging.OutgoingResponseMessage;
29 |
30 | public class RtuRequestHandler extends BaseRequestHandler {
31 | public RtuRequestHandler(ModbusSlaveSet slave) {
32 | super(slave);
33 | }
34 |
35 | public OutgoingResponseMessage handleRequest(IncomingRequestMessage req) throws Exception {
36 | RtuMessageRequest rtuRequest = (RtuMessageRequest) req;
37 | ModbusRequest request = rtuRequest.getModbusRequest();
38 | ModbusResponse response = handleRequestImpl(request);
39 | if (response == null)
40 | return null;
41 | return new RtuMessageResponse(response);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/serial/rtu/RtuSlave.java:
--------------------------------------------------------------------------------
1 | /*
2 | * ============================================================================
3 | * GNU General Public License
4 | * ============================================================================
5 | *
6 | * Copyright (C) 2006-2011 Serotonin Software Technologies Inc. http://serotoninsoftware.com
7 | * @author Matthew Lohbihler
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | * You should have received a copy of the GNU General Public License
19 | * along with this program. If not, see .
20 | */
21 | package com.zgkxzx.modbus4And.serial.rtu;
22 |
23 | import java.io.IOException;
24 |
25 | import com.zgkxzx.modbus4And.exception.ModbusInitException;
26 | import com.zgkxzx.modbus4And.serial.SerialPortWrapper;
27 | import com.zgkxzx.modbus4And.serial.SerialSlave;
28 | import com.zgkxzx.modbus4And.sero.messaging.MessageControl;
29 |
30 | public class RtuSlave extends SerialSlave {
31 | // Runtime fields
32 | private MessageControl conn;
33 |
34 | public RtuSlave(SerialPortWrapper wrapper) {
35 | super(wrapper);
36 | }
37 |
38 | @Override
39 | public void start() throws ModbusInitException {
40 | super.start();
41 |
42 | RtuMessageParser rtuMessageParser = new RtuMessageParser(false);
43 | RtuRequestHandler rtuRequestHandler = new RtuRequestHandler(this);
44 |
45 | conn = new MessageControl();
46 | conn.setExceptionHandler(getExceptionHandler());
47 |
48 | try {
49 | conn.start(transport, rtuMessageParser, rtuRequestHandler, null);
50 | transport.start("Modbus RTU slave");
51 | }
52 | catch (IOException e) {
53 | throw new ModbusInitException(e);
54 | }
55 | }
56 |
57 | @Override
58 | public void stop() {
59 | conn.close();
60 | super.stop();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/NotImplementedException.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc.
3 | @author Matthew Lohbihler
4 | */
5 | package com.zgkxzx.modbus4And.sero;
6 |
7 | /**
8 | * @author Matthew Lohbihler
9 | */
10 | public class NotImplementedException extends RuntimeException {
11 | static final long serialVersionUID = -1;
12 |
13 | public NotImplementedException() {
14 | super();
15 | }
16 |
17 | public NotImplementedException(String message) {
18 | super(message);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/ShouldNeverHappenException.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc.
3 | @author Matthew Lohbihler
4 | */
5 | package com.zgkxzx.modbus4And.sero;
6 |
7 | /**
8 | * @author Matthew Lohbihler
9 | */
10 | public class ShouldNeverHappenException extends RuntimeException {
11 | private static final long serialVersionUID = -1;
12 |
13 | public ShouldNeverHappenException(String message) {
14 | super(message);
15 | }
16 |
17 | public ShouldNeverHappenException(Throwable cause) {
18 | super(cause);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/epoll/InputStreamEPollWrapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Infinite Automation Software. All rights reserved.
3 | * @author Terry Packer
4 | */
5 | package com.zgkxzx.modbus4And.sero.epoll;
6 |
7 | import java.io.InputStream;
8 |
9 | /**
10 | * @author Terry Packer
11 | *
12 | */
13 | public interface InputStreamEPollWrapper {
14 |
15 | /**
16 | * @param in
17 | * @param inputStreamCallback
18 | */
19 | void add(InputStream in, Modbus4JInputStreamCallback inputStreamCallback);
20 |
21 | /**
22 | * @param in
23 | */
24 | void remove(InputStream in);
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/epoll/Modbus4JInputStreamCallback.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.epoll;
2 |
3 | import java.io.IOException;
4 |
5 | /**
6 | * A callback interface for input streams.
7 | *
8 | * NOTE: if the InputStreamEPoll instance is terminated, any running processes will be destroyed without any
9 | * notification to this callback.
10 | *
11 | * @author Matthew Lohbihler
12 | */
13 | public interface Modbus4JInputStreamCallback {
14 | /**
15 | * Called when content is read from the input stream.
16 | *
17 | * @param buf
18 | * the content that was read. This is a shared byte array. Contents can be manipulated within this call,
19 | * but the array itself should not be stored beyond the call since the contents will be changed.
20 | * @param len
21 | * the length of content that was read.
22 | */
23 | void input(byte[] buf, int len);
24 |
25 | /**
26 | * Called when the closure of the input stream is detected.
27 | */
28 | void closed();
29 |
30 | /**
31 | * Called if there is an {@link IOException} while reading input stream.
32 | *
33 | * @param e
34 | * the exception that was received
35 | */
36 | void ioException(IOException e);
37 |
38 | /**
39 | * Called if the InputStreamEPoll instance was terminated while the input stream was still registered.
40 | */
41 | void terminated();
42 | }
43 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/io/LineHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2006-2009 Serotonin Software Technologies Inc.
3 | @author Matthew Lohbihler
4 | */
5 | package com.zgkxzx.modbus4And.sero.io;
6 |
7 | /**
8 | * @author Matthew Lohbihler
9 | */
10 | public interface LineHandler {
11 | public void handleLine(String line);
12 |
13 | public void done();
14 | }
15 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/io/NullWriter.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.io;
2 |
3 | import java.io.IOException;
4 | import java.io.Writer;
5 |
6 | public class NullWriter extends Writer {
7 | @Override
8 | public void write(char[] cbuf, int off, int len) throws IOException {
9 | // no op
10 | }
11 |
12 | @Override
13 | public void flush() throws IOException {
14 | // no op
15 | }
16 |
17 | @Override
18 | public void close() throws IOException {
19 | // no op
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/DataConsumer.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import java.io.IOException;
4 |
5 | public interface DataConsumer {
6 | /**
7 | * Notifies the consumer that new data is available
8 | *
9 | * @param b
10 | * array of bytes representing the incoming information
11 | * @param len
12 | * length of the data
13 | */
14 | public void data(byte[] b, int len);
15 |
16 | public void handleIOException(IOException e);
17 | }
18 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/DefaultMessagingExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | public class DefaultMessagingExceptionHandler implements MessagingExceptionHandler {
4 | public void receivedException(Exception e) {
5 | e.printStackTrace();
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/EpollStreamTransport.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.io.OutputStream;
6 |
7 | import com.zgkxzx.modbus4And.sero.epoll.Modbus4JInputStreamCallback;
8 | import com.zgkxzx.modbus4And.sero.epoll.InputStreamEPollWrapper;
9 |
10 | /**
11 | * First, instatiate with the streams and epoll. Then add a data consumer, or create a message control and pass this as
12 | * the transport (which will make the message control the data consumer). Stop the transport by stopping the message
13 | * control).
14 | *
15 | * @author Matthew Lohbihler
16 | */
17 | public class EpollStreamTransport implements Transport {
18 | private final OutputStream out;
19 | private final InputStream in;
20 | private final InputStreamEPollWrapper epoll;
21 |
22 | public EpollStreamTransport(InputStream in, OutputStream out, InputStreamEPollWrapper epoll) {
23 | this.out = out;
24 | this.in = in;
25 | this.epoll = epoll;
26 | }
27 |
28 | public void setConsumer(final DataConsumer consumer) {
29 | epoll.add(in, new Modbus4JInputStreamCallback() {
30 | public void terminated() {
31 | removeConsumer();
32 | }
33 |
34 | public void ioException(IOException e) {
35 | consumer.handleIOException(e);
36 | }
37 |
38 | public void input(byte[] buf, int len) {
39 | consumer.data(buf, len);
40 | }
41 |
42 | public void closed() {
43 | removeConsumer();
44 | }
45 | });
46 | }
47 |
48 | public void removeConsumer() {
49 | epoll.remove(in);
50 | }
51 |
52 | public void write(byte[] data) throws IOException {
53 | out.write(data);
54 | out.flush();
55 | }
56 |
57 | public void write(byte[] data, int len) throws IOException {
58 | out.write(data, 0, len);
59 | out.flush();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/EpollStreamTransportCharSpaced.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2014 Infinite Automation Software. All rights reserved.
3 | * @author Terry Packer
4 | */
5 | package com.zgkxzx.modbus4And.sero.messaging;
6 |
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.io.OutputStream;
10 |
11 | import com.zgkxzx.modbus4And.sero.epoll.InputStreamEPollWrapper;
12 |
13 | /**
14 | * @author Terry Packer
15 | *
16 | */
17 | public class EpollStreamTransportCharSpaced extends EpollStreamTransport{
18 |
19 | private final long charSpacing; //Spacing for chars in nanoseconds
20 | private final OutputStream out; //Since the subclass has private members
21 |
22 | /**
23 | * @param in
24 | * @param out
25 | * @param epoll
26 | */
27 | public EpollStreamTransportCharSpaced(InputStream in, OutputStream out,
28 | InputStreamEPollWrapper epoll, long charSpacing) {
29 | super(in, out, epoll);
30 | this.out = out;
31 | this.charSpacing = charSpacing;
32 | }
33 |
34 | /**
35 | * Perform a write, ensure space between chars
36 | */
37 | @Override
38 | public void write(byte[] data) throws IOException {
39 |
40 | try{
41 | long waited = 0,writeStart,writeEnd, waitRemaining;
42 | for(byte b : data){
43 | writeStart = System.nanoTime();
44 | out.write(b);
45 | writeEnd = System.nanoTime();
46 | waited = writeEnd - writeStart;
47 | if(waited < this.charSpacing){
48 | waitRemaining = this.charSpacing - waited;
49 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000));
50 | }
51 |
52 | }
53 | }catch(Exception e){
54 | throw new IOException(e);
55 | }
56 | out.flush();
57 | }
58 |
59 | public void write(byte[] data, int len) throws IOException {
60 | try{
61 | long waited = 0,writeStart,writeEnd, waitRemaining;
62 | for(int i=0; i< len; i++){
63 | writeStart = System.nanoTime();
64 | out.write(data[i]);
65 | writeEnd = System.nanoTime();
66 | waited = writeEnd - writeStart;
67 | if(waited < this.charSpacing){
68 | waitRemaining = this.charSpacing - waited;
69 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000));
70 | }
71 |
72 | }
73 | }catch(Exception e){
74 | throw new IOException(e);
75 | }
76 | out.flush();
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/IncomingMessage.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | public interface IncomingMessage {
4 | // A marker interface
5 | }
6 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/IncomingRequestMessage.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 |
4 | public interface IncomingRequestMessage extends IncomingMessage {
5 | // A marker interface.
6 | }
7 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/IncomingResponseMessage.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 |
4 | public interface IncomingResponseMessage extends IncomingMessage {
5 | // A marker interface
6 | }
7 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/MessageParser.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import com.zgkxzx.modbus4And.sero.util.queue.ByteQueue;
4 |
5 | /**
6 | * Interface defining methods that are called when data arrives in the connection.
7 | *
8 | * @author Matthew Lohbihler
9 | */
10 | public interface MessageParser {
11 | /**
12 | * Attempt to parse a message out of the queue. Data in the queue may be discarded if it is unusable (i.e. a start
13 | * indicator is not found), but otherwise if a message is not found due to the data being incomplete, the method
14 | * should return null. As additional data arrives, it will be appended to the queue and this method will be called
15 | * again.
16 | *
17 | * Implementations should not modify the queue unless it is safe to do so. No copy of the data is made before
18 | * calling this method.
19 | *
20 | * @param queue
21 | * the queue from which to access data for the creation of the message
22 | * @return the message if one was able to be created, or null otherwise.
23 | * @throws Exception
24 | * if the data in the queue is sufficient to construct a message, but the message data is invalid, this
25 | * method must throw an exception, or it will keep getting the same data.
26 | */
27 | IncomingMessage parseMessage(ByteQueue queue) throws Exception;
28 | }
29 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/MessagingExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | public interface MessagingExceptionHandler {
4 | public void receivedException(Exception e);
5 | }
6 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/OutgoingMessage.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | public interface OutgoingMessage {
4 | /**
5 | * Return the byte array representing the serialization of the request.
6 | *
7 | * @return byte array representing the serialization of the request
8 | */
9 | byte[] getMessageData();
10 | }
11 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/OutgoingRequestMessage.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 |
4 | public interface OutgoingRequestMessage extends OutgoingMessage {
5 | /**
6 | * Whether the request is expecting a response or not.
7 | *
8 | * @return true if a response is expected, false otherwise.
9 | */
10 | boolean expectsResponse();
11 | }
12 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/OutgoingResponseMessage.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 |
4 | public interface OutgoingResponseMessage extends OutgoingMessage {
5 | // A marker interface.
6 | }
7 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/RequestHandler.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 |
4 | public interface RequestHandler {
5 | /**
6 | * Handle the request and return the appropriate response object.
7 | *
8 | * @param request
9 | * the request to handle
10 | * @return the response object or null if no response is to be sent. null may also be returned if the request is
11 | * handled asynchronously.
12 | * @throws Exception
13 | */
14 | OutgoingResponseMessage handleRequest(IncomingRequestMessage request) throws Exception;
15 | }
16 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/StreamTransport.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.io.OutputStream;
6 |
7 | /**
8 | * First, instatiate with the streams. Then add a data consumer, or create a message control and pass this as the
9 | * transport (which will make the message control the data consumer). Change the read delay if desired. This class
10 | * supports running in its own thread (start) or an external one (run), say from a thread pool. Both approaches are
11 | * delegated to the stream listener. In either case, stop the transport with the stop method (or just stop the message
12 | * control).
13 | *
14 | * @author Matthew Lohbihler
15 | */
16 | public class StreamTransport implements Transport, Runnable {
17 | protected OutputStream out;
18 | protected InputStream in;
19 | private InputStreamListener listener;
20 |
21 | public StreamTransport(InputStream in, OutputStream out) {
22 | this.out = out;
23 | this.in = in;
24 | }
25 |
26 | public void setReadDelay(int readDelay) {
27 | if (listener != null)
28 | listener.setReadDelay(readDelay);
29 | }
30 |
31 | public void start(String threadName) {
32 | listener.start(threadName);
33 | }
34 |
35 | public void stop() {
36 | listener.stop();
37 | }
38 |
39 | public void run() {
40 | listener.run();
41 | }
42 |
43 | public void setConsumer(DataConsumer consumer) {
44 | listener = new InputStreamListener(in, consumer);
45 | }
46 |
47 | public void removeConsumer() {
48 | listener.stop();
49 | listener = null;
50 | }
51 |
52 | public void write(byte[] data) throws IOException {
53 | out.write(data);
54 | out.flush();
55 | }
56 |
57 | public void write(byte[] data, int len) throws IOException {
58 | out.write(data, 0, len);
59 | out.flush();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/StreamTransportCharSpaced.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2014 Infinite Automation Software. All rights reserved.
3 | * @author Terry Packer
4 | */
5 | package com.zgkxzx.modbus4And.sero.messaging;
6 |
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.io.OutputStream;
10 |
11 | /**
12 | * @author Terry Packer
13 | *
14 | */
15 | public class StreamTransportCharSpaced extends StreamTransport{
16 |
17 | private final long charSpacing;
18 |
19 | /**
20 | * @param in
21 | * @param out
22 | */
23 | public StreamTransportCharSpaced(InputStream in, OutputStream out, long charSpacing) {
24 | super(in, out);
25 | this.charSpacing = charSpacing;
26 | }
27 |
28 | /**
29 | * Perform a write, ensure space between chars
30 | */
31 | @Override
32 | public void write(byte[] data) throws IOException {
33 |
34 | try{
35 | long waited = 0,writeStart,writeEnd, waitRemaining;
36 | for(byte b : data){
37 | writeStart = System.nanoTime();
38 | out.write(b);
39 | writeEnd = System.nanoTime();
40 | waited = writeEnd - writeStart;
41 | if(waited < this.charSpacing){
42 | waitRemaining = this.charSpacing - waited;
43 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000));
44 | }
45 |
46 | }
47 | }catch(Exception e){
48 | throw new IOException(e);
49 | }
50 | out.flush();
51 | }
52 |
53 | public void write(byte[] data, int len) throws IOException {
54 | try{
55 | long waited = 0,writeStart,writeEnd, waitRemaining;
56 | for(int i=0; i< len; i++){
57 | writeStart = System.nanoTime();
58 | out.write(data[i]);
59 | writeEnd = System.nanoTime();
60 | waited = writeEnd - writeStart;
61 | if(waited < this.charSpacing){
62 | waitRemaining = this.charSpacing - waited;
63 | Thread.sleep(waitRemaining / 1000000, (int)(waitRemaining % 1000000));
64 | }
65 |
66 | }
67 | }catch(Exception e){
68 | throw new IOException(e);
69 | }
70 | out.flush();
71 | }
72 |
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/TestableTransport.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import java.io.BufferedInputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.io.OutputStream;
7 |
8 | /**
9 | * Provides synchronization on the input stream read by wrapping it.
10 | *
11 | * @author Matthew Lohbihler
12 | */
13 | public class TestableTransport extends StreamTransport {
14 | public TestableTransport(InputStream in, OutputStream out) {
15 | super(new TestableBufferedInputStream(in), out);
16 | }
17 |
18 | public void testInputStream() throws IOException {
19 | ((TestableBufferedInputStream) in).test();
20 | }
21 |
22 | static class TestableBufferedInputStream extends BufferedInputStream {
23 | public TestableBufferedInputStream(InputStream in) {
24 | super(in);
25 | }
26 |
27 | @Override
28 | public synchronized int read(byte[] buf) throws IOException {
29 | return super.read(buf);
30 | }
31 |
32 | public synchronized void test() throws IOException {
33 | mark(1);
34 | int i = read();
35 | if (i == -1)
36 | throw new IOException("Stream closed");
37 | reset();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/TimeoutException.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import java.io.IOException;
4 |
5 | public class TimeoutException extends IOException {
6 | private static final long serialVersionUID = 1L;
7 |
8 | public TimeoutException(String message) {
9 | super(message);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/Transport.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import java.io.IOException;
4 |
5 | /**
6 | * A transport is a wrapper around the means by which data is transferred. So, there could be transports for serial
7 | * ports, sockets, UDP, email, etc.
8 | *
9 | * @author Matthew Lohbihler
10 | */
11 | public interface Transport {
12 | abstract void setConsumer(DataConsumer consumer) throws IOException;
13 |
14 | abstract void removeConsumer();
15 |
16 | abstract void write(byte[] data) throws IOException;
17 |
18 | abstract void write(byte[] data, int len) throws IOException;
19 | }
20 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/WaitingRoomException.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | import java.io.IOException;
4 |
5 | public class WaitingRoomException extends IOException {
6 | private static final long serialVersionUID = 1L;
7 |
8 | public WaitingRoomException(String message) {
9 | super(message);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/WaitingRoomKey.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 | /**
4 | * Waiting room keys are used to match requests with responses. Implementation need to have hashcode and equals
5 | * definitions.
6 | *
7 | * @author Matthew Lohbihler
8 | */
9 | public interface WaitingRoomKey {
10 | // Implementation needs to have hashcode and equals implementations.
11 | }
12 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/messaging/WaitingRoomKeyFactory.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.messaging;
2 |
3 |
4 | public interface WaitingRoomKeyFactory {
5 | WaitingRoomKey createWaitingRoomKey(OutgoingRequestMessage request);
6 |
7 | WaitingRoomKey createWaitingRoomKey(IncomingResponseMessage response);
8 | }
9 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/timer/SystemTimeSource.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.timer;
2 |
3 | /**
4 | * An implementation of TimeSource that returns the host time via System.
5 | *
6 | * @author Matthew Lohbihler
7 | */
8 | public class SystemTimeSource implements TimeSource {
9 | public long currentTimeMillis() {
10 | return System.currentTimeMillis();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/timer/TimeSource.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.timer;
2 |
3 | /**
4 | * An interface to abstract the source of current time away from System. This allows code to run in simulations where
5 | * the time is controlled explicitly.
6 | *
7 | * @author Matthew Lohbihler
8 | */
9 | public interface TimeSource {
10 | long currentTimeMillis();
11 | }
12 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/util/ProgressiveTask.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4And.sero.util;
2 |
3 |
4 | /**
5 | * @author Matthew Lohbihler
6 | */
7 | abstract public class ProgressiveTask implements Runnable {
8 | private boolean cancelled = false;
9 | protected boolean completed = false;
10 | private ProgressiveTaskListener listener;
11 |
12 | public ProgressiveTask() {
13 | // no op
14 | }
15 |
16 | public ProgressiveTask(ProgressiveTaskListener l) {
17 | listener = l;
18 | }
19 |
20 | public void cancel() {
21 | cancelled = true;
22 | }
23 |
24 | public boolean isCancelled() {
25 | return cancelled;
26 | }
27 |
28 | public boolean isCompleted() {
29 | return completed;
30 | }
31 |
32 | public final void run() {
33 | while (true) {
34 | if (isCancelled()) {
35 | declareFinished(true);
36 | break;
37 | }
38 |
39 | runImpl();
40 |
41 | if (isCompleted()) {
42 | declareFinished(false);
43 | break;
44 | }
45 | }
46 | completed = true;
47 | }
48 |
49 | protected void declareProgress(float progress) {
50 | ProgressiveTaskListener l = listener;
51 | if (l != null)
52 | l.progressUpdate(progress);
53 | }
54 |
55 | private void declareFinished(boolean cancelled) {
56 | ProgressiveTaskListener l = listener;
57 | if (l != null) {
58 | if (cancelled)
59 | l.taskCancelled();
60 | else
61 | l.taskCompleted();
62 | }
63 | }
64 |
65 | /**
66 | * Implementers of this method MUST return from it occasionally so that the cancelled status can be checked. Each
67 | * return must leave the class and thread state with the expectation that runImpl will not be called again, while
68 | * acknowledging the possibility that it will.
69 | *
70 | * Implementations SHOULD call the declareProgress method with each runImpl execution such that the listener can be
71 | * notified.
72 | *
73 | * Implementations MUST set the completed field to true when the task is finished.
74 | */
75 | abstract protected void runImpl();
76 | }
77 |
--------------------------------------------------------------------------------
/modbus4android/src/main/java/com/zgkxzx/modbus4And/sero/util/ProgressiveTaskListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2006-2007 Serotonin Software Technologies Inc.
3 |
4 | This program is free software; you can redistribute it and/or modify
5 | it under the terms of version 2 of the GNU General Public License as
6 | published by the Free Software Foundation and additional terms as
7 | specified by Serotonin Software Technologies Inc.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software Foundation, Inc.,
16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 |
18 | @author Matthew Lohbihler
19 | */
20 |
21 | package com.zgkxzx.modbus4And.sero.util;
22 |
23 | /**
24 | * @author Matthew Lohbihler
25 | *
26 | */
27 | public interface ProgressiveTaskListener {
28 | /**
29 | * Optionally called occasionally by the task to declare the progress that has been made.
30 | * @param progress float between 0 and 1 where 0 is no progress and 1 is completed.
31 | */
32 | void progressUpdate(float progress);
33 |
34 | /**
35 | * Notification that the task has been cancelled. Should only be called once for the task.
36 | */
37 | void taskCancelled();
38 |
39 | /**
40 | * Notification that the task has been completed. Should only be called once for the task.
41 | */
42 | void taskCompleted();
43 | }
44 |
--------------------------------------------------------------------------------
/modbus4android/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Modbus4Android
3 |
4 |
--------------------------------------------------------------------------------
/modbus4android/src/test/java/com/zgkxzx/modbus4android/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.zgkxzx.modbus4android;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/screenshot/read_coil.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/screenshot/read_coil.png
--------------------------------------------------------------------------------
/screenshot/read_discrete_input.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/screenshot/read_discrete_input.png
--------------------------------------------------------------------------------
/screenshot/read_holding_register.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/screenshot/read_holding_register.png
--------------------------------------------------------------------------------
/screenshot/read_input_registers.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/screenshot/read_input_registers.png
--------------------------------------------------------------------------------
/screenshot/write_coil.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/screenshot/write_coil.png
--------------------------------------------------------------------------------
/screenshot/write_register.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/screenshot/write_register.png
--------------------------------------------------------------------------------
/screenshot/write_registers.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zgkxzx/Modbus4Android/61dbbc60cac2f09ed4b30b4b73622b9fd38260c0/screenshot/write_registers.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':modbus4android'
2 |
--------------------------------------------------------------------------------