diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b59b0fd --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/Modbus485Master/Modbus485Master.class.nut b/Modbus485Master/Modbus485Master.device.lib.nut similarity index 92% rename from Modbus485Master/Modbus485Master.class.nut rename to Modbus485Master/Modbus485Master.device.lib.nut index 9fd59b5..fe2d590 100644 --- a/Modbus485Master/Modbus485Master.class.nut +++ b/Modbus485Master/Modbus485Master.device.lib.nut @@ -1,8 +1,29 @@ -// Copyright (c) 2017 Electric Imp -// This file is licensed under the MIT License -// http://opensource.org/licenses/MIT +// 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. class Modbus485Master extends ModbusMaster { + static VERSION = "1.0.1"; static MINIMUM_RESPONSE_LENGTH = 5; _uart = null; _rts = null; diff --git a/Modbus485Master/README.md b/Modbus485Master/README.md index fd9ffc8..5016e35 100644 --- a/Modbus485Master/README.md +++ b/Modbus485Master/README.md @@ -6,9 +6,9 @@ This library allows an imp to communicate with other devices via the Modbus-RS48 ``` #require "CRC16.class.nut:1.0.0" -#require "ModbusRTU.class.nut:1.0.0" -#require "ModbusMaster.class.nut:1.0.0" -#require "Modbus485Master.class.nut:1.0.0" +#require "ModbusRTU.device.lib.nut:1.0.1" +#require "ModbusMaster.device.lib.nut:1.0.1" +#require "Modbus485Master.device.lib.nut:1.0.1" ``` ## Hardware Setup @@ -27,7 +27,7 @@ The following instructions are applicable to Electric Imp’s [impAccelerator&tr 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). -### Constructor: Modbus485Master(*uart, rts, params*) +### Constructor: Modbus485Master(*uart, rts[, params]*) 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: @@ -265,16 +265,12 @@ This method performs a combination of one read operation and one write operation | *writeValue* | Blob | Yes | N/A | The value written into the holding register | | *callback* | Function | No | Null | The function to be fired when it receives response regarding this request. It takes two parameters, *error* and *result* | +**Note** The actual order of operation is determined by the implementation of user's device. + + #### Example ```squirrel -modbus.readWriteMultipleRegisters(0x01, 0x10, 0xFFFF, 0x0000, function(error, result) { - if (error) { - server.error(error); - } else { - server.log(result); - } -}.bindenv(this)); ``` ### readDeviceIdentification(*deviceAddress, readDeviceIdCode, objectId[, callback]*) @@ -347,4 +343,4 @@ The table below enumerates all the exception codes that can be possibly encounte ## License -The Modbus485Master library is licensed under the [MIT License](https://github.com/electricimp/Modbus/tree/master/LICENSE). +The Modbus485Master library is licensed under the [MIT License](../LICENSE). diff --git a/Modbus485Master/example/example.device.nut b/Modbus485Master/example/example.device.nut index 8a53fbf..d6a6a99 100644 --- a/Modbus485Master/example/example.device.nut +++ b/Modbus485Master/example/example.device.nut @@ -1,7 +1,31 @@ +// 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. + #require "CRC16.class.nut:1.0.0" -#require "ModbusRTU.class.nut:1.0.0" -#require "ModbusMaster.class.nut:1.0.0" -#require "Modbus485Master.class.nut:1.0.0" +#require "ModbusRTU.device.lib.nut:1.0.1" +#require "ModbusMaster.device.lib.nut:1.0.1" +#require "Modbus485Master.device.lib.nut:1.0.1" // this example demonstrates how to write and read values into/from holding registers const DEVICE_ADDRESS = 0x01; diff --git a/Modbus485Master/tests/device.test.nut b/Modbus485Master/tests/device.test.nut index 2f511df..553ba01 100644 --- a/Modbus485Master/tests/device.test.nut +++ b/Modbus485Master/tests/device.test.nut @@ -1,3 +1,27 @@ +// 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. + const DEVICE_ADDRESS = 1; function errorMessage(error, resolve, reject) { diff --git a/Modbus485Slave/Modbus485Slave.class.nut b/Modbus485Slave/Modbus485Slave.device.lib.nut similarity index 82% rename from Modbus485Slave/Modbus485Slave.class.nut rename to Modbus485Slave/Modbus485Slave.device.lib.nut index 2e5af05..0ea3a66 100644 --- a/Modbus485Slave/Modbus485Slave.class.nut +++ b/Modbus485Slave/Modbus485Slave.device.lib.nut @@ -1,8 +1,29 @@ -// Copyright (c) 2017 Electric Imp -// This file is licensed under the MIT License -// http://opensource.org/licenses/MIT +// 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. class Modbus485Slave extends ModbusSlave { + static VERSION = "1.0.1"; static MIN_REQUEST_LENGTH = 4; _slaveID = null; _uart = null; diff --git a/Modbus485Slave/README.md b/Modbus485Slave/README.md index 2674b66..cf7c101 100644 --- a/Modbus485Slave/README.md +++ b/Modbus485Slave/README.md @@ -6,8 +6,8 @@ This library empowers an imp to communicate with the Modbus Master via the RS485 ```squirrel #require "CRC16.class.nut:1.0.0" -#require "ModbusSlave.class.nut:1.0.0" -#require "Modbus485Slave.class.nut:1.0.0" +#require "ModbusSlave.device.lib.nut:1.0.1" +#require "Modbus485Slave.device.lib.nut:1.0.1" ``` ## Hardware Setup @@ -195,4 +195,4 @@ The table below enumerates all the exception codes that can be possibly encounte # License -The Modbus485Slave library is licensed under the [MIT License](https://github.com/electricimp/Mdobus/tree/master/LICENSE). +The Modbus485Slave library is licensed under the [MIT License](../LICENSE). diff --git a/Modbus485Slave/example/device.example.nut b/Modbus485Slave/example/device.example.nut index bcbdd63..f1884f5 100644 --- a/Modbus485Slave/example/device.example.nut +++ b/Modbus485Slave/example/device.example.nut @@ -1,6 +1,30 @@ +// 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. + #require "CRC16.class.nut:1.0.0" -#require "ModbusSlave.class.nut:1.0.0" -#require "Modbus485Slave.class.nut:1.0.0" +#require "ModbusSlave.device.lib.nut:1.0.1" +#require "Modbus485Slave.device.lib.nut:1.0.1" modbus <- Modbus485Slave(hardware.uart2, hardware.pinL, 1, { debug = true }); diff --git a/Modbus485Slave/tests/device.test.nut b/Modbus485Slave/tests/device.test.nut index 6cd6cba..34a3d9c 100644 --- a/Modbus485Slave/tests/device.test.nut +++ b/Modbus485Slave/tests/device.test.nut @@ -1,3 +1,28 @@ +// 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. + + // it is a bit hard to write test cases if the hardware is involved, // so the idea is to create a fake buffer and simulate the parsing requests and creating responses diff --git a/ModbusMaster/ModbusMaster.class.nut b/ModbusMaster/ModbusMaster.device.lib.nut similarity index 91% rename from ModbusMaster/ModbusMaster.class.nut rename to ModbusMaster/ModbusMaster.device.lib.nut index 1e333db..5b2c04a 100644 --- a/ModbusMaster/ModbusMaster.class.nut +++ b/ModbusMaster/ModbusMaster.device.lib.nut @@ -1,9 +1,29 @@ -// Copyright (c) 2017 Electric Imp -// This file is licensed under the MIT License -// http://opensource.org/licenses/MIT +// 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. class ModbusMaster { - static VERSION = "1.0.0"; + static VERSION = "1.0.1"; _debug = null; // diff --git a/ModbusMaster/README.md b/ModbusMaster/README.md index 3997dad..e5f275f 100644 --- a/ModbusMaster/README.md +++ b/ModbusMaster/README.md @@ -15,4 +15,4 @@ 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). diff --git a/ModbusRTU/ModbusRTU.class.nut b/ModbusRTU/ModbusRTU.device.lib.nut similarity index 91% rename from ModbusRTU/ModbusRTU.class.nut rename to ModbusRTU/ModbusRTU.device.lib.nut index 9d135d0..26e03a5 100644 --- a/ModbusRTU/ModbusRTU.class.nut +++ b/ModbusRTU/ModbusRTU.device.lib.nut @@ -1,6 +1,26 @@ -// Copyright (c) 2017 Electric Imp -// This file is licensed under the MIT License -// http://opensource.org/licenses/MIT +// 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. enum MODBUSRTU_SUB_FUNCTION_CODE { RETURN_QUERY_DATA = 0x0000, @@ -64,7 +84,7 @@ enum MODBUSRTU_OBJECT_ID { } class ModbusRTU { - static VERSION = "1.0.0"; + static VERSION = "1.0.1"; // resLen and reqLen are the length of the PDU static FUNCTION_CODES = { readCoils = { diff --git a/ModbusRTU/README.md b/ModbusRTU/README.md index 790219a..6ad0243 100644 --- a/ModbusRTU/README.md +++ b/ModbusRTU/README.md @@ -6,7 +6,7 @@ This library creates and parses Modbus Protocol Data Units (PDU). It depends on ``` #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 @@ -230,4 +230,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). diff --git a/ModbusRTU/lib/CRC16.class.nut b/ModbusRTU/lib/CRC16.class.nut index 18aedff..a3e6b92 100644 --- a/ModbusRTU/lib/CRC16.class.nut +++ b/ModbusRTU/lib/CRC16.class.nut @@ -1,3 +1,27 @@ +// MIT License +// +// Copyright 2015 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. + const CRC16_LOOKUP_LOW = "\x00\xC0\xC1\x01\xC3\x03\x02\xC2\xC6\x06\x07\xC7\x05\xC5\xC4\x04\xCC\x0C\x0D\xCD\x0F\xCF\xCE\x0E\x0A\xCA\xCB\x0B\xC9\x09\x08\xC8\xD8\x18\x19\xD9\x1B\xDB\xDA\x1A\x1E\xDE\xDF\x1F\xDD\x1D\x1C\xDC\x14\xD4\xD5\x15\xD7\x17\x16\xD6\xD2\x12\x13\xD3\x11\xD1\xD0\x10\xF0\x30\x31\xF1\x33\xF3\xF2\x32\x36\xF6\xF7\x37\xF5\x35\x34\xF4\x3C\xFC\xFD\x3D\xFF\x3F\x3E\xFE\xFA\x3A\x3B\xFB\x39\xF9\xF8\x38\x28\xE8\xE9\x29\xEB\x2B\x2A\xEA\xEE\x2E\x2F\xEF\x2D\xED\xEC\x2C\xE4\x24\x25\xE5\x27\xE7\xE6\x26\x22\xE2\xE3\x23\xE1\x21\x20\xE0\xA0\x60\x61\xA1\x63\xA3\xA2\x62\x66\xA6\xA7\x67\xA5\x65\x64\xA4\x6C\xAC\xAD\x6D\xAF\x6F\x6E\xAE\xAA\x6A\x6B\xAB\x69\xA9\xA8\x68\x78\xB8\xB9\x79\xBB\x7B\x7A\xBA\xBE\x7E\x7F\xBF\x7D\xBD\xBC\x7C\xB4\x74\x75\xB5\x77\xB7\xB6\x76\x72\xB2\xB3\x73\xB1\x71\x70\xB0\x50\x90\x91\x51\x93\x53\x52\x92\x96\x56\x57\x97\x55\x95\x94\x54\x9C\x5C\x5D\x9D\x5F\x9F\x9E\x5E\x5A\x9A\x9B\x5B\x99\x59\x58\x98\x88\x48\x49\x89\x4B\x8B\x8A\x4A\x4E\x8E\x8F\x4F\x8D\x4D\x4C\x8C\x44\x84\x85\x45\x87\x47\x46\x86\x82\x42\x43\x83\x41\x81\x80\x40"; const CRC16_LOOKUP_HIGH = "\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40\x00\xC1\x81\x40\x01\xC0\x80\x41\x00\xC1\x81\x40\x01\xC0\x80\x41\x01\xC0\x80\x41\x00\xC1\x81\x40"; diff --git a/ModbusRTU/tests/device.test.nut b/ModbusRTU/tests/device.test.nut index b4e17e0..8eb3cc0 100644 --- a/ModbusRTU/tests/device.test.nut +++ b/ModbusRTU/tests/device.test.nut @@ -1,3 +1,27 @@ +// 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. + const MINIMUM_RESPONSE_LENGTH = 5; const DEVICE_ADDRESS = 0x01; diff --git a/ModbusSlave/ModbusSlave.class.nut b/ModbusSlave/ModbusSlave.device.lib.nut similarity index 91% rename from ModbusSlave/ModbusSlave.class.nut rename to ModbusSlave/ModbusSlave.device.lib.nut index 0de705e..fcbe00f 100644 --- a/ModbusSlave/ModbusSlave.class.nut +++ b/ModbusSlave/ModbusSlave.device.lib.nut @@ -1,6 +1,26 @@ -// Copyright (c) 2017 Electric Imp -// This file is licensed under the MIT License -// http://opensource.org/licenses/MIT +// 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. enum MODBUSSLAVE_TARGET_TYPE { COIL, @@ -21,7 +41,7 @@ enum MODBUSSLAVE_EXCEPTION { } class ModbusSlave { - static VERSION = "1.0.0"; + static VERSION = "1.0.1"; static FUNCTION_CODES = { readCoil = { fcode = 0x01, diff --git a/ModbusSlave/README.md b/ModbusSlave/README.md index 8d6689a..6219203 100644 --- a/ModbusSlave/README.md +++ b/ModbusSlave/README.md @@ -11,4 +11,4 @@ This library empowers an imp to communicate the Modbus Master via the RS485 prot # 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). diff --git a/ModbusTCPMaster/ModbusTCPMaster.class.nut b/ModbusTCPMaster/ModbusTCPMaster.device.lib.nut similarity index 78% rename from ModbusTCPMaster/ModbusTCPMaster.class.nut rename to ModbusTCPMaster/ModbusTCPMaster.device.lib.nut index 0a98e82..05d3720 100644 --- a/ModbusTCPMaster/ModbusTCPMaster.class.nut +++ b/ModbusTCPMaster/ModbusTCPMaster.device.lib.nut @@ -1,8 +1,29 @@ -// Copyright (c) 2017 Electric Imp -// This file is licensed under the MIT License -// http://opensource.org/licenses/MIT +// 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. class ModbusTCPMaster extends ModbusMaster { + static VERSION = "1.0.1"; static MAX_TRANSACTION_COUNT = 65535; _transactions = null; _wiz = null; @@ -130,11 +151,12 @@ class ModbusTCPMaster extends ModbusMaster { function _send(PDU, properties) { _transactions[_transactionCount] <- properties; local ADU = _createADU(PDU); + // with or without success in transmission of data, the transaction count would be advanced + _transactionCount = (_transactionCount + 1) % MAX_TRANSACTION_COUNT; _connection.transmit(ADU, function(error) { if (error) { _callbackHandler(error, null, properties.callback); } else { - _transactionCount = (_transactionCount + 1) % MAX_TRANSACTION_COUNT; _log(ADU, "Outgoing ADU: "); } }.bindenv(this)); diff --git a/ModbusTCPMaster/README.md b/ModbusTCPMaster/README.md index c16f1f1..d7f9830 100644 --- a/ModbusTCPMaster/README.md +++ b/ModbusTCPMaster/README.md @@ -5,9 +5,9 @@ This library allows an imp to communicate with other devices via TCP/IP. It requ **To use this library, add the following statements to the top of your device code:** ``` -#require "ModbusRTU.class.nut:1.0.0" -#require "ModbusMaster.class.nut:1.0.0" -#require "ModbusTCPMaster.class.nut:1.0.0" +#require "ModbusRTU.device.lib.nut:1.0.1" +#require "ModbusMaster.device.lib.nut:1.0.1" +#require "ModbusTCPMaster.device.lib.nut:1.0.1" #require "W5500.device.nut:1.0.0" ``` @@ -25,9 +25,9 @@ The following instructions are applicable to Electric Imp’s [impAccelerator&tr 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). -### Constructor: ModbusTCPMaster(*wiz, debug*) +### Constructor: ModbusTCPMaster(*wiz[, debug]*) -Instantiate a new ModbusTCPMaster object. It takes two parameters: *wiz*, the Wiznet W5500 object that is driving the Ethernet link, and *debug* which, if enabled, prints the outgoing and incoming ADU for debugging purposes. +Instantiate a new ModbusTCPMaster object. It takes one required parameter: *wiz*, the [Wiznet W5500](https://github.com/electricimp/Wiznet_5500) object that is driving the Ethernet link, and one optional boolean parameter: *debug* which, if enabled, prints the outgoing and incoming ADU for debugging purposes. The default value of *debug* is `false`. #### Example @@ -52,10 +52,13 @@ This method configures the network and opens a TCP connection with the device. I | Parameter | Data Type | Required | Default Value | Description | | --- | --- | --- | --- | --- | | *connectionSettings* | Table | Yes | N/A | The device IP address and port. The device IP address can either be a string or an array of four bytes, for example: `[192, 168, 1, 37]` or `"192.168.1.37"`. The port can either be an integer or array of two bytes (the high and low bytes of an unsigned two-byte integer value), for example: `[0x10, 0x92]` or `4242` | -| *onConnectCallback* | Function | No | Null | The function to be fired when the connection is established | -| *onReconnectCallback* | Function | No | Null| The function to be fired when the connection is re-established | +| *onConnectCallback* | Function | No | Null | The function to be fired when the connection is established. The callback takes `error` and `conn` as parameters | +| *onReconnectCallback* | Function | No | Null| The function to be fired when the connection is re-established. The callback takes `error` and `conn` as parameters | -**Note** If an *onReconnectCallback* is not supplied, when the connection is re-established, the *onConnectCallback* will be fired. +**Note** + +1. If an *onReconnectCallback* is not supplied, when the connection is re-established, the *onConnectCallback* will be fired. +2. Depending on the configuration of the device, the connection will be severed after a certain amount of time and try to re-establish itself. Users of concern are advised to pass in a *onReconnectCallback* to handle this situation. Please refer to the device spec for more information on how to configure the default idle time. #### Example @@ -76,9 +79,9 @@ modbus.connect(connectionSettings, function(error, conn){ }); ``` -### disconnect(*callback*) +### disconnect(*[callback]*) -This method closes the existing TCP connection. +This method closes the existing TCP connection. It takes one optional parameter: *callback*, a function that will be executed when the connection has been closed. #### Example @@ -86,7 +89,7 @@ This method closes the existing TCP connection. modbus.disconnect(); ``` -### read(*targetType, startingAddress, quantity, values[, callback]*) +### read(*targetType, startingAddress, quantity[, callback]*) Function Code : 01, 02, 03, 04 @@ -144,7 +147,7 @@ This is a generic method used to write values into coils or registers. It has th | *values* | Integer, array, bool, blob | Yes | N/A | The values written into Coils or Registers. Please view ‘Notes’, below | | *callback* | Function | No | Null | The function to be fired when it receives response regarding this request. It takes two parameters, *error* and *result* | -#### Notes +#### Notes: 1. Integer, blob and array[integer] are applicable to *MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER*. Use array[integer] only when *quantity* is greater than one. @@ -297,16 +300,21 @@ This method performs a combination of one read operation and one write operation | *writeValue* | Blob | Yes | N/A | The value written into the holding register | | *callback* | Function | No | Null | The function to be fired when it receives response regarding this request. It takes two parameters, *error* and *result* | +**Note** The actual order of operation is determined by the implementation of user's device. + #### Example ```squirrel -modbus.readWriteMultipleRegisters(0x10, 0xFFFF, 0x0000, function(error, result) { +modbus.readWriteMultipleRegisters(0x0A, 2, 0x0A, 2, [28, 88], function(error, result) { if (error) { server.error(error); } else { - server.log(result); + foreach (index, value in result) { + server.log(format("Index : %d, value : %d", index, value)); + } } -}.bindenv(this)); +}); + ``` ### readDeviceIdentification(*readDeviceIdCode, objectId, [callback]*) @@ -378,4 +386,4 @@ The table below enumerates all the exception codes that can be possibly encounte ## License -The ModbusTCPMaster library is licensed under the [MIT License](https://github.com/electricimp/Modbus/tree/master/LICENSE). +The ModbusTCPMaster library is licensed under the [MIT License](../LICENSE). diff --git a/ModbusTCPMaster/example/device.example.nut b/ModbusTCPMaster/example/device.example.nut index 3273437..6651e72 100644 --- a/ModbusTCPMaster/example/device.example.nut +++ b/ModbusTCPMaster/example/device.example.nut @@ -1,7 +1,31 @@ +// 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. + #require "W5500.device.nut:1.0.0" -#require "ModbusRTU.class.nut:1.0.0" -#require "ModbusMaster.class.nut:1.0.0" -#require "ModbusTCPMaster.class.nut:1.0.0" +#require "ModbusRTU.device.lib.nut:1.0.1" +#require "ModbusMaster.device.lib.nut:1.0.1" +#require "ModbusTCPMaster.device.lib.nut:1.0.1" // this example shows how to use readWriteMultipleRegisters @@ -23,7 +47,7 @@ local connectionSettings = { }; // open the connection -modbus.connect(networkSettings, connectionSettings, function(error, conn) { +modbus.connect(connectionSettings, function(error, conn) { if (error) { server.log(error); } else { diff --git a/ModbusTCPMaster/tests/device.test.nut b/ModbusTCPMaster/tests/device.test.nut index 47d1311..2b04b0d 100644 --- a/ModbusTCPMaster/tests/device.test.nut +++ b/ModbusTCPMaster/tests/device.test.nut @@ -1,3 +1,28 @@ +// 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. + + const PASS_MESSAGE = "Pass"; function errorMessage(error, resolve, reject) { @@ -390,6 +415,30 @@ class DeviceTestCase extends ImpTestCase { }.bindenv(this)); } + // this test will timeout when it fails + function testSendRequestsInSuccession() { + return Promise(function(resolve, reject) { + _connection.then(function(value) { + local address = 0x0A; + local value = false; + local quantity = 1; + local isSuccess = false; + _modbus.write(MODBUSRTU_TARGET_TYPE.COIL, address, quantity, value, function(error, result) { + if (isSuccess) { + resolve(PASS_MESSAGE); + } + isSuccess = true; + }.bindenv(this)); + _modbus.write(MODBUSRTU_TARGET_TYPE.COIL, address, quantity, value, function(error, result) { + if (isSuccess) { + resolve(PASS_MESSAGE); + } + isSuccess = true; + }.bindenv(this)); + }.bindenv(this)); + }.bindenv(this)); + } + function tearDown() { _modbus.disconnect(); return "Connection closed"; diff --git a/README.md b/README.md index 530705b..2e9b27b 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,4 @@ This library enables an imp to communicate with other Modbus devices via TCP/IP. ## License -The Modbus libraries are licensed under the [MIT License](https://github.com/electricimp/Modbus/tree/master/LICENSE). +The Modbus libraries are licensed under the [MIT License](./LICENSE).