134 lines
3.3 KiB
C++
134 lines
3.3 KiB
C++
#ifndef SERIAL_EQUIPMENT_H
|
||
#define SERIAL_EQUIPMENT_H
|
||
|
||
#include <QtSerialPort/QSerialPort>
|
||
#include <QtSerialPort/QSerialPortInfo>
|
||
#include <QList>
|
||
#include <QTime>
|
||
#include <QCoreApplication>
|
||
#include <QEventLoop>
|
||
#include <QDebug>
|
||
#include <QMutex>
|
||
#include <QThread>
|
||
#include <QFile>
|
||
#include "XYComFunc_global.h"
|
||
//#include "CommomFunc/tool.h"
|
||
//#include "CommomFunc/datacheck.h"
|
||
#include "commonStruct.h"
|
||
//串口设备类
|
||
//实现串口通讯,目前支持固定包头固定长度的帧的解析。若非固定包头固定长度,请重写接收事件
|
||
|
||
|
||
/**
|
||
* @brief 串口设备类
|
||
* 实现串口通讯,默认支持固定包头包围,checksum,下标01为数据包长度的变长协议包,若不适用,清重写dataReceive事件
|
||
*
|
||
*/
|
||
class XYCOMFUNC_EXPORT SerialEquipment:public QObject
|
||
{
|
||
|
||
Q_OBJECT
|
||
|
||
Q_SIGNALS:
|
||
//帧接收事件
|
||
void onFrameReceived(QByteArray *receiveBuffer);
|
||
void onFrameSended(QByteArray *sendBuffer);
|
||
void onDataReceived(QByteArray *revBuffer);
|
||
|
||
|
||
public:
|
||
//默认构造
|
||
SerialEquipment();
|
||
//构造
|
||
SerialEquipment(QString portName,QSerialPort::BaudRate baunRate,QSerialPort::DataBits dataBits,QSerialPort::Parity parity,QSerialPort::StopBits stopBits,int readBufferSize);
|
||
|
||
SerialEquipment(QString portName);
|
||
|
||
SerialEquipment(QString portName,QSerialPort::BaudRate baunRate);
|
||
~SerialEquipment();
|
||
//设置串口参数
|
||
void setSerialInfo(QString portName,QSerialPort::BaudRate baunRate,QSerialPort::DataBits dataBits,QSerialPort::Parity parity,QSerialPort::StopBits stopBits,int readBufferSize);
|
||
void setSerialInfo(QString portName, QSerialPort::BaudRate baunRate);
|
||
//打开串口
|
||
bool openPort();
|
||
//关闭串口
|
||
bool closePort();
|
||
//获取串口状态
|
||
bool getPortState();
|
||
//设置帧头
|
||
void setFrameHead(QByteArray frameHead);
|
||
void setFrameTail(QByteArray frameTail);
|
||
//设置帧长
|
||
void setFrameSize(int iFramSize);
|
||
//发送指令
|
||
bool sendMsg(QString sendBuffer);
|
||
bool sendMsg(QByteArray buffer);
|
||
|
||
void setLogInfo(bool isLog);
|
||
|
||
|
||
SerialLogInfo *GetLogInfo();
|
||
void delayMsec(int mesc);
|
||
|
||
static QList<QString> getAllPorts();
|
||
|
||
|
||
Q_INVOKABLE void setNeedWait(bool value);
|
||
|
||
protected:
|
||
//串口类
|
||
QSerialPort *_serialPort;
|
||
//串口号
|
||
QString _strPortName;
|
||
//用于暂存帧信息的List
|
||
QByteArray _tempArray;
|
||
//QList<char> tempList;
|
||
//波特率
|
||
QSerialPort::BaudRate _iBaudRate;
|
||
//数据位
|
||
QSerialPort::DataBits _iDataBits;
|
||
//校验位
|
||
QSerialPort::Parity _iParity;
|
||
//停止位
|
||
QSerialPort::StopBits _iStopBits;
|
||
//缓冲区读取大小
|
||
int _iReadBufferSize;
|
||
//帧长度
|
||
int _iFrameSize;
|
||
//暂存上byte
|
||
char _lastByte;
|
||
//帧头
|
||
QByteArray _frameHead;
|
||
QByteArray _frameTail;
|
||
|
||
SerialLogInfo *_logInfo;
|
||
|
||
bool _isLog;
|
||
QMutex* _mutex;
|
||
|
||
int testJDCount;
|
||
int testTotalCount;
|
||
bool bTestDebug;
|
||
|
||
bool needWait;
|
||
|
||
|
||
protected:
|
||
//出现异常帧时,用于调整帧内容
|
||
QList<char> adjustList(QList<char> srcList);
|
||
QByteArray adjustList(QByteArray srcList);
|
||
void dataReceive1();
|
||
virtual void dealDate(QByteArray);
|
||
|
||
|
||
protected slots:
|
||
//串口接收事件
|
||
virtual void dataReceive();
|
||
|
||
virtual bool checkData(QByteArray buffer,int iTailIndex);
|
||
|
||
|
||
};
|
||
|
||
#endif // SERIAL_EQUIPMENT_H
|