Webcam Tracker  V1.0
Using openCV and an Arduino Uno
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members

is responsable for the serial communication between PC-application and the Arduino Uno More...

#include <SerialCom.h>

Public Member Functions

void write (char *message)
 Sends a message(command) to the Arduino Uno. More...
 
void readString ()
 Reads the serial port for incomming messages. More...
 
bool newMessageAvailable ()
 Shows if a new message is recieved from the Serial input. More...
 
string * getLastMessage ()
 Returns last message. More...
 

Static Public Member Functions

static SerialCominitialize (string portName, long baudeRate, int dataSize, char parityType, float nStopBits)
 Creates a object if there isn't one already. More...
 

Private Member Functions

 SerialCom (string portName, long baudeRate, int dataSize, char parityType, float nStopBits)
 Constructor. More...
 
void publishError (string message)
 shows a error message on the console More...
 
void publishSucces (string message)
 shows a succes message on the console More...
 

Private Attributes

ceSerial serial
 Object which holds the ceSerial communication class.
 
string line
 string of all characters in the serial message
 
string lastMessage
 last serial message
 
bool newMessage
 new message available flag
 

Static Private Attributes

static SerialCominstance = 0
 pointer to the current used instance
 

Detailed Description

is responsable for the serial communication between PC-application and the Arduino Uno

Constructor & Destructor Documentation

◆ SerialCom()

SerialCom::SerialCom ( string  portName,
long  baudeRate,
int  dataSize,
char  parityType,
float  nStopBits 
)
private

Constructor.

Parameters
portNamename of the COM-port
baudeRatebaudeRate of the COM-port
dataSizesize of the data
parityTypeparaity type
nStopBitsnumber of stop bits
14  {
15  string port = "\\\\.\\"+portName;
16  serial = ceSerial(port, baudeRate, dataSize, parityType, nStopBits);
17 
18  int opend = serial.Open();
19  switch (opend)
20  {
21  case 1: publishError("ERROR: No COM-port found named " + portName); break;
22  case 2: publishError("ERROR: Unable to clear serial buffer"); break;
23  case 3: publishError("ERROR: Unable to open COM-port"); break;
24  case 4: publishError("ERROR: Unable to set COM-port settings"); break;
25  case 5: publishError("ERROR: Unable to open COM-port"); break;
26  case 6: publishError("ERROR: Error creating read overlapped event"); break;
27  case 7: publishError("ERROR: Error creating write overlapped event"); break;
28  case 8: publishError("ERROR: Error getting time-outs"); break;
29  case 9: publishError("ERROR: Error setting time-outs"); break;
30  case 0: publishSucces("OK: Opend " + portName); break;
31  default: publishError("ERROR: An unspecified error occured"); break;
32  }
33 
34  if (opend != 0)
35  serial.Close();
36 }
ceSerial serial
Object which holds the ceSerial communication class.
Definition: SerialCom.h:27
void publishError(string message)
shows a error message on the console
Definition: SerialCom.cpp:38
void publishSucces(string message)
shows a succes message on the console
Definition: SerialCom.cpp:45
Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ publishError()

void SerialCom::publishError ( string  message)
private

shows a error message on the console

Also sets the message as lastMessage

Parameters
messagestring containing the message
39 {
40  lastMessage = message;
41  std::cerr << lastMessage << std::endl;
42  newMessage = true;
43 }
string lastMessage
last serial message
Definition: SerialCom.h:29
bool newMessage
new message available flag
Definition: SerialCom.h:30
Here is the caller graph for this function:

◆ publishSucces()

void SerialCom::publishSucces ( string  message)
private

shows a succes message on the console

Also sets the message as lastMessage

Parameters
messagestring containing the message
46 {
47  lastMessage = message;
48  std::cout << lastMessage << std::endl;
49  newMessage = true;
50 }
Here is the caller graph for this function:

◆ initialize()

SerialCom * SerialCom::initialize ( string  portName,
long  baudeRate,
int  dataSize,
char  parityType,
float  nStopBits 
)
static

Creates a object if there isn't one already.

Note
only one instance can excist because there can only be communicated with one Arduino Uno
Parameters
portNamename of the COM-port
baudeRatebaude rate of the COM-port
dataSizesize of the data
parityTypeparity type
nStopBitsnumber of stop bits
Returns
: SerialCom: Pointer to object instance
53 {
54  if (instance == 0) {
55  instance = new SerialCom(portName, baudeRate, dataSize, parityType, nStopBits);
56  }
57 
58  return instance;
59 }
SerialCom(string portName, long baudeRate, int dataSize, char parityType, float nStopBits)
Constructor.
Definition: SerialCom.cpp:14
static SerialCom * instance
pointer to the current used instance
Definition: SerialCom.h:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write()

void SerialCom::write ( char *  message)

Sends a message(command) to the Arduino Uno.

Won't send a message if the COM-port isn't opened.

Parameters
messagecommand to be send
62 {
63  if (serial.IsOpened()) {
64  if (!serial.Write(message))
65  publishError("ERROR: A write error occured");
66  }
67 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readString()

void SerialCom::readString ( )

Reads the serial port for incomming messages.

If ther isn't a character the readstring will break, so it's non-blocking.
When the character is a line feed(LF) the serial message will be pushed on to the deque.

69  {
70 
71  bool succes = false;
72  char ch = 0;
73  do
74  {
75  ch = serial.ReadChar(succes);
76  if (succes) {
77  if ((ch >= 0) && (ch != 10) && (ch != 13))
78  line += ch;
79  if (ch == '\n') {
80  lastMessage = line;
81  newMessage = true;
82  line = "";
83  }
84  }
85  } while ((ch != '\n') && (succes==true));
86 
87 }
string line
string of all characters in the serial message
Definition: SerialCom.h:28
Here is the caller graph for this function:

◆ newMessageAvailable()

bool SerialCom::newMessageAvailable ( )

Shows if a new message is recieved from the Serial input.

Returns
bool: new message available flag
90 {
91  if (newMessage) {
92  newMessage = false;
93  return true;
94  }
95  return false;
96 }
Here is the caller graph for this function:

◆ getLastMessage()

std::string * SerialCom::getLastMessage ( )

Returns last message.

Returns
string: last serial message
99 {
100  return &lastMessage;
101 }
Here is the caller graph for this function: