Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
25023f6
Merge pull request #1 from electricimp/develop
May 4, 2017
943ce58
Merge pull request #2 from mysticpants/develop
Aug 24, 2017
c1a59c9
fixed typos
Aug 24, 2017
cac2410
updated file names and require statements
Aug 24, 2017
a6f4d7f
added license and updated license links
Aug 24, 2017
b875229
updated version number
Aug 24, 2017
f63cd18
added version to extender classes
Aug 24, 2017
0265984
Fix minor typo
ElectricImpSampleCode Aug 25, 2017
af442cd
Merge pull request #3 from electricimp/develop
Aug 25, 2017
10628f5
updated Modbus485 to ModbusSerial
Sep 13, 2017
01526d7
Minor edits
ElectricImpSampleCode Sep 15, 2017
377d8d9
Minor edits
ElectricImpSampleCode Sep 15, 2017
f4c29b2
Merge pull request #4 from electricimp/develop
Mar 20, 2018
e8113ff
Update README.md
ElectricImpSampleCode Mar 21, 2018
81242c0
Update README.md
ElectricImpSampleCode Mar 21, 2018
6ff9fd4
Update README.md
ElectricImpSampleCode Mar 21, 2018
ee81e71
Update README.md
ElectricImpSampleCode Mar 21, 2018
7ed9535
Merge pull request #5 from electricimp/develop
Mar 21, 2018
d845bcc
Add Device Address as last parameter
lexal07 Dec 17, 2020
d02fad8
Update ModbusTCPMaster.device.lib.nut
alexey-nbl Dec 19, 2020
516e760
Update device.test.nut
alexey-nbl Dec 19, 2020
9812db1
Increased minor version, added TODO
lexal07 Dec 20, 2020
2faf2ff
Update ModbusTCPMaster.device.lib.nut
alexey-nbl Dec 20, 2020
a4cf3c2
Merge pull request #6 from nobitlost/deviceAddressLastParameter
zandr Dec 21, 2020
ab776b9
Add library usage recommendation note
smittytone Jul 18, 2022
c4af39e
Ditto
smittytone Jul 18, 2022
898f436
Finalize recommendation
Jul 18, 2022
7477a2c
Fix createReadPUD(), createWritePDU() targetTYpe parameter parsing
Aug 30, 2022
5ee1d01
Correct createReadPDU(), createWritePDU() entries
Aug 30, 2022
e197aa8
Remove unneeded access value from tables
Aug 30, 2022
fb21dc9
Correct switch() statements
Aug 30, 2022
f4df336
Update ownership
Nov 20, 2023
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
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

Copyright 2017 Electric Imp

SPDX-License-Identifier: MIT

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
8 changes: 0 additions & 8 deletions Modbus485Master/.gitignore

This file was deleted.

30 changes: 0 additions & 30 deletions Modbus485Master/example/example.device.nut

This file was deleted.

29 changes: 0 additions & 29 deletions Modbus485Slave/example/device.example.nut

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// Copyright (c) 2017 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
// MIT License
//
// Copyright 2017-19 Electric Imp
// Copyright 2020-23 KORE Wireless
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

class ModbusMaster {
static VERSION = "1.0.0";
static VERSION = "1.0.2";
_debug = null;

//
Expand Down
6 changes: 3 additions & 3 deletions ModbusMaster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ This class is the abstract class for the following classes, it should NOT be ins
**Please proceed to one of the following libraries**


# [Modbus485Master](../Modbus485Master/)
# [ModbusSerialMaster](../ModbusSerialMaster/)

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

# [ModbusTCPMaster](../ModbusTCPMaster/)

This library enables an imp to communicate with other devices via TCP/IP .

# License

The ModbusRTUMaster library is licensed under the [MIT License](https://github.com/electricimp/thethingsapi/tree/master/LICENSE).
The ModbusRTUMaster library is licensed under the [MIT License](../LICENSE).
7 changes: 0 additions & 7 deletions ModbusRTU/.gitignore

This file was deleted.

14 changes: 14 additions & 0 deletions ModbusRTU/.imptest
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"modelId": "KB2yibqKCKCh" /* I */,
"devices": [
"5000d8c46a56cdae" /* I */
],
"agentFile": false,
"deviceFile": "ModbusRTU.device.lib.nut",
"stopOnFailure": false,
"timeout": 10,
"tests": [
"*.test.nut",
"tests/**/*.test.nut"
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
// Copyright (c) 2017 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
// MIT License
//
// Copyright 2017-19 Electric Imp
// Copyright 2020-23 KORE Wireless
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

enum MODBUSRTU_SUB_FUNCTION_CODE {
RETURN_QUERY_DATA = 0x0000,
Expand Down Expand Up @@ -64,7 +85,7 @@ enum MODBUSRTU_OBJECT_ID {
}

class ModbusRTU {
static VERSION = "1.0.0";
static VERSION = "1.0.2";
// resLen and reqLen are the length of the PDU
static FUNCTION_CODES = {
readCoils = {
Expand Down Expand Up @@ -245,8 +266,26 @@ class ModbusRTU {
// function to create PDU for read
//
static function createReadPDU(targetType, startingAddress, quantity) {
local PDU = blob(targetType.reqLen);
PDU.writen(targetType.fcode, 'b');
local pduType;
switch(targetType) {
case MODBUSRTU_TARGET_TYPE.COIL:
pduType = FUNCTION_CODES.read_coils;
break;
case MODBUSRTU_TARGET_TYPE.DISCRETE_INPUT:
pduType = FUNCTION_CODES.readInputs;
break;
case MODBUSRTU_TARGET_TYPE.INPUT_REGISTER:
pduType = FUNCTION_CODES.readInputRegs;
break;
case MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER:
pduType = FUNCTION_CODES.readHoldingRegs;
break;
default:
throw "Incorrect targetType specified";
}

local PDU = blob(pduType.reqLen);
PDU.writen(pduType.fcode, 'b');
PDU.writen(swap2(startingAddress), 'w');
PDU.writen(swap2(quantity), 'w');
return PDU;
Expand All @@ -256,8 +295,20 @@ class ModbusRTU {
// function to create PDU for write
//
static function createWritePDU(targetType, startingAddress, numBytes, quantity, values) {
local pduType;
switch(targetType) {
case MODBUSRTU_TARGET_TYPE.COIL:
pduType = FUNCTION_CODES.writeSingleCoil;
break;
case MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER:
pduType = FUNCTION_CODES.writeSingleReg;
break;
default:
throw "Incorrect targetType specified";
}

local PDU = blob();
PDU.writen(targetType.fcode, 'b');
PDU.writen(pduType.fcode, 'b');
PDU.writen(swap2(startingAddress), 'w');
if (quantity > 1) {
PDU.writen(swap2(quantity), 'w');
Expand Down
28 changes: 20 additions & 8 deletions ModbusRTU/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

This library creates and parses Modbus Protocol Data Units (PDU). It depends on Electric Imp's [CRC16 library](https://github.com/electricimp/CRC16) to calculate the [CRC-16](https://en.wikipedia.org/wiki/Cyclic_redundancy_check) value of a string or blob.

**Note** You will not usually work with this library directly, but load it as a dependency for one of our other Modbus libraries, which target specific use cases:

* [ModbusSerialMaster](../ModbusMaster)
* [ModbusTCPMaster](../ModbusSerialMaster)

We recommend you work with one of these libraries unless your use case very specifically needs to perform PDU operations not provided by them.

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

```
#require "CRC16.class.nut:1.0.0"
#require "ModbusRTU.class.nut:1.0.0"
#require "ModbusRTU.device.lib.nut:1.0.1"
```

## ModbusRTU Class Usage
Expand All @@ -23,12 +30,12 @@ This method creates a PDU for *readData* operations. It takes the following para
| *startingAddress* | Integer | Yes | N/A | The address from which it begins reading values |
| *quantity* | Integer | Yes | N/A | The number of consecutive addresses the values are read from |

| Type | Value | Access |
| --- | --- | --- |
| Coil | *MODBUSRTU_TARGET_TYPE.COIL* | Read-Write |
| Discrete Input | *MODBUSRTU_TARGET_TYPE.DISCRETE_INPUT* | Read Only |
| Input Register | *MODBUSRTU_TARGET_TYPE.INPUT_REGISTER* | Read Only |
| Holding Register | *MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER* | Read-Write |
| Type | Value |
| --- | --- |
| Coil | *MODBUSRTU_TARGET_TYPE.COIL* |
| Discrete Input | *MODBUSRTU_TARGET_TYPE.DISCRETE_INPUT* |
| Input Register | *MODBUSRTU_TARGET_TYPE.INPUT_REGISTER* |
| Holding Register | *MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER* |

#### Example

Expand All @@ -52,6 +59,11 @@ This method creates a PDU for *writeData* operations. It takes the following par
| *quantity* | Integer | Yes | N/A | The number of consecutive addresses the values are written into |
| *values* | Integer, array ([integer, boolean]), boolean, blob | Yes | N/A | The values written into Coils or Registers |

| Type | Value |
| --- | --- |
| Coil | *MODBUSRTU_TARGET_TYPE.COIL* |
| Holding Register | *MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER* |

#### Example

```squirrel
Expand Down Expand Up @@ -230,4 +242,4 @@ local result = parse({

## License

The ModbusRTU library is licensed under the [MIT License](https://github.com/electricimp/Modbus/tree/master/LICENSE).
The ModbusRTU library is licensed under the [MIT License](../LICENSE).
33 changes: 0 additions & 33 deletions ModbusRTU/lib/CRC16.class.nut

This file was deleted.

Loading