Skip to content

Commit 3b20175

Browse files
committed
Add applications
1 parent 814a40f commit 3b20175

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+4954
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
QT -= gui
2+
3+
CONFIG += c++11 console
4+
CONFIG -= app_bundle
5+
6+
DEFINES += QT_DEPRECATED_WARNINGS
7+
8+
SOURCES += \
9+
main.cpp \
10+
boardmodel.cpp \
11+
boardv1.cpp \
12+
serialport.cpp
13+
14+
HEADERS += \
15+
boardmodel.h \
16+
boardv1.h \
17+
serialport.h

AbstractClassNamedBoard/AbstractClassNamedBoard.pro.user

Lines changed: 323 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "boardmodel.h"
2+
3+
BoardModel::~BoardModel() { }
4+
5+
void BoardModel::SetPortName(std::string value) { port_name = value; }
6+
std::string BoardModel::GetPortName() { return port_name; }
7+
void BoardModel::SetBaudRate(int value) { baud_rate = value; }
8+
int BoardModel::GetBaudRate() { return baud_rate; }
9+
void BoardModel::SetDataLength(uint value) { data_length = value; }
10+
uint BoardModel::GetDataLength() { return data_length; }
11+
void BoardModel::SetSensorId1(unsigned char id) { sensor_id_1 = id; }
12+
unsigned char BoardModel::GetSensorId1() { return sensor_id_1; }
13+
void BoardModel::SetSensorId2(unsigned char id) { sensor_id_2 = id; }
14+
unsigned char BoardModel::GetSensorId2() { return sensor_id_2; }

AbstractClassNamedBoard/boardmodel.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef BOARDMODEL_H
2+
#define BOARDMODEL_H
3+
4+
#include <iostream>
5+
#include <vector>
6+
7+
class BoardModel
8+
{
9+
public:
10+
virtual ~BoardModel() = 0;
11+
virtual bool Open() = 0;
12+
virtual bool IsOpen() = 0;
13+
virtual void Close() = 0;
14+
virtual std::vector<unsigned char> Read() = 0;
15+
16+
void SetPortName(std::string value);
17+
std::string GetPortName();
18+
void SetBaudRate(int value);
19+
int GetBaudRate();
20+
void SetDataLength(uint value);
21+
uint GetDataLength();
22+
void SetSensorId1(unsigned char id);
23+
unsigned char GetSensorId1();
24+
void SetSensorId2(unsigned char id);
25+
unsigned char GetSensorId2();
26+
27+
private:
28+
std::string port_name;
29+
int baud_rate;
30+
uint data_length;
31+
unsigned char sensor_id_1;
32+
unsigned char sensor_id_2;
33+
};
34+
35+
#endif // BOARDMODEL_H

AbstractClassNamedBoard/boardv1.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "boardv1.h"
2+
3+
bool BoardV1::Open()
4+
{
5+
bool state = false;
6+
7+
try {
8+
state = serial_port.Open(GetPortName(), GetBaudRate());
9+
} catch (std::exception& e) {
10+
state = false;
11+
}
12+
13+
return state;
14+
}
15+
16+
bool BoardV1::IsOpen()
17+
{
18+
bool state = false;
19+
20+
try {
21+
state = serial_port.IsOpen();
22+
} catch (std::exception& e) {
23+
state = false;
24+
}
25+
26+
return state;
27+
}
28+
29+
void BoardV1::Close()
30+
{
31+
try {
32+
serial_port.Close();
33+
} catch (std::exception& e) {
34+
35+
}
36+
}
37+
38+
std::vector<unsigned char> BoardV1::Read()
39+
{
40+
std::vector<unsigned char> data;
41+
42+
try {
43+
data = serial_port.Read(GetDataLength(), GetSensorId1(), GetSensorId2());
44+
} catch (std::exception& e) {
45+
data.clear();
46+
}
47+
48+
return data;
49+
}

AbstractClassNamedBoard/boardv1.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef BOARDV1_H
2+
#define BOARDV1_H
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <cmath>
7+
#include <chrono>
8+
#include "boardmodel.h"
9+
#include "serialport.h"
10+
11+
class BoardV1 : public BoardModel
12+
{
13+
public:
14+
~BoardV1() { }
15+
bool Open();
16+
bool IsOpen();
17+
void Close();
18+
std::vector<unsigned char> Read();
19+
20+
private:
21+
SerialPort serial_port;
22+
};
23+
24+
#endif // BOARDV1_H

AbstractClassNamedBoard/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <QCoreApplication>
2+
#include "boardmodel.h"
3+
#include "boardv1.h"
4+
5+
int main(int argc, char *argv[])
6+
{
7+
QCoreApplication a(argc, argv);
8+
9+
BoardModel *board_model = new BoardV1();
10+
board_model->SetBaudRate(3000000);
11+
board_model->SetDataLength(10);
12+
board_model->SetSensorId1(253);
13+
board_model->SetSensorId2(254);
14+
15+
board_model->Open();
16+
if (board_model->IsOpen()) {
17+
std::vector<unsigned char> read_data = board_model->Read();
18+
std::cout << "Read Data : ";
19+
for (uint i = 0; i < read_data.size(); i++)
20+
std::cout << read_data.at(i) << " ";
21+
std::cout << std::endl;
22+
}
23+
board_model->Close();
24+
25+
delete board_model;
26+
27+
return a.exec();
28+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include "serialport.h"
2+
3+
bool SerialPort::Open(std::string port_path, int baud_rate)
4+
{
5+
bool state = false;
6+
7+
try {
8+
if (IsOpen())
9+
Close();
10+
11+
const char* c_port_path = port_path.c_str();
12+
fd = open(c_port_path, O_RDWR | O_NOCTTY | O_SYNC);
13+
14+
struct termios tty;
15+
if (tcgetattr(fd, &tty) < 0) {
16+
fd = -1;
17+
state = false;
18+
}
19+
20+
if (baud_rate == 9600) {
21+
cfsetospeed(&tty, (speed_t)B9600);
22+
cfsetispeed(&tty, (speed_t)B9600);
23+
} else if (baud_rate == 230400) {
24+
cfsetospeed(&tty, (speed_t)B230400);
25+
cfsetispeed(&tty, (speed_t)B230400);
26+
} else if (baud_rate == 3000000) {
27+
cfsetospeed(&tty, (speed_t)B3000000);
28+
cfsetispeed(&tty, (speed_t)B3000000);
29+
}
30+
31+
tty.c_cflag |= (CLOCAL | CREAD);
32+
tty.c_cflag &= ~CSIZE;
33+
tty.c_cflag |= CS8;
34+
tty.c_cflag &= ~PARENB;
35+
tty.c_cflag &= ~CSTOPB;
36+
tty.c_cflag &= ~CRTSCTS;
37+
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
38+
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
39+
tty.c_oflag &= ~OPOST;
40+
tty.c_cc[VMIN] = 1;
41+
tty.c_cc[VTIME] = 1000;
42+
43+
if ((tcsetattr(fd, TCSANOW, &tty)) != 0) {
44+
fd = -1;
45+
state = false;
46+
}
47+
tcflush(fd, TCIFLUSH);
48+
state = true;
49+
} catch (std::exception& e) {
50+
state = false;
51+
}
52+
53+
return state;
54+
}
55+
56+
bool SerialPort::IsOpen()
57+
{
58+
if (fd >= 0)
59+
return true;
60+
61+
return false;
62+
}
63+
64+
void SerialPort::Close()
65+
{
66+
try {
67+
close(fd);
68+
fd = -1;
69+
} catch (std::exception& e) {
70+
71+
}
72+
}
73+
74+
std::vector<unsigned char> SerialPort::Read(uint length, unsigned char first_byte, unsigned char second_byte)
75+
{
76+
std::vector<unsigned char> read_data;
77+
78+
try {
79+
unsigned char buffer[length];
80+
int num_to_read = static_cast<int>(length);
81+
int read_data_length = 0;
82+
int total_read = 0;
83+
84+
while (num_to_read > 0) {
85+
read_data_length = read(fd, &buffer[total_read], num_to_read);
86+
87+
if (read_data_length > 0 && (buffer[0] == first_byte || buffer[0] == second_byte)) {
88+
total_read += read_data_length;
89+
num_to_read -= read_data_length;
90+
}
91+
}
92+
93+
for (uint i = 0; i < length; i++)
94+
read_data.push_back(buffer[i]);
95+
96+
} catch (std::exception& e) {
97+
read_data.clear();
98+
}
99+
100+
return read_data;
101+
}

AbstractClassNamedBoard/serialport.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SERIALPORT_H
2+
#define SERIALPORT_H
3+
4+
#include <iostream>
5+
#include <thread>
6+
#include <chrono>
7+
#include <vector>
8+
#include <unistd.h>
9+
#include <fcntl.h>
10+
#include <termios.h>
11+
12+
class SerialPort
13+
{
14+
public:
15+
bool Open(std::string port_path, int baud_rate);
16+
bool IsOpen();
17+
void Close();
18+
std::vector<unsigned char> Read(uint length, unsigned char first_byte, unsigned char second_byte);
19+
20+
private:
21+
const int error_count = 25;
22+
const unsigned char error_data_code = 0;
23+
const unsigned char error_usb_code = 255;
24+
25+
int fd = -1;
26+
};
27+
28+
#endif // SERIALPORT_H
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
QT -= gui
2+
3+
CONFIG += c++11 console
4+
CONFIG -= app_bundle
5+
6+
DEFINES += QT_DEPRECATED_WARNINGS
7+
8+
SOURCES += \
9+
main.cpp \
10+
basedata.cpp \
11+
deriveddata.cpp
12+
13+
HEADERS += \
14+
basedata.h \
15+
deriveddata.h

0 commit comments

Comments
 (0)