Commit e651a10fb00874c3dd93ef07429a16b5490a7d88

Authored by Peter M. Groen
1 parent cd824e9c

Implmenting ModbusBase class

src/modbusbase.cpp
... ... @@ -19,12 +19,43 @@ int ModbusBase::readInputBits(uint16_t address, uint16_t amount, bool *buffer)
19 19  
20 20 int ModbusBase::readHoldingRegisters(uint16_t address, uitn16_t amount, uint16_t *buffer)
21 21 {
22   -
  22 + if (m_connected)
  23 + {
  24 + modbusRead(address, amount, READ_REGS);
  25 + uint8_t to_rec[MAX_MSG_LENGTH]; // <-- Transport layer dependent .. ?
  26 + ssize_t result = modbusReceive(to_rec);
  27 + if (result == -1)
  28 + {
  29 + setBadConnection();
  30 + return BAD_CON;
  31 + }
  32 + modbusErrorHandle(to_rec, READ_REGS);
  33 + if (err)
  34 + {
  35 + return err_no;
  36 + }
  37 + for (auto i = 0; i < amount; i++)
  38 + {
  39 + buffer[i] = ((uint16_t)to_rec[9u + 2u * i]) << 8u;
  40 + buffer[i] += (uint16_t)to_rec[10u + 2u * i];
  41 + }
  42 + return 0;
  43 + }
  44 + else
  45 + {
  46 + setBadConnection();
  47 + return BAD_CON;
  48 + }
23 49 }
24 50  
25 51 int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t *buffer)
26 52 {
  53 + if (m_connected)
  54 + {
  55 + modbusRead(address, amount, READ_INPUT_REGS);
  56 + uint8_t to_rec[MSG_MAX_LENGTH];
27 57  
  58 + }
28 59 }
29 60  
30 61 int ModbusBase::writeCoil(uint16_t address, const bool &to_write)
... ... @@ -68,8 +99,9 @@ int ModbusBase::modbusRead(uint16_t address, uint16_t amount, int function_code)
68 99 uint8_t to_send[12];
69 100 buildRequest(to_send, address, function_code);
70 101 to_send[5] = 6;
71   - to_send[10] = (uint8_t)
72   - }
  102 + to_send[10] = (uint8_t)(amount >> 8u);
  103 + to_send[11] = (uint8_t)(amount & 0x00FFu);
  104 + return modbusSend(to_send, 12);
73 105 }
74 106  
75 107 int ModbusBase::modbusWrite(uint16_t address, uint16_t amount, int function_code, const uint16_t *value)
... ...
src/modbusbase.h
... ... @@ -69,7 +69,23 @@ public:
69 69 // Modbus implementation(s)
70 70 int readCoils(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible?
71 71 int readInputBits(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible?
  72 +
  73 + /*!
  74 + * Read Holding Registers
  75 + * MODBUS Function 0x03
  76 + * \param address - Reference Address
  77 + * \param amount - Amount of Registers to Read
  78 + * \param buffer - Buffer to Store Data Read from Registers
  79 + */
72 80 int readHoldingRegisters(uint16_t address, uitn16_t amount, uint16_t *buffer);
  81 +
  82 + /*!
  83 + * Read Input Registers
  84 + * MODBUS Function 0x04
  85 + * \param address - Reference Address
  86 + * \param amount - Amount of registers to read
  87 + * \param buffer - Buffer to store Data Read from Registers
  88 + */
73 89 int readInputRegisters(uint16_t address, uint16_t amount, uint16_t *buffer);
74 90  
75 91 int writeCoil(uint16_t address, const bool &to_write);
... ... @@ -95,17 +111,17 @@ private: // Methods
95 111 *
96 112 * \return int
97 113 */
98   - int modbusWrite(uint16_t address, uint16_t amount, int function_code, const uint16_t *value);
  114 + int modbusWrite(uint16_t address, uint16_t amount, int function_code, const uint16_t *value);
99 115 virtual ssize_t modbusSend(uint8_t *to_send, size_t length) = 0;
100 116 virtual ssize_t modbusReceive(uint8_t *buffer) const = 0;
101 117  
102   - void modbusErrorHandle(const uint8_t *msg, int function_code);
103   - void setBadConnection();
104   - void setBadInput();
  118 + void modbusErrorHandle(const uint8_t *msg, int function_code);
  119 + void setBadConnection();
  120 + void setBadInput();
105 121  
106 122 private: // Members (Giggity!)
107   - bool m_connected{};
108   - uint32_t m_msg_id{};
109   - int m_slaveId{};
  123 + bool m_connected{};
  124 + uint32_t m_msg_id{};
  125 + int m_slaveId{};
110 126  
111 127 };
... ...