modbus_msg_wrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef MODBUS_MSG_WRAPPER_H
18 #define MODBUS_MSG_WRAPPER_H
19 
20 #include <ros/time.h>
21 
22 #include <prbt_hardware_support/ModbusMsgInStamped.h>
25 
26 namespace prbt_hardware_support
27 {
28 
35 {
36 public:
41  ModbusMsgWrapper(const ModbusMsgInStampedConstPtr& modbus_msg_raw,
42  const ModbusApiSpec& api_spec);
43 
44  virtual ~ModbusMsgWrapper() = default;
45 
55  virtual void checkStructuralIntegrity() const;
56 
60  unsigned int getVersion() const;
61 
68  bool isDisconnect() const;
69 
70  const ros::Time& getTimeStamp() const;
71 
72 protected:
73 
79  bool hasRegister(uint32_t reg) const;
80 
84  uint16_t getRegister(uint32_t reg) const;
85 
89  bool hasVersion() const;
90 
94  const ModbusApiSpec& getApiSpec() const;
95 
96 private:
98  const ModbusMsgInStampedConstPtr msg_;
99 
100 };
101 
102 inline ModbusMsgWrapper::ModbusMsgWrapper(const ModbusMsgInStampedConstPtr& modbus_msg_raw,
103  const ModbusApiSpec& api_spec)
104  : api_spec_(api_spec)
105  , msg_(modbus_msg_raw)
106 {
107 }
108 
109 inline bool ModbusMsgWrapper::hasRegister(uint32_t reg) const
110 {
111  uint32_t relative_idx = reg - msg_->holding_registers.layout.data_offset;
112 
113  return msg_->holding_registers.data.size() > relative_idx;
114 }
115 
116 inline uint16_t ModbusMsgWrapper::getRegister(uint32_t reg) const
117 {
118  return msg_->holding_registers.data.at(reg - msg_->holding_registers.layout.data_offset);
119 }
120 
121 inline bool ModbusMsgWrapper::hasVersion() const
122 {
124 }
125 
126 inline unsigned int ModbusMsgWrapper::getVersion() const
127 {
129 }
130 
132 {
133  return msg_->disconnect.data;
134 }
135 
137 {
138  if(!hasVersion())
139  {
140  throw ModbusMsgWrapperException("Received message does not contain a version.");
141  }
142 }
143 
145 {
146  return api_spec_;
147 }
148 
150 {
151  return msg_->header.stamp;
152 }
153 
154 }
155 
156 #endif // MODBUS_MSG_WRAPPER_H
const ModbusApiSpec & getApiSpec() const
uint16_t getRegister(uint32_t reg) const
virtual void checkStructuralIntegrity() const
Checks that the Modbus messages consists of all registers needed.
bool hasRegister(uint32_t reg) const
Check if a certain holding register is define in the Modbus message.
const ModbusMsgInStampedConstPtr msg_
bool hasVersion() const
Check if the modbus_msg contains the API version.
ModbusMsgWrapper(const ModbusMsgInStampedConstPtr &modbus_msg_raw, const ModbusApiSpec &api_spec)
Construct a new Modbus Msg Wrapper object.
unsigned short getRegisterDefinition(const std::string &key) const
bool isDisconnect() const
Check if the Modbus message informs about a disconnect from the server.
Expection thrown upon construction of ModbusMsgWrapperBase of the message does not contain the requir...
Wrapper class to add semantic to a raw ModbusMsgInStamped.
Specifies the meaning of the holding registers.


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17