电机控制项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

133 lines
3.8 KiB

  1. #pragma once
  2. namespace XINJE
  3. {
  4. //类型定义
  5. typedef unsigned char Byte;
  6. typedef unsigned short UInt16;
  7. //XNet服务端口
  8. #define ServicePort 2323
  9. #define ServiceIp "127.0.0.1"
  10. //操作结果(仅告知操作是否成功)
  11. #define XNetCommResult int
  12. #define XNetCommResult_Err -1
  13. #define XNetCommResult_Success 0
  14. //XNet设备类型
  15. typedef enum
  16. {
  17. PLC_XD = 0,
  18. PLC_XE,
  19. TouchWin,
  20. WBox,
  21. _4GBox,
  22. COBox,
  23. ABox
  24. }XNetDevice;
  25. //线圈类型
  26. typedef enum
  27. {
  28. XNet_X = 1,
  29. XNet_Y = 2,
  30. XNet_M = 3,
  31. XNet_S = 4,
  32. XNet_T = 5,
  33. XNet_C = 6,
  34. XNet_ET = 7,
  35. XNet_HM = 8,
  36. XNet_HS = 9,
  37. XNet_HT = 10,
  38. XNet_HC = 11,
  39. XNet_HSC = 12,
  40. XNet_SM = 13,
  41. XNet_SSM = 14,
  42. XNet_TG = 15,
  43. XNet_HTG = 16,
  44. XNet_PF = 17,
  45. XNet_SEM = 18,
  46. XNet_STG = 19,//add 2016.4.8
  47. XNet_HSTG = 20,
  48. }CoilType;
  49. //寄存器类型
  50. typedef enum
  51. {
  52. XNet_D = 128,
  53. XNet_TD = 129,
  54. XNet_CD = 130,
  55. XNet_SD = 131,
  56. XNet_ETD = 133,
  57. XNet_ID = 134,
  58. XNet_QD = 135,
  59. XNet_HD = 136,
  60. XNet_HTD = 137,
  61. XNet_HCD = 138,
  62. XNet_HSCD = 139,
  63. XNet_HSD = 140,
  64. XNet_FD = 141,
  65. XNet_SFD = 142,
  66. XNet_SSFD = 143,
  67. XNet_SSD = 144,
  68. XNet_FS = 145,
  69. }RegType;
  70. //通信对象
  71. //每一个和每一个设备的连接都建议创建一个此对象,以避免不必要的线程同步
  72. class XNetClient
  73. {
  74. private:
  75. int Sock;
  76. unsigned short Net;
  77. unsigned short Station;
  78. unsigned char Com;
  79. Byte Type;
  80. Byte Serial;
  81. Byte Model;
  82. void LibXNetRegsToBuf(short* _regs, unsigned char* _buf, int _num);
  83. void LibXNetBufToRegs(unsigned char* _buf, unsigned short* _regs, int _num);
  84. void LibXNetCoilsToBuf(short* _coils, unsigned char* _buf, int _num);
  85. void LibXNetBufToCoils(char* _buf, short* _coils, Byte _startBit, int _num);
  86. unsigned short Byte2Word(Byte high, Byte low);
  87. public:
  88. //初始化WindowsSocket
  89. // XNetClient(char *ttyNo);
  90. static XNetCommResult WinSockInit();
  91. //启动XNet服务
  92. static XNetCommResult StartXNetWindows();
  93. static XNetCommResult CloseXNetWindows();
  94. //和XNet服务建立连接
  95. XNetCommResult XNetCommunication();
  96. //打开串口(网口也需配置一次,网口_com填129)
  97. XNetCommResult SetComPort(Byte _com);
  98. //关闭串口
  99. XNetCommResult ReleaseComPort(Byte _com);
  100. //直接指定设备地址
  101. void AdderLink(const char* _ipStr);
  102. void AdderLink(UInt16 _net, UInt16 _station);
  103. //对指定端口或网口查找设备,使本对象自动获取到设备地址
  104. XNetCommResult FindDevice(Byte _comPort, Byte _type, Byte _serial, Byte _model);
  105. XNetCommResult FindDevice(Byte _comPort, XNetDevice _dev);
  106. XNetCommResult FindDevice(Byte _comPort, const char* _id);
  107. //读写寄存器(用户提供一个short数组缓存,用于传入或获取XNet16位寄存器的值)
  108. XNetCommResult WriteRegs(Byte _regType, int _offSet, int _num, short* _writeRegs);
  109. XNetCommResult ReadRegs(Byte _regType, int _offSet, int _num, short* _readRegs);
  110. //读写线圈(用户提供一个short数组缓存,用于传入或获取XNet线圈状态,0为off,1为on)
  111. XNetCommResult WriteCoils(Byte _coilType, int _offSet, int _num, short* _writeCoils);
  112. XNetCommResult ReadCoils(Byte _coilType, int _offSet, int _num, short* _readCoil);
  113. // ~XNetClient();
  114. };
  115. }