readingthread.cpp
Go to the documentation of this file.
1 
8 
9 ReadingThread::ReadingThread(std::shared_ptr<Syntax> syntax,
10  ThreadDispatcher* dispatcher)
11  : syntax(syntax), dispatcher(dispatcher)
12 {
13  data = std::queue<std::string>();
14  errorCBregistered = false;
15 }
16 
18 {
19  active = true;
20  worker = std::thread(&ReadingThread::workerFunction, this);
21 }
22 
24 {
25  active = false;
26  wakeUp();
27  worker.join();
28 }
29 
31 {
32  this->error = error;
33  errorCBregistered = true;
34 }
35 
37 {
39 
40  while (active)
41  {
42  std::string message;
43  try
44  {
45  si.read(message, syntax->endOfMessage);
46  message.erase(
47  boost::remove_if(message, boost::is_any_of(syntax->endOfFrame +
48  syntax->endOfMessage)),
49  message.end());
50  if (message.size() > 1)
51  {
52  data.push(message);
53  dispatcher->wakeUp();
54  }
55  }
56  catch (std::exception& e)
57  {
59  error("An error occured while reading from the serial connection\n "
60  "Description: " +
61  std::string(e.what()));
62  }
63  }
64 }
65 
67 {
68  if (data.size() > 0)
69  {
70  std::string out(data.front());
71  data.pop();
72  return out;
73  }
74  else
75  {
76  return std::string("");
77  }
78 }
79 
80 const bool ReadingThread::isQueueEmpty() const { return data.empty(); }
void workerFunction()
Worker function, responsible for polling the serial input buffer.
void registerErrorCallback(errorCallbackPtr error)
Register an error function pointer to handle errors during communication.
static SerialInterface & instance()
Returns a singleton auf this class.
Header file for the ReadingThread class.
The ThreadDispatcher class coordinates other communication threads and handles start up and shut down...
ThreadDispatcher * dispatcher
Definition: readingthread.h:66
boost::function< void(const std::string &)> errorCallbackPtr
Definition: readingthread.h:24
std::shared_ptr< Syntax > syntax
Definition: readingthread.h:64
bool errorCBregistered
Definition: readingthread.h:68
ReadingThread(std::shared_ptr< Syntax > syntax, ThreadDispatcher *dispatcher)
ReadingThread constructor.
errorCallbackPtr error
Definition: readingthread.h:67
void startThread()
Start the polling of the serial input buffer..
void stopThread()
Stop the polling of the serial input buffer..
std::queue< std::string > data
Definition: readingthread.h:65
void wakeUp()
Unlocks the current worker thread.
const bool isQueueEmpty() const
Is a new line availible?
std::string getData()
Get retrieved data vom the serial input buffer.
The SerialInterface class provides an interface to a connected serial device.
void read(std::string &message, std::string &delimiter)
read a message from the serial input buffer


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13