|
| 1 | +/* |
| 2 | + This file is part of the Modbus library. |
| 3 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <errno.h> |
| 21 | + |
| 22 | +#include "ModbusClient.h" |
| 23 | + |
| 24 | +ModbusClient::ModbusClient() : |
| 25 | + _mb(NULL) |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +ModbusClient::~ModbusClient() |
| 30 | +{ |
| 31 | + if (_mb != NULL) { |
| 32 | + modbus_free(_mb); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +int ModbusClient::begin(modbus_t* mb) |
| 37 | +{ |
| 38 | + end(); |
| 39 | + |
| 40 | + _mb = mb; |
| 41 | + if (_mb == NULL) { |
| 42 | + return 0; |
| 43 | + } |
| 44 | + |
| 45 | + if (modbus_connect(_mb) != 0) { |
| 46 | + modbus_free(_mb); |
| 47 | + |
| 48 | + _mb = NULL; |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + return 1; |
| 53 | +} |
| 54 | + |
| 55 | +void ModbusClient::end() |
| 56 | +{ |
| 57 | + if (_mb != NULL) { |
| 58 | + modbus_close(_mb); |
| 59 | + modbus_free(_mb); |
| 60 | + |
| 61 | + _mb = NULL; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +void ModbusClient::setId(int id) |
| 66 | +{ |
| 67 | + modbus_set_slave(_mb, id); |
| 68 | +} |
| 69 | + |
| 70 | +int ModbusClient::readCoil(int address) |
| 71 | +{ |
| 72 | + uint8_t value; |
| 73 | + |
| 74 | + if (readCoils(address, &value, 1) < 0) { |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + return value; |
| 79 | +} |
| 80 | + |
| 81 | +int ModbusClient::readCoils(int address, uint8_t values[], int nb) |
| 82 | +{ |
| 83 | + if (modbus_read_bits(_mb, address, nb, values) < 0) { |
| 84 | + return 0; |
| 85 | + } |
| 86 | + |
| 87 | + return 1; |
| 88 | +} |
| 89 | + |
| 90 | +int ModbusClient::readDiscreteInput(int address) |
| 91 | +{ |
| 92 | + uint8_t value; |
| 93 | + |
| 94 | + if (!readDiscreteInputs(address, &value, 1)) { |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + return value; |
| 99 | +} |
| 100 | + |
| 101 | +int ModbusClient::readDiscreteInputs(int address, uint8_t values[], int nb) |
| 102 | +{ |
| 103 | + if (modbus_read_input_bits(_mb, address, nb, values) < 0) { |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + return 1; |
| 108 | +} |
| 109 | + |
| 110 | +long ModbusClient::readHoldingRegister(int address) |
| 111 | +{ |
| 112 | + uint16_t value; |
| 113 | + |
| 114 | + if (!readHoldingRegisters(address, &value, 1)) { |
| 115 | + return -1; |
| 116 | + } |
| 117 | + |
| 118 | + return value; |
| 119 | +} |
| 120 | + |
| 121 | +int ModbusClient::readHoldingRegisters(int address, uint16_t values[], int nb) |
| 122 | +{ |
| 123 | + if (modbus_read_registers(_mb, address, nb, values) < 0) { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + |
| 127 | + return 1; |
| 128 | +} |
| 129 | + |
| 130 | +long ModbusClient::readInputRegister(int address) |
| 131 | +{ |
| 132 | + uint16_t value; |
| 133 | + |
| 134 | + if (!readInputRegisters(address, &value, 1)) { |
| 135 | + return -1; |
| 136 | + } |
| 137 | + |
| 138 | + return value; |
| 139 | +} |
| 140 | + |
| 141 | +int ModbusClient::readInputRegisters(int address, uint16_t values[], int nb) |
| 142 | +{ |
| 143 | + if (modbus_read_input_registers(_mb, address, nb, values) < 0) { |
| 144 | + return 0; |
| 145 | + } |
| 146 | + |
| 147 | + return 1; |
| 148 | +} |
| 149 | + |
| 150 | +int ModbusClient::writeCoil(int address, uint8_t value) |
| 151 | +{ |
| 152 | + if (modbus_write_bit(_mb, address, value) < 0) { |
| 153 | + return 0; |
| 154 | + } |
| 155 | + |
| 156 | + return 1; |
| 157 | +} |
| 158 | + |
| 159 | +int ModbusClient::writeCoils(int address, const uint8_t values[], int nb) |
| 160 | +{ |
| 161 | + if (modbus_write_bits(_mb, address, nb, values) < 0) { |
| 162 | + return 0; |
| 163 | + } |
| 164 | + |
| 165 | + return 1; |
| 166 | +} |
| 167 | + |
| 168 | +int ModbusClient::writeRegister(int address, uint16_t value) |
| 169 | +{ |
| 170 | + if (modbus_write_register(_mb, address, value) < 0) { |
| 171 | + return 0; |
| 172 | + } |
| 173 | + |
| 174 | + return 1; |
| 175 | +} |
| 176 | + |
| 177 | +int ModbusClient::writeRegisters(int address, const uint16_t values[], int nb) |
| 178 | +{ |
| 179 | + if (modbus_write_registers(_mb, address, nb, values) < 0) { |
| 180 | + return 0; |
| 181 | + } |
| 182 | + |
| 183 | + return 1; |
| 184 | +} |
| 185 | + |
| 186 | +int ModbusClient::maskWriteRegister(int address, uint16_t andMask, uint16_t orMask) |
| 187 | +{ |
| 188 | + if (modbus_mask_write_register(_mb, address, andMask, orMask) < 0) { |
| 189 | + return 0; |
| 190 | + } |
| 191 | + |
| 192 | + return 1; |
| 193 | +} |
| 194 | + |
| 195 | +int ModbusClient::writeAndReadRegisters(int writeAddress, const uint16_t writeValues[], int writeNb, int readAddress, uint16_t readValues[], int readNb) |
| 196 | +{ |
| 197 | + if (modbus_write_and_read_registers(_mb, writeAddress, writeNb, writeValues, readAddress, readNb, readValues) < 0) { |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | + return 1; |
| 202 | +} |
| 203 | + |
| 204 | +const char* ModbusClient::lastError() |
| 205 | +{ |
| 206 | + if (errno == 0) { |
| 207 | + return NULL; |
| 208 | + } |
| 209 | + |
| 210 | + return modbus_strerror(errno); |
| 211 | +} |
0 commit comments