TcpipSCF.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  3. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 TCPIPSCF_EXPORTS
  4. // 符号编译的。在使用此 DLL 的
  5. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  6. // TCPIPSCF_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  7. // 符号视为是被导出的。
  8. #ifndef TCPIPSCF_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment (lib, "TcpipSCFX64D.lib")
  12. #else
  13. #pragma comment (lib, "TcpipSCFX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment (lib, "TcpipSCFD.lib")
  18. #else
  19. #pragma comment (lib, "TcpipSCF.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #ifdef TCPIPSCF_EXPORTS
  24. #define TCPIPSCF_API __declspec (dllexport)
  25. #define TCPIPSCF_C_API extern "C" __declspec (dllexport)
  26. #else
  27. #define TCPIPSCF_API __declspec (dllimport)
  28. #define TCPIPSCF_C_API extern "C" __declspec (dllimport)
  29. #endif
  30. #include <winsock2.h>
  31. //#include "SCF.h"
  32. #include "SCF.Detail.h"
  33. #include "ResDataObject.h"
  34. namespace eSCF = DIOS::Dev::Communication::Detail;
  35. TCPIPSCF_C_API eSCF::SCFDetail * CreateSCF ();
  36. namespace DIOS
  37. {
  38. namespace Dev
  39. {
  40. namespace Communication
  41. {
  42. namespace Detail
  43. {
  44. // 此类是从 TcpipSCF.dll 导出的
  45. class TCPIPSCF_API TcpipSCF : public eSCF::SCFDetail
  46. {
  47. using super = eSCF::SCFDetail;
  48. int ConnectTcpip (const char *ip, WORD port, DWORD timeout);
  49. public:
  50. TcpipSCF (void);
  51. virtual ~TcpipSCF ();
  52. //设置连接,参数也由json传过来
  53. /*
  54. {
  55. "connect":"COM",
  56. "port":"COM1",
  57. "baudrate":"3200",
  58. "bytesize":"8",
  59. "parity":"1",
  60. "stopbits":"1",
  61. }
  62. */
  63. int Connect (ResDataObject & Connectprameters, tPacketPredate callback = NULL, SCF_TRANSFERTYPE TransferType = SCF_NORMAL_TRANSFER, DWORD CommunicateTimeout = 75000);
  64. //断开连接
  65. void Disconnect ();
  66. //判断是否连接
  67. virtual bool isConnected () const override;
  68. const char* GetConnectionType ();
  69. //将数据包通过SCF进行发送
  70. int SendPacket (const char *pPacket, DWORD length, DWORD timeout);
  71. int SendPacket (SCFPacket *pPacket, DWORD timeout);
  72. protected:
  73. string m_IP;
  74. WORD m_Port;
  75. bool Connected;
  76. hostent hes;
  77. servent servs;
  78. SOCKET Socketfd;
  79. SOCKADDR_IN sa;
  80. unsigned long tulong;
  81. unsigned long * tulongptr;
  82. int Error;
  83. int ReadData (char *pPacket, DWORD length, DWORD timeout);
  84. struct hostent * Gethostbyname (const char *name1);
  85. struct servent * Getservbyname (const char *name, const char *prot);
  86. };
  87. }
  88. }
  89. }
  90. }