#ifndef SERIALWORKER_H
#define SERIALWORKER_H

#include <QObject>
#include <QSerialPort>
#include <QString>
#include <QByteArray>
#include <QDebug>
#include <QThread>
#include <QMutex>

class SerialWorker : public QObject
{
    Q_OBJECT
public:
    explicit SerialWorker(QObject *parent = NULL);
    ~SerialWorker();

public slots:

    void init_port(QString comName);  //初始化串口
    void open_serial_port();
    void close_serial_port();
    void handle_data();  //处理接收到的数据
    void write_data(QByteArray SendBytes);     //发送数据

    void change_send_flag();
    void change_test_start_flag_slot();

signals:
    //接收数据
    void receive_data(QByteArray tmp);
    void chang_openbtn_to_close();
    void show_success_msgbox();
    void show_failed_msgbox();
    void close_serial_port_success();

    void write_target_success();
    void write_target_failed();

    void serial_port_not_open();
    void set_stage1_edit_box(unsigned char, float, float, uint16_t, unsigned char);
    void set_stage2_edit_box(unsigned char, float, float, uint16_t, unsigned char);
    void set_stage3_edit_box(unsigned char, float, float, uint16_t, unsigned char);
    void set_stage4_edit_box(unsigned char, float, float, uint16_t, unsigned char);
    void set_stage5_edit_box(unsigned char, float, float, uint16_t, unsigned char);
    void set_stage6_edit_box(unsigned char, float, float, uint16_t, unsigned char);

    void show_checkbox_state(unsigned char);
    void show_run_state_box_state(unsigned char);

private:
    QThread *serial_thread;
    QSerialPort *port;
    bool m_bOpen;
    QByteArray m_recBuf;

    QString m_comName;
    QMutex mutex;

    char m_write_flag;
    char m_read_flag;
    char m_crc_flag;
    int m_framecount;
    int m_whole_len;
    int m_data_len;

    int m_send_flag;
    int m_test_start_flag;
    QByteArray m_dealSendBytes;

    QMap<float, double> m_qmap_fd;
    // QVector<float> m_qvecf_time;
    // QVector<float> m_qvecf_cur_time;
    // QVector<double> m_qvecd_temper;
    // QVector<double> m_qvecd_dsc;
    // QVector<double> m_qvecd_riserate;
    // QVector<float> m_qvecf_cooltemper;

    char m_run_state_continue;
    union{
        float f;
        unsigned char x[4];
    }data_t;
    union{
        float f;
        unsigned char x[4];
    }data_curaccum_t;
    union{
        double d;
        unsigned char x[8];
    }data_temper;
    union{
        double d;
        unsigned char x[8];
    }data_dsc;
    union{
        double d;
        unsigned char x[8];
    }data_riserate;
    union{
        float f;
        unsigned char x[4];
    }data_cooltemper;

    char m_run_state;

    unsigned char m_stage1_state;
    union{
        float f;
        unsigned char x[4];
    }stage1_until;
    union{
        float f;
        unsigned char x[4];
    }stage1_riserate;
    uint16_t m_stage1_time;
    unsigned char m_stage1_airchoose;

    unsigned char m_stage2_state;
    union{
        float f;
        unsigned char x[4];
    }stage2_until;
    union{
        float f;
        unsigned char x[4];
    }stage2_riserate;
    uint16_t m_stage2_time;
    unsigned char m_stage2_airchoose;

    unsigned char m_stage3_state;
    union{
        float f;
        unsigned char x[4];
    }stage3_until;
    union{
        float f;
        unsigned char x[4];
    }stage3_riserate;
    uint16_t m_stage3_time;
    unsigned char m_stage3_airchoose;

    unsigned char m_stage4_state;
    union{
        float f;
        unsigned char x[4];
    }stage4_until;
    union{
        float f;
        unsigned char x[4];
    }stage4_riserate;
    uint16_t m_stage4_time;
    unsigned char m_stage4_airchoose;

    unsigned char m_stage5_state;
    union{
        float f;
        unsigned char x[4];
    }stage5_until;
    union{
        float f;
        unsigned char x[4];
    }stage5_riserate;
    uint16_t m_stage5_time;
    unsigned char m_stage5_airchoose;

    unsigned char m_stage6_state;
    union{
        float f;
        unsigned char x[4];
    }stage6_until;
    union{
        float f;
        unsigned char x[4];
    }stage6_riserate;
    uint16_t m_stage6_time;
    unsigned char m_stage6_airchoose;

    union{
        float f;
        unsigned char x[4];
    }fit_coff_a;
    union{
        float f;
        unsigned char x[4];
    }fit_coff_b;
    union{
        float f;
        unsigned char x[4];
    }fit_coff_c;


    // char m_stage2_state;
    // char m_stage3_state;
    // char m_stage4_state;
    // char m_stage5_state;

    uint16_t ModbusCRC16(QByteArray senddata);
    uint16_t CRC16_MODBUS(unsigned char *data, unsigned int datalen);
    void InvertUint16(unsigned short *dBuf, unsigned short *srcBuf);
};


#endif // SERIALWORKER_H
