#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QObject>
#include <QSerialPort>
#include <QMutex>

class SerialPort : public QObject
{
    Q_OBJECT
public:
    explicit SerialPort(QObject *parent = nullptr);
    ~SerialPort();

    //    Q_INVOKABLE bool serialConnect(QString port,QString baudrate,QString databits,QString parity,QString stopbits);
    //    Q_INVOKABLE void closeConnect();
    //    Q_INVOKABLE bool serialWrite(QString sendStr);
    //    Q_INVOKABLE bool serialWriteReponse(QString sendStr);
    //    Q_INVOKABLE QString serialRead();

    bool serialConnect(QString port,QString baudrate,QString databits,QString parity,QString stopbits);
    void closeConnect();
    //bool serialWrite(QString sendStr);
    //bool serialWriteReponse(QString sendStr);
    QByteArray serialRead();
    bool getOpenStatus();

signals:
    void sendPortStatus(bool status);
    void sendReceiveData();
    void sendCrcError();
    void sendTimeout();



public slots:
    void receiveConnect(QString port,QString baudrate,QString databits,QString parity,QString stopbits);
    void receiveWrite(QByteArray sendData);
    void receiveOnlyWrite(QByteArray sendData);
    void reveiveClose();
    void receiveInit();
    //void recieveData();

private:
    QSerialPort *m_serial;
    QByteArray mData;
    QMutex mutex;

    void crcCheck(uint8_t *data, int length,uint8_t *crc_h,uint8_t *crc_l);


};

#endif // SERIALPORT_H
