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.

366 lines
8.6 KiB

  1. #include <stdio.h>
  2. #include <windows.h>
  3. BOOL DoComIo(LPSTR lpCom,DWORD Baud,BYTE Size,BYTE Parity,BYTE Stop);
  4. //#define COM_DEB 1
  5. #define NUM 128
  6. #define print printf
  7. #define SETTINGS1 "COM1",9600,8,NOPARITY,ONESTOPBIT
  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. DWORD dwSize;
  12. DWORD dwRIT,dwRTTM,dwRTTC;
  13. DWORD main(int argc, char *argv[], char *envp[])
  14. {
  15. BOOL bRc;
  16. CHAR chDummy;
  17. BOOL bSize,bRIT,bRTTM,bRTTC;
  18. UNREFERENCED_PARAMETER(envp);
  19. bRIT = bRTTM = bSize = bRTTC = FALSE;
  20. while(argc--)
  21. {
  22. switch(argv[argc][1])
  23. {
  24. // case 'f' :
  25. // case 'F' : {
  26. // if (argv[argc][0] != '-') break;
  27. // printf("filename option=%s\n\n",argv[argc]);
  28. // sscanf(argv[argc],"%c %c %s",&chDummy,&chDummy,lpFilename);
  29. // printf("filename to be displayed=%s\n\n",lpFilename);
  30. // bFile = TRUE;
  31. // break;
  32. // }
  33. case 'I' :
  34. case 'i' : {
  35. if (argv[argc][0] != '-') break;
  36. printf("RIT option=%s\n\n",argv[argc]);
  37. sscanf(argv[argc],"%c %c %lx",&chDummy,&chDummy,&dwRIT);
  38. printf("RIT =%lx\n\n",dwRIT);
  39. bRIT = TRUE;
  40. break;
  41. }
  42. case 'M' :
  43. case 'm' : {
  44. if (argv[argc][0] != '-') break;
  45. printf("RTTM option=%s\n\n",argv[argc]);
  46. sscanf(argv[argc],"%c %c %lx",&chDummy,&chDummy,&dwRTTM);
  47. printf("RTTM =%lx\n\n",dwRTTM);
  48. bRTTM = TRUE;
  49. break;
  50. }
  51. case 'C' :
  52. case 'c' : {
  53. if (argv[argc][0] != '-') break;
  54. printf("RTTC option=%s\n\n",argv[argc]);
  55. sscanf(argv[argc],"%c %c %lx",&chDummy,&chDummy,&dwRTTC);
  56. printf("RTTC =%lx\n\n",dwRTTC);
  57. bRTTC = TRUE;
  58. break;
  59. }
  60. case 'S' :
  61. case 's' : {
  62. if (argv[argc][0] != '-') break;
  63. printf("size option=%s\n\n",argv[argc]);
  64. sscanf(argv[argc],"%c %c %lx",&chDummy,&chDummy,&dwSize);
  65. printf("Size to be xfered =%lx bytes\n\n",dwSize);
  66. if (dwSize > NUM)
  67. {
  68. printf("dwSize[%lx] should be < %lx\n",dwSize,NUM);
  69. return -1;
  70. }
  71. bSize = TRUE;
  72. break;
  73. }
  74. default: {
  75. break;
  76. }
  77. }
  78. }
  79. if (!bRIT || !bRTTM || !bRTTC ||!bSize)
  80. {
  81. printf("\n\nOptions are required!!\n\n");
  82. printf("timeout <required options>\n\n");
  83. printf("options:\n");
  84. printf(" -i<read interval timeout > eg: -i0x0000>\n\n");
  85. printf(" -m<read total timeout multiplier> eg: -m0x0000>\n\n");
  86. printf(" -c<read total timeout constant> eg: -c0x0000>\n\n");
  87. printf(" -s<size in bytes to be xfred eg: -s0x10>\n\n");
  88. return (-1);
  89. }
  90. print("\n\n *** Doing COM TEST with [port=%s Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  91. SETTINGS1);
  92. bRc = DoComIo(SETTINGS1);
  93. if (!bRc) {
  94. print("\n\nCOM TEST FAILED********************************\n\n");
  95. }
  96. return 0;
  97. }
  98. BOOL DoComIo(LPSTR lpCom,DWORD Baud,BYTE Size,BYTE Parity,BYTE Stop)
  99. {
  100. CHAR WrBuffer[NUM];
  101. CHAR RdBuffer[NUM];
  102. DWORD i;
  103. HANDLE hCommPort;
  104. DCB dcb;
  105. BOOL bRc;
  106. DWORD dwNumWritten,dwNumRead,dwErrors;
  107. COMMTIMEOUTS CommTimeOuts;
  108. print("\n\n *** COMM TEST START [port=%s,Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  109. lpCom,Baud,Size,Parity,Stop);
  110. print("Opening the comm port for read write\n");
  111. hCommPort = CreateFile(
  112. lpCom,
  113. GENERIC_READ|GENERIC_WRITE,
  114. 0, // exclusive
  115. NULL, // sec attr
  116. OPEN_EXISTING,
  117. 0, // no attributes
  118. NULL); // no template
  119. if (hCommPort == (HANDLE)-1)
  120. {
  121. print("FAIL: OpenComm failed rc: %lx\n",hCommPort);
  122. return FALSE;
  123. }
  124. print("Opening the comm port for read write: SUCCESS hCommPort=%lx\n",hCommPort);
  125. print("Setting the line characteristics on comm \n");
  126. if (!GetCommState(hCommPort,&dcb))
  127. {
  128. printf("FAIL: Couldn't get the dcb: %d\n",GetLastError());
  129. return FALSE;
  130. }
  131. dcb.DCBlength = sizeof(DCB);
  132. // dcb.DCBversion = 0x0002; BUG BUG in spec not in header
  133. dcb.BaudRate = Baud;
  134. dcb.ByteSize = Size;
  135. dcb.Parity = Parity;
  136. dcb.StopBits = Stop;
  137. bRc = SetupComm(hCommPort,1024,1024);
  138. printf("bRc from CommSetup = %lx\n",bRc);
  139. if (!bRc)
  140. {
  141. printf("FAIL: comsetup\n");
  142. }
  143. bRc = SetCommState(hCommPort,&dcb);
  144. if (!bRc)
  145. {
  146. print("FAIL: cannot set the comm state rc:%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("Setting the line characteristics on comm: SUCCESS\n");
  155. CommTimeOuts.ReadIntervalTimeout = dwRIT;
  156. CommTimeOuts.ReadTotalTimeoutMultiplier = dwRTTM;
  157. CommTimeOuts.ReadTotalTimeoutConstant = dwRTTC;
  158. CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
  159. CommTimeOuts.WriteTotalTimeoutConstant = 0;
  160. printf("setting timeouts: RIT=%lx RTTM = %lx RTTC = %lx\n",
  161. dwRIT, dwRTTM, dwRTTC);
  162. bRc = SetCommTimeouts(hCommPort, &CommTimeOuts);
  163. printf("bRc from setcommtimeouts = %lx\n",bRc);
  164. if (!bRc)
  165. {
  166. printf("FAIL: setcommtimeouts\n");
  167. }
  168. printf("Filling the buffer with the known chars \n");
  169. for (i=0; i<dwSize; i++)
  170. {
  171. //WrBuffer[i] = 'a';
  172. WrBuffer[i] = (CHAR)i;
  173. }
  174. print("Filling the buffer with the known chars : SUCCESS\n");
  175. #ifdef COM_DEB
  176. print("Dumping the buffer before sending it to comm\n");
  177. for (i=0; i< dwSize; i++)
  178. {
  179. //print("%c",RdBuffer[i]);
  180. print(" %d ",WrBuffer[i]);
  181. }
  182. print("\nDumping the buffer before sending it to comm SUCCESS\n");
  183. #endif
  184. print("Filling the Rdbuffer with the known chars (0xFF) to makeit dirty\n");
  185. for (i=0; i< dwSize; i++)
  186. {
  187. RdBuffer[i] = (CHAR)'0xFF';
  188. }
  189. print("Filling the Rdbuffer with the known chars (0xFF): SUCCESS\n");
  190. print("Writting this buffer to the comm port\n");
  191. bRc = WriteFile( hCommPort,
  192. WrBuffer,
  193. dwSize,
  194. &dwNumWritten,
  195. NULL);
  196. if (!bRc)
  197. {
  198. print("FAIL: cannot Write To the comm port:%lx\n",bRc);
  199. bRc = CloseHandle(hCommPort);
  200. if (!bRc)
  201. {
  202. print("FAIL: cannot close the comm port:%lx\n",bRc);
  203. }
  204. return FALSE;
  205. }
  206. print("Writting this buffer to the comm port: SUCCESS rc:%lx, byteswritten:%lx\n",
  207. bRc,dwNumWritten);
  208. print("Reading this buffer from the comm port\n");
  209. bRc = ReadFile( hCommPort,
  210. RdBuffer,
  211. dwSize,
  212. &dwNumRead,
  213. NULL);
  214. if (!bRc)
  215. {
  216. print("FAIL: cannot Read From the comm port:%lx\n",bRc);
  217. bRc = CloseHandle(hCommPort);
  218. if (!bRc)
  219. {
  220. print("FAIL: cannot close the comm port:%lx\n",bRc);
  221. }
  222. return FALSE;
  223. }
  224. print("Reading this buffer from the comm port: SUCCESS rc:%lx, bytesread:%lx\n",
  225. bRc,dwNumRead);
  226. //#ifdef COM_DEB
  227. print("Dumping the Rdbuffer with the comm data\n");
  228. for (i=0; i< dwSize; i++)
  229. {
  230. //print("%c",RdBuffer[i]);
  231. print(" %d ",RdBuffer[i]);
  232. }
  233. print("\nDumping the Rdbuffer with the comm data: SUCCESS\n");
  234. //#endif
  235. print("Comparing the rd and wr buffers\n");
  236. for (i=0; i< dwSize; i++)
  237. {
  238. if (RdBuffer[i] != WrBuffer[i])
  239. {
  240. print("FAIL: BufferMisMatch: RdBuffer[%d]=%lx,WrBuffer[%d]=%lx\n",
  241. i,RdBuffer[i],i,WrBuffer[i]);
  242. bRc = CloseHandle(hCommPort);
  243. if (!bRc)
  244. {
  245. print("FAIL: cannot close the comm port:%lx\n",bRc);
  246. }
  247. return FALSE;
  248. }
  249. }
  250. print("Comparing the rd and wr buffers: SUCCESS\n");
  251. bRc = ClearCommError(hCommPort,&dwErrors,NULL);
  252. print("ClearCommError: rc= %lx and dwErrors=%lx\n",bRc,dwErrors);
  253. bRc = PurgeComm(hCommPort,0);
  254. print("PurgeComm (%lx,0) rc = %lx\n",hCommPort,bRc);
  255. //bRc = FlushFileBuffers(hCommPort);
  256. //print("flushfilebuffers(%lx) rc = %lx\n",hCommPort,bRc);
  257. print("Closing the comm port\n");
  258. bRc = CloseHandle(hCommPort);
  259. if (!bRc)
  260. {
  261. print("FAIL: cannot close the comm port:%lx\n",bRc);
  262. return FALSE;
  263. }
  264. print("\n\n*** COMM TEST OVER*** \n\n");
  265. }