mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
Add VISA lib
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef TCPCLIENT_H
|
||||
#define TCPCLIENT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTcpSocket>
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "visa.hpp"
|
||||
#include <QCoreApplication>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef VISA_HPP
|
||||
#define VISA_HPP
|
||||
|
||||
#include "tcp_client.hpp"
|
||||
#include <QObject>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user