DriverClient.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // DriverClient.h - 与内核驱动的通信封装
  2. // 职责:CreateFile 打开 \\.\CommModifyKit 设备 + 封装 DeviceIoControl 调用
  3. #pragma once
  4. #include "pch.h"
  5. #include "../common/CommKitIoctl.h"
  6. namespace commkit {
  7. class DriverClient {
  8. public:
  9. DriverClient();
  10. ~DriverClient();
  11. DriverClient(const DriverClient&) = delete;
  12. DriverClient& operator=(const DriverClient&) = delete;
  13. // 打开驱动控制设备 \\.\CommModifyKit
  14. // 返回 true 表示成功;失败时通过 GetLastError() 查询 Win32 错误码
  15. bool Open();
  16. // 关闭驱动设备句柄
  17. void Close();
  18. // 是否已打开
  19. bool IsOpen() const { return handle_ != INVALID_HANDLE_VALUE; }
  20. // 获取驱动设备句柄(供 EventLoop 复用)
  21. HANDLE GetHandle() const { return handle_; }
  22. // ====== IOCTL 封装 ======
  23. // 注册回调管道(InitMonitor 调用)
  24. // 告诉驱动开始工作,DLL 工作线程会从 READ_EVENTS 拉事件
  25. bool RegisterCallback();
  26. // 绑定(启用监控)指定串口
  27. bool AttachPort(DWORD com_number);
  28. // 解绑(停止监控)指定串口
  29. bool DetachPort(DWORD com_number);
  30. // 向指定串口写数据
  31. bool WritePort(DWORD com_number, const void* data, int len);
  32. // 从指定串口发起异步读请求
  33. bool ReadPort(DWORD com_number, const void* data, int len);
  34. // 批量读取捕获的事件(阻塞或返回 0 条)
  35. // events_out:输出缓冲区;max_count:最多多少条
  36. // 返回实际读取到的条数,<0 表示错误
  37. int ReadEvents(COMMKIT_EVENT* events_out, int max_count);
  38. // 解绑所有端口并清空事件队列(FreeMonitor 调用)
  39. bool FreeAll();
  40. private:
  41. // 通用同步 IOCTL 调用封装
  42. bool SyncIoctl(DWORD ioctl_code, void* in_buf, DWORD in_size,
  43. void* out_buf, DWORD out_size, DWORD* returned);
  44. HANDLE handle_ = INVALID_HANDLE_VALUE;
  45. };
  46. } // namespace commkit