Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Modbusino] - simple modbus rtu library #2043

Merged
merged 36 commits into from
May 8, 2020
Merged
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
94735bb
Add modbusino library + patch + sample
kmihaylov Feb 1, 2020
2f762e7
Merge branch 'develop' of https://github.com/SmingHub/Sming into feat…
kmihaylov Feb 1, 2020
b1b5323
Fix modbusino's component.mk component variables assignation, += inst…
kmihaylov Feb 1, 2020
2be888f
Add delays when sending a message.
kmihaylov Feb 1, 2020
00e5b36
Changes to generic sample. Proper initialization (not yet done) of RE…
kmihaylov Feb 1, 2020
a216f2e
Make the MB slave address a config variable
kmihaylov Feb 5, 2020
d83ba81
New patch file introducing configurable pre- and post-transmission de…
kmihaylov Feb 5, 2020
d12aabe
Modify example and component.mk to support MB_SLAVE_ADDR
kmihaylov Feb 5, 2020
64a28ce
Add README file for the component
kmihaylov Feb 5, 2020
e00c137
URL to the original repository is added
kmihaylov Feb 5, 2020
44c5a9c
Fix a typo
kmihaylov Feb 5, 2020
a6c4363
Update sample's README
kmihaylov Feb 5, 2020
f97a89f
Requested changes
kmihaylov Feb 6, 2020
abe7d5d
Requested changes
kmihaylov Feb 6, 2020
947caa7
Minor documentation tweaks. Keep consistent style.
mikee47 Feb 6, 2020
ccf0e80
Change modbusino origin url
kmihaylov Feb 6, 2020
932f622
Remote modbusino.patch. Sming dedicated fork is used
kmihaylov Feb 6, 2020
c157b74
modbusino updates
kmihaylov Feb 7, 2020
d141a7c
use modbusino.setup() for serial initialization
kmihaylov Feb 7, 2020
b89f799
modbusino updates
kmihaylov Feb 7, 2020
4a47fdf
Towards event driven modbusino
kmihaylov Feb 7, 2020
14b3cdf
Update Modbusino fork
kmihaylov Feb 8, 2020
d673721
Remove timers. Use RX calback for modbus
kmihaylov Feb 9, 2020
241f145
Update modbusino fork. Use of callbacks
kmihaylov Feb 9, 2020
e859bcd
Update modbusino fork. Do not use rxCallback when master only reads r…
kmihaylov Feb 9, 2020
fb9fa8b
Update modbusino library
kmihaylov Apr 14, 2020
a573d54
Merge branch 'develop' of https://github.com/SmingHub/Sming into feat…
kmihaylov Apr 14, 2020
aa5fbcb
Update modbusino library
kmihaylov Apr 14, 2020
3720d43
Fix coding style, add .cs
kmihaylov Apr 15, 2020
d9f3e54
make cs
kmihaylov Apr 15, 2020
30900f6
Update README.rst, loop not used, remove of PRE and POST delay variables
kmihaylov Apr 23, 2020
e43f2ff
Update samle's README
kmihaylov Apr 23, 2020
fc70591
Update the global modbusino README and component.mk
kmihaylov Apr 23, 2020
1644e10
Update README introducing some information about the observed RE line…
kmihaylov May 7, 2020
6676735
Update submodule hash
kmihaylov May 7, 2020
63f6512
Fix conflict on .gitmodules, update branch
kmihaylov May 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -138,3 +138,7 @@
path = Sming/Libraries/ModbusMaster/ModbusMaster
url = https://github.com/nomis/ModbusMaster.git
ignore = dirty
[submodule "Libraries.modbusino"]
path = Sming/Libraries/modbusino/modbusino
url = https://github.com/stephane/modbusino.git
ignore = dirty
27 changes: 27 additions & 0 deletions Sming/Libraries/modbusino/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
modbusino RTU Library (modbus slave)
====================================

The author's project is at https://github.com/stephane/modbusino
kmihaylov marked this conversation as resolved.
Show resolved Hide resolved

modbusino is lightweight RTU modbus slave library that supports 'read holding registers' and 'write multiple registers' functions.

Four environment variables deifne the RS485 interface hardware pins and pre- and post-transmission delay.
kmihaylov marked this conversation as resolved.
Show resolved Hide resolved

.. envvar:: RS485_RE_PIN

:envvar:`RS485_RE_PIN` defines the RE GPIO pin number.


.. envvar:: RS485_TX_LEVEL

:envvar:`RS485_TX_LEVEL` defines the pin level (LOW or HIGH) when transmitting.


.. envvar:: MB_PRE_TX_DELAY

With :envvar:`MB_PRE_TX_DELAY` (in microseconds) the delay between changing the RE pin level and sending data can be set. Used to ensure proper line detection by the master device.


.. envvar:: MB_POST_TX_DELAY

:envvar:`MB_POST_TX_DELAY` is the time in microseconds between the end of data transmission and releasing the line.
kmihaylov marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions Sming/Libraries/modbusino/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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)

COMPONENT_VARS += MB_PRE_TX_DELAY
MB_PRE_TX_DELAY ?= 100
COMPONENT_CXXFLAGS += -DMB_PRE_TX_DELAY=$(MB_PRE_TX_DELAY)

COMPONENT_VARS += MB_POST_TX_DELAY
MB_POST_TX_DELAY ?= 1000
COMPONENT_CXXFLAGS += -DMB_POST_TX_DELAY=$(MB_POST_TX_DELAY)
kmihaylov marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions Sming/Libraries/modbusino/modbusino
Submodule modbusino added at b907a4
58 changes: 58 additions & 0 deletions Sming/Libraries/modbusino/modbusino.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
diff --git a/Modbusino.cpp b/Modbusino.cpp
index 3ba3c14..9e6474b 100644
--- a/Modbusino.cpp
+++ b/Modbusino.cpp
@@ -100,7 +100,11 @@ static void send_msg(uint8_t *msg, uint8_t msg_length)
msg[msg_length++] = crc >> 8;
msg[msg_length++] = crc & 0x00FF;

+ digitalWrite(RS485_RE_PIN, RS485_TX_LEVEL);
+ delayMicroseconds(MB_PRE_TX_DELAY);
Serial.write(msg, msg_length);
+ delayMicroseconds(MB_POST_TX_DELAY);
+ digitalWrite(RS485_RE_PIN, ! RS485_TX_LEVEL);
}

static uint8_t response_exception(uint8_t slave, uint8_t function,
@@ -123,7 +127,7 @@ static void flush(void)
/* Wait a moment to receive the remaining garbage but avoid getting stuck
* because the line is saturated */
while (Serial.available() && i++ < 10) {
- Serial.flush();
+ Serial.clear();
delay(3);
}
}
@@ -134,7 +138,7 @@ static int receive(uint8_t *req, uint8_t _slave)
uint8_t length_to_read;
uint8_t req_index;
uint8_t step;
- uint8_t function;
+ uint8_t function=0;

/* We need to analyse the message step by step. At the first step, we want
* to reach the function code because all packets contain this
@@ -187,11 +191,6 @@ static int receive(uint8_t *req, uint8_t _slave)
flush();
if (req[_MODBUS_RTU_SLAVE] == _slave
|| req[_MODBUS_RTU_SLAVE] == MODBUS_BROADCAST_ADDRESS) {
- /* It's for me so send an exception (reuse req) */
- uint8_t rsp_length = response_exception(
- _slave, function, MODBUS_EXCEPTION_ILLEGAL_FUNCTION,
- req);
- send_msg(req, rsp_length);
return -1 - MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
}

@@ -210,11 +209,6 @@ static int receive(uint8_t *req, uint8_t _slave)
flush();
if (req[_MODBUS_RTU_SLAVE] == _slave
|| req[_MODBUS_RTU_SLAVE] == MODBUS_BROADCAST_ADDRESS) {
- /* It's for me so send an exception (reuse req) */
- uint8_t rsp_length = response_exception(
- _slave, function,
- MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, req);
- send_msg(req, rsp_length);
return -1 - MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
}
return -1;
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
7 changes: 7 additions & 0 deletions Sming/Libraries/modbusino/samples/generic/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Modbusino RTU generic sample
============================

kmihaylov marked this conversation as resolved.
Show resolved Hide resolved
The sample code provides three element uint16_t array used for the modbus slave registers. These values are printed each second through UART1.
mbSlaveLoopTimer checks for new data each 10 milliseconds (Only checks for new data. Serial communication is handled by hardware.)
kmihaylov marked this conversation as resolved.
Show resolved Hide resolved

Several environment variables (envvar:`RS485_RE_PIN` :envvar:`RS485_TX_LEVEL` :envvar:`MB_PRE_TX_DELAY` :envvar:`MB_POST_TX_DELAY`) can be used for configuration.
33 changes: 33 additions & 0 deletions Sming/Libraries/modbusino/samples/generic/app/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <SmingCore.h>
slaff marked this conversation as resolved.
Show resolved Hide resolved
#include <Debug.h>
#include <Modbusino.h>

Timer mbSlaveLoopTimer;
Timer mbPrintTimer;
ModbusinoSlave MbSlave(MB_SLAVE_ADDR);
HardwareSerial debugComPort(UART1);

uint16_t tab_reg[3] = {0,0,0};

void mbSlaveLoop() {
uint8_t res = MbSlave.loop(tab_reg,3);
if( res < 0 ) debugf("error: %d\r\n");
}

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

void init()
{
pinMode(15, OUTPUT);
Serial.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_FULL);
debugComPort.begin(SERIAL_BAUD_RATE, SERIAL_8N1,
SERIAL_TX_ONLY);
debugComPort.systemDebugOutput(true);
Debug.setDebug(debugComPort);
Debug.start();

mbSlaveLoopTimer.initializeMs(10,mbSlaveLoop).start();
mbPrintTimer.initializeMs(1000,mbPrint).start();
}
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)