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.

262 lines
7.1 KiB

2 years ago
2 years ago
2 years ago
  1. #include "serial.h"
  2. #include <stdio.h>
  3. #include <windows.h>
  4. /***
  5. *
  6. * stopbits只有三种选择
  7. * 1.1
  8. * 2.1.5
  9. * 3.2
  10. * -1
  11. */
  12. static int switch_get_stopbits(int stopbits) {
  13. int real_stopbits;
  14. switch (stopbits) {
  15. case 1:
  16. real_stopbits = ONESTOPBIT;
  17. break;
  18. case 2:
  19. real_stopbits = ONE5STOPBITS;
  20. break;
  21. case 3:
  22. real_stopbits = TWOSTOPBITS;
  23. break;
  24. default:
  25. real_stopbits = -1;
  26. break;
  27. }
  28. return real_stopbits;
  29. }
  30. PORT OpenPort(int idx) {
  31. HANDLE hComm;
  32. TCHAR comname[100];
  33. wsprintf(comname, TEXT("\\\\.\\COM%d"), idx);
  34. hComm = CreateFile(comname, // port name
  35. GENERIC_READ | GENERIC_WRITE, // Read/Write
  36. 0, // No Sharing
  37. NULL, // No Security
  38. OPEN_EXISTING, // Open existing port only
  39. 0, // Non Overlapped I/O
  40. NULL); // Null for Comm Devices
  41. if (hComm == INVALID_HANDLE_VALUE) return NULL;
  42. COMMTIMEOUTS timeouts = {0};
  43. timeouts.ReadIntervalTimeout = 50;
  44. timeouts.ReadTotalTimeoutConstant = 50;
  45. timeouts.ReadTotalTimeoutMultiplier = 10;
  46. timeouts.WriteTotalTimeoutConstant = 50;
  47. timeouts.WriteTotalTimeoutMultiplier = 10;
  48. if (SetCommTimeouts(hComm, &timeouts) == FALSE) return NULL;
  49. if (SetCommMask(hComm, EV_RXCHAR) == FALSE) return NULL;
  50. printf("open%d ok\n", idx);
  51. return hComm;
  52. }
  53. void ClosePort(PORT com_port) { CloseHandle(com_port); }
  54. int SetPortBoudRate(PORT com_port, int rate) {
  55. DCB dcbSerialParams = {0};
  56. BOOL Status;
  57. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  58. Status = GetCommState(com_port, &dcbSerialParams);
  59. if (Status == FALSE) return FALSE;
  60. dcbSerialParams.BaudRate = rate;
  61. Status = SetCommState(com_port, &dcbSerialParams);
  62. return Status;
  63. }
  64. int SetPortDataBits(PORT com_port, int bits) {
  65. DCB dcbSerialParams = {0};
  66. BOOL Status;
  67. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  68. Status = GetCommState(com_port, &dcbSerialParams);
  69. if (Status == FALSE) return FALSE;
  70. dcbSerialParams.ByteSize = bits;
  71. Status = SetCommState(com_port, &dcbSerialParams);
  72. return Status;
  73. }
  74. int SetPortStopBits(PORT com_port, int bits) {
  75. DCB dcbSerialParams = {0};
  76. BOOL Status;
  77. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  78. Status = GetCommState(com_port, &dcbSerialParams);
  79. if (Status == FALSE) return FALSE;
  80. dcbSerialParams.StopBits = bits;
  81. Status = SetCommState(com_port, &dcbSerialParams);
  82. return Status;
  83. }
  84. // 默认为无校验。NOPARITY 0; ODDPARITY 1;EVENPARITY 2;MARKPARITY 3;SPACEPARITY 4
  85. int SetPortParity(PORT com_port, int parity) {
  86. DCB dcbSerialParams = {0};
  87. BOOL Status;
  88. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  89. Status = GetCommState(com_port, &dcbSerialParams);
  90. if (Status == FALSE) return FALSE;
  91. dcbSerialParams.Parity = parity;
  92. Status = SetCommState(com_port, &dcbSerialParams);
  93. return Status;
  94. }
  95. int GetPortBoudRate(PORT com_port) {
  96. DCB dcbSerialParams = {0};
  97. BOOL Status;
  98. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  99. Status = GetCommState(com_port, &dcbSerialParams);
  100. if (Status == FALSE) return -1;
  101. return dcbSerialParams.BaudRate;
  102. }
  103. int GetPortDataBits(PORT com_port) {
  104. DCB dcbSerialParams = {0};
  105. BOOL Status;
  106. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  107. Status = GetCommState(com_port, &dcbSerialParams);
  108. if (Status == FALSE) return -1;
  109. return dcbSerialParams.ByteSize;
  110. }
  111. int GetPortStopBits(PORT com_port) {
  112. DCB dcbSerialParams = {0};
  113. BOOL Status;
  114. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  115. Status = GetCommState(com_port, &dcbSerialParams);
  116. if (Status == FALSE) return -1;
  117. return dcbSerialParams.StopBits;
  118. }
  119. int GetPortParity(PORT com_port) {
  120. DCB dcbSerialParams = {0};
  121. BOOL Status;
  122. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  123. Status = GetCommState(com_port, &dcbSerialParams);
  124. if (Status == FALSE) return -1;
  125. return dcbSerialParams.Parity;
  126. }
  127. int SendData(PORT com_port, const char *data) {
  128. DWORD dNoOFBytestoWrite = strlen(data);
  129. DWORD dNoOfBytesWritten;
  130. BOOL Status = WriteFile(com_port, data, dNoOFBytestoWrite, &dNoOfBytesWritten, NULL);
  131. if (Status == FALSE) {
  132. return -1;
  133. } else {
  134. printf("%s\n", data);
  135. }
  136. return dNoOfBytesWritten;
  137. }
  138. int ReciveData(PORT com_port, char *data, int len) {
  139. DWORD dwEventMask;
  140. DWORD NoBytesRead;
  141. BOOL Status = WaitCommEvent(com_port, &dwEventMask, NULL);
  142. if (Status == FALSE) {
  143. return FALSE;
  144. }
  145. Status = ReadFile(com_port, data, len, &NoBytesRead, NULL);
  146. data[NoBytesRead] = 0;
  147. if (Status == FALSE) {
  148. return FALSE;
  149. } else {
  150. printf("%s\n", data);
  151. }
  152. return TRUE;
  153. }
  154. PORT serial_init(int idx, int rate, int databits, int stopbits, int parity) {
  155. int ret = 0;
  156. PORT com_port;
  157. com_port = OpenPort(idx);
  158. if (com_port == INVALID_HANDLE_VALUE) {
  159. printf("open COM%d fail\n", idx);
  160. return NULL;
  161. }
  162. ret = SetPortBoudRate(com_port, rate);
  163. if (ret == FALSE) {
  164. printf("set COM%d band fail\n", idx);
  165. return NULL;
  166. }
  167. ret = SetPortDataBits(com_port, databits);
  168. if (ret == FALSE) {
  169. printf("set COM%d databits fail\n", idx);
  170. return NULL;
  171. }
  172. stopbits = switch_get_stopbits(stopbits);
  173. if (stopbits == -1) {
  174. printf("stopbits set error\n");
  175. }
  176. ret = SetPortStopBits(com_port, stopbits);
  177. if (ret == FALSE) {
  178. printf("set COM%d stopbits fail\n", idx);
  179. return NULL;
  180. }
  181. ret = SetPortParity(com_port, parity);
  182. if (ret == FALSE) {
  183. printf("set COM%d parity fail\n", idx);
  184. return NULL;
  185. }
  186. return com_port;
  187. }
  188. int Serial_SendData(PORT com_port, const char *data, int len) {
  189. DWORD dNoOfBytesWritten;
  190. BOOL Status = WriteFile(com_port, data, len, &dNoOfBytesWritten, NULL);
  191. if (Status == FALSE)
  192. return -1;
  193. else
  194. printf("send ok\n");
  195. return 0;
  196. }
  197. int Serial_ReciveData(PORT com_port, char *data, int len) {
  198. DWORD dwEventMask;
  199. DWORD NoBytesRead;
  200. BOOL Status = WaitCommEvent(com_port, &dwEventMask, NULL);
  201. if (Status == FALSE) {
  202. return -1;
  203. }
  204. Status = ReadFile(com_port, data, len, &NoBytesRead, NULL);
  205. data[NoBytesRead] = 0;
  206. if (Status == FALSE)
  207. return -1;
  208. else
  209. printf("rcv ok\n");
  210. return NoBytesRead;
  211. }
  212. /* 测试函数,demo工程放在main中,但是我不需要,暂时放在这 */
  213. void serial_test(int serial_com_id) {
  214. PORT COM1;
  215. char buff[1024] = {0};
  216. int rcv_len = 0;
  217. printf("Start open com%d\n", serial_com_id);
  218. COM1 = serial_init(serial_com_id, 115200, 8, 1, 0);
  219. while (1) {
  220. Serial_SendData(COM1, "hello finny", 12);
  221. memset(buff, 0, 1024);
  222. rcv_len = Serial_ReciveData(COM1, buff, 1024);
  223. printf("rcv:%s\n", buff);
  224. Sleep(1);
  225. }
  226. }