iop_serial.h
Go to the documentation of this file.
1 /** @file iop_serial.h Process image via serial line */
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_SERIAL_H
13 #define FAUDES_IOP_SERIAL_H
14 
15 
16 // Include core-libary and iodevice
17 #include "corefaudes.h"
18 #include "iop_vdevice.h"
19 #include "iop_sdevice.h"
20 
21 
22 // If configured to have sspi device
23 #ifdef FAUDES_IODEVICE_SERIAL
24 
25 // Include serial specifics (posix systems)
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <termios.h>
29 
30 
31 
32 namespace faudes {
33 
34 
35 
36 /**
37  * Processimage synchronisation via serial line interface
38  *
39  * This device is derived from the signal based sDevice to
40  * emulate component interconnection to behave like parallel
41  * digital wiring, however, physically using the
42  * serial interface with a simple commonication protocol.
43  *
44  * A typical application scenario is to simulate a physical
45  * plant on one PC and to implement decentralized controllers
46  * on further PCs. The distributed controllers
47  * may use the rtxx protocol or the simplenet protocol to communicate
48  * via ethernet. The interface with the physical plant
49  * is modelled as a so called process image that indicates
50  * line levels. The spiDevice synchronizes the process images among the
51  * components. In particular, plant simulation incl. the interaction
52  * with the controllers will not interfere with the communication among
53  * the controllers. This is a requirement for the evaluation of
54  * real time communication protocols such as rtxx.
55  *
56  * The spiDevice distinguishes between one master component
57  * and an arbitrary number of slave components. The master must be
58  * connected to each slave via a dedicated serial port. Every input
59  * signal from the master must match an output signal of no more than one slave.
60  * Any output signal of the master may be considered as an input of
61  * any slave.
62  *
63  * To configure a spiDevice, you must
64  * - specify the role using either the tag
65  * <tt>&lt;Role value="master"/&gt;</tt> or <tt>&lt;Role value="slave"/&gt;</tt>
66  * - specify the serial interface, using tags of the form
67  * &lt;DeviceFile value="/dev/ttyS0"/&gt, &lt;DeviceFile value="/dev/ttyS1"/&gt etc
68  * - setup events to correspond to edges of line levels, see alo faudes::sDevice
69  *
70  *
71  *
72  * Note: this device compiles under Linux only.
73  * It must be explicitely enabled Makefile.plugin.
74  *
75  * @ingroup IODevicePlugin
76  */
77 
78 class FAUDES_API spiDevice : public sDevice {
79 
80 FAUDES_TYPE_DECLARATION(SpiDevice,spiDevice,sDevice)
81 
82  public:
83 
84  /**
85  * Default constructor
86  */
87  spiDevice(void);
88 
89  /**
90  * Copy constructor (not implemented!)
91  */
92  spiDevice(const spiDevice&) : sDevice() {};
93 
94  /**
95  * Explicit destructor.
96  */
97  virtual ~spiDevice(void);
98 
99 
100  /**
101  * Clear all configuration (implies Stop)
102  */
103  virtual void Clear(void);
104 
105 
106  /**
107  *
108  * Compile to internal data-structures.
109  *
110  * Exception in misconfiguration/inconsistencies
111  *
112  */
113  virtual void Compile(void);
114 
115  /**
116  * Activate the device.
117  * This function opens/initializes serial lines and
118  * starts the -background thread for edage detection.
119  *
120  * @exception Exception
121  * - not yet configured (id not configured)
122  * - failed to open serial lines (id not configured)
123  */
124  virtual void Start(void);
125 
126 
127  /**
128  * Deactivate the device. This function shuts down the seriale lines,
129  * stops the background thread and sets all output signals to 0.
130  */
131  virtual void Stop(void);
132 
133 
134 protected:
135 
136 
137  /**
138  * IO Hook, inputs
139  *
140  * @return
141  * True on success.
142  *
143  */
144  virtual bool DoReadSignalsPre(void);
145 
146  /**
147  * IO Hook, inputs
148  */
149  virtual void DoReadSignalsPost(void);
150 
151  /**
152  * Get input signal.
153  *
154  * Extract bit value from image.
155  *
156  * @param bitaddr
157  * Abstract bit address
158  * @return
159  * True for logic level high;
160  */
161  virtual bool DoReadSignal(int bitaddr);
162 
163  /**
164  * IO Hook, outputs
165  *
166  * @return
167  * True on success
168  *
169  */
170  virtual bool DoWriteSignalsPre(void);
171 
172  /**
173  * IO Hook, outputs
174  *
175  */
176  virtual void DoWriteSignalsPost(void);
177 
178  /**
179  * Set output signal.
180  *
181  * Set value of bit in process image.
182  *
183  * @param bitaddr
184  * Abstract bit address
185  * @param value
186  * True for logic level high;
187  *
188  */
189  virtual void DoWriteSignal(int bitaddr, bool value);
190 
191 
192  /**
193  * Loop hook.
194  *
195  * This function is called once during each cycle of the
196  * backgroud thread. It implements the actual communication via the serial
197  * interfaces. To each slave, the master will send its process image and
198  * await for the slave to reply by the image. In it's reply, the slave will
199  * set/clr its output lines. To keep things simple, the process image is
200  * passed on completely, even those lines that are not relevant for the
201  * respective client.
202  *
203  */
204  virtual void DoLoopCallback(void);
205 
206  /**
207  * Read non-event-related configuration data from tokenreader
208  *
209  * @param rTr
210  * TokenReader to read from
211  * @param rLabel
212  * Section to read
213  * @param pContext
214  * Read context to provide contextual information
215  *
216  */
217  void DoReadPreface(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
218 
219  /**
220  * Write non-event-related configuration data to tokenreader
221  *
222  * @param rTw
223  * TokenWriter to write
224  * @param rLabel
225  * Section to write
226  * @param pContext
227  * Context to provide contextual information
228  *
229  */
230  void DoWritePreface(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const ;
231 
232 
233  /** Role: master/slave */
234  bool mMaster;
235 
236  /** Interface(s) */
237  std::vector<std::string> mDeviceFiles;
238  std::vector<int> mPorts;
239 
240  /** process image from serial line */
241  char* mpImage;
242  char* pInputImage;
243  char* pOutputImage;
244  char* mpOutputMask;
245 
246 
247 
248 }; //class spiDevice
249 
250 } //namespace
251 
252 
253 #endif // configured
254 
255 #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
Includes all libFAUDES headers, no plugins.
Virtual device for signal based io
Virtual device for interface definition
libFAUDES resides within the namespace faudes.

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