Commit fac782881bba7edb2499a538843821f804ba382a

Authored by Peter M. Groen
2 parents e651a10f f80bfff0

Merge branch 'feat/pgroen/modbus-tcp-implementation' of github.com:pgroen/modbus…

…-cpp into feat/pgroen/modbus-tcp-implementation
src/modbusbase.cpp
@@ -9,12 +9,77 @@ @@ -9,12 +9,77 @@
9 9
10 int ModbusBase::readCoils(uint16_t address, uint16_t amount, bool *buffer) 10 int ModbusBase::readCoils(uint16_t address, uint16_t amount, bool *buffer)
11 { 11 {
  12 + if (m_connected)
  13 + {
  14 + if (amount > 2040)
  15 + {
  16 + setBadInput();
  17 + return EX_BAD_DATA;
  18 + }
12 19
  20 + modbusRead(address, amount, READ_COILS);
  21 + uint8_t to_rec[MAX_MSG_LENGTH];
  22 + ssize_t result = modbusReceive(to_rec);
  23 + if (result == -1)
  24 + {
  25 + setBadConnection();
  26 + return BAD_CON;
  27 + }
  28 +
  29 + modbusErrorHandle(to_rec, READ_COILS);
  30 + if (err)
  31 + {
  32 + return err_no;
  33 + }
  34 +
  35 + for (auto i = 0; i < amount; i++)
  36 + {
  37 + buffer[i] = (bool)((to_rec[9u + i / 8u] >> (i % 8u)) & 1u);
  38 + }
  39 + return 0;
  40 + }
  41 + else
  42 + {
  43 + setBadConnection();
  44 + return BAD_CON;
  45 + }
13 } 46 }
14 47
15 int ModbusBase::readInputBits(uint16_t address, uint16_t amount, bool *buffer) 48 int ModbusBase::readInputBits(uint16_t address, uint16_t amount, bool *buffer)
16 { 49 {
  50 + if (m_connected)
  51 + {
  52 + if (amount > 2040)
  53 + {
  54 + setBadInput();
  55 + return EX_BAD_DATA;
  56 + }
17 57
  58 + modbusRead(address, amount, READ_INPUT_BITS);
  59 + uint8_t to_rec[MAX_MSG_LENGTH];
  60 + ssize_t result = modbusReceive(to_rec);
  61 + if (result == -1)
  62 + {
  63 + setBadConnection();
  64 + return BAD_CON;
  65 + }
  66 +
  67 + if (err)
  68 + {
  69 + return err_no;
  70 + }
  71 +
  72 + for (auto i = 0; i < amount; i++)
  73 + {
  74 + buffer[i] = (bool)((to_rec[9u + i / 8u] >> (i % 8u)) & 1u);
  75 + }
  76 + modbusErrorHandle(to_rec, READ_INPUT_BITS);
  77 + return 0;
  78 + }
  79 + else
  80 + {
  81 + return BAD_CON;
  82 + }
18 } 83 }
19 84
20 int ModbusBase::readHoldingRegisters(uint16_t address, uitn16_t amount, uint16_t *buffer) 85 int ModbusBase::readHoldingRegisters(uint16_t address, uitn16_t amount, uint16_t *buffer)
@@ -54,13 +119,39 @@ int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t * @@ -54,13 +119,39 @@ int ModbusBase::readInputRegisters(uint16_t address, uint16_t amount, uint16_t *
54 { 119 {
55 modbusRead(address, amount, READ_INPUT_REGS); 120 modbusRead(address, amount, READ_INPUT_REGS);
56 uint8_t to_rec[MSG_MAX_LENGTH]; 121 uint8_t to_rec[MSG_MAX_LENGTH];
  122 + size_t result = modbusReceive(to_rec);
  123 + if (result == -1)
  124 + {
  125 + setBadConnection();
  126 + return BAD_CON;
  127 + }
57 128
  129 + modbusErrorHandle(to_rec, READ_INPUT_REGS);
  130 + if (err)
  131 + {
  132 + return err_no;
  133 + }
  134 +
  135 + for (auto i = 0; i < amount; i++)
  136 + {
  137 + buffer[i] = ((uint16_t)to_rec[9u + 2u * i]) << 8u;
  138 + buffer[i] = (uint16_t)to_rec[10u + 2u * i];
  139 + }
  140 + return 0;
  141 + }
  142 + else
  143 + {
  144 + setBadConnection();
  145 + return BAD_CON;
58 } 146 }
59 } 147 }
60 148
61 int ModbusBase::writeCoil(uint16_t address, const bool &to_write) 149 int ModbusBase::writeCoil(uint16_t address, const bool &to_write)
62 { 150 {
63 - 151 + if (m_connected)
  152 + {
  153 + int value = to_write * 0xFF00;
  154 + }
64 } 155 }
65 156
66 int ModbusBase::writeRegister(uint16_t address, const uint16_t &value) 157 int ModbusBase::writeRegister(uint16_t address, const uint16_t &value)
src/modbusbase.h
@@ -67,7 +67,22 @@ public: @@ -67,7 +67,22 @@ public:
67 virtual bool isConnected() const - 0; 67 virtual bool isConnected() const - 0;
68 68
69 // Modbus implementation(s) 69 // Modbus implementation(s)
  70 + /*!
  71 + * Read Coils
  72 + * MODBUS Function 0x01
  73 + * \param address - Reference Address
  74 + * \param amount - Amount of Coils to Read
  75 + * \param buffer - Buffer to Store Data Read from Coils
  76 + */
70 int readCoils(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible? 77 int readCoils(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible?
  78 +
  79 + /*!
  80 + * Read Input Bits
  81 + * MODBUS Function 0x02
  82 + * \param address - Refernce Address
  83 + * \param amount - Amount of BITS to Read
  84 + * \param buffer - Buffer to store Data Read from Input Bits
  85 + */
71 int readInputBits(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible? 86 int readInputBits(uint16_t address, uint16_t amount, bool *buffer); // Replace buffer with something sensible?
72 87
73 /*! 88 /*!
@@ -88,9 +103,18 @@ public: @@ -88,9 +103,18 @@ public:
88 */ 103 */
89 int readInputRegisters(uint16_t address, uint16_t amount, uint16_t *buffer); 104 int readInputRegisters(uint16_t address, uint16_t amount, uint16_t *buffer);
90 105
  106 + /*!
  107 + * Write Single Coils
  108 + * MODBUS Function 0x05
  109 + * \param address - Reference Address
  110 + * \param to_write - Value to be written to Coil
  111 + */
91 int writeCoil(uint16_t address, const bool &to_write); 112 int writeCoil(uint16_t address, const bool &to_write);
  113 +
92 int writeRegister(uint16_t address, const uint16_t &value); 114 int writeRegister(uint16_t address, const uint16_t &value);
  115 +
93 int writeCoils(uint16_t address, uint16_t amount, const bool *value); 116 int writeCoils(uint16_t address, uint16_t amount, const bool *value);
  117 +
94 int writeRegisters(uint16_t address, uint16_t amount, const uint16_t *value); 118 int writeRegisters(uint16_t address, uint16_t amount, const uint16_t *value);
95 119
96 private: // Methods 120 private: // Methods