ModBusTCP with some fixes
ModBus-TCP.h@5:1bf121618c2f, 2020-12-03 (annotated)
- Committer:
- kcherneha_aitheon
- Date:
- Thu Dec 03 14:01:33 2020 +0000
- Revision:
- 5:1bf121618c2f
- Parent:
- 4:41ee166a0e6c
fixed error jump to case label [-fpermissive]
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gulchi | 4:41ee166a0e6c | 1 | /* Copyright (C) 2017 Christian Gulde, MIT License |
gulchi | 4:41ee166a0e6c | 2 | * |
gulchi | 4:41ee166a0e6c | 3 | * ModBus-TCP includes code from HTTPD(Hiroshi Suga), which is licensed under the MIT license: |
okini3939 | 0:d18dff347122 | 4 | * |
okini3939 | 0:d18dff347122 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
okini3939 | 0:d18dff347122 | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
okini3939 | 0:d18dff347122 | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
okini3939 | 0:d18dff347122 | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
okini3939 | 0:d18dff347122 | 9 | * furnished to do so, subject to the following conditions: |
okini3939 | 0:d18dff347122 | 10 | * |
okini3939 | 0:d18dff347122 | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
okini3939 | 0:d18dff347122 | 12 | * substantial portions of the Software. |
okini3939 | 0:d18dff347122 | 13 | * |
okini3939 | 0:d18dff347122 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
okini3939 | 0:d18dff347122 | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
okini3939 | 0:d18dff347122 | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
okini3939 | 0:d18dff347122 | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
okini3939 | 0:d18dff347122 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gulchi | 4:41ee166a0e6c | 19 | * |
okini3939 | 0:d18dff347122 | 20 | */ |
okini3939 | 0:d18dff347122 | 21 | |
gulchi | 2:fcd20e2cd110 | 22 | #ifndef MODBUS_TCP_H |
gulchi | 2:fcd20e2cd110 | 23 | #define MODBUS_TCP |
okini3939 | 0:d18dff347122 | 24 | |
okini3939 | 0:d18dff347122 | 25 | #include "mbed.h" |
okini3939 | 0:d18dff347122 | 26 | #include "rtos.h" |
dgriffin65 | 1:b724fdb741e7 | 27 | #include "NetworkStack.h" |
gulchi | 3:ebea8e061ae6 | 28 | #include "EthernetInterface.h" |
gulchi | 3:ebea8e061ae6 | 29 | //#include "CBuffer.h" |
okini3939 | 0:d18dff347122 | 30 | |
gulchi | 3:ebea8e061ae6 | 31 | //#define DEBUG |
okini3939 | 0:d18dff347122 | 32 | |
gulchi | 2:fcd20e2cd110 | 33 | #define MODBUS_TCP_PORT 502 |
gulchi | 3:ebea8e061ae6 | 34 | #define MODBUS_MAX_CLIENTS 3 |
gulchi | 2:fcd20e2cd110 | 35 | #define MODBUS_KEEPALIVE 10 |
gulchi | 3:ebea8e061ae6 | 36 | #define MODBUS_TIMEOUT 50000 |
gulchi | 3:ebea8e061ae6 | 37 | #define MODBUS_MAX_HANDLES 2 |
gulchi | 3:ebea8e061ae6 | 38 | |
gulchi | 3:ebea8e061ae6 | 39 | #define MODBUS_MAX_REGISTER 256 |
gulchi | 3:ebea8e061ae6 | 40 | |
okini3939 | 0:d18dff347122 | 41 | |
gulchi | 2:fcd20e2cd110 | 42 | #define MODBUS_CMD_SIZE 100 |
gulchi | 3:ebea8e061ae6 | 43 | #define MODBUS_BUF_SIZE 128 |
gulchi | 2:fcd20e2cd110 | 44 | #define MODBUS_STACK_SIZE (1024 * 6) |
gulchi | 3:ebea8e061ae6 | 45 | //efine HTTPD_ENABLE_CLOSER |
okini3939 | 0:d18dff347122 | 46 | |
okini3939 | 0:d18dff347122 | 47 | //Debug is disabled by default |
okini3939 | 0:d18dff347122 | 48 | #if defined(DEBUG) and (!defined(TARGET_LPC11U24)) |
okini3939 | 0:d18dff347122 | 49 | #define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__); |
okini3939 | 0:d18dff347122 | 50 | #define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__); |
okini3939 | 0:d18dff347122 | 51 | #define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__); |
okini3939 | 0:d18dff347122 | 52 | #define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__); |
okini3939 | 0:d18dff347122 | 53 | #else |
okini3939 | 0:d18dff347122 | 54 | #define DBG(x, ...) |
okini3939 | 0:d18dff347122 | 55 | #define WARN(x, ...) |
okini3939 | 0:d18dff347122 | 56 | #define ERR(x, ...) |
okini3939 | 0:d18dff347122 | 57 | #define INFO(x, ...) |
okini3939 | 0:d18dff347122 | 58 | #endif |
okini3939 | 0:d18dff347122 | 59 | |
okini3939 | 0:d18dff347122 | 60 | |
gulchi | 2:fcd20e2cd110 | 61 | class Modbus { |
okini3939 | 0:d18dff347122 | 62 | public: |
okini3939 | 0:d18dff347122 | 63 | |
okini3939 | 0:d18dff347122 | 64 | struct STATE { |
okini3939 | 0:d18dff347122 | 65 | Thread *thread; |
dgriffin65 | 1:b724fdb741e7 | 66 | TCPSocket *client; |
gulchi | 3:ebea8e061ae6 | 67 | //volatile Request req; |
gulchi | 3:ebea8e061ae6 | 68 | //CircBuffer <char>*buf; |
gulchi | 2:fcd20e2cd110 | 69 | |
gulchi | 3:ebea8e061ae6 | 70 | char start_message[8]; |
gulchi | 2:fcd20e2cd110 | 71 | |
okini3939 | 0:d18dff347122 | 72 | }; |
okini3939 | 0:d18dff347122 | 73 | |
gulchi | 2:fcd20e2cd110 | 74 | Modbus (); |
okini3939 | 0:d18dff347122 | 75 | |
gulchi | 3:ebea8e061ae6 | 76 | int start (EthernetInterface *ns, int port = MODBUS_TCP_PORT); |
okini3939 | 0:d18dff347122 | 77 | |
okini3939 | 0:d18dff347122 | 78 | // --- HTTPD_req.cpp --- |
okini3939 | 0:d18dff347122 | 79 | void httpdError (int id, int err); |
okini3939 | 0:d18dff347122 | 80 | |
gulchi | 2:fcd20e2cd110 | 81 | |
okini3939 | 0:d18dff347122 | 82 | // --- HTTPD_util.cpp --- |
dgriffin65 | 1:b724fdb741e7 | 83 | TCPSocket *getClientSocket(int id) { |
gulchi | 3:ebea8e061ae6 | 84 | if (id >= MODBUS_MAX_CLIENTS) return NULL; |
dgriffin65 | 1:b724fdb741e7 | 85 | return _state[id].client; |
dgriffin65 | 1:b724fdb741e7 | 86 | } |
gulchi | 3:ebea8e061ae6 | 87 | int send (int id, unsigned short buffer[], int len); |
gulchi | 3:ebea8e061ae6 | 88 | |
gulchi | 3:ebea8e061ae6 | 89 | int attachRead (void (*funcCgi)(int id, unsigned short start_address, unsigned short length), int startRegister = 0); |
okini3939 | 0:d18dff347122 | 90 | |
gulchi | 2:fcd20e2cd110 | 91 | static Modbus * getInstance() { |
okini3939 | 0:d18dff347122 | 92 | return _inst; |
okini3939 | 0:d18dff347122 | 93 | }; |
okini3939 | 0:d18dff347122 | 94 | |
okini3939 | 0:d18dff347122 | 95 | private : |
gulchi | 2:fcd20e2cd110 | 96 | static Modbus *_inst; |
okini3939 | 0:d18dff347122 | 97 | Thread *_daemon; |
dgriffin65 | 1:b724fdb741e7 | 98 | TCPServer _server; |
dgriffin65 | 1:b724fdb741e7 | 99 | |
gulchi | 3:ebea8e061ae6 | 100 | EthernetInterface *m_ns; |
okini3939 | 0:d18dff347122 | 101 | |
gulchi | 2:fcd20e2cd110 | 102 | #ifdef MODBUS_ENABLE_CLOSER |
gulchi | 2:fcd20e2cd110 | 103 | struct STATE _state[MODBUS_MAX_CLIENTS + 1]; |
okini3939 | 0:d18dff347122 | 104 | #else |
gulchi | 2:fcd20e2cd110 | 105 | struct STATE _state[MODBUS_MAX_CLIENTS]; |
okini3939 | 0:d18dff347122 | 106 | #endif |
okini3939 | 0:d18dff347122 | 107 | |
okini3939 | 0:d18dff347122 | 108 | struct HANDLER { |
gulchi | 3:ebea8e061ae6 | 109 | int startRegister; |
gulchi | 3:ebea8e061ae6 | 110 | void (*funcCgiRead)(int id, unsigned short start_address, unsigned short length); |
gulchi | 3:ebea8e061ae6 | 111 | void (*funcCgiWrite)(int id); |
gulchi | 2:fcd20e2cd110 | 112 | } _handler[MODBUS_MAX_HANDLES]; |
okini3939 | 0:d18dff347122 | 113 | |
okini3939 | 0:d18dff347122 | 114 | int _handler_count; |
okini3939 | 0:d18dff347122 | 115 | |
dgriffin65 | 1:b724fdb741e7 | 116 | static void daemon (); |
okini3939 | 0:d18dff347122 | 117 | static void child (void const *arg); |
okini3939 | 0:d18dff347122 | 118 | static void closer (void const *arg); |
okini3939 | 0:d18dff347122 | 119 | |
okini3939 | 0:d18dff347122 | 120 | // --- HTTPD_req.cpp --- |
gulchi | 3:ebea8e061ae6 | 121 | |
gulchi | 3:ebea8e061ae6 | 122 | void recvData (int id, char buf[], int size); |
gulchi | 3:ebea8e061ae6 | 123 | int mb_exception_handler(int id, char data_in[], unsigned short length, char error); |
okini3939 | 0:d18dff347122 | 124 | |
gulchi | 2:fcd20e2cd110 | 125 | |
okini3939 | 0:d18dff347122 | 126 | |
okini3939 | 0:d18dff347122 | 127 | // --- HTTPD_util.cpp --- |
gulchi | 3:ebea8e061ae6 | 128 | int getHandler (int startRegister); |
gulchi | 3:ebea8e061ae6 | 129 | int sendToClient(int id, char buffer[], unsigned short len); |
okini3939 | 0:d18dff347122 | 130 | int from_hex (int ch); |
okini3939 | 0:d18dff347122 | 131 | int to_hex (int code); |
okini3939 | 0:d18dff347122 | 132 | }; |
okini3939 | 0:d18dff347122 | 133 | |
okini3939 | 0:d18dff347122 | 134 | #endif |