|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QSerialPort>
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include "frame/Frame.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Device : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
Device();
|
|
|
|
|
Device(QString portName, QString baudRate, QString dataBits, QString stopBits, QString parity);
|
|
|
|
|
virtual ~Device();
|
|
|
|
|
|
|
|
|
|
void SetPortName(QString portName) { m_portName = portName; }
|
|
|
|
|
void SetBaudRate(QString baudRate) { m_baudRate = baudRate; }
|
|
|
|
|
void SetDataBits(QString dataBits) { m_dataBits = dataBits; }
|
|
|
|
|
void SetStopBits(QString stopBits) { m_stopBits = stopBits; }
|
|
|
|
|
void SetParity(QString parity) { m_parity = parity; }
|
|
|
|
|
|
|
|
|
|
QString GetPortName() { return m_portName; }
|
|
|
|
|
QString GetBaudRate() { return m_baudRate; }
|
|
|
|
|
QString GetDataBits() { return m_dataBits; }
|
|
|
|
|
QString GetStopBits() { return m_stopBits; }
|
|
|
|
|
QString GetParity() { return m_parity; }
|
|
|
|
|
|
|
|
|
|
bool Open();
|
|
|
|
|
bool Close();
|
|
|
|
|
bool IsOpen() { return m_serialPort.isOpen(); }
|
|
|
|
|
bool SendData(int cmd, std::string str = "", int msec=1000);
|
|
|
|
|
bool SendData(int cmd, Data *data, int msec);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ClearRecvData();
|
|
|
|
|
|
|
|
|
|
// 接受数据定时器
|
|
|
|
|
void InitRecvTimer(int msec=1000);
|
|
|
|
|
void StartRecvTimer(int msec=1000);
|
|
|
|
|
void StopRecvTimer();
|
|
|
|
|
|
|
|
|
|
// 解析线程
|
|
|
|
|
void startParseThread();
|
|
|
|
|
void stopParseThread();
|
|
|
|
|
void runParseThread();
|
|
|
|
|
|
|
|
|
|
signals: // 信号
|
|
|
|
|
//void recv_data(QString portName, QByteArray data);
|
|
|
|
|
void recv_data(QString portName, unsigned char cmd, QByteArray data);
|
|
|
|
|
void recv_timeout(QString portName);
|
|
|
|
|
|
|
|
|
|
public slots: // 槽
|
|
|
|
|
// 接收数据
|
|
|
|
|
void slot_readData();
|
|
|
|
|
|
|
|
|
|
// 接受数据定时器
|
|
|
|
|
void slot_recvTimer();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QSerialPort::BaudRate convertBaudRate(QString baudRate);
|
|
|
|
|
QSerialPort::DataBits convertDataBits(QString dataBits);
|
|
|
|
|
QSerialPort::StopBits convertStopBits(QString stopBits);
|
|
|
|
|
QSerialPort::Parity convertParity(QString parity);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QSerialPort m_serialPort; // 串口
|
|
|
|
|
|
|
|
|
|
QString m_portName; // 串口名
|
|
|
|
|
QString m_baudRate; // 波特率
|
|
|
|
|
QString m_dataBits; // 数据位
|
|
|
|
|
QString m_stopBits; // 停止位
|
|
|
|
|
QString m_parity; // 校验位
|
|
|
|
|
|
|
|
|
|
QByteArray m_recvData;
|
|
|
|
|
QMutex m_recvMutex;
|
|
|
|
|
|
|
|
|
|
// 接收数据定时器
|
|
|
|
|
QTimer m_recvTimer;
|
|
|
|
|
|
|
|
|
|
// 解析线程
|
|
|
|
|
std::thread m_parseThread;
|
|
|
|
|
std::mutex m_parseMutex;
|
|
|
|
|
bool m_bRunParseThread;
|
|
|
|
|
|
|
|
|
|
// 日志重复打印问题处理
|
|
|
|
|
QString m_lastLogString; // 最后一次打印的日志数据
|
|
|
|
|
int m_lastLogTimes; // 最后一次打印的日志次数
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|