【C++】Modbus通讯

【C++】Modbus通讯

2016年06月22日 20:37:48 Taily老段 阅读数:10298

版权声明:本文为博主原创文章,未经博主允许不得转载。如遇到疑问,评论会给出答复。学习交流——关注页面微信公众号。【吃良心,拉思想】 https://blog.youkuaiyun.com/Taily_Duan/article/details/51736825

 

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.  
  5. #ifdef __cplusplus

  6. extern "C"

  7. {

  8. #endif

  9. /*1.对单个PLC操作*/

  10. /*读一个或多个模拟量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/

  11. char MODBUS_S_ReadMultiRegD(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, short int *list);

  12. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/

  13. char MODBUS_S_ReadMultiRegM(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list);

  14. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/

  15. char MODBUS_S_ReadMultiRegM_1x(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list);

  16. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/

  17. char MODBUS_S_WriteSingRegD(unsigned char rtu, unsigned short int RegAdd, short int var);

  18. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/

  19. char MODBUS_S_WriteSingRegM(unsigned char rtu, unsigned short int RegAdd, bool var);

  20.  
  21. /*2.对全部PLC操作*/

  22. char MODBUS_S_A_WriteSingRegD(unsigned short int RegAdd, bool var);

  23. char MODBUS_S_A_WriteSingRegM(unsigned short int RegAdd, short int var);

  24.  
  25. /*3.端口操作*/

  26. char MODBUS_S_Init(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,

  27. unsigned char xParity, unsigned char xStopBit);

  28. char MODBUS_S_UnInit();

  29.  
  30. #ifdef __cplusplus

  31. }

  32. #endif

  33.  
  34.  
  35. #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.  
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////

  12. /*1.对单个PLC操作*/

  13. /*读一个或多个模拟量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/

  14. /*40000*/

  15. char MODBUS_S_ReadMultiRegD(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, short int *list)

  16. {

  17. unsigned short int crc16;

  18. unsigned short int crctmp;

  19. short int vartmp;

  20.  
  21. memset(buff, 0x00, 255);

  22. buff[0]= rtu;

  23. buff[1]= 0x03;

  24. buff[2]= (unsigned char)((RegAdd-40001) >> 8);

  25. buff[3]= (unsigned char)(RegAdd-40001);

  26. buff[4]= (unsigned char)(RegCount >> 8);

  27. buff[5]= (unsigned char)RegCount;

  28. crc16= CalcCrcFast(buff, 6);

  29. buff[6]= (unsigned char)(crc16 >> 8);

  30. buff[7]= (unsigned char)crc16;

  31.  
  32. //发送数据

  33. unsigned long strlen;

  34. if(IsOpen())

  35. {

  36. //发送数据

  37. strlen= WriteChar(8, (char *)buff);

  38. if(strlen==8)

  39. {

  40. //读数据

  41. memset(buff, 0x00, 255);

  42. Sleep(50);

  43. strlen= ReadChar(255, (char *)buff, 1000);

  44. if(strlen==0)

  45. {

  46. //无返回

  47. return(-2);

  48. }

  49. else

  50. {

  51. //返回长度有效,解析接收缓冲区

  52. if(strlen== (3+(RegCount *2)+ 2) && buff[0]== rtu && buff[1]== 0x03)

  53. {

  54. crc16= CalcCrcFast(buff, 3+(RegCount*2));

  55. crctmp= buff[strlen-2];

  56. crctmp= crctmp << 8 | buff[strlen-1];

  57. if(crc16== crctmp )

  58. {

  59. for(int i=0; i< RegCount; i++)

  60. {

  61. vartmp= buff[3+(2*i)];

  62. vartmp= vartmp << 8;

  63. vartmp= vartmp | buff[3+((2*i)+1)];

  64.  
  65. list[i]= vartmp;

  66. }

  67. }

  68. else

  69. {

  70. return(-1);

  71. }

  72. }

  73. else

  74. {

  75. return(-1);

  76. }

  77. }

  78. }

  79. else

  80. {

  81. return(-2);

  82. }

  83. }

  84. else

  85. {

  86. return(-2);

  87. }

  88.  
  89. return(1);

  90. }

  91.  
  92. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/

  93. /*00000*/

  94. char MODBUS_S_ReadMultiRegM(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list)

  95. {

  96. unsigned short int crc16;

  97. unsigned short int crctmp;

  98.  
  99. memset(buff, 0x00, 255);

  100. buff[0]= rtu;

  101. buff[1]= 0x01;

  102. buff[2]= (unsigned char)(RegAdd >> 8);

  103. buff[3]= (unsigned char)RegAdd;

  104. buff[4]= (unsigned char)(RegCount >> 8);

  105. buff[5]= (unsigned char)RegCount;

  106. crc16= CalcCrcFast(buff, 6);

  107. buff[6]= (unsigned char)(crc16 >> 8);

  108. buff[7]= (unsigned char)crc16;

  109.  
  110. //发送数据

  111. unsigned long strlen;

  112. if(IsOpen())

  113. {

  114. //发送数据

  115. strlen= WriteChar(8, (char *)buff);

  116. if(strlen==8)

  117. {

  118. //读数据

  119. memset(buff, 0x00, 255);

  120. Sleep(50);

  121. strlen= ReadChar(255, (char *)buff, 1000);

  122. if(strlen==0)

  123. {

  124. //无返回

  125. return(-2);

  126. }

  127. else

  128. {

  129. //返回长度有效,解析接收缓冲区

  130. if(strlen== (3+((RegCount+7)/8)+ 2) && buff[0]== rtu && buff[1]== 0x01)

  131. {

  132. crc16= CalcCrcFast(buff, 3+((RegCount +7)/8));

  133. crctmp= buff[strlen-2];

  134. crctmp= crctmp << 8 | buff[strlen-1];

  135. if(crc16== crctmp )

  136. {

  137. unsigned char row=0, col=0;

  138. for(int i=0; i<RegCount; i++)

  139. {

  140. row= i / 8;

  141. col= i % 8;

  142. list[i]= buff[3 + row] >> col & 0x01;

  143. }

  144. }

  145. else

  146. {

  147. return(-1);

  148. }

  149. }

  150. else

  151. {

  152. return(-1);

  153. }

  154. }

  155. }

  156. else

  157. {

  158. return(-2);

  159. }

  160. }

  161. else

  162. {

  163. return(-2);

  164. }

  165.  
  166. return(1);

  167. }

  168.  
  169. /*读一个或多个开关量 参数:站号,开始地址, 读的数量, 返回数据变量指针 返回:-1表示CRC校验失败,-2表示无应答, 大于零表示接收数据成功*/

  170. /*10000*/

  171. char MODBUS_S_ReadMultiRegM_1x(unsigned char rtu, unsigned short int RegAdd, unsigned short int RegCount, bool *list)

  172. {

  173. unsigned short int crc16;

  174. unsigned short int crctmp;

  175.  
  176. memset(buff, 0x00, 255);

  177. buff[0]= rtu;

  178. buff[1]= 0x02;

  179. buff[2]= (unsigned char)((RegAdd-10001)>> 8);

  180. buff[3]= (unsigned char)(RegAdd-10001);

  181. buff[4]= (unsigned char)(RegCount >> 8);

  182. buff[5]= (unsigned char)RegCount;

  183. crc16= CalcCrcFast(buff, 6);

  184. buff[6]= (unsigned char)(crc16 >> 8);

  185. buff[7]= (unsigned char)crc16;

  186.  
  187. //发送数据

  188. unsigned long strlen;

  189. if(IsOpen())

  190. {

  191. //发送数据

  192. strlen= WriteChar(8, (char *)buff);

  193. if(strlen==8)

  194. {

  195. //读数据

  196. memset(buff, 0x00, 255);

  197. Sleep(50);

  198. strlen= ReadChar(255, (char *)buff, 1000);

  199. if(strlen==0)

  200. {

  201. //无返回

  202. return(-2);

  203. }

  204. else

  205. {

  206. //返回长度有效,解析接收缓冲区

  207. if(strlen== (3+((RegCount+7)/8)+ 2) && buff[0]== rtu && buff[1]== 0x02)

  208. {

  209. crc16= CalcCrcFast(buff, 3+((RegCount +7)/8));

  210. crctmp= buff[strlen-2];

  211. crctmp= crctmp << 8 | buff[strlen-1];

  212. if(crc16== crctmp )

  213. {

  214. unsigned char row=0, col=0;

  215. for(int i=0; i<RegCount; i++)

  216. {

  217. row= i / 8;

  218. col= i % 8;

  219. list[i]= buff[3 + row] >> col & 0x01;

  220. }

  221. }

  222. else

  223. {

  224. return(-1);

  225. }

  226. }

  227. else

  228. {

  229. return(-1);

  230. }

  231. }

  232. }

  233. else

  234. {

  235. return(-2);

  236. }

  237. }

  238. else

  239. {

  240. return(-2);

  241. }

  242.  
  243. return(1);

  244. }

  245.  
  246. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/

  247. /*40000*/

  248. char MODBUS_S_WriteSingRegD(unsigned char rtu, unsigned short int RegAdd, short int var)

  249. {

  250. unsigned short int crc16;

  251.  
  252. memset(buff, 0x00, 255);

  253. buff[0]= rtu;

  254. buff[1]= 0x06;

  255. buff[2]= (unsigned char)((RegAdd-40001) >> 8);

  256. buff[3]= (unsigned char)(RegAdd-40001);

  257. buff[4]= (unsigned char)(var >> 8);

  258. buff[5]= (unsigned char)var;

  259. crc16= CalcCrcFast(buff, 6);

  260. buff[6]= (unsigned char)(crc16 >> 8);

  261. buff[7]= (unsigned char)crc16;

  262.  
  263. //发送数据

  264. unsigned long strlen;

  265. if(IsOpen())

  266. {

  267. //发送数据

  268. strlen= WriteChar(8, (char *)buff);

  269. if(strlen==8)

  270. {

  271. Sleep(50);

  272. strlen= ReadChar(255, (char *)buff, 1000);

  273. //返回长度有效,解析接收缓冲区

  274. if(!(strlen== 8 && buff[0]== rtu && buff[1]== 0x06))

  275. {

  276. return(-1);

  277. }

  278. }

  279. else

  280. {

  281. return(-1);

  282. }

  283. }

  284. else

  285. {

  286. return(-1);

  287. }

  288.  
  289. return(1);

  290. }

  291.  
  292. /*写一个模拟量 参数:站号,写地址, 返回数据变量指针 返回:小于零失败,大于零表示成功*/

  293. char MODBUS_S_WriteSingRegM(unsigned char rtu, unsigned short int RegAdd, bool var)

  294. {

  295. unsigned short int crc16;

  296.  
  297. memset(buff, 0x00, 255);

  298. buff[0]= rtu;

  299. buff[1]= 0x05;

  300. buff[2]= (unsigned char)(RegAdd >> 8);

  301. buff[3]= (unsigned char)RegAdd;

  302. if(var)

  303. {

  304. buff[4]= 0xff;

  305. buff[5]= 0x00;

  306. }

  307. else

  308. {

  309. buff[4]= 0x00;

  310. buff[5]= 0x00;

  311. }

  312. crc16= CalcCrcFast(buff, 6);

  313. buff[6]= (unsigned char)(crc16 >> 8);

  314. buff[7]= (unsigned char)crc16;

  315.  
  316. //发送数据

  317. unsigned long strlen;

  318. if(IsOpen())

  319. {

  320. //发送数据

  321. Sleep(50);

  322. strlen= WriteChar(8, (char *)buff);

  323. if(strlen==8)

  324. {

  325. Sleep(50);

  326. strlen= ReadChar(255, (char *)buff, 1000);

  327. //返回长度有效,解析接收缓冲区

  328. if(!(strlen== 8 && buff[0]== rtu && buff[1]== 0x05))

  329. {

  330. return(-1);

  331. }

  332. }

  333. else

  334. {

  335. return(-1);

  336. }

  337. }

  338. else

  339. {

  340. return(-1);

  341. }

  342.  
  343. return(1);

  344. }

  345.  
  346. //////////////////////////////////////////////////////////////////////////////////////////////////////////////

  347. /*2.对全部PLC操作*/

  348. char MODBUS_S_A_WriteSingRegD(unsigned short int RegAdd, short int var)

  349. {

  350. unsigned short int crc16;

  351.  
  352. memset(buff, 0x00, 255);

  353. buff[0]= 0xff;

  354. buff[1]= 0x06;

  355. buff[2]= (unsigned char)((RegAdd -40001) >> 8);

  356. buff[3]= (unsigned char)(RegAdd -40001);

  357. buff[4]= (unsigned char)(var >> 8);

  358. buff[5]= (unsigned char)var;

  359. crc16= CalcCrcFast(buff, 6);

  360. buff[6]= (unsigned char)(crc16 >> 8);

  361. buff[7]= (unsigned char)crc16;

  362.  
  363. //发送数据

  364. unsigned long strlen;

  365. if(IsOpen())

  366. {

  367. //发送数据

  368. strlen= WriteChar(8, (char *)buff);

  369. if(strlen!=8)

  370. {

  371. return(-1);

  372. }

  373. }

  374. else

  375. {

  376. return(-1);

  377. }

  378.  
  379. return(1);

  380. }

  381.  
  382. char MODBUS_S_A_WriteSingRegM(unsigned short int RegAdd, bool var)

  383. {

  384. unsigned short int crc16;

  385.  
  386. memset(buff, 0x00, 255);

  387. buff[0]= 0xff;

  388. buff[1]= 0x05;

  389. buff[2]= (unsigned char)(RegAdd >> 8);

  390. buff[3]= (unsigned char)RegAdd;

  391. if(var)

  392. {

  393. buff[4]= 0xff;

  394. buff[5]= 0x00;

  395. }

  396. else

  397. {

  398. buff[4]= 0x00;

  399. buff[5]= 0x00;

  400. }

  401. crc16= CalcCrcFast(buff, 6);

  402. buff[6]= (unsigned char)(crc16 >> 8);

  403. buff[7]= (unsigned char)crc16;

  404.  
  405. //发送数据

  406. unsigned long strlen;

  407. if(IsOpen())

  408. {

  409. //发送数据

  410. strlen= WriteChar(8, (char *)buff);

  411. if(strlen!=8)

  412. {

  413. return(-1);

  414. }

  415. }

  416. else

  417. {

  418. return(-1);

  419. }

  420.  
  421. return(1);

  422. }

  423.  
  424. ////////////////////////////////////////////////////////////////////////////////////////////////////

  425. char MODBUS_S_Init(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,

  426. unsigned char xParity, unsigned char xStopBit)

  427. {

  428. /*

  429. Parity :

  430. EVENPARITY Even

  431. MARKPARITY Mark

  432. NOPARITY No parity

  433. ODDPARITY Odd

  434. SPACEPARITY Space

  435. */

  436. /*BaudRate:

  437. CBR_110

  438. CBR_19200

  439. CBR_300

  440. CBR_38400

  441. CBR_600

  442. CBR_56000

  443. CBR_1200

  444. CBR_57600

  445. CBR_2400

  446. CBR_115200

  447. CBR_4800

  448. CBR_128000

  449. CBR_9600

  450. CBR_256000

  451. CBR_14400

  452. */

  453. /*

  454. ONESTOPBIT 1 stop bit

  455. ONE5STOPBITS 1.5 stop bits

  456. TWOSTOPBITS

  457. */

  458.  
  459. if(OpenPort(xPort,xBabd,xDataSize,xParity, xStopBit,4096,4096,1000))

  460. {

  461. return(1);

  462. }

  463. else

  464. {

  465. return(0);

  466. }

  467. }

  468.  
  469. char MODBUS_S_UnInit()

  470. {

  471. ClosePort();

  472. return(1);

  473. }


MODBUS_SHARE.h

 

 
  1. //Download by http://www.NewXing.com

  2. #if !defined(MSHAREH)

  3. #define MSHAREH

  4.  
  5. #ifdef __cplusplus

  6. extern "C"

  7. {

  8. #endif

  9.  
  10. unsigned short CalcCrcFast(unsigned char*puchMsg,unsigned short usDataLen);

  11.  
  12. #ifdef __cplusplus

  13. }

  14. #endif

  15.  
  16. #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.  
  35. const unsigned char m_auchCRCLo[]=

  36. {

  37. 0x00,0xC0,0xC1,0x01,0xC3,0x03,0x02,0xC2,0xC6,0x06,

  38. 0x07,0xC7,0x05,0xC5,0xC4,0x04,0xCC,0x0C,0x0D,0xCD,

  39. 0x0F,0xCF,0xCE,0x0E,0x0A,0xCA,0xCB,0x0B,0xC9,0x09,

  40. 0x08,0xC8,0xD8,0x18,0x19,0xD9,0x1B,0xDB,0xDA,0x1A,

  41. 0x1E,0xDE,0xDF,0x1F,0xDD,0x1D,0x1C,0xDC,0x14,0xD4,

  42. 0xD5,0x15,0xD7,0x17,0x16,0xD6,0xD2,0x12,0x13,0xD3,

  43. 0x11,0xD1,0xD0,0x10,0xF0,0x30,0x31,0xF1,0x33,0xF3,

  44. 0xF2,0x32,0x36,0xF6,0xF7,0x37,0xF5,0x35,0x34,0xF4,

  45. 0x3C,0xFC,0xFD,0x3D,0xFF,0x3F,0x3E,0xFE,0xFA,0x3A,

  46. 0x3B,0xFB,0x39,0xF9,0xF8,0x38,0x28,0xE8,0xE9,0x29,

  47. 0xEB,0x2B,0x2A,0xEA,0xEE,0x2E,0x2F,0xEF,0x2D,0xED,

  48. 0xEC,0x2C,0xE4,0x24,0x25,0xE5,0x27,0xE7,0xE6,0x26,

  49. 0x22,0xE2,0xE3,0x23,0xE1,0x21,0x20,0xE0,0xA0,0x60,

  50. 0x61,0xA1,0x63,0xA3,0xA2,0x62,0x66,0xA6,0xA7,0x67,

  51. 0xA5,0x65,0x64,0xA4,0x6C,0xAC,0xAD,0x6D,0xAF,0x6F,

  52. 0x6E,0xAE,0xAA,0x6A,0x6B,0xAB,0x69,0xA9,0xA8,0x68,

  53. 0x78,0xB8,0xB9,0x79,0xBB,0x7B,0x7A,0xBA,0xBE,0x7E,

  54. 0x7F,0xBF,0x7D,0xBD,0xBC,0x7C,0xB4,0x74,0x75,0xB5,

  55. 0x77,0xB7,0xB6,0x76,0x72,0xB2,0xB3,0x73,0xB1,0x71,

  56. 0x70,0xB0,0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,

  57. 0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,0x9C,0x5C,

  58. 0x5D,0x9D,0x5F,0x9F,0x9E,0x5E,0x5A,0x9A,0x9B,0x5B,

  59. 0x99,0x59,0x58,0x98,0x88,0x48,0x49,0x89,0x4B,0x8B,

  60. 0x8A,0x4A,0x4E,0x8E,0x8F,0x4F,0x8D,0x4D,0x4C,0x8C,

  61. 0x44,0x84,0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,

  62. 0x43,0x83,0x41,0x81,0x80,0x40

  63. }

  64. ;

  65.  
  66. unsigned short CalcCrcFast(unsigned char*puchMsg,unsigned short usDataLen)

  67. {

  68. unsigned char uchCRCHi=0xFF ;

  69. unsigned char uchCRCLo=0xFF ;

  70. unsigned short uIndex ;

  71.  
  72. while(usDataLen--)

  73. {

  74. uIndex=uchCRCHi^*puchMsg++;

  75. uchCRCHi=uchCRCLo^m_auchCRCHi[uIndex];

  76. uchCRCLo=m_auchCRCLo[uIndex];

  77. }

  78. return(uchCRCHi<<8|uchCRCLo);

  79. }


PORT.h

 

 
  1. //Download by http://www.NewXing.com

  2. #if !defined(MPORT)

  3. #define MPORT

  4.  
  5.  
  6. #ifdef __cplusplus

  7. extern "C"

  8. {

  9. #endif

  10.  
  11. inline bool IsOpen();

  12. bool OpenPort(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,

  13. unsigned char xParity, unsigned char xStopBit, unsigned long InputBuffSize,

  14. unsigned long OutputBuffSize, unsigned long dwTimerOut);

  15. void ClosePort();

  16. void ClearBuffer();

  17. unsigned long ReadChar(unsigned long dwBufferLength, char *buff, unsigned long dwWaitTime);

  18. unsigned long WriteChar(unsigned long dwBufferLength, char *buff);

  19.  
  20. #ifdef __cplusplus

  21. }

  22. #endif

  23.  
  24. #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. //////////////////////////////////////////////////////////////////////////////////////////////////////////

  9. DCB dcb;

  10. COMMTIMEOUTS CommTimerOuts;

  11. HANDLE hCom= INVALID_HANDLE_VALUE;

  12.  
  13. OVERLAPPED m_overlappedRead;

  14. OVERLAPPED m_overlappedWrite;

  15.  
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////

  17. //通讯端口是否打开

  18. inline bool IsOpen()

  19. {

  20. return hCom != INVALID_HANDLE_VALUE;

  21. }

  22.  
  23. //设置超时

  24. void SetTimerOut(unsigned long dwTimerOut= 5000)

  25. {

  26. if(!IsOpen())

  27. {

  28. return;

  29. }

  30.  
  31. CommTimerOuts.ReadIntervalTimeout= MAXDWORD;

  32. CommTimerOuts.ReadTotalTimeoutConstant= 0;

  33. CommTimerOuts.ReadTotalTimeoutMultiplier= 0;

  34. CommTimerOuts.WriteTotalTimeoutConstant= dwTimerOut;

  35. CommTimerOuts.WriteTotalTimeoutMultiplier= 0;

  36.  
  37. SetCommTimeouts(hCom, &CommTimerOuts);

  38. }

  39.  
  40. //设置DCB参数

  41. bool SetDCBParm(unsigned long xBabd, unsigned char xDataSize,

  42. unsigned char xParity, unsigned char xStopBit)

  43. {

  44. if(!IsOpen())

  45. {

  46. return false;

  47. }

  48.  
  49. if (!GetCommState(hCom, &dcb))

  50. {

  51. #ifdef _DEBUG

  52. printf ("错误: %d << 等到通讯口参数失败.\n", GetLastError());

  53. #endif

  54.  
  55. ClosePort();

  56. return (false);

  57. }

  58. //设置通讯参数

  59. dcb.DCBlength= sizeof(DCB);

  60. dcb.BaudRate = xBabd; // set the baud rate

  61. dcb.ByteSize = xDataSize; // data size, xmit, and rcv

  62. dcb.Parity = xParity; // no parity bit

  63. dcb.StopBits = xStopBit; // one stop bit

  64. if (!SetCommState(hCom, &dcb))

  65. {

  66. #ifdef _DEBUG

  67. printf ("错误: %d << 设置通讯端口参数失败.\n", GetLastError());

  68. #endif

  69. ClosePort();

  70. return (false);

  71. }

  72.  
  73. return true;

  74. }

  75.  
  76. //设置端口缓冲区大小

  77. bool SetPortBuffSize(unsigned long InputBuffSize, unsigned long OutputBuffSize)

  78. {

  79. if(!IsOpen())

  80. {

  81. return false;

  82. }

  83.  
  84. if(!SetupComm(hCom, InputBuffSize, OutputBuffSize))

  85. {

  86. #ifdef _DEBUG

  87. printf ("错误: %d << 设置通讯端口缓冲失败.\n", GetLastError());

  88. #endif

  89. ClosePort();

  90. return (false);

  91. }

  92.  
  93. return true;

  94. }

  95.  
  96. //清理所有缓冲区

  97. void ClearBuffer()

  98. {

  99. if(!IsOpen())

  100. {

  101. return;

  102. }

  103.  
  104. PurgeComm(hCom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

  105. }

  106.  
  107. //当前接收缓冲区字节数目;

  108. unsigned long GetInBuffCount()

  109. {

  110. if(!IsOpen())

  111. {

  112. return(0);

  113. }

  114.  
  115. DWORD dwErrorFalgs;

  116. COMSTAT Comstat;

  117.  
  118. ClearCommError(hCom, &dwErrorFalgs, &Comstat);

  119. return Comstat.cbInQue;

  120. }

  121.  
  122. //当前发送缓冲区字节数目;

  123. unsigned long GetOutBuffCount()

  124. {

  125. if(!IsOpen())

  126. {

  127. return false;

  128. }

  129.  
  130. DWORD dwErrorFalgs;

  131. COMSTAT Comstat;

  132.  
  133. ClearCommError(hCom, &dwErrorFalgs, &Comstat);

  134. return Comstat.cbOutQue;

  135. }

  136.  
  137. ///打开串口////////////////////////////////////////////////////////////////////////////////////////////////////////////

  138. bool OpenPort(unsigned long xPort, unsigned long xBabd, unsigned char xDataSize,

  139. unsigned char xParity, unsigned char xStopBit, unsigned long InputBuffSize,

  140. unsigned long OutputBuffSize, unsigned long dwTimerOut)

  141. {

  142. if(IsOpen())

  143. {

  144. ClosePort();

  145. }

  146. //设置事件

  147. memset(&m_overlappedRead,0,sizeof(OVERLAPPED));

  148. m_overlappedRead.hEvent= CreateEvent(NULL,FALSE,TRUE,"portread");

  149. ASSERT(m_overlappedRead.hEvent != INVALID_HANDLE_VALUE);

  150.  
  151. memset(&m_overlappedWrite,0,sizeof(OVERLAPPED));

  152. m_overlappedWrite.hEvent= CreateEvent(NULL,FALSE,TRUE,"portwrite");

  153. ASSERT(m_overlappedWrite.hEvent != INVALID_HANDLE_VALUE);

  154. //取得串口字符

  155. char com_str[255];

  156. strcpy(com_str, "COM");

  157. ltoa(xPort, com_str + 3, 10);

  158. //打开通讯端口

  159. hCom = CreateFile(com_str,

  160. GENERIC_READ | GENERIC_WRITE,

  161. 0,

  162. NULL,

  163. OPEN_EXISTING,

  164. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,

  165. NULL

  166. );

  167. if (hCom == INVALID_HANDLE_VALUE)

  168. {

  169. #ifdef _DEBUG

  170. printf ("错误: %d << 打开通讯口失败,请检查是否已经安装串口设备.\n", GetLastError());

  171. #endif

  172. return (false);

  173. }

  174. SetPortBuffSize(InputBuffSize,OutputBuffSize);

  175. SetDCBParm(xBabd,xDataSize,xParity,xStopBit);

  176. SetTimerOut(dwTimerOut);

  177. //清理缓冲器

  178. PurgeComm(hCom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

  179. //启动成功

  180. #ifdef _DEBUG

  181. printf ("成功开启端口 %d.\n", com_str);

  182. #endif

  183. return(true);

  184. }

  185.  
  186. //关闭串口;

  187. void ClosePort()

  188. {

  189. if(IsOpen())

  190. {

  191. CloseHandle(hCom);

  192. hCom= INVALID_HANDLE_VALUE;

  193. }

  194.  
  195. //ResetEvent(m_overlappedRead.hEvent);

  196. //ResetEvent(m_overlappedWrite.hEvent);

  197.  
  198. if(m_overlappedRead.hEvent!=NULL)

  199. {

  200. CloseHandle(m_overlappedRead.hEvent);

  201. }

  202. if(m_overlappedWrite.hEvent!=NULL)

  203. {

  204. CloseHandle(m_overlappedWrite.hEvent);

  205. }

  206. }

  207.  
  208. //异步读数据

  209. unsigned long ReadChar(unsigned long dwBufferLength, char *buff, unsigned long dwWaitTime=20)

  210. {

  211. if(!IsOpen())

  212. {

  213. return(0);

  214. }

  215.  
  216. DWORD dwError;

  217. COMSTAT Stat;

  218. if(::ClearCommError(hCom, &dwError, &Stat) && dwError > 0) //清除错误

  219. {

  220. ::PurgeComm(hCom, PURGE_RXABORT | PURGE_RXCLEAR); /*清除输入缓冲区*/

  221. return 0;

  222. }

  223. if(!Stat.cbInQue)// 缓冲区无数据

  224. {

  225. return 0;

  226. }

  227.  
  228. unsigned long uReadLength = 0;

  229. dwBufferLength = dwBufferLength - 1 > Stat.cbInQue ? Stat.cbInQue : dwBufferLength - 1;

  230. if(!::ReadFile(hCom, buff, dwBufferLength, &uReadLength, &m_overlappedRead)) //2000 下 ReadFile 始终返回 True

  231. {

  232. if(::GetLastError() == ERROR_IO_PENDING) // 结束异步I/O

  233. {

  234. WaitForSingleObject(m_overlappedRead.hEvent, dwWaitTime); //等待20ms

  235. if(!::GetOverlappedResult(hCom, &m_overlappedRead, &uReadLength, false))

  236. {

  237. if(::GetLastError() != ERROR_IO_INCOMPLETE)//其他错误

  238. {

  239. uReadLength = 0;

  240. }

  241. }

  242. }

  243. else

  244. {

  245. uReadLength = 0;

  246. }

  247. }

  248.  
  249. return uReadLength;

  250. }

  251.  
  252. //异步写数据

  253. unsigned long WriteChar(unsigned long dwBufferLength, char *buff)

  254. {

  255. if(!IsOpen())

  256. {

  257. return 0;

  258. }

  259.  
  260. DWORD dwError;

  261. if(ClearCommError(hCom, &dwError, NULL) && dwError > 0) //清除错误

  262. {

  263. PurgeComm(hCom, PURGE_TXABORT | PURGE_TXCLEAR);

  264. }

  265.  
  266. unsigned long uWriteLength = 0;

  267. if(!WriteFile(hCom, buff, dwBufferLength, &uWriteLength, &m_overlappedWrite))

  268. {

  269. if(GetLastError() == ERROR_IO_PENDING)

  270. {

  271. DWORD m_tmp=0;

  272. m_tmp= WaitForSingleObject(m_overlappedWrite.hEvent, 1000);

  273. if(m_tmp== WAIT_TIMEOUT || m_tmp== WAIT_ABANDONED)

  274. {

  275. return(0);

  276. }

  277. else if(m_tmp== WAIT_OBJECT_0)

  278. {

  279. if(!GetOverlappedResult(hCom,&m_overlappedWrite,&uWriteLength,false))

  280. {

  281. return(0);

  282. }

  283. else

  284. {

  285. return uWriteLength;

  286. }

  287. }

  288.  
  289. uWriteLength = 0;

  290. }

  291. }

  292.  
  293. return uWriteLength;

  294. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值