Skip to content

Commit f4c29b2

Browse files
author
betzrhodes
authored
Merge pull request #4 from electricimp/develop
updated Modbus485 to ModbusSerial
2 parents af442cd + 377d8d9 commit f4c29b2

File tree

21 files changed

+206
-151
lines changed

21 files changed

+206
-151
lines changed

Diff for: .gitignore

-2
This file was deleted.

Diff for: Modbus485Master/.gitignore

-8
This file was deleted.

Diff for: ModbusMaster/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ This class is the abstract class for the following classes, it should NOT be ins
55
**Please proceed to one of the following libraries**
66

77

8-
# [Modbus485Master](../Modbus485Master/)
8+
# [ModbusSerialMaster](../ModbusSerialMaster/)
99

10-
This library allows an imp to communicate with other devices via the Modbus-RS485 protocol.
10+
This library allows an imp to communicate with other devices via Modbus-RS485 or Modbus-RS232 protocol.
1111

1212
# [ModbusTCPMaster](../ModbusTCPMaster/)
1313

Diff for: ModbusRTU/.gitignore

-7
This file was deleted.

Diff for: ModbusRTU/.imptest

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"modelId": "KB2yibqKCKCh" /* I */,
3+
"devices": [
4+
"5000d8c46a56cdae" /* I */
5+
],
6+
"agentFile": false,
7+
"deviceFile": "ModbusRTU.device.lib.nut",
8+
"stopOnFailure": false,
9+
"timeout": 10,
10+
"tests": [
11+
"*.test.nut",
12+
"tests/**/*.test.nut"
13+
]
14+
}

Diff for: ModbusRTU/lib/CRC16.class.nut

-57
This file was deleted.

Diff for: ModbusRTU/tests/device.test.nut

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2323
// OTHER DEALINGS IN THE SOFTWARE.
2424

25+
// -----------------------------------------------------------------------------
26+
27+
// This test can run on any Imp.
28+
29+
@include "github:electricimp/CRC16/CRC16.class.nut";
30+
2531
const MINIMUM_RESPONSE_LENGTH = 5;
2632
const DEVICE_ADDRESS = 0x01;
2733

28-
2934
function parse (fakeBuffer, params){
3035
local length = fakeBuffer.len();
3136
if (length < MINIMUM_RESPONSE_LENGTH){
@@ -40,7 +45,6 @@ function parse (fakeBuffer, params){
4045
return result;
4146
}
4247

43-
4448
class DeviceTestCase extends ImpTestCase {
4549

4650
function setUp() {

Diff for: ModbusSerialMaster/.imptest

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"modelId": "KB2yibqKCKCh" /* I */,
3+
"devices": [
4+
"5000d8c46a56cdae" /* I */
5+
],
6+
"agentFile": false,
7+
"deviceFile": false,
8+
"stopOnFailure": false,
9+
"timeout": 10,
10+
"tests": [
11+
"*.test.nut",
12+
"tests/**/*.test.nut"
13+
]
14+
}

Diff for: Modbus485Master/Modbus485Master.device.lib.nut renamed to ModbusSerialMaster/ModbusSerialMaster.device.lib.nut

+22-11
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2323
// OTHER DEALINGS IN THE SOFTWARE.
2424

25-
class Modbus485Master extends ModbusMaster {
26-
static VERSION = "1.0.1";
25+
class ModbusSerialMaster extends ModbusMaster {
26+
27+
static VERSION = "2.0.0";
2728
static MINIMUM_RESPONSE_LENGTH = 5;
29+
2830
_uart = null;
2931
_rts = null;
3032
_timeout = null;
@@ -50,7 +52,7 @@ class Modbus485Master extends ModbusMaster {
5052
// @item {float} timeout - 1.0 second by default
5153
// @item {bool} debug - false by default. If enabled, the outgoing and incoming ADU will be printed for debugging purpose
5254
//
53-
constructor(uart, rts, params = {}) {
55+
constructor(uart, rts = null, params = {}) {
5456
base.constructor(("debug" in params) ? params.debug : false);
5557
if (!("CRC16" in getroottable())) {
5658
throw "Must include CRC16 library v1.0.0+";
@@ -65,10 +67,14 @@ class Modbus485Master extends ModbusMaster {
6567
_timeout = ("timeout" in params) ? params.timeout : 1.0;
6668
_receiveBuffer = blob();
6769
_uart = uart;
68-
_rts = rts;
6970
_queue = [];
70-
_uart.configure(baudRate, dataBits, parity, stopBits, NO_CTSRTS, _uartCallback.bindenv(this));
71-
_rts.configure(DIGITAL_OUT, 0);
71+
if (rts != null) {
72+
_rts = rts;
73+
_uart.configure(baudRate, dataBits, parity, stopBits, NO_CTSRTS, _uartCallback.bindenv(this));
74+
_rts.configure(DIGITAL_OUT, 0);
75+
} else {
76+
_uart.configure(baudRate, dataBits, parity, stopBits, 0x00, _uartCallback.bindenv(this));
77+
}
7278
}
7379

7480
//
@@ -335,13 +341,18 @@ class Modbus485Master extends ModbusMaster {
335341
}
336342
_callback = properties.callback;
337343
local frame = _createADU(PDU);
338-
local rw = _rts.write.bindenv(_rts);
339344
local uw = _uart.write.bindenv(_uart);
340345
local uf = _uart.flush.bindenv(_uart);
341-
rw(1);
342-
uw(frame);
343-
uf();
344-
rw(0);
346+
if (_rts != null) {
347+
local rw = _rts.write.bindenv(_rts);
348+
rw(1);
349+
uw(frame);
350+
uf();
351+
rw(0);
352+
} else {
353+
uw(frame);
354+
uf();
355+
}
345356
_log(frame, "Outgoing ADU : ");
346357
_responseTimer = _responseTimeoutFactory(_timeout);
347358
}

Diff for: Modbus485Master/README.md renamed to ModbusSerialMaster/README.md

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Modbus485Master
1+
# ModbusSerialMaster
22

3-
This library allows an imp to communicate with other devices via the Modbus-RS485 protocol.
3+
This library allows an imp to communicate with other devices via the Modbus-RS485 or Modbus-RS232 protocol.
44

55
**To use this library, add the following statements to the top of your device code:**
66

77
```
88
#require "CRC16.class.nut:1.0.0"
99
#require "ModbusRTU.device.lib.nut:1.0.1"
1010
#require "ModbusMaster.device.lib.nut:1.0.1"
11-
#require "Modbus485Master.device.lib.nut:1.0.1"
11+
#require "ModbusSerialMaster.device.lib.nut:2.0.0"
1212
```
1313

1414
## Hardware Setup
@@ -23,34 +23,34 @@ The following instructions are applicable to Electric Imp’s [impAccelerator&tr
2323
6. Power up the Fieldbus Gateway
2424
7. Configure the Fieldbus Gateway for Internet access using BlinkUp&trade;
2525

26-
## Modbus485Master Class Usage
26+
## ModbusSerialMaster Class Usage
2727

2828
This is the main library class. It implements most of the functions listed in the [Modbus specification](http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf).
2929

30-
### Constructor: Modbus485Master(*uart, rts[, params]*)
30+
### Constructor: ModbusSerialMaster(*uart[, rts][, params]*)
3131

32-
Instantiate a new Modbus485Master object and set the configuration of the UART bus over which it operates. The parameters *uart* and *rts* are, respectively, the imp UART in use and an imp GPIO pin which will be used to control flow. The *params* parameter is optional and takes a table containing the following keys:
32+
Instantiates a new ModbusSerialMaster object and configures the UART bus over which it operates. The *uart* parameter is an imp UART object. The optional *rts* parameter should be used for RS485 communications when you are using an imp GPIO pin for control flow. The *params* parameter is optional and takes a table containing the following keys:
3333

3434
| Key | Default | Notes |
3535
| ------ | ----------- | ------------------------------------------------------------------------------- |
3636
| baudRate | 19200 | The baud rate of the UART connection |
3737
| dataBits | 8 | The word size on the UART connection in bits (7 or 8 bits) |
38-
| parity | *PARITY_NONE* | Parity configuration of the UART connection |
38+
| parity | *PARITY_NONE* | Parity configuration of the UART connection |
3939
| stopBits | 1 | Number of stop bits (1 or 2) on the UART connection |
4040
| timeout | 1.0 | The maximum time allowed for one request |
4141
| debug | `false` | If enabled, the outgoing and incoming ADU will be printed for debugging purpose |
4242

4343
#### Example
4444

4545
```squirrel
46-
modbus <- Modbus485Master(hardware.uart2, hardware.pinL);
46+
modbus <- ModbusSerialMaster(hardware.uart2, hardware.pinL);
4747
```
4848

49-
## Modbus485Master Class Methods
49+
## ModbusSerialMaster Class Methods
5050

5151
### read(*deviceAddress, targetType, startingAddress, quantity[, callback]*)
5252

53-
Function Code : 01, 02, 03, 04
53+
Function Codes: 01, 02, 03, 04
5454

5555
This is a generic method used to read values from a single coil, register, or multiple coils and registers. It takes the following parameters:
5656

@@ -73,7 +73,7 @@ This is a generic method used to read values from a single coil, register, or mu
7373

7474
```squirrel
7575
// Read from a single coil
76-
modbus.read(0x01, MODBUSRTU_TARGET_TYPE.DISCRETE_INPUT, 0x01, 1, function(error, result) {
76+
modbus.read(0x01, MODBUSRTU_TARGET_TYPE.COIL, 0x0001, 1, function(error, result) {
7777
if (error) {
7878
server.error(error);
7979
} else {
@@ -82,7 +82,7 @@ modbus.read(0x01, MODBUSRTU_TARGET_TYPE.DISCRETE_INPUT, 0x01, 1, function(error,
8282
}.bindenv(this));
8383
8484
// Read from multiple registers
85-
modbus.read(0x01, MODBUSRTU_TARGET_TYPE.INPUT_REGISTER, 0x01 , 5, function(error, results) {
85+
modbus.read(0x01, MODBUSRTU_TARGET_TYPE.INPUT_REGISTER, 0x7000 , 2, function(error, results) {
8686
if (error) {
8787
server.error(error);
8888
} else {
@@ -95,7 +95,7 @@ modbus.read(0x01, MODBUSRTU_TARGET_TYPE.INPUT_REGISTER, 0x01 , 5, function(error
9595

9696
### write(*deviceAddress, targetType, startingAddress, quantity, values[, callback]*)
9797

98-
Function Code : 05, 06, 15, 16
98+
Function Codes: 05, 06, 15, 16
9999

100100
This is a generic method used to write values to multiple coils and registers. It takes the following parameters:
101101

@@ -119,7 +119,7 @@ This is a generic method used to write values to multiple coils and registers. I
119119

120120
```squirrel
121121
// Write to a single coil
122-
modbus.write(0x01, MODBUSRTU_TARGET_TYPE.COIL, 0x01, 1, true, function(error, result) {
122+
modbus.write(0x01, MODBUSRTU_TARGET_TYPE.COIL, 8192, 1, true, function(error, result) {
123123
if (error) {
124124
server.error(error);
125125
} else {
@@ -128,20 +128,18 @@ modbus.write(0x01, MODBUSRTU_TARGET_TYPE.COIL, 0x01, 1, true, function(error, re
128128
}.bindenv(this));
129129
130130
// Write to multiple registers
131-
modbus.write(0x01, MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER, 0x01, 5, [false, true, false, true, true], function(error, results) {
131+
modbus.write(0x01, MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER, 9, 3, [188, 80, 18], function(error, result) {
132132
if (error) {
133133
server.error(error);
134134
} else {
135-
foreach(key, value in results) {
136-
server.log(key + " : " + value);
137-
}
135+
server.log(result);
138136
}
139137
}.bindenv(this));
140138
```
141139

142140
### readExceptionStatus(*deviceAddress[, callback]*)
143141

144-
Function Code : 07
142+
Function Code: 07
145143

146144
This method reads the contents of eight Exception Status outputs in a remote device (address passed into the first parameter. If a callback is supplied, it will be triggered when a response regarding this request is received. The callback takes two parameters: *error* and *result*.
147145

@@ -159,7 +157,7 @@ modbus.readExceptionStatus(0x01, function(error, result) {
159157

160158
### diagnostics(*deviceAddress, subFunctionCode, data[, callback]*)
161159

162-
Function Code : 08
160+
Function Code: 08
163161

164162
This method provides a series of tests for checking the communication system between a client (Master) device and a server (Slave), or for checking various internal error conditions within a server. It takes the following parameters:
165163

@@ -206,7 +204,7 @@ modbus.diagnostics(0x01, MODBUSRTU_SUB_FUNCTION_CODE.RESTART_COMMUNICATION_OPTIO
206204

207205
### reportSlaveID(*deviceAddress[, callback]*)
208206

209-
Function Code : 17
207+
Function Code: 17
210208

211209
This method reads the description of the type, the current status and other information specific to a remote device whose address is specified in the method’s first parameter. The second, optional parameter is a function that will be fired when a response regarding this request is received. It takes two parameters, *error* and *result*.
212210

@@ -225,7 +223,7 @@ modbus.reportSlaveID(0x01, function(error, result) {
225223

226224
### maskWriteRegister(*deviceAddress, referenceAddress, AND_Mask, OR_Mask[, callback]*)
227225

228-
Function Code : 22
226+
Function Code: 22
229227

230228
This method modifies the contents of a specified holding register using a combination of an AND mask, an OR mask and the register’s current contents. The function can be used to set or clear individual bits in the register. It takes the following parameters:
231229

@@ -251,7 +249,7 @@ modbus.maskWriteRegister(0x01, 0x10, 0xFFFF, 0x0000, function(error, result) {
251249

252250
### readWriteMultipleRegisters(*deviceAddress, readingStartAddress, readQuantity, writeStartAddress, writeQuantity, writeValue[, callback]*)
253251

254-
Function Code : 23
252+
Function Code: 23
255253

256254
This method performs a combination of one read operation and one write operation in a single Modbus transaction. The write operation is performed before the read. It takes the following parameters:
257255

@@ -271,11 +269,20 @@ This method performs a combination of one read operation and one write operation
271269
#### Example
272270

273271
```squirrel
272+
modbus.readWriteMultipleRegisters(0x01, 9, 3, 9, 3, [188, 80, 18], function(error, result) {
273+
if (error) {
274+
errorMessage(error, resolve, reject);
275+
} else {
276+
foreach(key, value in results) {
277+
server.log(key + " : " + value);
278+
}
279+
}
280+
}.bindenv(this));
274281
```
275282

276283
### readDeviceIdentification(*deviceAddress, readDeviceIdCode, objectId[, callback]*)
277284

278-
Function Code : 43/14
285+
Function Code: 43/14
279286

280287
This method lets you read the identification and additional information relative to the physical and functional description of a remote device. It takes the following parameters:
281288

@@ -343,4 +350,4 @@ The table below enumerates all the exception codes that can be possibly encounte
343350

344351
## License
345352

346-
The Modbus485Master library is licensed under the [MIT License](../LICENSE).
353+
The ModbusSerialMaster library is licensed under the [MIT License](../LICENSE).

0 commit comments

Comments
 (0)