iop_modbus.h
Go to the documentation of this file.
1 /** @file iop_modbus.h Process image via modbus/tcp */
2 
3 /*
4  FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2011, Thomas Moor.
7 
8 */
9 
10 
11 
12 #ifndef FAUDES_IOP_MODBUS_H
13 #define FAUDES_IOP_MODBUS_H
14 
15 
16 // Include core-libary and iodevice
17 #include "corefaudes.h"
18 #include "iop_vdevice.h"
19 #include "iop_sdevice.h"
20 #include "iop_simplenet.h"
21 
22 
23 // If configured to have a modbus device
24 #ifdef FAUDES_IODEVICE_MODBUS
25 
26 
27 
28 namespace faudes {
29 
30 
31 
32 /**
33  * Processimage synchronisation via Modbus/TCP
34  *
35  * This device is derived from the signal based sDevice to
36  * read and write line levels via ethernet using the Modbus/TCP
37  * protocol; see also the <a href="http://www.modbus.org">Modbus Organization</a>.
38  * When configured as master, the mbDevice
39  * initiates commucation with a specified list of slaves in order
40  * to retrieve input line levels and to set output line levels. The latter are
41  * locally buffered and are propagated to the event-based interface
42  * inherited via the base class sDevice.
43  * When configured as slave, the mbDevice accepts connections from any Modbus
44  * master, will response to their request sand update the local line-level buffer accordingly.
45  *
46  * Further dedatils on the configuration, incl. an example, are provided
47  * by the <a href="../reference/simulator_devices.html#ModbusDevice">ModbusDevice user-reference</a>
48  *
49  * Technical details.
50  * - The mbDevice slave implements reading and writing bits/coils/registers/holding registers,
51  * incl. the multi-read/write variants; regardless which commads you use, they all refer
52  * to the one process image implicitly defined by the event configuration.
53  * - The mbDevice matser usees the commands read multiple bits and write multiple coils
54  * for process image synchonisation.
55  * - All network communication come with quite relaxed timeouts. Please let us know, if
56  * you require more strict timeout behaviour.
57  * - Network communication is currently implemented synchronous with the edge detection
58  * background task; this is restrictive and may be changed in future revisions.
59  * - Communication uses an additional line-level buffer; a better solution would be
60  * to share the line buffer with the sDevice; however, this is not supported by the current
61  * sDevice interface.
62  * - Programatic configuration is still incomplete.
63  * - The mbDevice compiles with MinGW/Windows, however, it was not properly tested
64  * in this configuration; please let us know, if you plan to use mbDevice in a
65  * Windows context.
66  *
67  * Note: This device must be explicitely enabled in Makefile.plugin.
68  *
69  * @ingroup IODevicePlugin
70  */
71 
72 class FAUDES_API mbDevice : public sDevice {
73 
75 
76  public:
77 
78  /**
79  * Default constructor
80  */
81  mbDevice(void);
82 
83  /**
84  * Copy constructor (not implemented!)
85  */
86  mbDevice(const mbDevice&) : sDevice() {};
87 
88  /**
89  * Explicit destructor.
90  */
91  virtual ~mbDevice(void);
92 
93 
94  /**
95  * Clear all configuration (implies Stop)
96  */
97  virtual void Clear(void);
98 
99 
100  /**
101  *
102  * Compile to internal data-structures.
103  *
104  * Exception in misconfiguration/inconsistencies
105  *
106  */
107  virtual void Compile(void);
108 
109 
110  /**
111  * Append remotely implemented outputs.
112  * You must
113  * (re-)compile the mbDevice after adding remote outputs.
114  *
115  * @param mbid
116  * Remote device id
117  * @param mbaddr
118  * bitaddress within the remote device
119  * @param cnt
120  * number of output bits to bw written
121  * @param fdaddr
122  * address within local process image
123  */
124  void AppendRemoteOutputs(int mbid, int mbaddr, int cnt, int fdaddr);
125 
126 
127  /**
128  * Append remotely implemented inputs.
129  * You must
130  * (re-)compile the mbDevice after adding remote inputs.
131  *
132  * @param mbid
133  * Remote device id
134  * @param mbaddr
135  * bitaddress within the remote device
136  * @param cnt
137  * number of input bits to bw read
138  * @param fdaddr
139  * address within local process image
140  */
141  void AppendRemoteInputs(int mbid, int mbaddr, int cnt, int fdaddr);
142 
143  /**
144  * Set server address of this node.
145  * Note: you can only set th server address while the
146  * device is down.
147  *
148  * @param rAddr
149  * IP address of the remote Modbus device, e.g. "localhost:1502"
150  * @exception Exception
151  * - No valid address (id 551) (NOT IMPLEMENTED)
152  */
153  void SlaveAddress(const std::string& rAddr);
154 
155 
156  /**
157  * Activate the device.
158  * This function opens/initializes a network connection and
159  * starts the -background thread for communication and edge detection.
160  *
161  * @exception Exception
162  * - not yet configured (id not configured)
163  * - failed to network connection (id not configured)
164  */
165  virtual void Start(void);
166 
167 
168  /**
169  * Deactivate the device. This function shuts down the network,
170  * stops the background thread and sets all output signals to 0.
171  */
172  virtual void Stop(void);
173 
174 
175 protected:
176 
177 
178  /**
179  * IO Hook, inputs
180  *
181  * @return
182  * True on success.
183  *
184  */
185  virtual bool DoReadSignalsPre(void);
186 
187  /**
188  * IO Hook, inputs
189  */
190  virtual void DoReadSignalsPost(void);
191 
192  /**
193  * Get input signal.
194  *
195  * Extract bit value from image.
196  *
197  * @param bitaddr
198  * Abstract bit address
199  * @return
200  * True for logic level high;
201  */
202  virtual bool DoReadSignal(int bitaddr);
203 
204  /**
205  * IO Hook, outputs
206  *
207  * @return
208  * True on success
209  *
210  */
211  virtual bool DoWriteSignalsPre(void);
212 
213  /**
214  * IO Hook, outputs
215  *
216  */
217  virtual void DoWriteSignalsPost(void);
218 
219  /**
220  * Set output signal.
221  *
222  * Set value of bit in process image.
223  *
224  * @param bitaddr
225  * Abstract bit address
226  * @param value
227  * True for logic level high;
228  *
229  */
230  virtual void DoWriteSignal(int bitaddr, bool value);
231 
232 
233  /**
234  * Loop hook.
235  *
236  * This function is called once during each cycle of the
237  * backgroud thread. It implements the actual communication via Modbus/TCP.
238  *
239  */
240  virtual void DoLoopCallback(void);
241 
242  /**
243  * Read non-event-related configuration data from tokenreader
244  *
245  * @param rTr
246  * TokenReader to read from
247  * @param rLabel
248  * Section to read
249  * @param pContext
250  * Read context to provide contextual information
251  *
252  */
253  void DoReadPreface(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
254 
255  /**
256  * Write non-event-related configuration data to tokenreader
257  *
258  * @param rTw
259  * TokenWriter to write
260  * @param rLabel
261  * Section to write
262  * @param pContext
263  * Context to provide contextual information
264  *
265  */
266  void DoWritePreface(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const ;
267 
268 
269  /** Role: master/slave */
271 
272  /** IP addresses */
274 
275  /** Modbus address ranges */
276  typedef struct {
277  bool mInputs;
278  int mMbId;
281  int mCount;
282  } IoRange;
283  std::vector< IoRange > mSlaveIoRanges;
284 
285  /** Remote process image buffer */
287  char* mpImage;
288  char* pInputImage;
291 
292  /** Background thread: tcp connection to remote slave */
295  char* mMessage;
298 
299  /** Background thread: tcp connection to remote masters */
300  std::vector<int> mMasterSockets;
301 
302  /** I/O helper */
303  int MbFlushBuffers(void);
304  int MbSendRequest(int id);
305  int MbReceiveResponse(void);
306  int MbReceiveRequest(int mastersock);
307  int MbSendResponse(int mastersock);
308 
309 
310 }; //class spiDevice
311 
312 } //namespace
313 
314 
315 #endif // configured
316 
317 #endif // include
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
faudes type declaration macro
Definition: cfl_types.h:867
Simplenet node address.
Definition: iop_simplenet.h:31
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
Processimage synchronisation via Modbus/TCP.
Definition: iop_modbus.h:72
char * pInputImage
Definition: iop_modbus.h:288
bool mMasterRole
Role: master/slave.
Definition: iop_modbus.h:270
mbDevice(const mbDevice &)
Copy constructor (not implemented!)
Definition: iop_modbus.h:86
int mImageSize
Remote process image buffer.
Definition: iop_modbus.h:286
char * mpOutputMask
Definition: iop_modbus.h:290
std::vector< IoRange > mSlaveIoRanges
Definition: iop_modbus.h:283
std::vector< int > mMasterSockets
Background thread: tcp connection to remote masters.
Definition: iop_modbus.h:300
int mSlaveSocket
Background thread: tcp connection to remote slave.
Definition: iop_modbus.h:293
char * pOutputImage
Definition: iop_modbus.h:289
SimplenetAddress mSlaveAddress
IP addresses.
Definition: iop_modbus.h:273
An sDevice implements signal based semantics for faudes events.
Definition: iop_sdevice.h:291
Includes all libFAUDES headers, no plugins.
Virtual device for signal based io
Simple networked events via TCP/IP.
Virtual device for interface definition
libFAUDES resides within the namespace faudes.
Modbus address ranges.
Definition: iop_modbus.h:276

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen