#ifndef SERIAL_PORT_H
#define SERIAL_PORT_H
#include <cstdint>
#include <string>
#include <thread>
#include <vector>
#include <windows.h>

namespace zsy
{
    class SerialPort
    {
    public:
        class Processor
        {
        public:
            virtual uint32_t process(const std::vector<uint8_t> &buffer) = 0;

            virtual ~Processor() = default;
        };

        Processor *processor;
        // 0-->不可用，1-->已打开，2-->已关闭，3-->错误
        std::atomic<uint8_t> status;
        // 0-->不可用，1-->正在读取，2-->已停止，3-->错误
        std::atomic<uint8_t> readStatus;

        SerialPort(
            const std::string &port,
            uint32_t baudRate,
            uint8_t byteSize,
            uint8_t parity,
            uint8_t stopBits,
            Processor *processor);
        ~SerialPort();

    private:
        std::vector<uint8_t> *buffer;

        std::string port;
        uint32_t baudRate;
        uint8_t byteSize;
        uint8_t parity;
        uint8_t stopBits;
        std::thread *readThread;
        HANDLE com;
        OVERLAPPED waitOverlapped; //用于等待数据
        OVERLAPPED readOverlapped; //用于读取数据
        void read();
    };
} // zsy

#endif //SERIAL_PORT_H
