ModBusTCP with some fixes
ModBus_util.cpp@3:ebea8e061ae6, 2017-06-30 (annotated)
- Committer:
- gulchi
- Date:
- Fri Jun 30 08:17:01 2017 +0000
- Revision:
- 3:ebea8e061ae6
Working ModBusTCP Interface with Mbed-os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gulchi | 3:ebea8e061ae6 | 1 | /* Copyright (C) 2013 Hiroshi Suga, MIT License |
gulchi | 3:ebea8e061ae6 | 2 | * |
gulchi | 3:ebea8e061ae6 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gulchi | 3:ebea8e061ae6 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gulchi | 3:ebea8e061ae6 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gulchi | 3:ebea8e061ae6 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gulchi | 3:ebea8e061ae6 | 7 | * furnished to do so, subject to the following conditions: |
gulchi | 3:ebea8e061ae6 | 8 | * |
gulchi | 3:ebea8e061ae6 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gulchi | 3:ebea8e061ae6 | 10 | * substantial portions of the Software. |
gulchi | 3:ebea8e061ae6 | 11 | * |
gulchi | 3:ebea8e061ae6 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gulchi | 3:ebea8e061ae6 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gulchi | 3:ebea8e061ae6 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gulchi | 3:ebea8e061ae6 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gulchi | 3:ebea8e061ae6 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gulchi | 3:ebea8e061ae6 | 17 | */ |
gulchi | 3:ebea8e061ae6 | 18 | |
gulchi | 3:ebea8e061ae6 | 19 | #include "ModBus-TCP.h" |
gulchi | 3:ebea8e061ae6 | 20 | #include <ctype.h> |
gulchi | 3:ebea8e061ae6 | 21 | #include <stdlib.h> |
gulchi | 3:ebea8e061ae6 | 22 | #include <string.h> |
gulchi | 3:ebea8e061ae6 | 23 | |
gulchi | 3:ebea8e061ae6 | 24 | |
gulchi | 3:ebea8e061ae6 | 25 | |
gulchi | 3:ebea8e061ae6 | 26 | int Modbus::getHandler (int startRegister) { |
gulchi | 3:ebea8e061ae6 | 27 | int i; |
gulchi | 3:ebea8e061ae6 | 28 | |
gulchi | 3:ebea8e061ae6 | 29 | for (i = 0; i < _handler_count; i ++) { |
gulchi | 3:ebea8e061ae6 | 30 | if (_handler[i].startRegister == startRegister) { |
gulchi | 3:ebea8e061ae6 | 31 | // found |
gulchi | 3:ebea8e061ae6 | 32 | return i; |
gulchi | 3:ebea8e061ae6 | 33 | } |
gulchi | 3:ebea8e061ae6 | 34 | } |
gulchi | 3:ebea8e061ae6 | 35 | return -1; |
gulchi | 3:ebea8e061ae6 | 36 | } |
gulchi | 3:ebea8e061ae6 | 37 | |
gulchi | 3:ebea8e061ae6 | 38 | |
gulchi | 3:ebea8e061ae6 | 39 | int Modbus::attachRead (void (*funcCgi)(int, unsigned short, unsigned short), int startRegister) { |
gulchi | 3:ebea8e061ae6 | 40 | if (_handler_count < MODBUS_MAX_HANDLES) { |
gulchi | 3:ebea8e061ae6 | 41 | _handler[_handler_count].startRegister = startRegister; |
gulchi | 3:ebea8e061ae6 | 42 | _handler[_handler_count].funcCgiRead = funcCgi; |
gulchi | 3:ebea8e061ae6 | 43 | _handler_count ++; |
gulchi | 3:ebea8e061ae6 | 44 | return 0; |
gulchi | 3:ebea8e061ae6 | 45 | } else { |
gulchi | 3:ebea8e061ae6 | 46 | return -1; |
gulchi | 3:ebea8e061ae6 | 47 | } |
gulchi | 3:ebea8e061ae6 | 48 | } |
gulchi | 3:ebea8e061ae6 | 49 | |
gulchi | 3:ebea8e061ae6 | 50 | |
gulchi | 3:ebea8e061ae6 | 51 | int Modbus::send (int id, unsigned short buffer[], int len) { |
gulchi | 3:ebea8e061ae6 | 52 | |
gulchi | 3:ebea8e061ae6 | 53 | char out[MODBUS_BUF_SIZE]; |
gulchi | 3:ebea8e061ae6 | 54 | |
gulchi | 3:ebea8e061ae6 | 55 | |
gulchi | 3:ebea8e061ae6 | 56 | // Build MBAP Header |
gulchi | 3:ebea8e061ae6 | 57 | //Transaction Number :: Copy from Org message |
gulchi | 3:ebea8e061ae6 | 58 | out[0] = _state[id].start_message[0]; |
gulchi | 3:ebea8e061ae6 | 59 | out[1] = _state[id].start_message[1]; |
gulchi | 3:ebea8e061ae6 | 60 | |
gulchi | 3:ebea8e061ae6 | 61 | //Protcoll Identifier :: Always 0x0000 |
gulchi | 3:ebea8e061ae6 | 62 | out[2] = 0x00; |
gulchi | 3:ebea8e061ae6 | 63 | out[3] = 0x00; |
gulchi | 3:ebea8e061ae6 | 64 | |
gulchi | 3:ebea8e061ae6 | 65 | // Length (bytes to follow after length bytes) of TCP Frame |
gulchi | 3:ebea8e061ae6 | 66 | out[4] = (char)(((len*2)+3)>> 8); |
gulchi | 3:ebea8e061ae6 | 67 | out[5] = (char)(((len*2)+3) & 0x00ff); |
gulchi | 3:ebea8e061ae6 | 68 | |
gulchi | 3:ebea8e061ae6 | 69 | //unit identifier (normally 0xff, but 0x00 is allowed |
gulchi | 3:ebea8e061ae6 | 70 | out[6] = 0xff; |
gulchi | 3:ebea8e061ae6 | 71 | |
gulchi | 3:ebea8e061ae6 | 72 | //MBAP Header build finished |
gulchi | 3:ebea8e061ae6 | 73 | |
gulchi | 3:ebea8e061ae6 | 74 | // Now the ModBus response |
gulchi | 3:ebea8e061ae6 | 75 | //Function code :: Copy from org message |
gulchi | 3:ebea8e061ae6 | 76 | out[7] = _state[id].start_message[7]; |
gulchi | 3:ebea8e061ae6 | 77 | |
gulchi | 3:ebea8e061ae6 | 78 | //Byte count of Modbus response |
gulchi | 3:ebea8e061ae6 | 79 | out[8] = len*2; |
gulchi | 3:ebea8e061ae6 | 80 | |
gulchi | 3:ebea8e061ae6 | 81 | for(int i=0; i<len; i++) { |
gulchi | 3:ebea8e061ae6 | 82 | out[(i*2)+9] = (char)(buffer[i] >> 8); |
gulchi | 3:ebea8e061ae6 | 83 | out[(i*2)+10] = (char)(buffer[i] & 0x00ff); |
gulchi | 3:ebea8e061ae6 | 84 | } |
gulchi | 3:ebea8e061ae6 | 85 | |
gulchi | 3:ebea8e061ae6 | 86 | |
gulchi | 3:ebea8e061ae6 | 87 | |
gulchi | 3:ebea8e061ae6 | 88 | DBG("sending anser on child %i", id); |
gulchi | 3:ebea8e061ae6 | 89 | return _state[id].client->send(out, (len*2)+9); |
gulchi | 3:ebea8e061ae6 | 90 | } |
gulchi | 3:ebea8e061ae6 | 91 | |
gulchi | 3:ebea8e061ae6 | 92 | int Modbus::sendToClient(int id, char buffer[], unsigned short len) { |
gulchi | 3:ebea8e061ae6 | 93 | return _state[id].client->send(buffer, len); |
gulchi | 3:ebea8e061ae6 | 94 | } |
gulchi | 3:ebea8e061ae6 | 95 | |
gulchi | 3:ebea8e061ae6 | 96 | |
gulchi | 3:ebea8e061ae6 | 97 | int Modbus::from_hex (int ch) { |
gulchi | 3:ebea8e061ae6 | 98 | return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; |
gulchi | 3:ebea8e061ae6 | 99 | } |
gulchi | 3:ebea8e061ae6 | 100 | |
gulchi | 3:ebea8e061ae6 | 101 | int Modbus::to_hex (int code) { |
gulchi | 3:ebea8e061ae6 | 102 | static char hex[] = "0123456789abcdef"; |
gulchi | 3:ebea8e061ae6 | 103 | return hex[code & 15]; |
gulchi | 3:ebea8e061ae6 | 104 | } |