当前位置:   article > 正文

【C++】Modbus通讯_c++ modbus

c++ modbus


MODBUS_SERVER.h

MODBUS_SERVER.cpp

MODBUS_SHARE.h

MODBUS_SHARE.cpp

PORT.h

PORT.cpp

两个VC++ Modbus通信例子源代码.rar


modbus 协议编程 C++

MODBUS_SERVER.h

  1. //Download by http://www.NewXing.com
  2. #if !defined(MSERVERH)
  3. #define MSERVERH
  4. #ifdef __cplusplus
  5. extern "C"
  6. {
  7. #endif
  8. /*1.对单个PLC操作*/
  9. /*读一个或多个模拟量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/
  10. char MODBUS_S_ReadMultiRegD(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, short int *list);
  11. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/
  12. char MODBUS_S_ReadMultiRegM(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list);
  13. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/
  14. char MODBUS_S_ReadMultiRegM_1x(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list);
  15. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/
  16. char MODBUS_S_WriteSingRegD(unsigned char rtu, unsigned short int RegAdd, short int var);
  17. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/
  18. char MODBUS_S_WriteSingRegM(unsigned char rtu, unsigned short int RegAdd, bool var);
  19. /*2.对全部PLC操作*/
  20. char MODBUS_S_A_WriteSingRegD(unsigned short int RegAdd, bool var);
  21. char MODBUS_S_A_WriteSingRegM(unsigned short int RegAdd, short int var);
  22. /*3.端口操作*/
  23. char MODBUS_S_Init(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,
  24. unsigned char xParity, unsigned char xStopBit);
  25. char MODBUS_S_UnInit();
  26. #ifdef __cplusplus
  27. }
  28. #endif
  29. #endif

MODBUS_SERVER.cpp

  1. #include "stdafx.h"
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include "MODBUS_SHARE.h"
  6. #include "MODBUS_SERVER.h"
  7. #include "PORT.h"
  8. //Download by http://www.NewXing.com
  9. unsigned char buff[256];
  10. /*1.对单个PLC操作*/
  11. /*读一个或多个模拟量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/
  12. /*40000*/
  13. char MODBUS_S_ReadMultiRegD(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, short int *list)
  14. {
  15. unsigned short int crc16;
  16. unsigned short int crctmp;
  17. short int vartmp;
  18. memset(buff, 0x00, 255);
  19. buff[0]= rtu;
  20. buff[1]= 0x03;
  21. buff[2]= (unsigned char)((RegAdd-40001) >> 8);
  22. buff[3]= (unsigned char)(RegAdd-40001);
  23. buff[4]= (unsigned char)(RegCount >> 8);
  24. buff[5]= (unsigned char)RegCount;
  25. crc16= CalcCrcFast(buff, 6);
  26. buff[6]= (unsigned char)(crc16 >> 8);
  27. buff[7]= (unsigned char)crc16;
  28. //发送数据
  29. unsigned long strlen;
  30. if(IsOpen())
  31. {
  32. //发送数据
  33. strlen= WriteChar(8, (char *)buff);
  34. if(strlen==8)
  35. {
  36. //读数据
  37. memset(buff, 0x00, 255);
  38. Sleep(50);
  39. strlen= ReadChar(255, (char *)buff, 1000);
  40. if(strlen==0)
  41. {
  42. //无返回
  43. return(-2);
  44. }
  45. else
  46. {
  47. //返回长度有效,解析接收缓冲区
  48. if(strlen== (3+(RegCount *2)+ 2) && buff[0]== rtu && buff[1]== 0x03)
  49. {
  50. crc16= CalcCrcFast(buff, 3+(RegCount*2));
  51. crctmp= buff[strlen-2];
  52. crctmp= crctmp << 8 | buff[strlen-1];
  53. if(crc16== crctmp )
  54. {
  55. for(int i=0; i< RegCount; i++)
  56. {
  57. vartmp= buff[3+(2*i)];
  58. vartmp= vartmp << 8;
  59. vartmp= vartmp | buff[3+((2*i)+1)];
  60. list[i]= vartmp;
  61. }
  62. }
  63. else
  64. {
  65. return(-1);
  66. }
  67. }
  68. else
  69. {
  70. return(-1);
  71. }
  72. }
  73. }
  74. else
  75. {
  76. return(-2);
  77. }
  78. }
  79. else
  80. {
  81. return(-2);
  82. }
  83. return(1);
  84. }
  85. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/
  86. /*00000*/
  87. char MODBUS_S_ReadMultiRegM(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list)
  88. {
  89. unsigned short int crc16;
  90. unsigned short int crctmp;
  91. memset(buff, 0x00, 255);
  92. buff[0]= rtu;
  93. buff[1]= 0x01;
  94. buff[2]= (unsigned char)(RegAdd >> 8);
  95. buff[3]= (unsigned char)RegAdd;
  96. buff[4]= (unsigned char)(RegCount >> 8);
  97. buff[5]= (unsigned char)RegCount;
  98. crc16= CalcCrcFast(buff, 6);
  99. buff[6]= (unsigned char)(crc16 >> 8);
  100. buff[7]= (unsigned char)crc16;
  101. //发送数据
  102. unsigned long strlen;
  103. if(IsOpen())
  104. {
  105. //发送数据
  106. strlen= WriteChar(8, (char *)buff);
  107. if(strlen==8)
  108. {
  109. //读数据
  110. memset(buff, 0x00, 255);
  111. Sleep(50);
  112. strlen= ReadChar(255, (char *)buff, 1000);
  113. if(strlen==0)
  114. {
  115. //无返回
  116. return(-2);
  117. }
  118. else
  119. {
  120. //返回长度有效,解析接收缓冲区
  121. if(strlen== (3+((RegCount+7)/8)+ 2) && buff[0]== rtu && buff[1]== 0x01)
  122. {
  123. crc16= CalcCrcFast(buff, 3+((RegCount +7)/8));
  124. crctmp= buff[strlen-2];
  125. crctmp= crctmp << 8 | buff[strlen-1];
  126. if(crc16== crctmp )
  127. {
  128. unsigned char row=0, col=0;
  129. for(int i=0; i<RegCount; i++)
  130. {
  131. row= i / 8;
  132. col= i % 8;
  133. list[i]= buff[3 + row] >> col & 0x01;
  134. }
  135. }
  136. else
  137. {
  138. return(-1);
  139. }
  140. }
  141. else
  142. {
  143. return(-1);
  144. }
  145. }
  146. }
  147. else
  148. {
  149. return(-2);
  150. }
  151. }
  152. else
  153. {
  154. return(-2);
  155. }
  156. return(1);
  157. }
  158. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/
  159. /*10000*/
  160. char MODBUS_S_ReadMultiRegM_1x(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list)
  161. {
  162. unsigned short int crc16;
  163. unsigned short int crctmp;
  164. memset(buff, 0x00, 255);
  165. buff[0]= rtu;
  166. buff[1]= 0x02;
  167. buff[2]= (unsigned char)((RegAdd-10001)>> 8);
  168. buff[3]= (unsigned char)(RegAdd-10001);
  169. buff[4]= (unsigned char)(RegCount >> 8);
  170. buff[5]= (unsigned char)RegCount;
  171. crc16= CalcCrcFast(buff, 6);
  172. buff[6]= (unsigned char)(crc16 >> 8);
  173. buff[7]= (unsigned char)crc16;
  174. //发送数据
  175. unsigned long strlen;
  176. if(IsOpen())
  177. {
  178. //发送数据
  179. strlen= WriteChar(8, (char *)buff);
  180. if(strlen==8)
  181. {
  182. //读数据
  183. memset(buff, 0x00, 255);
  184. Sleep(50);
  185. strlen= ReadChar(255, (char *)buff, 1000);
  186. if(strlen==0)
  187. {
  188. //无返回
  189. return(-2);
  190. }
  191. else
  192. {
  193. //返回长度有效,解析接收缓冲区
  194. if(strlen== (3+((RegCount+7)/8)+ 2) && buff[0]== rtu && buff[1]== 0x02)
  195. {
  196. crc16= CalcCrcFast(buff, 3+((RegCount +7)/8));
  197. crctmp= buff[strlen-2];
  198. crctmp= crctmp << 8 | buff[strlen-1];
  199. if(crc16== crctmp )
  200. {
  201. unsigned char row=0, col=0;
  202. for(int i=0; i<RegCount; i++)
  203. {
  204. row= i / 8;
  205. col= i % 8;
  206. list[i]= buff[3 + row] >> col & 0x01;
  207. }
  208. }
  209. else
  210. {
  211. return(-1);
  212. }
  213. }
  214. else
  215. {
  216. return(-1);
  217. }
  218. }
  219. }
  220. else
  221. {
  222. return(-2);
  223. }
  224. }
  225. else
  226. {
  227. return(-2);
  228. }
  229. return(1);
  230. }
  231. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/
  232. /*40000*/
  233. char MODBUS_S_WriteSingRegD(unsigned char rtu, unsigned short int RegAdd, short int var)
  234. {
  235. unsigned short int crc16;
  236. memset(buff, 0x00, 255);
  237. buff[0]= rtu;
  238. buff[1]= 0x06;
  239. buff[2]= (unsigned char)((RegAdd-40001) >> 8);
  240. buff[3]= (unsigned char)(RegAdd-40001);
  241. buff[4]= (unsigned char)(var >> 8);
  242. buff[5]= (unsigned char)var;
  243. crc16= CalcCrcFast(buff, 6);
  244. buff[6]= (unsigned char)(crc16 >> 8);
  245. buff[7]= (unsigned char)crc16;
  246. //发送数据
  247. unsigned long strlen;
  248. if(IsOpen())
  249. {
  250. //发送数据
  251. strlen= WriteChar(8, (char *)buff);
  252. if(strlen==8)
  253. {
  254. Sleep(50);
  255. strlen= ReadChar(255, (char *)buff, 1000);
  256. //返回长度有效,解析接收缓冲区
  257. if(!(strlen== 8 && buff[0]== rtu && buff[1]== 0x06))
  258. {
  259. return(-1);
  260. }
  261. }
  262. else
  263. {
  264. return(-1);
  265. }
  266. }
  267. else
  268. {
  269. return(-1);
  270. }
  271. return(1);
  272. }
  273. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/
  274. char MODBUS_S_WriteSingRegM(unsigned char rtu, unsigned short int RegAdd, bool var)
  275. {
  276. unsigned short int crc16;
  277. memset(buff, 0x00, 255);
  278. buff[0]= rtu;
  279. buff[1]= 0x05;
  280. buff[2]= (unsigned char)(RegAdd >> 8);
  281. buff[3]= (unsigned char)RegAdd;
  282. if(var)
  283. {
  284. buff[4]= 0xff;
  285. buff[5]= 0x00;
  286. }
  287. else
  288. {
  289. buff[4]= 0x00;
  290. buff[5]= 0x00;
  291. }
  292. crc16= CalcCrcFast(buff, 6);
  293. buff[6]= (unsigned char)(crc16 >> 8);
  294. buff[7]= (unsigned char)crc16;
  295. //发送数据
  296. unsigned long strlen;
  297. if(IsOpen())
  298. {
  299. //发送数据
  300. Sleep(50);
  301. strlen= WriteChar(8, (char *)buff);
  302. if(strlen==8)
  303. {
  304. Sleep(50);
  305. strlen= ReadChar(255, (char *)buff, 1000);
  306. //返回长度有效,解析接收缓冲区
  307. if(!(strlen== 8 && buff[0]== rtu && buff[1]== 0x05))
  308. {
  309. return(-1);
  310. }
  311. }
  312. else
  313. {
  314. return(-1);
  315. }
  316. }
  317. else
  318. {
  319. return(-1);
  320. }
  321. return(1);
  322. }
  323. //
  324. /*2.对全部PLC操作*/
  325. char MODBUS_S_A_WriteSingRegD(unsigned short int RegAdd, short int var)
  326. {
  327. unsigned short int crc16;
  328. memset(buff, 0x00, 255);
  329. buff[0]= 0xff;
  330. buff[1]= 0x06;
  331. buff[2]= (unsigned char)((RegAdd -40001) >> 8);
  332. buff[3]= (unsigned char)(RegAdd -40001);
  333. buff[4]= (unsigned char)(var >> 8);
  334. buff[5]= (unsigned char)var;
  335. crc16= CalcCrcFast(buff, 6);
  336. buff[6]= (unsigned char)(crc16 >> 8);
  337. buff[7]= (unsigned char)crc16;
  338. //发送数据
  339. unsigned long strlen;
  340. if(IsOpen())
  341. {
  342. //发送数据
  343. strlen= WriteChar(8, (char *)buff);
  344. if(strlen!=8)
  345. {
  346. return(-1);
  347. }
  348. }
  349. else
  350. {
  351. return(-1);
  352. }
  353. return(1);
  354. }
  355. char MODBUS_S_A_WriteSingRegM(unsigned short int RegAdd, bool var)
  356. {
  357. unsigned short int crc16;
  358. memset(buff, 0x00, 255);
  359. buff[0]= 0xff;
  360. buff[1]= 0x05;
  361. buff[2]= (unsigned char)(RegAdd >> 8);
  362. buff[3]= (unsigned char)RegAdd;
  363. if(var)
  364. {
  365. buff[4]= 0xff;
  366. buff[5]= 0x00;
  367. }
  368. else
  369. {
  370. buff[4]= 0x00;
  371. buff[5]= 0x00;
  372. }
  373. crc16= CalcCrcFast(buff, 6);
  374. buff[6]= (unsigned char)(crc16 >> 8);
  375. buff[7]= (unsigned char)crc16;
  376. //发送数据
  377. unsigned long strlen;
  378. if(IsOpen())
  379. {
  380. //发送数据
  381. strlen= WriteChar(8, (char *)buff);
  382. if(strlen!=8)
  383. {
  384. return(-1);
  385. }
  386. }
  387. else
  388. {
  389. return(-1);
  390. }
  391. return(1);
  392. }
  393. char MODBUS_S_Init(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,
  394. unsigned char xParity, unsigned char xStopBit)
  395. {
  396. /*
  397. Parity :
  398. EVENPARITY Even
  399. MARKPARITY Mark
  400. NOPARITY No parity
  401. ODDPARITY Odd
  402. SPACEPARITY Space
  403. */
  404. /*BaudRate:
  405. CBR_110
  406. CBR_19200
  407. CBR_300
  408. CBR_38400
  409. CBR_600
  410. CBR_56000
  411. CBR_1200
  412. CBR_57600
  413. CBR_2400
  414. CBR_115200
  415. CBR_4800
  416. CBR_128000
  417. CBR_9600
  418. CBR_256000
  419. CBR_14400
  420. */
  421. /*
  422. ONESTOPBIT 1 stop bit
  423. ONE5STOPBITS 1.5 stop bits
  424. TWOSTOPBITS
  425. */
  426. if(OpenPort(xPort,xBabd,xDataSize,xParity, xStopBit,4096,4096,1000))
  427. {
  428. return(1);
  429. }
  430. else
  431. {
  432. return(0);
  433. }
  434. }
  435. char MODBUS_S_UnInit()
  436. {
  437. ClosePort();
  438. return(1);
  439. }

MODBUS_SHARE.h

  1. //Download by http://www.NewXing.com
  2. #if !defined(MSHAREH)
  3. #define MSHAREH
  4. #ifdef __cplusplus
  5. extern "C"
  6. {
  7. #endif
  8. unsigned short CalcCrcFast(unsigned char*puchMsg,unsigned short usDataLen);
  9. #ifdef __cplusplus
  10. }
  11. #endif
  12. #endif

MODBUS_SHARE.cpp

  1. #include "stdafx.h"
  2. #include "MODBUS_SHARE.h"
  3. //Download by http://www.NewXing.com
  4. const unsigned char m_auchCRCHi[]=
  5. {
  6. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  7. 0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  8. 0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
  9. 0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
  10. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
  11. 0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,
  12. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
  13. 0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  14. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  15. 0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
  16. 0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
  17. 0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
  18. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  19. 0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
  20. 0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
  21. 0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
  22. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  23. 0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  24. 0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
  25. 0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  26. 0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
  27. 0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
  28. 0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
  29. 0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  30. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  31. 0x80,0x41,0x00,0xC1,0x81,0x40
  32. }
  33. ;
  34. const unsigned char m_auchCRCLo[]=
  35. {
  36. 0x00,0xC0,0xC1,0x01,0xC3,0x03,0x02,0xC2,0xC6,0x06,
  37. 0x07,0xC7,0x05,0xC5,0xC4,0x04,0xCC,0x0C,0x0D,0xCD,
  38. 0x0F,0xCF,0xCE,0x0E,0x0A,0xCA,0xCB,0x0B,0xC9,0x09,
  39. 0x08,0xC8,0xD8,0x18,0x19,0xD9,0x1B,0xDB,0xDA,0x1A,
  40. 0x1E,0xDE,0xDF,0x1F,0xDD,0x1D,0x1C,0xDC,0x14,0xD4,
  41. 0xD5,0x15,0xD7,0x17,0x16,0xD6,0xD2,0x12,0x13,0xD3,
  42. 0x11,0xD1,0xD0,0x10,0xF0,0x30,0x31,0xF1,0x33,0xF3,
  43. 0xF2,0x32,0x36,0xF6,0xF7,0x37,0xF5,0x35,0x34,0xF4,
  44. 0x3C,0xFC,0xFD,0x3D,0xFF,0x3F,0x3E,0xFE,0xFA,0x3A,
  45. 0x3B,0xFB,0x39,0xF9,0xF8,0x38,0x28,0xE8,0xE9,0x29,
  46. 0xEB,0x2B,0x2A,0xEA,0xEE,0x2E,0x2F,0xEF,0x2D,0xED,
  47. 0xEC,0x2C,0xE4,0x24,0x25,0xE5,0x27,0xE7,0xE6,0x26,
  48. 0x22,0xE2,0xE3,0x23,0xE1,0x21,0x20,0xE0,0xA0,0x60,
  49. 0x61,0xA1,0x63,0xA3,0xA2,0x62,0x66,0xA6,0xA7,0x67,
  50. 0xA5,0x65,0x64,0xA4,0x6C,0xAC,0xAD,0x6D,0xAF,0x6F,
  51. 0x6E,0xAE,0xAA,0x6A,0x6B,0xAB,0x69,0xA9,0xA8,0x68,
  52. 0x78,0xB8,0xB9,0x79,0xBB,0x7B,0x7A,0xBA,0xBE,0x7E,
  53. 0x7F,0xBF,0x7D,0xBD,0xBC,0x7C,0xB4,0x74,0x75,0xB5,
  54. 0x77,0xB7,0xB6,0x76,0x72,0xB2,0xB3,0x73,0xB1,0x71,
  55. 0x70,0xB0,0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,
  56. 0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,0x9C,0x5C,
  57. 0x5D,0x9D,0x5F,0x9F,0x9E,0x5E,0x5A,0x9A,0x9B,0x5B,
  58. 0x99,0x59,0x58,0x98,0x88,0x48,0x49,0x89,0x4B,0x8B,
  59. 0x8A,0x4A,0x4E,0x8E,0x8F,0x4F,0x8D,0x4D,0x4C,0x8C,
  60. 0x44,0x84,0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,
  61. 0x43,0x83,0x41,0x81,0x80,0x40
  62. }
  63. ;
  64. unsigned short CalcCrcFast(unsigned char*puchMsg,unsigned short usDataLen)
  65. {
  66. unsigned char uchCRCHi=0xFF ;
  67. unsigned char uchCRCLo=0xFF ;
  68. unsigned short uIndex ;
  69. while(usDataLen--)
  70. {
  71. uIndex=uchCRCHi^*puchMsg++;
  72. uchCRCHi=uchCRCLo^m_auchCRCHi[uIndex];
  73. uchCRCLo=m_auchCRCLo[uIndex];
  74. }
  75. return(uchCRCHi<<8|uchCRCLo);
  76. }

PORT.h

  1. //Download by http://www.NewXing.com
  2. #if !defined(MPORT)
  3. #define MPORT
  4. #ifdef __cplusplus
  5. extern "C"
  6. {
  7. #endif
  8. inline bool IsOpen();
  9. bool OpenPort(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,
  10. unsigned char xParity, unsigned char xStopBit, unsigned long InputBuffSize,
  11. unsigned long OutputBuffSize, unsigned long dwTimerOut);
  12. void ClosePort();
  13. void ClearBuffer();
  14. unsigned long ReadChar(unsigned long dwBufferLength, char *buff, unsigned long dwWaitTime);
  15. unsigned long WriteChar(unsigned long dwBufferLength, char *buff);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #endif

PORT.cpp

  1. //Download by http://www.NewXing.com
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include "PORT.h"
  7. //
  8. DCB dcb;
  9. COMMTIMEOUTS CommTimerOuts;
  10. HANDLE hCom= INVALID_HANDLE_VALUE;
  11. OVERLAPPED m_overlappedRead;
  12. OVERLAPPED m_overlappedWrite;
  13. //
  14. //通讯端口是否打开
  15. inline bool IsOpen()
  16. {
  17. return hCom != INVALID_HANDLE_VALUE;
  18. }
  19. //设置超时
  20. void SetTimerOut(unsigned long dwTimerOut= 5000)
  21. {
  22. if(!IsOpen())
  23. {
  24. return;
  25. }
  26. CommTimerOuts.ReadIntervalTimeout= MAXDWORD;
  27. CommTimerOuts.ReadTotalTimeoutConstant= 0;
  28. CommTimerOuts.ReadTotalTimeoutMultiplier= 0;
  29. CommTimerOuts.WriteTotalTimeoutConstant= dwTimerOut;
  30. CommTimerOuts.WriteTotalTimeoutMultiplier= 0;
  31. SetCommTimeouts(hCom, &CommTimerOuts);
  32. }
  33. //设置DCB参数
  34. bool SetDCBParm(unsigned long xBabd, unsigned char xDataSize,
  35. unsigned char xParity, unsigned char xStopBit)
  36. {
  37. if(!IsOpen())
  38. {
  39. return false;
  40. }
  41. if (!GetCommState(hCom, &dcb))
  42. {
  43. #ifdef _DEBUG
  44. printf ("错误: %d << 等到通讯口参数失败.\n", GetLastError());
  45. #endif
  46. ClosePort();
  47. return (false);
  48. }
  49. //设置通讯参数
  50. dcb.DCBlength= sizeof(DCB);
  51. dcb.BaudRate = xBabd; // set the baud rate
  52. dcb.ByteSize = xDataSize; // data size, xmit, and rcv
  53. dcb.Parity = xParity; // no parity bit
  54. dcb.StopBits = xStopBit; // one stop bit
  55. if (!SetCommState(hCom, &dcb))
  56. {
  57. #ifdef _DEBUG
  58. printf ("错误: %d << 设置通讯端口参数失败.\n", GetLastError());
  59. #endif
  60. ClosePort();
  61. return (false);
  62. }
  63. return true;
  64. }
  65. //设置端口缓冲区大小
  66. bool SetPortBuffSize(unsigned long InputBuffSize, unsigned long OutputBuffSize)
  67. {
  68. if(!IsOpen())
  69. {
  70. return false;
  71. }
  72. if(!SetupComm(hCom, InputBuffSize, OutputBuffSize))
  73. {
  74. #ifdef _DEBUG
  75. printf ("错误: %d << 设置通讯端口缓冲失败.\n", GetLastError());
  76. #endif
  77. ClosePort();
  78. return (false);
  79. }
  80. return true;
  81. }
  82. //清理所有缓冲区
  83. void ClearBuffer()
  84. {
  85. if(!IsOpen())
  86. {
  87. return;
  88. }
  89. PurgeComm(hCom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
  90. }
  91. //当前接收缓冲区字节数目;
  92. unsigned long GetInBuffCount()
  93. {
  94. if(!IsOpen())
  95. {
  96. return(0);
  97. }
  98. DWORD dwErrorFalgs;
  99. COMSTAT Comstat;
  100. ClearCommError(hCom, &dwErrorFalgs, &Comstat);
  101. return Comstat.cbInQue;
  102. }
  103. //当前发送缓冲区字节数目;
  104. unsigned long GetOutBuffCount()
  105. {
  106. if(!IsOpen())
  107. {
  108. return false;
  109. }
  110. DWORD dwErrorFalgs;
  111. COMSTAT Comstat;
  112. ClearCommError(hCom, &dwErrorFalgs, &Comstat);
  113. return Comstat.cbOutQue;
  114. }
  115. ///打开串口
  116. bool OpenPort(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,
  117. unsigned char xParity, unsigned char xStopBit, unsigned long InputBuffSize,
  118. unsigned long OutputBuffSize, unsigned long dwTimerOut)
  119. {
  120. if(IsOpen())
  121. {
  122. ClosePort();
  123. }
  124. //设置事件
  125. memset(&m_overlappedRead,0,sizeof(OVERLAPPED));
  126. m_overlappedRead.hEvent= CreateEvent(NULL,FALSE,TRUE,"portread");
  127. ASSERT(m_overlappedRead.hEvent != INVALID_HANDLE_VALUE);
  128. memset(&m_overlappedWrite,0,sizeof(OVERLAPPED));
  129. m_overlappedWrite.hEvent= CreateEvent(NULL,FALSE,TRUE,"portwrite");
  130. ASSERT(m_overlappedWrite.hEvent != INVALID_HANDLE_VALUE);
  131. //取得串口字符
  132. char com_str[255];
  133. strcpy(com_str, "COM");
  134. ltoa(xPort, com_str + 3, 10);
  135. //打开通讯端口
  136. hCom = CreateFile(com_str,
  137. GENERIC_READ | GENERIC_WRITE,
  138. 0,
  139. NULL,
  140. OPEN_EXISTING,
  141. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  142. NULL
  143. );
  144. if (hCom == INVALID_HANDLE_VALUE)
  145. {
  146. #ifdef _DEBUG
  147. printf ("错误: %d << 打开通讯口失败,请检查是否已经安装串口设备.\n", GetLastError());
  148. #endif
  149. return (false);
  150. }
  151. SetPortBuffSize(InputBuffSize,OutputBuffSize);
  152. SetDCBParm(xBabd,xDataSize,xParity,xStopBit);
  153. SetTimerOut(dwTimerOut);
  154. //清理缓冲器
  155. PurgeComm(hCom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
  156. //启动成功
  157. #ifdef _DEBUG
  158. printf ("成功开启端口 %d.\n", com_str);
  159. #endif
  160. return(true);
  161. }
  162. //关闭串口;
  163. void ClosePort()
  164. {
  165. if(IsOpen())
  166. {
  167. CloseHandle(hCom);
  168. hCom= INVALID_HANDLE_VALUE;
  169. }
  170. //ResetEvent(m_overlappedRead.hEvent);
  171. //ResetEvent(m_overlappedWrite.hEvent);
  172. if(m_overlappedRead.hEvent!=NULL)
  173. {
  174. CloseHandle(m_overlappedRead.hEvent);
  175. }
  176. if(m_overlappedWrite.hEvent!=NULL)
  177. {
  178. CloseHandle(m_overlappedWrite.hEvent);
  179. }
  180. }
  181. //异步读数据
  182. unsigned long ReadChar(unsigned long dwBufferLength, char *buff, unsigned long dwWaitTime=20)
  183. {
  184. if(!IsOpen())
  185. {
  186. return(0);
  187. }
  188. DWORD dwError;
  189. COMSTAT Stat;
  190. if(::ClearCommError(hCom, &dwError, &Stat) && dwError > 0) //清除错误
  191. {
  192. ::PurgeComm(hCom, PURGE_RXABORT | PURGE_RXCLEAR); /*清除输入缓冲区*/
  193. return 0;
  194. }
  195. if(!Stat.cbInQue)// 缓冲区无数据
  196. {
  197. return 0;
  198. }
  199. unsigned long uReadLength = 0;
  200. dwBufferLength = dwBufferLength - 1 > Stat.cbInQue ? Stat.cbInQue : dwBufferLength - 1;
  201. if(!::ReadFile(hCom, buff, dwBufferLength, &uReadLength, &m_overlappedRead)) //2000 下 ReadFile 始终返回 True
  202. {
  203. if(::GetLastError() == ERROR_IO_PENDING) // 结束异步I/O
  204. {
  205. WaitForSingleObject(m_overlappedRead.hEvent, dwWaitTime); //等待20ms
  206. if(!::GetOverlappedResult(hCom, &m_overlappedRead, &uReadLength, false))
  207. {
  208. if(::GetLastError() != ERROR_IO_INCOMPLETE)//其他错误
  209. {
  210. uReadLength = 0;
  211. }
  212. }
  213. }
  214. else
  215. {
  216. uReadLength = 0;
  217. }
  218. }
  219. return uReadLength;
  220. }
  221. //异步写数据
  222. unsigned long WriteChar(unsigned long dwBufferLength, char *buff)
  223. {
  224. if(!IsOpen())
  225. {
  226. return 0;
  227. }
  228. DWORD dwError;
  229. if(ClearCommError(hCom, &dwError, NULL) && dwError > 0) //清除错误
  230. {
  231. PurgeComm(hCom, PURGE_TXABORT | PURGE_TXCLEAR);
  232. }
  233. unsigned long uWriteLength = 0;
  234. if(!WriteFile(hCom, buff, dwBufferLength, &uWriteLength, &m_overlappedWrite))
  235. {
  236. if(GetLastError() == ERROR_IO_PENDING)
  237. {
  238. DWORD m_tmp=0;
  239. m_tmp= WaitForSingleObject(m_overlappedWrite.hEvent, 1000);
  240. if(m_tmp== WAIT_TIMEOUT || m_tmp== WAIT_ABANDONED)
  241. {
  242. return(0);
  243. }
  244. else if(m_tmp== WAIT_OBJECT_0)
  245. {
  246. if(!GetOverlappedResult(hCom,&m_overlappedWrite,&uWriteLength,false))
  247. {
  248. return(0);
  249. }
  250. else
  251. {
  252. return uWriteLength;
  253. }
  254. }
  255. uWriteLength = 0;
  256. }
  257. }
  258. return uWriteLength;
  259. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/492670
推荐阅读
相关标签
  

闽ICP备14008679号