123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #pragma once
- #include <string>
- #include <memory>
- #include <functional>
- #include <thread>
- #include <atomic>
- #include <condition_variable>
- #include <queue>
- #include <mutex>
- #include "SCF.h"
- #include "ResDataObject.h"
- class SCFWrapper {
- public:
- // 回调函数类型定义
- using PacketParserCallback = std::function<PACKET_RET(const char*, uint32_t, uint32_t&)>;
- using DataReceivedCallback = std::function<void(const char*, uint32_t)>;
- using ReadDataCallback = std::function<int(char*, uint32_t, uint32_t)>;
-
- 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<SCF> m_scfInstance;
-
- // 库句柄
- void* m_libraryHandle;
-
- // 错误信息
- mutable std::string m_lastError;
-
- // 连接状态
- std::atomic<bool> m_connected;
- // 库类型
- std::string m_libraryType;
- // 自动接收相关成员
- std::atomic<bool> 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;
- };
|