Commit 40939880272e963303885af7c068e7c7a09ee030
1 parent
0be34396
Fixed TCP Connection
Showing
4 changed files
with
33 additions
and
35 deletions
src/CMakeLists.txt
| ... | ... | @@ -11,8 +11,8 @@ set(SRC_LIST |
| 11 | 11 | ${CMAKE_CURRENT_SOURCE_DIR}/modbusbase.cpp |
| 12 | 12 | ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.h |
| 13 | 13 | ${CMAKE_CURRENT_SOURCE_DIR}/modbustcp.cpp |
| 14 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.h | |
| 15 | - ${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.cpp | |
| 14 | + #${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.h | |
| 15 | + #${CMAKE_CURRENT_SOURCE_DIR}/modbusrtu.cpp | |
| 16 | 16 | ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.h |
| 17 | 17 | ${CMAKE_CURRENT_SOURCE_DIR}/connectionconfig.cpp |
| 18 | 18 | ) | ... | ... |
src/modbusbase.cpp
| ... | ... | @@ -20,7 +20,7 @@ int ModbusBase::readCoils(uint16_t address, uint16_t amount, bool *buffer) |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | modbusRead(address, amount, READ_COILS); |
| 23 | - uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 23 | + uint8_t to_rec[m_max_message_length]; | |
| 24 | 24 | ssize_t result = modbusReceive(to_rec); |
| 25 | 25 | if (result == -1) |
| 26 | 26 | { |
| ... | ... | @@ -29,9 +29,9 @@ int ModbusBase::readCoils(uint16_t address, uint16_t amount, bool *buffer) |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | modbusErrorHandle(to_rec, READ_COILS); |
| 32 | - if (err) | |
| 32 | + if (m_error) | |
| 33 | 33 | { |
| 34 | - return err_no; | |
| 34 | + return m_error_number; | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | for (auto i = 0; i < amount; i++) |
| ... | ... | @@ -58,7 +58,7 @@ int ModbusBase::readInputBits(uint16_t address, uint16_t amount, bool *buffer) |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | modbusRead(address, amount, READ_INPUT_BITS); |
| 61 | - uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 61 | + uint8_t to_rec[m_max_message_length]; | |
| 62 | 62 | ssize_t result = modbusReceive(to_rec); |
| 63 | 63 | if (result == -1) |
| 64 | 64 | { |
| ... | ... | @@ -66,9 +66,9 @@ int ModbusBase::readInputBits(uint16_t address, uint16_t amount, bool *buffer) |
| 66 | 66 | return BAD_CON; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - if (err) | |
| 69 | + if (m_error) | |
| 70 | 70 | { |
| 71 | - return err_no; | |
| 71 | + return m_error_number; | |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | for (auto i = 0; i < amount; i++) |
| ... | ... | @@ -89,7 +89,7 @@ int ModbusBase::readHoldingRegisters(uint16_t address, uint16_t amount, uint16_t |
| 89 | 89 | if (m_connected) |
| 90 | 90 | { |
| 91 | 91 | modbusRead(address, amount, READ_REGS); |
| 92 | - uint8_t to_rec[MAX_MSG_LENGTH]; // <-- Transport layer dependent .. ? | |
| 92 | + uint8_t to_rec[m_max_message_length]; // <-- Transport layer dependent .. ? | |
| 93 | 93 | ssize_t result = modbusReceive(to_rec); |
| 94 | 94 | if (result == -1) |
| 95 | 95 | { |
| ... | ... | @@ -97,9 +97,9 @@ int ModbusBase::readHoldingRegisters(uint16_t address, uint16_t amount, uint16_t |
| 97 | 97 | return BAD_CON; |
| 98 | 98 | } |
| 99 | 99 | modbusErrorHandle(to_rec, READ_REGS); |
| 100 | - if (err) | |
| 100 | + if (m_error) | |
| 101 | 101 | { |
| 102 | - return err_no; | |
| 102 | + return m_error_number; | |
| 103 | 103 | } |
| 104 | 104 | for (auto i = 0; i < amount; i++) |
| 105 | 105 | { |
| ... | ... | @@ -120,7 +120,7 @@ int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t * |
| 120 | 120 | if (m_connected) |
| 121 | 121 | { |
| 122 | 122 | modbusRead(address, amount, READ_INPUT_REGS); |
| 123 | - uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 123 | + uint8_t to_rec[m_max_message_length]; | |
| 124 | 124 | ssize_t result = modbusReceive(to_rec); |
| 125 | 125 | if (result == -1) |
| 126 | 126 | { |
| ... | ... | @@ -129,9 +129,9 @@ int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t * |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | modbusErrorHandle(to_rec, READ_INPUT_REGS); |
| 132 | - if (err) | |
| 132 | + if (m_error) | |
| 133 | 133 | { |
| 134 | - return err_no; | |
| 134 | + return m_error_number; | |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | for (auto i = 0; i < amount; i++) |
| ... | ... | @@ -154,7 +154,7 @@ int ModbusBase::writeCoil(uint16_t address, const bool &to_write) |
| 154 | 154 | { |
| 155 | 155 | int value = to_write * 0xFF00; |
| 156 | 156 | modbusWrite(address, 1, WRITE_COIL, reinterpret_cast<uint16_t *>(&value)); |
| 157 | - uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 157 | + uint8_t to_rec[m_max_message_length]; | |
| 158 | 158 | ssize_t result = modbusReceive(to_rec); |
| 159 | 159 | if (result == -1) |
| 160 | 160 | { |
| ... | ... | @@ -163,9 +163,9 @@ int ModbusBase::writeCoil(uint16_t address, const bool &to_write) |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | modbusErrorHandle(to_rec, WRITE_COIL); |
| 166 | - if (err) | |
| 166 | + if (m_error) | |
| 167 | 167 | { |
| 168 | - return err_no; | |
| 168 | + return m_error_number; | |
| 169 | 169 | } |
| 170 | 170 | return 0; |
| 171 | 171 | } |
| ... | ... | @@ -181,7 +181,7 @@ int ModbusBase::writeRegister(uint16_t address, const uint16_t &value) |
| 181 | 181 | if (m_connected) |
| 182 | 182 | { |
| 183 | 183 | modbusWrite(address, 1, WRITE_REG, &value); |
| 184 | - uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 184 | + uint8_t to_rec[m_max_message_length]; | |
| 185 | 185 | ssize_t result = modbusReceive(to_rec); |
| 186 | 186 | if (result == -1) |
| 187 | 187 | { |
| ... | ... | @@ -190,9 +190,9 @@ int ModbusBase::writeRegister(uint16_t address, const uint16_t &value) |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | modbusErrorHandle(to_rec, WRITE_COIL); |
| 193 | - if (err) | |
| 193 | + if (m_error) | |
| 194 | 194 | { |
| 195 | - return err_no; | |
| 195 | + return m_error_number; | |
| 196 | 196 | } |
| 197 | 197 | return 0; |
| 198 | 198 | } |
| ... | ... | @@ -216,7 +216,7 @@ int ModbusBase::writeCoils(uint16_t address, uint16_t amount, const bool *value) |
| 216 | 216 | modbusWrite(address, amount, WRITE_COILS, temp); |
| 217 | 217 | delete[] temp; |
| 218 | 218 | |
| 219 | - uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 219 | + uint8_t to_rec[m_max_message_length]; | |
| 220 | 220 | ssize_t result = modbusReceive(to_rec); |
| 221 | 221 | if (result == -1) |
| 222 | 222 | { |
| ... | ... | @@ -225,9 +225,9 @@ int ModbusBase::writeCoils(uint16_t address, uint16_t amount, const bool *value) |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | modbusErrorHandle(to_rec, WRITE_COILS); |
| 228 | - if (err) | |
| 228 | + if (m_error) | |
| 229 | 229 | { |
| 230 | - return err_no; | |
| 230 | + return m_error_number; | |
| 231 | 231 | } |
| 232 | 232 | return 0; |
| 233 | 233 | } |
| ... | ... | @@ -252,9 +252,9 @@ int ModbusBase::writeRegisters(uint16_t address, uint16_t amount, const uint16_t |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | modbusErrorHandle(to_rec, WRITE_COILS); |
| 255 | - if (err) | |
| 255 | + if (m_error) | |
| 256 | 256 | { |
| 257 | - return err_no; | |
| 257 | + return m_error_number; | |
| 258 | 258 | } |
| 259 | 259 | return 0; |
| 260 | 260 | } | ... | ... |
src/modbusbase.h
| ... | ... | @@ -20,9 +20,6 @@ |
| 20 | 20 | #define LOG(...) (void)0 |
| 21 | 21 | #endif |
| 22 | 22 | |
| 23 | -#define MAX_MSG_LENGTH_TCP 260 | |
| 24 | -#define MAX_MSG_LENGTH_RTU 252 | |
| 25 | - | |
| 26 | 23 | // Function Codes |
| 27 | 24 | #define READ_COILS 0x01 |
| 28 | 25 | #define READ_INPUT_BITS 0x02 |
| ... | ... | @@ -150,6 +147,8 @@ public: |
| 150 | 147 | bool getError() const { return m_error; } |
| 151 | 148 | int getErrorNumber() const { return m_error_number; } |
| 152 | 149 | std::string getErrorMessage() const { return m_error_message;} |
| 150 | + void setMaxMessageLength(unsigned int max_message_length) { m_max_message_length = max_message_length; } | |
| 151 | + unsigned int getMaxMessageLength() const { return m_max_message_length; } | |
| 153 | 152 | |
| 154 | 153 | private: // Methods |
| 155 | 154 | /*! |
| ... | ... | @@ -212,6 +211,7 @@ private: // Members (Giggity!) |
| 212 | 211 | bool m_error{}; |
| 213 | 212 | int m_error_number{}; |
| 214 | 213 | std::string m_error_message; |
| 214 | + unsigned int m_max_message_length; | |
| 215 | 215 | |
| 216 | 216 | }; |
| 217 | 217 | ... | ... |
src/modbustcp.cpp
| ... | ... | @@ -50,20 +50,18 @@ bool ModbusTCP::Connect() |
| 50 | 50 | |
| 51 | 51 | void ModbusTCP::Close() |
| 52 | 52 | { |
| 53 | - | |
| 54 | -} | |
| 55 | - | |
| 56 | -bool ModbusTCP::isConnected() const | |
| 57 | -{ | |
| 58 | - | |
| 53 | + X_CLOSE_SOCKET(m_socket); | |
| 59 | 54 | } |
| 60 | 55 | |
| 61 | 56 | ssize_t ModbusTCP::modbusSend(uint8_t *to_send, size_t length) |
| 62 | 57 | { |
| 58 | + uint32_t message_id = getMessageId(); | |
| 59 | + setMessageId(message_id++); | |
| 63 | 60 | |
| 61 | + return send(m_socket, reinterpret_cast<const char *>(to_send), static_cast<size_t>(length), 0); | |
| 64 | 62 | } |
| 65 | 63 | |
| 66 | 64 | ssize_t ModbusTCP::modbusReceive(uint8_t *buffer) const |
| 67 | 65 | { |
| 68 | - | |
| 66 | + return recv(m_socket, reinterpret_cast<char *>(buffer), getMaxMessageLength(), 0); | |
| 69 | 67 | } | ... | ... |