SMACC2
Loading...
Searching...
No Matches
cp_modbus_relay.hpp
Go to the documentation of this file.
1// Copyright 2024 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20#pragma once
21
23#include <smacc2/smacc.hpp>
24
25#include <cstdint>
26#include <functional>
27
28namespace cl_modbus_tcp_relay
29{
30
31// Forward declarations for events
32template <typename TSource, typename TOrthogonal>
34
35template <typename TSource, typename TOrthogonal>
37
52{
53public:
54 static constexpr int NUM_CHANNELS = 8;
55 static constexpr int COIL_BASE_ADDRESS = 0x0000;
56
58 virtual ~CpModbusRelay();
59
60 void onInitialize() override;
61
65 template <typename TOrthogonal, typename TClient>
74
75 // Single channel operations (channel 1-8)
76 bool writeCoil(int channel, bool state);
77 bool readCoil(int channel, bool & state);
78
79 // All channels operations
80 bool writeAllCoils(bool state);
81 bool writeAllCoils(uint8_t mask);
82 bool readAllCoils(uint8_t & states);
83
84 // Signals for operation results
85 smacc2::SmaccSignal<void(int channel, bool state)> onWriteSuccess_;
86 smacc2::SmaccSignal<void(int channel, const std::string & error)> onWriteFailure_;
87 smacc2::SmaccSignal<void(uint8_t states)> onReadComplete_;
88
89 // Signal connection helpers
90 template <typename T>
91 smacc2::SmaccSignalConnection onWriteSuccess(void (T::*callback)(int, bool), T * object)
92 {
93 return this->getStateMachine()->createSignalConnection(onWriteSuccess_, callback, object);
94 }
95
96 template <typename T>
98 void (T::*callback)(int, const std::string &), T * object)
99 {
100 return this->getStateMachine()->createSignalConnection(onWriteFailure_, callback, object);
101 }
102
103 template <typename T>
104 smacc2::SmaccSignalConnection onReadComplete(void (T::*callback)(uint8_t), T * object)
105 {
106 return this->getStateMachine()->createSignalConnection(onReadComplete_, callback, object);
107 }
108
109private:
111
112 std::function<void()> postWriteSuccessEvent_;
113 std::function<void()> postWriteFailureEvent_;
114
115 // Helper to convert channel (1-8) to coil address (0-7)
116 int channelToAddress(int channel) const { return COIL_BASE_ADDRESS + (channel - 1); }
117
118 // Validate channel number
119 bool isValidChannel(int channel) const { return channel >= 1 && channel <= NUM_CHANNELS; }
120};
121
122} // namespace cl_modbus_tcp_relay
Component that manages Modbus TCP connection lifecycle and heartbeat monitoring.
Component that handles Modbus coil read/write operations for 8-channel relay.
bool isValidChannel(int channel) const
smacc2::SmaccSignal< void(uint8_t states)> onReadComplete_
std::function< void()> postWriteSuccessEvent_
smacc2::SmaccSignal< void(int channel, bool state)> onWriteSuccess_
smacc2::SmaccSignalConnection onReadComplete(void(T::*callback)(uint8_t), T *object)
int channelToAddress(int channel) const
smacc2::SmaccSignalConnection onWriteFailure(void(T::*callback)(int, const std::string &), T *object)
void onStateOrthogonalAllocation()
Configure component for event posting during orthogonal allocation.
smacc2::SmaccSignal< void(int channel, const std::string &error)> onWriteFailure_
bool readCoil(int channel, bool &state)
smacc2::SmaccSignalConnection onWriteSuccess(void(T::*callback)(int, bool), T *object)
bool writeCoil(int channel, bool state)
std::function< void()> postWriteFailureEvent_
ISmaccStateMachine * getStateMachine()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection SmaccSignalConnection