Roomba App  1.0
communication.cpp
Go to the documentation of this file.
1 
12 #include <QDebug>
13 #include <QString>
14 #include <QByteArray>
15 
16 #include "communication.hh"
17 
19 
21 
22 bool Communication::openSerialPort(const char* serialPort)
23 {
24  // _device = new QSerialPort();
25  _device->setBaudRate(QSerialPort::Baud115200);
26  _device->setFlowControl(QSerialPort::NoFlowControl);
27  _device->setStopBits(QSerialPort::OneStop);
28  _device->setDataBits(QSerialPort::Data8);
29  _device->setParity(QSerialPort::NoParity);
30  _device->setPortName(serialPort);
31 
32  if(_device->open(QSerialPort::ReadOnly))
33  {
34  _opened = true;
35  return true;
36  }
37  else
38  {
39  qDebug() << "Serial port not open";
40  return false;
41  }
42 }
43 
45 {
46  if(isOpen())
47  {
49  _device->close();
50  _opened = false;
51  return true;
52  }
53  else
54  {
55  return false;
56  }
57 }
58 
59 void Communication::setStatus(QLabel* statusBar)
60 {
61  if(_opened)
62  {
63  statusBar->setText("Połączono z portem szeregowym | Nazwa portu: " + _device->portName());
64  }
65  else
66  {
67  statusBar->setText("Rozłączono z portem szeregowym");
68  }
69 }
70 
72 {
73  while(_continue)
74  {
75  while(_device->waitForReadyRead())
76  {
77  while(_device->canReadLine())
78  {
79  QByteArray line = _device->readLine();
80  if(line[line.length() - 1] == '\n')
81  {
82  line[line.length() - 1] = 0;
83  }
84  if(line[line.length() - 2] == '\r')
85  {
86  line[line.length() - 2] = 0;
87  }
88  _frame.addFrame(line.toStdString());
89  }
90  }
91  }
92 }
void receiveData()
Receive line one by one in loop until end of communication.
bool closeSerialPort()
Close serial port.
~Communication()
Destruct Communication object.
QSerialPort * _device
Serial device handle.
bool _continue
Identifier whether to continue communicating with the serial.
Communication()
Construct new Communication object.
bool openSerialPort()
Wraper function for opening serial port.
void setStatus(QLabel *statusBar)
Set status of port openning on status bar.
void endCommunication()
End communicaiton with serial device.
bool isOpen()
Check wether serial poet is open.
FrameBuffer _frame
Frames buffer.
bool _opened
Identifier whether serial device is open.
void addFrame(const std::string &frame)
Add one frame to the end of the list.
Definition: frameBuffer.cpp:22
Header file for Communication class.