当前位置:   article > 正文

C++ RS232串口通信_c++ rs232通讯

c++ rs232通讯

头文件

  1. #ifndef SERIALCOM_H
  2. #define SERIALCOM_H
  3. #include <sys/select.h>
  4. #include <stdio.h>
  5. class SerialCom
  6. {
  7. public:
  8. SerialCom();
  9. ~SerialCom();
  10. int openCom(char *pcom,int baud);
  11. int sent_to(char *pstr,int len);
  12. int recv_from(char *precvBuf);
  13. void closeCom();
  14. int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
  15. private:
  16. int comHandle;
  17. fd_set m_stRfds;
  18. };
  19. #endif // SERIALCOM_H

源文件

  1. #include "serialcom.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <termios.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. SerialCom::SerialCom()
  11. {
  12. comHandle = 0;
  13. }
  14. SerialCom::~SerialCom()
  15. {
  16. if(comHandle)
  17. closeCom();
  18. comHandle = 0;
  19. }
  20. int SerialCom::set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
  21. {
  22. struct termios newtio,oldtio;
  23. if ( tcgetattr( fd,&oldtio) != 0) {
  24. perror("SetupSerial 1");
  25. g_MainCoreLog->LogError("[%s][%s]()-[%d]SetupSerial "
  26. , __FILE__, __FUNCTION__, __LINE__);
  27. return -1;
  28. }
  29. bzero( &newtio, sizeof( newtio ) );
  30. newtio.c_cflag |= CLOCAL | CREAD;
  31. newtio.c_cflag &= ~CSIZE;
  32. switch( nBits )
  33. {
  34. case 7:
  35. newtio.c_cflag |= CS7;
  36. break;
  37. case 8:
  38. newtio.c_cflag |= CS8;
  39. break;
  40. }
  41. switch( nEvent )
  42. {
  43. case 'O':
  44. newtio.c_cflag |= PARENB;
  45. newtio.c_cflag |= PARODD;
  46. newtio.c_iflag |= (INPCK );
  47. break;
  48. case 'E':
  49. newtio.c_iflag |= (INPCK);
  50. newtio.c_cflag |= PARENB;
  51. newtio.c_cflag &= ~PARODD;
  52. break;
  53. case 'N':
  54. newtio.c_cflag &= ~PARENB;
  55. break;
  56. }
  57. switch( nSpeed )
  58. {
  59. case 2400:
  60. cfsetispeed(&newtio, B2400);
  61. cfsetospeed(&newtio, B2400);
  62. break;
  63. case 4800:
  64. cfsetispeed(&newtio, B4800);
  65. cfsetospeed(&newtio, B4800);
  66. break;
  67. case 9600:
  68. cfsetispeed(&newtio, B9600);
  69. cfsetospeed(&newtio, B9600);
  70. break;
  71. case 115200:
  72. cfsetispeed(&newtio, B115200);
  73. cfsetospeed(&newtio, B115200);
  74. break;
  75. case 460800:
  76. cfsetispeed(&newtio, B460800);
  77. cfsetospeed(&newtio, B460800);
  78. break;
  79. default:
  80. cfsetispeed(&newtio, B9600);
  81. cfsetospeed(&newtio, B9600);
  82. break;
  83. }
  84. if( nStop == 1 )
  85. {
  86. newtio.c_cflag &= ~CSTOPB;
  87. }
  88. else if ( nStop == 2 )
  89. {
  90. newtio.c_cflag |= CSTOPB;
  91. }
  92. newtio.c_cc[VTIME] = 1;///* 设置超时10 seconds*/
  93. newtio.c_cc[VMIN] = 8;
  94. tcflush(fd,TCIFLUSH);
  95. if((tcsetattr(fd,TCSANOW,&newtio))!=0)
  96. {
  97. perror("com set error");
  98. g_MainCoreLog->LogError("[%s][%s]()-[%d]com set error "
  99. , __FILE__, __FUNCTION__, __LINE__);
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. int SerialCom::openCom(char *pcom,int baud)
  105. {
  106. struct termios termios;
  107. g_MainCoreLog->LogInfo("[%s][%s]()-[%d]openCom "
  108. , __FILE__, __FUNCTION__, __LINE__);
  109. bzero(&termios,sizeof(termios));
  110. comHandle = open(pcom,O_RDWR|O_NOCTTY); //|O_NOCTTY|O_NONBLOCK
  111. if(comHandle < 0)
  112. {
  113. g_MainCoreLog->LogError("[%s][%s]()-[%d]com open failed "
  114. , __FILE__, __FUNCTION__, __LINE__);
  115. return -1;
  116. }
  117. g_MainCoreLog->LogInfo("[%s][%s]()-[%d]opecom open successed,COM %d Baud:%d "
  118. , __FILE__, __FUNCTION__, __LINE__,comHandle,baud);
  119. set_opt(comHandle, baud, 8, 'N', 1);
  120. return 0;
  121. }
  122. int SerialCom::sent_to(char *pstr,int len)
  123. {
  124. if(comHandle <= 0)
  125. return -1;
  126. if (write(comHandle, pstr, len) != len)
  127. {
  128. g_MainCoreLog->LogError("[%s][%s]()-[%d]send cmd failed "
  129. , __FILE__, __FUNCTION__, __LINE__);
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. int SerialCom::recv_from(char *precvBuf)
  135. {
  136. if(comHandle <= 0)
  137. {
  138. printf("failed comHandle <= 0 \r\n");
  139. g_MainCoreLog->LogError("[%s][%s]()-[%d]failed comHandle <= 0 "
  140. , __FILE__, __FUNCTION__, __LINE__);
  141. return -1;
  142. }
  143. int rlen = 0;
  144. //for(int i=0;i<8;i++)
  145. {
  146. // printf("start recv 1 byte \r\n");
  147. tcflush(comHandle,TCIFLUSH);
  148. int iNum = read(comHandle,(void*)precvBuf,8);
  149. // int iNum = read(comHandle,(void*)precvBuf,8);
  150. //printf("iNum:%d \r\n",iNum);
  151. rlen+=iNum;
  152. }
  153. return rlen;
  154. }
  155. void SerialCom::closeCom()
  156. {
  157. if(comHandle <= 0)
  158. return;
  159. close(comHandle);
  160. }

 

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号