diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 490d47b..ca96cf5 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -4,7 +4,7 @@ endif() set(CMAKE_AUTOMOC ON) -find_package(Qt6 REQUIRED COMPONENTS Core WebSockets SerialBus) +find_package(Qt6 REQUIRED COMPONENTS Core WebSockets SerialBus Network) qt_standard_project_setup() qt_add_executable(server) @@ -35,6 +35,7 @@ endif() target_link_libraries(server PRIVATE Qt::WebSockets Qt::SerialBus + Qt::Network magic_enum::magic_enum spdlog UI diff --git a/server/VISA/tcp_client.cpp b/server/VISA/tcp_client.cpp new file mode 100644 index 0000000..e121a7a --- /dev/null +++ b/server/VISA/tcp_client.cpp @@ -0,0 +1,30 @@ +#include "tcp_client.hpp" + +Tcp_client::Tcp_client() : QObject(0) +{ + m_tcp_socket = new QTcpSocket(this); + + connect(m_tcp_socket, &QTcpSocket::readyRead, this, [=, this] { ready_to_read(); }); + connect(m_tcp_socket, &QTcpSocket::connected, this, [=, this] { emit connected(); }); + connect(m_tcp_socket, &QTcpSocket::disconnected, this, [=, this] { emit disconnected(); }); +} + +void Tcp_client::tcp_connect(QString ip, quint16 port) +{ + m_tcp_socket->connectToHost(ip, port); +} + +void Tcp_client::tcp_disconnect() +{ + m_tcp_socket->disconnectFromHost(); +} + +void Tcp_client::tcp_send(QString data) +{ + m_tcp_socket->write((const char *)data.data()); +} + +void Tcp_client::ready_to_read() +{ + emit data_ready(m_tcp_socket->read(1024)); +} diff --git a/server/VISA/tcp_client.hpp b/server/VISA/tcp_client.hpp new file mode 100644 index 0000000..79dcad6 --- /dev/null +++ b/server/VISA/tcp_client.hpp @@ -0,0 +1,28 @@ +#ifndef TCPCLIENT_H +#define TCPCLIENT_H + +#include +#include + +#define QD qDebug() << "File:" << __FILE__ << "Function:" << __PRETTY_FUNCTION__ << "Line:" << __LINE__ << "=>" + +class Tcp_client : public QObject +{ + Q_OBJECT + public: + Tcp_client(); + void tcp_connect(QString ip, quint16 port); + void tcp_disconnect(void); + void tcp_send(QString data); + + signals: + void data_ready(QString data); + void connected(); + void disconnected(); + + private: + void ready_to_read(); + QTcpSocket *m_tcp_socket; +}; + +#endif // TCPCLIENT_H diff --git a/server/VISA/visa.cpp b/server/VISA/visa.cpp new file mode 100644 index 0000000..5ed1ba6 --- /dev/null +++ b/server/VISA/visa.cpp @@ -0,0 +1,70 @@ +#include "visa.hpp" +#include + +Visa::Visa(QObject *parent) : QObject(parent), m_connected(false), m_reconnect(false), m_data_ready(false) +{ + QObject::connect(&m_client, &Tcp_client::connected, this, [=, this] { + m_connected = true; + emit s_connected(); + }); + QObject::connect(&m_client, &Tcp_client::disconnected, this, [=, this] { + m_connected = false; + emit s_disconnected(); + if (m_reconnect) + { + connect(m_ip, m_port); + m_reconnect = false; + } + }); + QObject::connect(&m_client, &Tcp_client::data_ready, this, [=, this](QString data) { + m_data = data; + m_data_ready = true; + }); +} +Visa::~Visa() +{ + if (m_connected) + m_client.disconnect(); +} + +void Visa::connect(QString ip, quint16 port) +{ + if (m_connected) + { + m_client.disconnect(); + m_ip = ip; + m_port = port; + m_reconnect = true; + return; + } + m_client.tcp_connect(ip, port); +} + +void Visa::disconnect() +{ + m_client.tcp_disconnect(); +} + +bool Visa::connected() const +{ + return m_connected; +} + +void Visa::send(QString data) +{ + m_client.tcp_send(data); +} + +QString Visa::querry(QString data) +{ + uint32_t counter = 0; + m_data.clear(); + send(data); + while (!m_data_ready || counter > 1024) + { + QCoreApplication::processEvents(); + counter++; + } + + return m_data; +} diff --git a/server/VISA/visa.hpp b/server/VISA/visa.hpp new file mode 100644 index 0000000..714b3b3 --- /dev/null +++ b/server/VISA/visa.hpp @@ -0,0 +1,35 @@ +#ifndef VISA_HPP +#define VISA_HPP + +#include "tcp_client.hpp" +#include + +class Visa : public QObject +{ + Q_OBJECT + public: + explicit Visa(QObject *parent = nullptr); + ~Visa(); + void connect(QString ip, quint16 port); + void disconnect(); + + bool connected() const; + + void send(QString data); + QString querry(QString data); + + signals: + void s_disconnected(); + void s_connected(); + + private: + Tcp_client m_client; + QString m_ip; + quint16 m_port; + bool m_connected; + bool m_reconnect; + bool m_data_ready; + QString m_data; +}; + +#endif // VISA_HPP