Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ project(solar-car-dashboard)

# Find the required Qt packages
find_package(QT NAMES Qt5 Qt6 COMPONENTS Core Quick)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick SerialPort REQUIRED)

message(----------QT-VERSION:${QT_VERSION_MAJOR}-----------)
# Set C++ standard to C++20
Expand All @@ -23,6 +23,7 @@ set(SOURCES
telemetrylib/TCP.cpp
telemetrylib/Telemetry.cpp
telemetrylib/SQL.cpp
telemetrylib/Serial.cpp
gps/gps.cpp
3rdparty/serial/serialib.cpp
)
Expand Down Expand Up @@ -50,5 +51,5 @@ qt_add_resources(RESOURCES
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS} ${RESOURCES})

# Link against the required Qt modules
target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::SerialPort)
target_include_directories(${PROJECT_NAME} PRIVATE ./)
6 changes: 4 additions & 2 deletions backendprocesses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ void BackendProcesses::comm_status(bool s) {
}

void BackendProcesses::startThread() {
std::vector<DTI*> obj(2); //create a bunch of DTI instances and add them into this array in order of priority to be sent to telemetrylib
std::vector<DTI*> obj(3); //create a bunch of DTI instances and add them into this array in order of priority to be sent to telemetrylib
long long first_msec = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();

obj[0]=new SQL(QString::fromStdString(std::to_string(first_msec))); //This sends data to the cloud server
obj[1]=new UDP(QHostAddress("192.168.1.18"), 4003); //This sends data to the chase car
obj[2] = new Serial("/dev/ttyS0");

this->tel = new Telemetry(obj);
connect(this->tel, &Telemetry::eng_dash_connection, this, &BackendProcesses::comm_status); //for notifing the system connection status
}
Expand Down Expand Up @@ -104,7 +106,7 @@ void BackendProcesses::threadProcedure()
all_bytes_in_minute.push_back("</bsr>");

// only output the file when our buffer has reached
if (all_bytes_in_minute.size() >= 10000 || min_time != last_minute) {
if (all_bytes_in_minute.size() >= 100000 || min_time != last_minute) {
std::ofstream(basePath.toStdString() + std::to_string(curr_msec) + "_all_bytes.bin", std::ios::binary)
.write(all_bytes_in_minute.data(), all_bytes_in_minute.size());
last_minute = min_time;
Expand Down
1 change: 1 addition & 0 deletions backendprocesses.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "telemetrylib/TCP.cpp"
#include "telemetrylib/SQL.cpp"
#include "telemetrylib/UDP.cpp"
#include "telemetrylib/Serial.cpp"

struct timestampOffsets {
int hr;
Expand Down
93 changes: 93 additions & 0 deletions telemetrylib/Serial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "DTI.h"
#include <thread>
#include <QSerialPort>
#include <QTimer>
#include <QSocketNotifier>

class Serial : public DTI {
public:
Serial(QString SerialDevice) {
// Initialize serial port with the provided device name
device = SerialDevice;
serial.setPortName(SerialDevice);

// Set Baud rate, Data bits, Parity, Stop bits, and Flow control
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);

// Connect readyRead signal to a slot for reading incoming data
connect(&serial, &QSerialPort::readyRead, this, &Serial::readData);

// Connect errorOccurred signal to handle errors, such as device unplugged
connect(&serial, QOverload<QSerialPort::SerialPortError>::of(&QSerialPort::errorOccurred),
this, &Serial::handleError);

// Setup timer for checking serial port status and reconnecting if necessary
connect(&reconnectTimer, &QTimer::timeout, this, &Serial::checkConnection);
reconnectTimer.start(5000); // Check every 5 seconds
}

~Serial() {
// Close the serial port upon destruction
serial.close();
}

void sendData(QByteArray bytes, long long timestamp) override {
qDebug()<<"sending via Serial";
bytes.push_front("<bsr>");
bytes.push_back("</bsr>");
// Write data to the serial port
int returnCode = serial.write(bytes);
if (returnCode == -1) {
qDebug()<<"Error occurred send data";
serial.close();
}
serial.flush();
}

private slots:
void readData() {
// Read data from the serial port when data is available
QByteArray data = serial.readAll();
// Process the received data as needed
processReceivedData(data);
}

void checkConnection() {
if (!serial.isOpen()) {
qDebug() << "Serial port disconnected. Reconnecting...";
serial.close();
serial.open(QIODevice::ReadWrite);
if (!serial.isOpen()) {
qDebug() << "Failed to reconnect to serial port";
}
} else {
qDebug() << "Still open";
}
}

void handleError(QSerialPort::SerialPortError error) {
if (error == QSerialPort::ResourceError) {
qDebug() << "Serial port error occurred. Reconnecting...";
// Attempt to reconnect
serial.close();
serial.open(QIODevice::ReadWrite);
if (!serial.isOpen()) {
qDebug() << "Failed to reconnect to serial port";
}
}
}

private:
QSerialPort serial;
QString device;
QTimer reconnectTimer;

void processReceivedData(const QByteArray &data) {
// Implement your data processing logic here
qDebug() << "Received data:" << data;
}
};