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 {
34 {
35 public:
40  ModbusMsgWrapper(const ModbusMsgInStampedConstPtr& modbus_msg_raw, const ModbusApiSpec& api_spec);
41 
42  virtual ~ModbusMsgWrapper() = default;
43 
53  virtual void checkStructuralIntegrity() const;
54 
58  unsigned int getVersion() const;
59 
66  bool isDisconnect() const;
67 
68  const ros::Time& getTimeStamp() const;
69 
70 protected:
76  bool hasRegister(uint32_t reg) const;
77 
81  uint16_t getRegister(uint32_t reg) const;
82 
86  bool hasVersion() const;
87 
91  const ModbusApiSpec& getApiSpec() const;
92 
93 private:
95  const ModbusMsgInStampedConstPtr msg_;
96 };
97 
98 inline ModbusMsgWrapper::ModbusMsgWrapper(const ModbusMsgInStampedConstPtr& modbus_msg_raw,
99  const ModbusApiSpec& api_spec)
100  : api_spec_(api_spec), msg_(modbus_msg_raw)
101 {
102 }
103 
104 inline bool ModbusMsgWrapper::hasRegister(uint32_t reg) const
105 {
106  uint32_t relative_idx = reg - msg_->holding_registers.layout.data_offset;
107 
108  return msg_->holding_registers.data.size() > relative_idx;
109 }
110 
111 inline uint16_t ModbusMsgWrapper::getRegister(uint32_t reg) const
112 {
113  return msg_->holding_registers.data.at(reg - msg_->holding_registers.layout.data_offset);
114 }
115 
116 inline bool ModbusMsgWrapper::hasVersion() const
117 {
119 }
120 
121 inline unsigned int ModbusMsgWrapper::getVersion() const
122 {
124 }
125 
127 {
128  return msg_->disconnect.data;
129 }
130 
132 {
133  if (!hasVersion())
134  {
135  throw ModbusMsgWrapperException("Received message does not contain a version.");
136  }
137 }
138 
140 {
141  return api_spec_;
142 }
143 
145 {
146  return msg_->header.stamp;
147 }
148 
149 } // namespace prbt_hardware_support
150 
151 #endif // MODBUS_MSG_WRAPPER_H
bool hasRegister(uint32_t reg) const
Check if a certain holding register is define in the Modbus message.
bool hasVersion() const
Check if the modbus_msg contains the API version.
unsigned short getRegisterDefinition(const std::string &key) const
const ModbusMsgInStampedConstPtr msg_
uint16_t getRegister(uint32_t reg) const
virtual void checkStructuralIntegrity() const
Checks that the Modbus messages consists of all registers needed.
const ModbusApiSpec & getApiSpec() const
bool isDisconnect() const
Check if the Modbus message informs about a disconnect from the server.
ModbusMsgWrapper(const ModbusMsgInStampedConstPtr &modbus_msg_raw, const ModbusApiSpec &api_spec)
Construct a new Modbus Msg Wrapper object.
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 Mon Aug 10 2020 03:10:57