Commit ea10dfe97be2741f3aa78a2b2731cc0199a4f874
1 parent
40939880
Fixed TCP Connection
Showing
4 changed files
with
61 additions
and
10 deletions
src/connectionconfig.h
| ... | ... | @@ -6,6 +6,8 @@ |
| 6 | 6 | */ |
| 7 | 7 | #pragma once |
| 8 | 8 | |
| 9 | +#include <string> | |
| 10 | + | |
| 9 | 11 | namespace osdev { |
| 10 | 12 | namespace components { |
| 11 | 13 | namespace modbus { |
| ... | ... | @@ -13,7 +15,53 @@ namespace modbus { |
| 13 | 15 | class ConnectionConfig |
| 14 | 16 | { |
| 15 | 17 | public: |
| 18 | + enum class E_CONNECTIONTYPE | |
| 19 | + { | |
| 20 | + SERIAL, | |
| 21 | + TCP, | |
| 22 | + UNKNOWN | |
| 23 | + }; | |
| 24 | + | |
| 25 | + enum class E_PARITY | |
| 26 | + { | |
| 27 | + ODD, | |
| 28 | + EVEN, | |
| 29 | + NONE | |
| 30 | + }; | |
| 31 | + | |
| 32 | + enum class E_BAUDRATE | |
| 33 | + { | |
| 34 | + R1200 = 1200, | |
| 35 | + R2400 = 2400, | |
| 36 | + R4800 = 4800, | |
| 37 | + R9600 = 9600, | |
| 38 | + R19200 = 19200, | |
| 39 | + R38400 = 38400, | |
| 40 | + R57600 = 57600, | |
| 41 | + R76800 = 76800, | |
| 42 | + R115200 = 115200 | |
| 43 | + }; | |
| 44 | + | |
| 16 | 45 | ConnectionConfig(); |
| 46 | + | |
| 47 | + // Getters and Setters | |
| 48 | + void setConnectionType( E_CONNECTIONTYPE type ) { m_conType = type; } | |
| 49 | + E_CONNECTIONTYPE getConnectionType() const { return m_conType; } | |
| 50 | + void setBaudRate( E_BAUDRATE baud_rate ) { m_baudRate = baud_rate; } | |
| 51 | + E_BAUDRATE getBaudRate() const { return m_baudRate; } | |
| 52 | + void setParity(E_PARITY parity) { m_parity = parity; } | |
| 53 | + E_PARITY getParity() const { return m_parity; } | |
| 54 | + | |
| 55 | +private: | |
| 56 | + E_CONNECTIONTYPE m_conType = E_CONNECTIONTYPE::UNKNOWN; | |
| 57 | + E_BAUDRATE m_baudRate = E_BAUDRATE::R9600; | |
| 58 | + E_PARITY m_parity = E_PARITY::NONE; | |
| 59 | + int m_dataBits = 8; | |
| 60 | + int m_stopBits = 1; | |
| 61 | + std::string m_ipAddress{}; | |
| 62 | + unsigned int m_portNUmber = 502; | |
| 63 | + int m_frameTimeOut = -1; | |
| 64 | + | |
| 17 | 65 | }; |
| 18 | 66 | |
| 19 | 67 | } // End namespace modbus | ... | ... |
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[m_max_message_length]; | |
| 23 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 24 | 24 | ssize_t result = modbusReceive(to_rec); |
| 25 | 25 | if (result == -1) |
| 26 | 26 | { |
| ... | ... | @@ -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[m_max_message_length]; | |
| 61 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 62 | 62 | ssize_t result = modbusReceive(to_rec); |
| 63 | 63 | if (result == -1) |
| 64 | 64 | { |
| ... | ... | @@ -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[m_max_message_length]; // <-- Transport layer dependent .. ? | |
| 92 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 93 | 93 | ssize_t result = modbusReceive(to_rec); |
| 94 | 94 | if (result == -1) |
| 95 | 95 | { |
| ... | ... | @@ -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[m_max_message_length]; | |
| 123 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 124 | 124 | ssize_t result = modbusReceive(to_rec); |
| 125 | 125 | if (result == -1) |
| 126 | 126 | { |
| ... | ... | @@ -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[m_max_message_length]; | |
| 157 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 158 | 158 | ssize_t result = modbusReceive(to_rec); |
| 159 | 159 | if (result == -1) |
| 160 | 160 | { |
| ... | ... | @@ -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[m_max_message_length]; | |
| 184 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 185 | 185 | ssize_t result = modbusReceive(to_rec); |
| 186 | 186 | if (result == -1) |
| 187 | 187 | { |
| ... | ... | @@ -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[m_max_message_length]; | |
| 219 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 220 | 220 | ssize_t result = modbusReceive(to_rec); |
| 221 | 221 | if (result == -1) |
| 222 | 222 | { |
| ... | ... | @@ -243,7 +243,7 @@ int ModbusBase::writeRegisters(uint16_t address, uint16_t amount, const uint16_t |
| 243 | 243 | if (m_connected) |
| 244 | 244 | { |
| 245 | 245 | modbusWrite(address ,amount, WRITE_REGS, value); |
| 246 | - uint8_t to_rec[MAX_MSG_LENGTH]; | |
| 246 | + uint8_t *to_rec = new uint8_t[m_max_message_length]; | |
| 247 | 247 | ssize_t result = modbusReceive(to_rec); |
| 248 | 248 | if (result == -1) |
| 249 | 249 | { | ... | ... |
src/modbustcp.cpp
src/modbustcp.h
| ... | ... | @@ -26,6 +26,9 @@ using SOCKADDR_IN = struct sockaddr_in; |
| 26 | 26 | #define X_CLOSE_SOCKET(s) close(s) |
| 27 | 27 | #define X_ISCONNECTSUCCEED(s) ((s) >= 0) |
| 28 | 28 | |
| 29 | +/*! | |
| 30 | + * \brief The ModbusTCP class | |
| 31 | + */ | |
| 29 | 32 | class ModbusTCP : public ModbusBase |
| 30 | 33 | { |
| 31 | 34 | public: |
| ... | ... | @@ -65,7 +68,7 @@ private: // Methods |
| 65 | 68 | |
| 66 | 69 | private: // Members |
| 67 | 70 | uint16_t m_port {}; |
| 68 | - std::string m_host; | |
| 71 | + std::string m_host{}; | |
| 69 | 72 | X_SOCKET m_socket{}; |
| 70 | 73 | SOCKADDR_IN m_server{}; |
| 71 | 74 | }; | ... | ... |