SCFWrapper.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include <string>
  3. #include <memory>
  4. #include <functional>
  5. #include <thread>
  6. #include <atomic>
  7. #include <condition_variable>
  8. #include <queue>
  9. #include <mutex>
  10. #include "SCF.h"
  11. #include "ResDataObject.h"
  12. class SCFWrapper {
  13. public:
  14. // 回调函数类型定义
  15. using PacketParserCallback = std::function<PACKET_RET(const char*, uint32_t, uint32_t&)>;
  16. using DataReceivedCallback = std::function<void(const char*, uint32_t)>;
  17. using ReadDataCallback = std::function<int(char*, uint32_t, uint32_t)>;
  18. SCFWrapper();
  19. SCFWrapper(const SCFWrapper& other);
  20. SCFWrapper& operator=(const SCFWrapper& other);
  21. ~SCFWrapper();
  22. // 初始化SCF库
  23. bool Initialize(const std::string& dllName);
  24. // 连接设备
  25. int Connect(const ResDataObject& connectionParams,
  26. PacketParserCallback callback = nullptr,
  27. SCF_TRANSFERTYPE transferType = SCF_NORMAL_TRANSFER,
  28. unsigned int timeout = 3000);
  29. // 断开连接
  30. void Disconnect();
  31. // 检查连接状态
  32. bool IsConnected();
  33. // 获取连接类型
  34. std::string GetConnectionType();
  35. // 锁定SCF资源
  36. int Lock(unsigned int timeout);
  37. // 解锁SCF资源
  38. void Unlock();
  39. // 发送数据
  40. int SendPacket(const char* data, unsigned int length,
  41. unsigned int timeout, unsigned int& retLength);
  42. // 发送SCF数据包
  43. int SendPacket(SCFPacket* packet, unsigned int timeout);
  44. // 接收数据
  45. int ReceivePacket(char* buffer, unsigned int length,
  46. unsigned int timeout, unsigned int& retLength);
  47. // 接收SCF数据包
  48. int ReceivePacket(SCFPacket* packet, unsigned int timeout);
  49. // 设置数据接收回调(异步方式)
  50. void SetDataReceivedCallback(DataReceivedCallback callback);
  51. // 启动自动数据接收
  52. bool StartAutoReceive();
  53. // 停止自动数据接收
  54. void StopAutoReceive();
  55. // 获取错误信息
  56. std::string GetLastError() const;
  57. private:
  58. // 加载SCF库
  59. bool LoadSCFLibrary(const std::string& dllName);
  60. // 卸载SCF库
  61. void UnloadSCFLibrary();
  62. // 自动数据接收线程函数
  63. void AutoReceiveThread();
  64. // 读取数据实现方法
  65. int ReadDataImpl(char* pPacket, uint32_t length, uint32_t timeout);
  66. // 复制内部状态
  67. void CopyFrom(const SCFWrapper& other);
  68. // 内部SCF实例
  69. std::shared_ptr<SCF> m_scfInstance;
  70. // 库句柄
  71. void* m_libraryHandle;
  72. // 错误信息
  73. mutable std::string m_lastError;
  74. // 连接状态
  75. std::atomic<bool> m_connected;
  76. // 库类型
  77. std::string m_libraryType;
  78. // 自动接收相关成员
  79. std::atomic<bool> m_autoReceiveRunning;
  80. std::thread m_autoReceiveThread;
  81. DataReceivedCallback m_dataReceivedCallback;
  82. // 线程安全保护
  83. mutable std::mutex m_instanceMutex;
  84. mutable std::mutex m_callbackMutex;
  85. // 连接参数
  86. ResDataObject m_connectionParams;
  87. // 包解析回调
  88. PacketParserCallback m_packetParserCallback;
  89. // 传输类型
  90. SCF_TRANSFERTYPE m_transferType;
  91. // 通信超时
  92. unsigned int m_communicateTimeout;
  93. };