SerialSCF.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  2. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 SERIALSCF_EXPORTS
  3. // 符号编译的。在使用此 DLL 的
  4. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  5. // SERIALSCF_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  6. // 符号视为是被导出的。
  7. #ifndef SERIALSCF_EXPORTS
  8. #ifdef _WIN64
  9. #ifdef _DEBUG
  10. #pragma comment (lib, "Dios.Dev.SerialSCFX64D.lib")
  11. #else
  12. #pragma comment (lib, "Dios.Dev.SerialSCFX64.lib")
  13. #endif
  14. #else
  15. #ifdef _DEBUG
  16. #pragma comment (lib, "Dios.Dev.SerialSCFD.lib")
  17. #else
  18. #pragma comment (lib, "Dios.Dev.SerialSCF.lib")
  19. #endif
  20. #endif
  21. #endif
  22. #ifdef SERIALSCF_EXPORTS
  23. #define SERIALSCF_API __declspec (dllexport)
  24. #define SERIALSCF_C_API extern "C" __declspec (dllexport)
  25. #else
  26. #define SERIALSCF_API __declspec (dllimport)
  27. #define SERIALSCF_C_API extern "C" __declspec (dllimport)
  28. #endif
  29. #include "SCF.Detail.h"
  30. #include "ResDataObject.h"
  31. namespace eSCF = DIOS::Dev::Communication::Detail;
  32. SERIALSCF_C_API eSCF::SCFDetail * CreateSCF ();
  33. namespace DIOS
  34. {
  35. namespace Dev
  36. {
  37. namespace Communication
  38. {
  39. namespace Detail
  40. {
  41. // 此类是从 SerialSCF.dll 导出的
  42. class SERIALSCF_API SerialSCF : public eSCF::SCFDetail
  43. {
  44. using super = eSCF::SCFDetail;
  45. HANDLE hCom;
  46. OVERLAPPED m_osRead;
  47. OVERLAPPED m_osWrite;
  48. string m_PortName;
  49. DWORD m_BaudRate;
  50. DWORD m_ByteSize;
  51. DWORD m_Parity;
  52. DWORD m_StopBits;
  53. DWORD m_ReadTimeout;
  54. DWORD m_WriteTimeout;
  55. int ConnectCOM (const char *pCom, DWORD BaudRate, DWORD ByteSize, DWORD Parity, DWORD StopBits);
  56. public:
  57. SerialSCF (void);
  58. virtual ~SerialSCF ();
  59. //设置连接,参数也由json传过来
  60. /*
  61. {
  62. "connect":"COM",
  63. "port":"COM1",
  64. "baudrate":"3200",
  65. "bytesize":"8",
  66. "parity":"1",
  67. "stopbits":"1",
  68. }
  69. */
  70. int Connect (ResDataObject & Connectprameters, tPacketPredate callback = nullptr, SCF_TRANSFERTYPE TransferType = SCF_NORMAL_TRANSFER, DWORD msTimeOut = 0) override;
  71. //断开连接
  72. void Disconnect () override;
  73. //判断是否连接
  74. bool isConnected () const override;
  75. const char* GetConnectionType ();
  76. //将数据包通过SCF进行发送
  77. int SendPacket (const char *pPacket, DWORD length, DWORD timeout);
  78. int SendPacket (SCFPacket *pPacket, DWORD timeout);
  79. protected:
  80. //继承类的读包流程,直接从SCF接口中读取数据,通常由SCF的读取线程调用,当上层直接调用的时候需要锁住读取线程,再调用该函数
  81. int ReadData (char *pPacket, DWORD length, DWORD timeout);
  82. //int ReadData (SCFPacket *pPacket, DWORD timeout);
  83. };
  84. }
  85. }
  86. }
  87. }