diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60c7d78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,138 @@ +.gradle +**/build/ +!src/**/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Avoid ignore Gradle wrappper properties +!gradle-wrapper.properties + +# Cache of project +.gradletasknamecache + +# Eclipse Gradle plugin generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath +# Created by https://www.toptal.com/developers/gitignore/api/intellij +# Edit at https://www.toptal.com/developers/gitignore?templates=intellij + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +# Azure Toolkit for IntelliJ plugin +# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij +.idea/**/azureSettings.xml + +# End of https://www.toptal.com/developers/gitignore/api/intellij diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..55157ea --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +modbus_master_rtu \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..9a8b7e5 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7afafdb --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..75ee111 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Modbus_RTU_Master/ModbusRTUMaster.java b/Modbus_RTU_Master/ModbusRTUMaster.java deleted file mode 100644 index b4e1c57..0000000 --- a/Modbus_RTU_Master/ModbusRTUMaster.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.example; - -import com.ghgande.j2mod.modbus.ModbusException; -import com.ghgande.j2mod.modbus.facade.ModbusSerialMaster; -import com.ghgande.j2mod.modbus.procimg.InputRegister; -import com.ghgande.j2mod.modbus.procimg.Register; -import com.ghgande.j2mod.modbus.procimg.SimpleRegister; -import com.ghgande.j2mod.modbus.util.SerialParameters; - -public class ModbusRTUMaster { - - public static void main(String[] args) { - // Установка параметров порта - SerialParameters parameters = new SerialParameters(); - parameters.setPortName("COM3"); // Имя порта - parameters.setBaudRate(9600); // Скорость передачи - parameters.setDatabits(8); // Биты данных, в нашем случае передается по 8 битов - parameters.setParity("None"); // Без бита четности - parameters.setStopbits(1); // Стоповый бит 1 - parameters.setEncoding("rtu"); // Кодирование RTU - parameters.setEcho(false); // Лок. эхо выкл - - // Создание экземпляра ModbusSerialMaster - ModbusSerialMaster modbusMaster = new ModbusSerialMaster(parameters); - - try { - modbusMaster.connect(); // Подключение к устройству - - int slaveId = 1; // ID устройства slave - int registerAddress = 0; // Адрес регистра - - // Чтение значения из регистра - InputRegister[] registers = modbusMaster.readInputRegisters(slaveId, registerAddress, 1); - int value = registers[0].toUnsignedShort(); - System.out.println("Значение регистра: " + value); - - // Запись значения в регистр - int newValue = 123; - SimpleRegister register = new SimpleRegister(newValue); - modbusMaster.writeSingleRegister(slaveId, registerAddress, register); - System.out.println("Значение успешно записано в регистр."); - - modbusMaster.disconnect(); // Отключение от устройства - }catch (ModbusException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } -} - - - diff --git a/Modbus_RTU_Master/pom.xml b/Modbus_RTU_Master/pom.xml deleted file mode 100644 index b305470..0000000 --- a/Modbus_RTU_Master/pom.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - 4.0.0 - - org.example - ModbusV3 - 1.0-SNAPSHOT - - - 20 - 20 - UTF-8 - - - - - com.ghgande - j2mod - 3.1.1 - - - - org.slf4j - slf4j-api - 1.7.5 - - - org.slf4j - slf4j-simple - 1.6.4 - - - - net.wimpi - jamod - 1.2 - - - - com.intelligt.modbus - jlibmodbus - 1.2.9.7 - - - - - \ No newline at end of file diff --git a/Modbus_RTU_Master_jlibmodbus/jssc-2.9.4.jar b/Modbus_RTU_Master_jlibmodbus/jssc-2.9.4.jar deleted file mode 100644 index a754a20..0000000 Binary files a/Modbus_RTU_Master_jlibmodbus/jssc-2.9.4.jar and /dev/null differ diff --git a/Modbus_RTU_Master_jlibmodbus/pom.xml b/Modbus_RTU_Master_jlibmodbus/pom.xml deleted file mode 100644 index 9dbe8b6..0000000 --- a/Modbus_RTU_Master_jlibmodbus/pom.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - 4.0.0 - - org.example - ModbusMasterJlibModbus - 1.0-SNAPSHOT - - - 20 - 20 - UTF-8 - - - - - com.intelligt.modbus - jlibmodbus - 1.2.9.7 - - - - org.slf4j - slf4j-api - 2.0.0-alpha0 - - - - \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..072e5e5 --- /dev/null +++ b/build.gradle @@ -0,0 +1,12 @@ +apply plugin: 'java' + +sourceCompatibility = 1.5 +version = '1.0' + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..3d0dee6 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..b50e094 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Jun 16 19:50:49 MSK 2023 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..8a0b282 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/mbrtu/build.gradle b/mbrtu/build.gradle new file mode 100644 index 0000000..12be60b --- /dev/null +++ b/mbrtu/build.gradle @@ -0,0 +1,14 @@ +apply plugin: 'java' + +sourceCompatibility = 1.5 +version = '1.0' + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' + compile group: 'com.github.kochedykov', name: 'jlibmodbus', version: '1.2.9.0' + +} \ No newline at end of file diff --git a/mbrtu/mbrtu.iml b/mbrtu/mbrtu.iml new file mode 100644 index 0000000..f28f4c5 --- /dev/null +++ b/mbrtu/mbrtu.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mbrtu/src/main/java/MainSlave.java b/mbrtu/src/main/java/MainSlave.java new file mode 100644 index 0000000..88c8ff0 --- /dev/null +++ b/mbrtu/src/main/java/MainSlave.java @@ -0,0 +1,69 @@ +import com.intelligt.modbus.jlibmodbus.Modbus; +import com.intelligt.modbus.jlibmodbus.ModbusSlave; +import com.intelligt.modbus.jlibmodbus.ModbusSlaveFactory; +import com.intelligt.modbus.jlibmodbus.data.DataHolder; +import com.intelligt.modbus.jlibmodbus.data.ModbusHoldingRegisters; +import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException; +import com.intelligt.modbus.jlibmodbus.serial.*; +import jssc.SerialPortList; + +import java.io.Console; + +public class MainSlave { + + static public void main(String[] arg) { + SerialParameters sp = new SerialParameters(); + Modbus.setLogLevel(Modbus.LogLevel.LEVEL_DEBUG); + + try { + String[] dev_list = SerialPortList.getPortNames(); + System.out.println("Доступные порты:"); + + for (int n = 0; n < dev_list.length; n++) { + System.out.println(dev_list[n]); + } + + if (dev_list.length > 0) { + sp.setDevice(arg[arg.length - 1]); + sp.setBaudRate(SerialPort.BaudRate.BAUD_RATE_9600); + sp.setDataBits(8); + sp.setParity(SerialPort.Parity.NONE); + sp.setStopBits(1); + + + SerialUtils.setSerialPortFactory(new SerialPortFactoryJSSC()); + ModbusSlave m = ModbusSlaveFactory.createModbusSlaveRTU(sp); + + DataHolder dataHolder = new DataHolder(); + ModbusHoldingRegisters registers = new ModbusHoldingRegisters(10); + registers.set(0, 101); + registers.set(1, 105); + registers.set(2, 660); + + dataHolder.setHoldingRegisters(registers); + m.setDataHolder(dataHolder); + + try { + m.listen(); + int symbol = 0; + while (symbol >= 0) { + symbol = System.in.read(); + if (symbol != 10) + registers.set(0, symbol); + } + + } catch (ModbusIOException e) { + e.printStackTrace(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + e.printStackTrace(); + } + } + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/Modbus_RTU_Master_jlibmodbus/MasterRTU.java b/mbrtu/src/main/java/main.java similarity index 76% rename from Modbus_RTU_Master_jlibmodbus/MasterRTU.java rename to mbrtu/src/main/java/main.java index c572398..b6dd12f 100644 --- a/Modbus_RTU_Master_jlibmodbus/MasterRTU.java +++ b/mbrtu/src/main/java/main.java @@ -1,13 +1,14 @@ -package org.example; - import com.intelligt.modbus.jlibmodbus.Modbus; -import com.intelligt.modbus.jlibmodbus.master.ModbusMaster; -import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory; +import com.intelligt.modbus.jlibmodbus.ModbusMaster; +import com.intelligt.modbus.jlibmodbus.ModbusMasterFactory; import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException; import com.intelligt.modbus.jlibmodbus.serial.*; import jssc.SerialPortList; -public class MasterRTU { +import java.util.logging.Logger; + + +public class main { static public void main(String[] arg) { SerialParameters sp = new SerialParameters(); @@ -17,6 +18,7 @@ public class MasterRTU { //Вывод доступных портов String[] dev_list = SerialPortList.getPortNames(); + System.out.println("Доступные порты:"); for (int n = 0; n < dev_list.length; n++) { @@ -25,7 +27,7 @@ public class MasterRTU { if (dev_list.length > 0) { //Настройка - sp.setDevice(dev_list[1]); + sp.setDevice(arg[arg.length - 1]); sp.setBaudRate(SerialPort.BaudRate.BAUD_RATE_9600); sp.setDataBits(8); sp.setParity(SerialPort.Parity.NONE); @@ -34,11 +36,12 @@ public class MasterRTU { //Создание и подключение через jssc SerialUtils.setSerialPortFactory(new SerialPortFactoryJSSC()); ModbusMaster m = ModbusMasterFactory.createModbusMasterRTU(sp); + m.connect(); int slaveId = 1; // id устройства slave int offset = 0; // Начальный адресс - int quantity = 10; //Количество условных портов + int quantity = 3; //Количество условных портов try { int[] registerValues = m.readHoldingRegisters(slaveId, offset, quantity); // Настройка тип регситра(в данном случае установлены регистры хранения) @@ -46,23 +49,17 @@ public class MasterRTU { //Вывод for (int value : registerValues) { System.out.println("Адресс: " + offset + ", Значение: " + value); - - if (offset == 0 && value == 101) { - System.out.println("Hello"); - } - offset++; } - - } catch (RuntimeException e) { - throw e; + } catch (ModbusIOException mioE) { + Modbus.log().throwing("???", "???", mioE); } catch (Exception e) { - e.printStackTrace(); + Modbus.log().throwing("???", "???", e); } finally { try { m.disconnect(); - } catch (ModbusIOException e1) { - e1.printStackTrace(); + } catch (ModbusIOException mioE) { + Modbus.log().throwing("???", "???", mioE); } } } @@ -72,4 +69,4 @@ public class MasterRTU { e.printStackTrace(); } } -} \ No newline at end of file +} diff --git a/modbus_master-slave_rtu.iml b/modbus_master-slave_rtu.iml new file mode 100644 index 0000000..6314f96 --- /dev/null +++ b/modbus_master-slave_rtu.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..e45742e --- /dev/null +++ b/settings.gradle @@ -0,0 +1,4 @@ +rootProject.name = 'modbus_master_rtu' + +include 'mbrtu' +