TcpipSCF.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "SCF.h"
  31. #include "ResDataObject.h"
  32. #include <winsock2.h>
  33. TCPIPSCF_C_API SCF* GetSCF();
  34. TCPIPSCF_C_API void ReleseSCF(SCF* p);
  35. // 此类是从 TcpipSCF.dll 导出的
  36. class TCPIPSCF_API TcpipSCF : public SCF{
  37. HANDLE hCom;
  38. int ConnectTcpip(const char *ip, WORD port, DWORD timeout);
  39. public:
  40. TcpipSCF(void);
  41. virtual ~TcpipSCF();
  42. //设置连接,参数也由json传过来
  43. /*
  44. {
  45. "connect":"COM",
  46. "port":"COM1",
  47. "baudrate":"3200",
  48. "bytesize":"8",
  49. "parity":"1",
  50. "stopbits":"1",
  51. }
  52. */
  53. int Connect(ResDataObject & Connectprameters, PacketParser callback = NULL, SCF_TRANSFERTYPE TransferType = SCF_NORMAL_TRANSFER, DWORD CommunicateTimeout = 75000);
  54. //断开连接
  55. void Disconnect();
  56. //判断是否连接
  57. bool isConnected();
  58. const char* GetConnectionType();
  59. //将数据包通过SCF进行发送
  60. int SendPacket(const char *pPacket, DWORD length, DWORD timeout);
  61. int SendPacket(SCFPacket *pPacket, DWORD timeout);
  62. protected:
  63. string *m_pIp;
  64. WORD m_Port;
  65. bool Connected;
  66. hostent hes;
  67. servent servs;
  68. SOCKET Socketfd;
  69. SOCKADDR_IN sa;
  70. unsigned long tulong;
  71. unsigned long *tulongptr;
  72. int Error;
  73. int ReadData(char *pPacket, DWORD length, DWORD timeout);
  74. struct hostent * Gethostbyname(const char *name1);
  75. struct servent * Getservbyname(const char *name, const char *prot);
  76. };