Roomba App  1.0
communication.cpp
Go to the documentation of this file.
1 
16 #include <QByteArray>
17 #include <QString>
18 
19 #include "communication.hh"
20 
21 bool Communication::openSerialPort(const char* serialPort)
22 {
23  _device = new QSerialPort();
24  _device->setBaudRate(QSerialPort::Baud115200);
25  _device->setFlowControl(QSerialPort::NoFlowControl);
26  _device->setStopBits(QSerialPort::OneStop);
27  _device->setDataBits(QSerialPort::Data8);
28  _device->setParity(QSerialPort::NoParity);
29  _device->setPortName(serialPort);
30 
31  if(_device->open(QSerialPort::ReadOnly))
32  {
33  _opened = true;
34  return true;
35  }
36  else
37  {
38  return false;
39  }
40 }
41 
43 {
44  if(isOpen())
45  {
46  _device->close();
47  _opened = false;
48  return true;
49  }
50  else
51  {
52  return false;
53  }
54 }
55 
57 {
58  while(_continue)
59  {
60  while(_device->waitForReadyRead())
61  {
62  while(_device->canReadLine())
63  {
64  if(!_continue) return;
65  QByteArray line = _device->readLine();
66  if(line[line.length() - 1] == '\n')
67  {
68  line[line.length() - 1] = 0;
69  }
70  if(line[line.length() - 2] == '\r')
71  {
72  line[line.length() - 2] = 0;
73  }
74  _frame.addFrame(line.toStdString());
75  }
76  }
77  }
78 }
void receiveData()
Receive line one by one from the serial device in loop until end of communication.
bool closeSerialPort()
Close the serial port.
QSerialPort * _device
Serial device handle.
bool _continue
Identifier whether to continue communication with serial device.
bool openSerialPort()
Wrapper function for opening a serial port.
bool isOpen() const
Check if the serial port is open.
FrameBuffer _frame
Circular buffer for frames.
bool _opened
Identifier whether serial device is open.
void addFrame(const std::string &frame)
Add one frame at the end of list.
Definition: frameBuffer.cpp:19
Communication class declaration.