Commit a457c65874d5f8c7f1b42d9ceee7aebb484df4fb
1 parent
fac78288
Implmenting ModbusBase class
Showing
2 changed files
with
115 additions
and
1 deletions
src/modbusbase.cpp
| ... | ... | @@ -118,7 +118,7 @@ int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t * |
| 118 | 118 | if (m_connected) |
| 119 | 119 | { |
| 120 | 120 | modbusRead(address, amount, READ_INPUT_REGS); |
| 121 | - uint8_t to_rec[MSG_MAX_LENGTH]; | |
| 121 | + uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 122 | 122 | size_t result = modbusReceive(to_rec); |
| 123 | 123 | if (result == -1) |
| 124 | 124 | { |
| ... | ... | @@ -151,22 +151,116 @@ int ModbusBase::writeCoil(uint16_t address, const bool &to_write) |
| 151 | 151 | if (m_connected) |
| 152 | 152 | { |
| 153 | 153 | int value = to_write * 0xFF00; |
| 154 | + modbusWrite(address, 1, WRITE_COIL, (uint16_t *)&value); | |
| 155 | + uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 156 | + ssize_t result = modbusReceive(to_rec); | |
| 157 | + if (result == -1) | |
| 158 | + { | |
| 159 | + setBadConnection(); | |
| 160 | + return BAD_CON; | |
| 161 | + } | |
| 162 | + | |
| 163 | + modbusErrorHandle(to_rec, WRITE_COIL); | |
| 164 | + if (err) | |
| 165 | + { | |
| 166 | + return err_no; | |
| 167 | + } | |
| 168 | + return 0; | |
| 169 | + } | |
| 170 | + else | |
| 171 | + { | |
| 172 | + setBadConnection(); | |
| 173 | + return BAD_CON; | |
| 154 | 174 | } |
| 155 | 175 | } |
| 156 | 176 | |
| 157 | 177 | int ModbusBase::writeRegister(uint16_t address, const uint16_t &value) |
| 158 | 178 | { |
| 179 | + if (m_connected) | |
| 180 | + { | |
| 181 | + modbusWrite(address, 1, WRITE_REG, &value); | |
| 182 | + uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 183 | + ssize_t result = modebusReceive(to_rec); | |
| 184 | + if (result == -1) | |
| 185 | + { | |
| 186 | + setBadConnection(); | |
| 187 | + return BAD_CON; | |
| 188 | + } | |
| 159 | 189 | |
| 190 | + modbusErrorHandle(to_rec, WRITE_COIL); | |
| 191 | + if (err) | |
| 192 | + { | |
| 193 | + return err_no; | |
| 194 | + } | |
| 195 | + return 0; | |
| 196 | + } | |
| 197 | + else | |
| 198 | + { | |
| 199 | + setBadConnection(); | |
| 200 | + return BAD_CON; | |
| 201 | + } | |
| 160 | 202 | } |
| 161 | 203 | |
| 162 | 204 | int ModbusBase::writeCoils(uint16_t address, uint16_t amount, const bool *value) |
| 163 | 205 | { |
| 206 | + if (m_connected) | |
| 207 | + { | |
| 208 | + uint16_t *temp = new uint16_t[amount]; | |
| 209 | + for (int i = 0; i < amount; i++) | |
| 210 | + { | |
| 211 | + temp[i] = (uint16_t)value[i]; | |
| 212 | + } | |
| 213 | + | |
| 214 | + modbusWrite(address, amount, WRITE_COILS, temp); | |
| 215 | + delete[] temp; | |
| 164 | 216 | |
| 217 | + uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 218 | + ssize_t result = modbusReceive(to_rec); | |
| 219 | + if (result == -1) | |
| 220 | + { | |
| 221 | + setBadConnection(); | |
| 222 | + return BAD_CON; | |
| 223 | + } | |
| 224 | + | |
| 225 | + modbusErrorHandle(to_rec, WRITE_COILS); | |
| 226 | + if (err) | |
| 227 | + { | |
| 228 | + return err_no; | |
| 229 | + } | |
| 230 | + return 0; | |
| 231 | + } | |
| 232 | + else | |
| 233 | + { | |
| 234 | + setBadConnection(); | |
| 235 | + return BAD_CON; | |
| 236 | + } | |
| 165 | 237 | } |
| 166 | 238 | |
| 167 | 239 | int ModbusBase::writeRegisters(uint16_t address, uint16_t amount, const uint16_t *value) |
| 168 | 240 | { |
| 241 | + if (m_connected) | |
| 242 | + { | |
| 243 | + modbusWrite(address ,amount, WRITE_REGS, value); | |
| 244 | + uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 245 | + ssize_t result = modbusReceive(to_rec); | |
| 246 | + if (result == -1) | |
| 247 | + { | |
| 248 | + setBadConnection(); | |
| 249 | + return BAD_CON; | |
| 250 | + } | |
| 169 | 251 | |
| 252 | + modbusErrorHandle(to_rec, WRITE_COILS); | |
| 253 | + if (err) | |
| 254 | + { | |
| 255 | + return err_no; | |
| 256 | + } | |
| 257 | + return 0; | |
| 258 | + } | |
| 259 | + else | |
| 260 | + { | |
| 261 | + setBadConnection(); | |
| 262 | + return BAD_CON; | |
| 263 | + } | |
| 170 | 264 | } |
| 171 | 265 | |
| 172 | 266 | void ModbusBase::buildRequest(uint8_t *to_send, uint16_t address, int function_code) const | ... | ... |
src/modbusbase.h
| ... | ... | @@ -111,10 +111,30 @@ public: |
| 111 | 111 | */ |
| 112 | 112 | int writeCoil(uint16_t address, const bool &to_write); |
| 113 | 113 | |
| 114 | + /*! | |
| 115 | + * Write Single Register | |
| 116 | + * MODBUS Function 0x06 | |
| 117 | + * \param address - Reference Address | |
| 118 | + * \param value - Value to Be Written to Register | |
| 119 | + */ | |
| 114 | 120 | int writeRegister(uint16_t address, const uint16_t &value); |
| 115 | 121 | |
| 122 | + /*! | |
| 123 | + * Write Multiple Coils | |
| 124 | + * MODBUS Function 0x0F | |
| 125 | + * \param address - Reference Address | |
| 126 | + * \param amount - Amount of coils to write | |
| 127 | + * \param value - Values to Be Written to Coils | |
| 128 | + */ | |
| 116 | 129 | int writeCoils(uint16_t address, uint16_t amount, const bool *value); |
| 117 | 130 | |
| 131 | + /*! | |
| 132 | + * Write Multiple Registers | |
| 133 | + * MODBUS Function 0x10 | |
| 134 | + * \param address - Reference Address | |
| 135 | + * \param amount - Amount of Value to Write | |
| 136 | + * \param value - Values to Be Written to the Registers | |
| 137 | + */ | |
| 118 | 138 | int writeRegisters(uint16_t address, uint16_t amount, const uint16_t *value); |
| 119 | 139 | |
| 120 | 140 | private: // Methods | ... | ... |