Skip to content

Commit

Permalink
[Modbusino] - simple modbus rtu library (#2043)
Browse files Browse the repository at this point in the history
* Add modbusino library + patch + sample + Readme.

Co-authored-by: mikee47 <[email protected]>
  • Loading branch information
kmihaylov and mikee47 authored May 8, 2020
1 parent c8630b3 commit 6ad6d86
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@
[submodule "Libraries.Arduino_TensorFlowLite"]
path = Sming/Libraries/Arduino_TensorFlowLite
url = https://github.com/slaff/Arduino_TensorFlowLite.git
[submodule "Libraries.modbusino"]
path = Sming/Libraries/modbusino/modbusino
url = https://github.com/kmihaylov/modbusino.git
ignore = dirty
26 changes: 26 additions & 0 deletions Sming/Libraries/modbusino/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
modbusino RTU Library (modbus slave)
====================================

modbusino is lightweight RTU `Modbus <https://en.wikipedia.org/wiki/Modbus>`__
slave library that supports 'read holding registers' and 'write multiple registers' functions.
Please note that prior to commit 02dff3c (branch Sming, port URL https://github.com/kmihaylov/modbusino)
a delay may occur after sending a message (more information can be found in the PR thread #2043,
https://github.com/SmingHub/Sming/pull/2043#issuecomment-615945823).


Configuration variables
-----------------------


.. envvar:: RS485_RE_PIN

Default: 15

GPIO pin number for RE (Receive-Enable) output.


.. envvar:: RS485_TX_LEVEL

Default: HIGH.

Active level for RE pin during transmission.
11 changes: 11 additions & 0 deletions Sming/Libraries/modbusino/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
COMPONENT_SUBMODULES := modbusino
COMPONENT_SRCDIRS := modbusino
COMPONENT_INCDIRS := modbusino

COMPONENT_VARS += RS485_RE_PIN
RS485_RE_PIN ?= 15
COMPONENT_CXXFLAGS += -DRS485_RE_PIN=$(RS485_RE_PIN)

COMPONENT_VARS += RS485_TX_LEVEL
RS485_TX_LEVEL ?= HIGH
COMPONENT_CXXFLAGS += -DRS485_TX_LEVEL=$(RS485_TX_LEVEL)
1 change: 1 addition & 0 deletions Sming/Libraries/modbusino/modbusino
Submodule modbusino added at 02dff3
9 changes: 9 additions & 0 deletions Sming/Libraries/modbusino/samples/generic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#####################################################################
#### Please don't change this file. Use component.mk instead ####
#####################################################################

ifndef SMING_HOME
$(error SMING_HOME is not set: please configure it as an environment variable)
endif

include $(SMING_HOME)/project.mk
16 changes: 16 additions & 0 deletions Sming/Libraries/modbusino/samples/generic/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Modbusino RTU generic sample
============================

.. highlight:: bash

The sample code provides three element uint16_t array used for the modbus slave registers. These values are printed each second through UART1.
mbPrint() is triggered on valid write multiple registers (0x10) command.

Several environment variables (envvar:`RS485_RE_PIN` :envvar:`RS485_TX_LEVEL`) can be used for configuration.
The slave address is defined with envvar:`MB_SLAVE_ADDR`. For example::

make MB_SLAVE_ADDR=2

These variables can be listed with::

make list-config
Empty file.
24 changes: 24 additions & 0 deletions Sming/Libraries/modbusino/samples/generic/app/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <SmingCore.h>
#include <Debug.h>
#include <Modbusino.h>

#define ARRLEN 3
uint16_t mbDataArray[ARRLEN] = {0, 0, 0};
ModbusinoSlave mbSlave(MB_SLAVE_ADDR, mbDataArray, ARRLEN);

HardwareSerial debugComPort(UART1);

void mbPrint()
{
debugf("Register values, #0: %d, #1: %d, #2: %d", mbDataArray[0], mbDataArray[1], mbDataArray[2]);
}

void init()
{
debugComPort.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_TX_ONLY);
debugComPort.systemDebugOutput(true);
Debug.setDebug(debugComPort);
Debug.start();
mbSlave.setup(SERIAL_BAUD_RATE);
mbSlave.setRxCallback(mbPrint);
}
6 changes: 6 additions & 0 deletions Sming/Libraries/modbusino/samples/generic/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARDUINO_LIBRARIES := modbusino
DISABLE_SPIFFS = 1

CONFIG_VARS += MB_SLAVE_ADDR
MB_SLAVE_ADDR ?= 1
APP_CFLAGS += -DMB_SLAVE_ADDR=$(MB_SLAVE_ADDR)

0 comments on commit 6ad6d86

Please sign in to comment.