48 lines
1.4 KiB
C++
48 lines
1.4 KiB
C++
#ifndef SERIALPORTMANAGER_H
|
|
#define SERIALPORTMANAGER_H
|
|
|
|
#include <QObject>
|
|
#include <QSerialPort>
|
|
|
|
|
|
class SerialPortManager : public QObject {
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit SerialPortManager(QObject *parent = nullptr);
|
|
~SerialPortManager();
|
|
|
|
enum ConnectionMode {
|
|
Manual,
|
|
Auto
|
|
};
|
|
|
|
void setConnectionMode(ConnectionMode mode);
|
|
void configureSerialPort(const QString &portName, int baudRate, int dataBits, int stopBits, int parity);
|
|
bool connectSerialPort();
|
|
void disconnectSerialPort();
|
|
QSerialPort* getSerialPort() const;
|
|
QByteArray hexStringToByteArray(const QString &hex);
|
|
void sendModbusCommand(const QString &hexCommand);
|
|
double receiveModbusResponse(int RESPONSE_LENGTH);
|
|
double sendModbus_receive(const QString &hexCommand, int RESPONSE_LENGTH);
|
|
bool sendModbus_receive_2(const QString &hexCommand, int RESPONSE_LENGTH);
|
|
|
|
private:
|
|
QSerialPort *serialPort;
|
|
ConnectionMode connectionMode;
|
|
QString portName;
|
|
int baudRate;
|
|
int dataBits;
|
|
int stopBits;
|
|
int parity;
|
|
|
|
QString autoConnect(const QByteArray &command, const QByteArray &expectedResponse);
|
|
bool testPort(QSerialPort &port, const QByteArray &command, const QByteArray &expectedResponse);
|
|
uint16_t CRC16_Calculate(const QByteArray &data);
|
|
QByteArray command = "01040000000131CA";
|
|
QByteArray expectedResponse = "010402";
|
|
};
|
|
|
|
#endif // SERIALPORTMANAGER_H
|