Skip to content

Commit

Permalink
Merge pull request #1 from retrodaredevil/tcp-refactor
Browse files Browse the repository at this point in the history
Tcp refactor and Gradle
  • Loading branch information
retrodaredevil committed May 18, 2020
2 parents 788b9d5 + 9d91b6e commit 8cbb971
Show file tree
Hide file tree
Showing 61 changed files with 1,361 additions and 263 deletions.
264 changes: 264 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,167 @@
# Custom stuff:
*.attach_pid*


# Edit at https://www.gitignore.io/?templates=java,linux,macos,gradle,windows,eclipse,intellij,netbeans

### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

### Eclipse Patch ###
# Eclipse Core
.project

# JDT-specific (Eclipse Java Development Tools)
.classpath

# Annotation Processing
.apt_generated

.sts4-cache/

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# 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

# 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/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

# 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
.idea/**/sonarlint/

# SonarQube Plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator/

### Java ###
# Compiled class file
*.class

Expand All @@ -14,14 +175,117 @@
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### NetBeans ###
**/nbproject/private/
**/nbproject/Makefile-*.mk
**/nbproject/Package-*.bash
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

### Gradle ###
.gradle

# Ignore Gradle GUI config
gradle-app.setting

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Cache of project
.gradletasknamecache

# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties

### Gradle Patch ###
**/build/

# End of https://www.gitignore.io/api/java,linux,macos,gradle,windows,eclipse,intellij,netbeans

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
/target/
/bin/

Expand Down
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ This library aims to provide an open source heavily object oriented approach to
## Features
* Create custom function codes by implementing `MessageHandler`.
* Total freedom to extend any class and override its behavior.
* Supports Ascii and RTU encoding. Also create your own custom encoding by implementing `IODataEncoder`.
* Supports Ascii, RTU, and TCP encoding. Also create your own custom encoding by implementing `IODataEncoder`.
* Supports CRC and LRC checksums. Automatically checks CRC while using RTU and LRC while using Ascii.
* Parse ModbusMessages (allows you to easily respond to a master).
* Modbus logic is not coupled to serial logic.
* Uses common interfaces. This makes it easy to swap out implementations. Decide to switch from Ascii encoding to using
TCP? No problem.

## Drawbacks
* Not set up for asynchronous requests
* Not set up for multiple requests at once for TCP (you must request and wait for response)

## Importing
[![](https://jitpack.io/v/retrodaredevil/io-lib.svg)](https://jitpack.io/#retrodaredevil/io-lib)
Expand All @@ -25,7 +32,8 @@ allprojects {
}
dependencies {
implementation 'com.github.retrodaredevil:io-lib:<LATEST RELEASE>'
implementation 'com.github.retrodaredevil.io-lib:core:<LATEST RELEASE>'
implementation 'com.github.retrodaredevil.io-lib:jSerialComm:<LATEST RELEASE>'
}
```

Expand All @@ -47,18 +55,33 @@ IOBundle ioBundle = JSerialIOBundle.createPort("/dev/ttyUSB0", serialConfig);
Initializing modbus:
```java
IOBundle ioBundle = ...;
ModbusSlaveBus modbus = new IOModbusSlaveBus(ioBundle, new RTUDataEncoder(2000, 20, 4)); // 2 second initial timeout, 20ms timeout for end of message, 4ms sleep
ModbusSlaveBus modbus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder(2000, 20, 4)); // 2 second initial timeout, 20ms timeout for end of message, 4ms sleep
ModbusSlave slave = new ImmutableAddressModbusSlave(options.getModbusAddress(), modbus);
```
Using modbus:
```java
private static final MessageHandler<int[]> BATTERY_VOLTAGE = new ReadRegistersHandler(0x0101, 1);
private static final MessageHandler<int[]> BATTERY_VOLTAGE = new ReadRegisters(0x0101, 1);
private final ModbusSlave slave = ...;
public float getBatteryVoltage() {
return slave.sendRequestMessage(BATTERY_VOLTAGE)[0] / 10.0F;
}
```

## Exceptions
There are many places in this library where checked exceptions are thrown. Such as `MessageParseException`s, `SerialPortException`s.
However, you should also be aware of `ModbusRuntimeException`s. These can pop up in just about any place that deals with Modbus.
These are runtime exceptions for convenience. You likely aren't able to deal with them when they first
pop up, so you usually handle them later up the call stack.

## Using Asynchronously
This library doesn't deal with threads at all. Everything is set up to be synchronous. However, if you want to use this
asynchronously, you can set up your own way of executing a request asynchronously. This library won't fight you.
Since almost everything in this library is immutable you usually don't have to worry about putting locks on
objects because they don't have mutable state.

If you do use this asynchronously, remember that you cannot make two requests to two different devices because they
may come back out of order, which the library does not support.

## 8 Bit and 16 Bit
Since this project deals with Modbus, there are times when code is dealing with 8 bit data or 16 bit data.
Sometimes it can be difficult to tell which one it is. If we wanted code to be most readable, we would make `byte` represent
Expand All @@ -69,8 +92,14 @@ rarely see `short` being used.
* `short` is always 16 bit data
* `int` could be either 8 bit or 16 bit

## Testing this library
If you want to test this library, you can use https://www.modbusdriver.com/diagslave.html

* Testing TCP: `sudo ./diagslave -m tcp -a 0 -p 502` (address of 0 is valid for TCP)

## Projects using this
* [SolarThing](https://github.com/wildmountainfarms/solarthing)

## TODO
* Implement Modbus exception codes and throw Java Exceptions corresponding to them
* Support two byte slave addressing
Loading

0 comments on commit 8cbb971

Please sign in to comment.