Source code of Windows XP (NT5)
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.

277 lines
7.0 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. //#define COM_DEB 1
  4. #define NUM 128
  5. #define print printf
  6. #define SETTINGS1 "COM1",9600,8,NOPARITY,ONESTOPBIT
  7. #define SETTINGS15 "COM1",9600,8,NOPARITY,ONE5STOPBITS
  8. #define SETTINGS2 "COM1",4800,8,NOPARITY,ONESTOPBIT
  9. #define SETTINGS3 "COM1",2400,8,NOPARITY,ONESTOPBIT
  10. #define SETTINGS4 "COM1",1200,8,NOPARITY,ONESTOPBIT
  11. BOOL DoComIo(LPSTR lpCom,DWORD Baud,BYTE Size,BYTE Parity,BYTE Stop);
  12. DWORD main(int argc, char *argv[], char *envp[])
  13. {
  14. BOOL bRc;
  15. UNREFERENCED_PARAMETER(argc);
  16. UNREFERENCED_PARAMETER(argv);
  17. UNREFERENCED_PARAMETER(envp);
  18. print("\n\n *** Doing COM TEST with [port=%s Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  19. SETTINGS1);
  20. bRc = DoComIo(SETTINGS1);
  21. if (!bRc) {
  22. print("\n\nCOM TEST FAILED********************************\n\n");
  23. }
  24. print("\n\n *** Doing COM TEST with [port=%s Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  25. SETTINGS15);
  26. bRc = DoComIo(SETTINGS15);
  27. if (!bRc) {
  28. print("\n\nCOM TEST FAILED********************************\n\n");
  29. }
  30. print("\n\n *** Doing COM TEST with [port=%s Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  31. SETTINGS2);
  32. bRc = DoComIo(SETTINGS2);
  33. if (!bRc) {
  34. print("\n\nCOM TEST FAILED********************************\n\n");
  35. }
  36. print("\n\n *** Doing COM TEST with [port=%s Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  37. SETTINGS3);
  38. bRc = DoComIo(SETTINGS3);
  39. if (!bRc) {
  40. print("\n\nCOM TEST FAILED********************************\n\n");
  41. }
  42. print("\n\n *** Doing COM TEST with [port=%s Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  43. SETTINGS4);
  44. bRc = DoComIo(SETTINGS4);
  45. if (!bRc) {
  46. print("\n\nCOM TEST FAILED********************************\n\n");
  47. }
  48. return 0;
  49. }
  50. BOOL DoComIo(LPSTR lpCom,DWORD Baud,BYTE Size,BYTE Parity,BYTE Stop)
  51. {
  52. CHAR WrBuffer[NUM];
  53. CHAR RdBuffer[NUM];
  54. DWORD i;
  55. HANDLE hCommPort;
  56. DCB dcb;
  57. BOOL bRc;
  58. DWORD dwNumWritten,dwNumRead,dwErrors;
  59. print("\n\n *** COMM TEST START [port=%s,Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  60. lpCom,Baud,Size,Parity,Stop);
  61. print("Opening the comm port for read write\n");
  62. hCommPort = CreateFile(
  63. lpCom,
  64. GENERIC_READ|GENERIC_WRITE,
  65. 0, // exclusive
  66. NULL, // sec attr
  67. OPEN_EXISTING,
  68. 0, // no attributes
  69. NULL); // no template
  70. if (hCommPort == (HANDLE)-1)
  71. {
  72. print("FAIL: OpenComm failed rc: %lx\n",hCommPort);
  73. return FALSE;
  74. }
  75. print("Opening the comm port for read write: SUCCESS hCommPort=%lx\n",hCommPort);
  76. print("Setting the line characteristics on comm \n");
  77. if(!GetCommState(
  78. hCommPort,
  79. &dcb
  80. )) {
  81. printf("Could not get the state of the comm port: %d\n",GetLastError());
  82. return FALSE;
  83. }
  84. dcb.DCBlength = sizeof(DCB);
  85. // dcb.DCBversion = 0x0002;
  86. dcb.BaudRate = Baud;
  87. dcb.ByteSize = Size;
  88. dcb.Parity = Parity;
  89. dcb.StopBits = Stop;
  90. //dcb.RlsTimeout = 10000; 10sec
  91. //dcb.CtsTimeout = 10000; 10sec
  92. //dcb.DsrTimeout = 10000; 10sec
  93. dcb.fBinary = 1; // binary data xmit
  94. dcb.fParity = 0; // dont bother about parity
  95. dcb.fOutxCtsFlow= 0; // no cts flow control
  96. dcb.fOutxDsrFlow= 0; // no dsr flow control
  97. dcb.fDtrControl = DTR_CONTROL_DISABLE; // dont bother about dtr
  98. dcb.fRtsControl = RTS_CONTROL_DISABLE; // dont bother about dtr
  99. dcb.fOutX =0; // disable xoff handling
  100. dcb.fInX =0; // disable xon handling
  101. dcb.fErrorChar = 0; // forget about parity char
  102. dcb.fNull = 0; // forget about the null striping
  103. //dcb.fChEvt = 0; forget about event char
  104. bRc = SetCommState(hCommPort,&dcb);
  105. if (!bRc)
  106. {
  107. print("FAIL: cannot set the comm state rc:%lx\n",bRc);
  108. bRc = CloseHandle(hCommPort);
  109. if (!bRc)
  110. {
  111. print("FAIL: cannot close the comm port:%lx\n",bRc);
  112. }
  113. return FALSE;
  114. }
  115. print("Setting the line characteristics on comm: SUCCESS\n");
  116. print("Filling the buffer with the known chars \n");
  117. for (i=0; i< NUM; i++)
  118. {
  119. //WrBuffer[i] = 'a';
  120. WrBuffer[i] = (CHAR)i;
  121. }
  122. print("Filling the buffer with the known chars : SUCCESS\n");
  123. #ifdef COM_DEB
  124. print("Dumping the buffer before sending it to comm\n");
  125. for (i=0; i< NUM; i++)
  126. {
  127. //print("%c",RdBuffer[i]);
  128. print(" %d ",WrBuffer[i]);
  129. }
  130. print("\nDumping the buffer before sending it to comm SUCCESS\n");
  131. #endif
  132. print("Filling the Rdbuffer with the known chars (0xFF) to makeit dirty\n");
  133. for (i=0; i< NUM; i++)
  134. {
  135. RdBuffer[i] = (CHAR)'0xFF';
  136. }
  137. print("Filling the Rdbuffer with the known chars (0xFF): SUCCESS\n");
  138. print("Writting this buffer to the comm port\n");
  139. bRc = WriteFile( hCommPort,
  140. WrBuffer,
  141. NUM,
  142. &dwNumWritten,
  143. NULL);
  144. if (!bRc)
  145. {
  146. print("FAIL: cannot Write To the comm port:%lx\n",bRc);
  147. bRc = CloseHandle(hCommPort);
  148. if (!bRc)
  149. {
  150. print("FAIL: cannot close the comm port:%lx\n",bRc);
  151. }
  152. return FALSE;
  153. }
  154. print("Writting this buffer to the comm port: SUCCESS rc:%lx, byteswritten:%lx\n",
  155. bRc,dwNumWritten);
  156. print("Reading this buffer from the comm port\n");
  157. bRc = ReadFile( hCommPort,
  158. RdBuffer,
  159. NUM,
  160. &dwNumRead,
  161. NULL);
  162. if (!bRc)
  163. {
  164. print("FAIL: cannot Read From the comm port:%lx\n",bRc);
  165. bRc = CloseHandle(hCommPort);
  166. if (!bRc)
  167. {
  168. print("FAIL: cannot close the comm port:%lx\n",bRc);
  169. }
  170. return FALSE;
  171. }
  172. print("Reading this buffer from the comm port: SUCCESS rc:%lx, bytesread:%lx\n",
  173. bRc,dwNumRead);
  174. #ifdef COM_DEB
  175. print("Dumping the Rdbuffer with the comm data\n");
  176. for (i=0; i< NUM; i++)
  177. {
  178. //print("%c",RdBuffer[i]);
  179. print(" %d ",RdBuffer[i]);
  180. }
  181. print("\nDumping the Rdbuffer with the comm data: SUCCESS\n");
  182. #endif
  183. print("Comparing the rd and wr buffers\n");
  184. for (i=0; i< NUM; i++)
  185. {
  186. if (RdBuffer[i] != WrBuffer[i])
  187. {
  188. print("FAIL: BufferMisMatch: RdBuffer[%d]=%lx,WrBuffer[%d]=%lx\n",
  189. i,RdBuffer[i],i,WrBuffer[i]);
  190. bRc = CloseHandle(hCommPort);
  191. if (!bRc)
  192. {
  193. print("FAIL: cannot close the comm port:%lx\n",bRc);
  194. }
  195. return FALSE;
  196. }
  197. }
  198. print("Comparing the rd and wr buffers: SUCCESS\n");
  199. bRc = ClearCommError(hCommPort,&dwErrors,NULL);
  200. print("ClearCommError: rc= %lx and dwErrors=%lx\n",bRc,dwErrors);
  201. //bRc = PurgeComm(hCommPort,0);
  202. print("BYPASS PurgeComm BUG (%lx,0) rc = %lx\n",hCommPort,bRc);
  203. print("Closing the comm port\n");
  204. bRc = CloseHandle(hCommPort);
  205. if (!bRc)
  206. {
  207. print("FAIL: cannot close the comm port:%lx\n",bRc);
  208. return FALSE;
  209. }
  210. print("\n\n*** COMM TEST OVER*** \n\n");
  211. }