#pragma once #include #include #include #include #include #include #include #include #include "SCF.h" #include "ResDataObject.h" class SCFWrapper { public: // 回调函数类型定义 using PacketParserCallback = std::function; using DataReceivedCallback = std::function; using ReadDataCallback = std::function; SCFWrapper(); SCFWrapper(const SCFWrapper& other); SCFWrapper& operator=(const SCFWrapper& other); ~SCFWrapper(); // 初始化SCF库 bool Initialize(const std::string& dllName); // 连接设备 int Connect(const ResDataObject& connectionParams, PacketParserCallback callback = nullptr, SCF_TRANSFERTYPE transferType = SCF_NORMAL_TRANSFER, unsigned int timeout = 3000); // 断开连接 void Disconnect(); // 检查连接状态 bool IsConnected(); // 获取连接类型 std::string GetConnectionType(); // 锁定SCF资源 int Lock(unsigned int timeout); // 解锁SCF资源 void Unlock(); // 发送数据 int SendPacket(const char* data, unsigned int length, unsigned int timeout, unsigned int& retLength); // 发送SCF数据包 int SendPacket(SCFPacket* packet, unsigned int timeout); // 接收数据 int ReceivePacket(char* buffer, unsigned int length, unsigned int timeout, unsigned int& retLength); // 接收SCF数据包 int ReceivePacket(SCFPacket* packet, unsigned int timeout); // 设置数据接收回调(异步方式) void SetDataReceivedCallback(DataReceivedCallback callback); // 启动自动数据接收 bool StartAutoReceive(); // 停止自动数据接收 void StopAutoReceive(); // 获取错误信息 std::string GetLastError() const; private: // 加载SCF库 bool LoadSCFLibrary(const std::string& dllName); // 卸载SCF库 void UnloadSCFLibrary(); // 自动数据接收线程函数 void AutoReceiveThread(); // 读取数据实现方法 int ReadDataImpl(char* pPacket, uint32_t length, uint32_t timeout); // 复制内部状态 void CopyFrom(const SCFWrapper& other); // 内部SCF实例 std::shared_ptr m_scfInstance; // 库句柄 void* m_libraryHandle; // 错误信息 mutable std::string m_lastError; // 连接状态 std::atomic m_connected; // 库类型 std::string m_libraryType; // 自动接收相关成员 std::atomic m_autoReceiveRunning; std::thread m_autoReceiveThread; DataReceivedCallback m_dataReceivedCallback; // 线程安全保护 mutable std::mutex m_instanceMutex; mutable std::mutex m_callbackMutex; // 连接参数 ResDataObject m_connectionParams; // 包解析回调 PacketParserCallback m_packetParserCallback; // 传输类型 SCF_TRANSFERTYPE m_transferType; // 通信超时 unsigned int m_communicateTimeout; };