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.

456 lines
10 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. //#define COM_DEB 1
  4. #define NUM 1024
  5. //128
  6. #define print printf
  7. #define THPRINTF fprintf
  8. #define L_DEBUG stderr
  9. #ifdef RUN_ON_MIPS
  10. #define COM "COM2"
  11. #else
  12. #define COM "COM1"
  13. #endif
  14. #define SETTINGS1 lpFilename,9600,8,NOPARITY,ONESTOPBIT
  15. #define SETTINGS15 lpFilename,9600,5,NOPARITY,ONE5STOPBITS
  16. #define SETTINGS2 lpFilename,4800,8,NOPARITY,ONESTOPBIT
  17. #define SETTINGS3 lpFilename,2400,8,NOPARITY,ONESTOPBIT
  18. #define SETTINGS4 lpFilename,300,8,NOPARITY,ONESTOPBIT
  19. CHAR lpFilename[100];
  20. DWORD dwPacketSize,dwLoop;
  21. BOOL DoComIo(LPSTR lpCom,DWORD Baud,BYTE Size,BYTE Parity,BYTE Stop);
  22. DWORD main(int argc, char *argv[], char *envp[])
  23. {
  24. BOOL bRc;
  25. CHAR chDummy;
  26. BOOL bFile,bPkt,bLoop;
  27. DWORD dwSize,dwSizeHigh,i,j,dwError;
  28. HANDLE hFile,hMOFile;
  29. LPVOID lpBaseAddress;
  30. LPBYTE lpByte;
  31. BYTE Byte = 0x00;
  32. UNREFERENCED_PARAMETER(envp);
  33. bFile = bPkt = bLoop = FALSE;
  34. while(argc--)
  35. {
  36. switch(argv[argc][1])
  37. {
  38. case 'f' :
  39. case 'F' : {
  40. if (argv[argc][0] != '-') break;
  41. THPRINTF(L_DEBUG,"comport name option=%s\n\n",argv[argc]);
  42. sscanf(argv[argc],"%c %c %s",&chDummy,&chDummy,lpFilename);
  43. THPRINTF(L_DEBUG,"com port name =%s\n\n",lpFilename);
  44. bFile = TRUE;
  45. break;
  46. }
  47. case 'p' :
  48. case 'P' : {
  49. if (argv[argc][0] != '-') break;
  50. THPRINTF(L_DEBUG,"comport packet size =%s\n\n",argv[argc]);
  51. sscanf(argv[argc],"%c %c %d",&chDummy,&chDummy,&dwPacketSize);
  52. THPRINTF(L_DEBUG,"com xfer packet size =%d\n\n",dwPacketSize);
  53. bPkt = TRUE;
  54. break;
  55. }
  56. case 'l' :
  57. case 'L' : {
  58. if (argv[argc][0] != '-') break;
  59. THPRINTF(L_DEBUG,"comport packet xfer loopcnt =%s\n\n",argv[argc]);
  60. sscanf(argv[argc],"%c %c %d",&chDummy,&chDummy,&dwLoop);
  61. THPRINTF(L_DEBUG,"com xfer loop cnt =%d\n\n",dwLoop);
  62. bLoop = TRUE;
  63. break;
  64. }
  65. default: {
  66. break;
  67. }
  68. }
  69. }
  70. if (!bFile || !bPkt || !bLoop)
  71. {
  72. THPRINTF(L_DEBUG,"\n\nOptions are required!!\n\n");
  73. THPRINTF(L_DEBUG,"comtst <required options>\n\n");
  74. THPRINTF(L_DEBUG,"options: -f<com port name>\n\n");
  75. THPRINTF(L_DEBUG,"options: -p<com xfer pkt size>\n\n");
  76. THPRINTF(L_DEBUG,"options: -l<xfer loop cnt>\n\n");
  77. return (1);
  78. }
  79. THPRINTF(L_DEBUG,"Doing comtst with com port name=[%s]\n",
  80. lpFilename);
  81. print("\n\n *** Doing COM TEST with [port=%s Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  82. SETTINGS1);
  83. bRc = DoComIo(SETTINGS1);
  84. if (!bRc) {
  85. print("\n\nCOM TEST FAILED********************************\n\n");
  86. }
  87. return 0;
  88. }
  89. BOOL DoComIo(LPSTR lpCom,DWORD Baud,BYTE Size,BYTE Parity,BYTE Stop)
  90. {
  91. COMMTIMEOUTS CommTimeOuts;
  92. CHAR WrBuffer[4096];
  93. CHAR RdBuffer[4096];
  94. DWORD i,l;
  95. HANDLE hCommPort;
  96. DCB dcb;
  97. BOOL bRc;
  98. BYTE Byte;
  99. DWORD dwNumWritten,dwNumRead,dwErrors;
  100. print("\n\n *** COMM TEST START [port=%s,Baud=%d,Size=%d,Parity=%d,Stop=%d]***\n\n",
  101. lpCom,Baud,Size,Parity,Stop);
  102. print("Opening the comm port for read write\n");
  103. hCommPort = CreateFile(
  104. lpCom,
  105. GENERIC_READ|GENERIC_WRITE,
  106. 0, // exclusive
  107. NULL, // sec attr
  108. OPEN_EXISTING,
  109. 0, // no attributes
  110. NULL); // no template
  111. if (hCommPort == (HANDLE)-1)
  112. {
  113. print("FAIL: OpenComm failed rc: %lx\n",hCommPort);
  114. return FALSE;
  115. }
  116. print("Opening the comm port for read write: SUCCESS hCommPort=%lx\n",hCommPort);
  117. print("Setting the line characteristics on comm \n");
  118. //printf("doing getcommstate for priming the dcb with defaults\n");
  119. bRc = GetCommState(hCommPort,&dcb);
  120. if (!bRc)
  121. {
  122. printf("FAIL: getcommstate failed\n");
  123. return FALSE;
  124. }
  125. dcb.DCBlength = sizeof(DCB);
  126. // dcb.DCBversion = 0x0002; BUG BUG in spec not in header
  127. dcb.BaudRate = Baud;
  128. dcb.ByteSize = Size;
  129. dcb.Parity = Parity;
  130. dcb.StopBits = Stop;
  131. //dcb.RlsTimeout = 10000; 10sec
  132. //dcb.CtsTimeout = 10000; 10sec
  133. //dcb.DsrTimeout = 10000; 10sec
  134. dcb.fBinary = 1; // binary data xmit
  135. dcb.fParity = 0; // dont bother about parity
  136. dcb.fOutxCtsFlow= 0; // no cts flow control
  137. dcb.fOutxDsrFlow= 0; // no dsr flow control
  138. dcb.fDtrControl = DTR_CONTROL_DISABLE; // dont bother about dtr
  139. dcb.fRtsControl = RTS_CONTROL_DISABLE; // dont bother about dtr
  140. dcb.fOutX =0; // disable xoff handling
  141. dcb.fInX =0; // disable xon handling
  142. dcb.fErrorChar = FALSE; // forget about parity char
  143. dcb.ErrorChar = '*';
  144. dcb.fNull = 0; // forget about the null striping
  145. dcb.XonChar = 0x0;
  146. dcb.XonLim = 0x0;
  147. dcb.XoffChar = 0xFF;
  148. dcb.XoffLim = 0xFF;
  149. dcb.EofChar = 0x00;
  150. dcb.EvtChar = 'x';
  151. dcb.wReserved = 0; // mbz
  152. dcb.fTXContinueOnXoff = FALSE; // old behaviour
  153. dcb.fAbortOnError = FALSE; // old behaviour
  154. //dcb.fChEvt = 0; forget about event char
  155. bRc = SetCommState(hCommPort,&dcb);
  156. if (!bRc)
  157. {
  158. print("FAIL: cannot set the comm state rc:%lx\n",bRc);
  159. bRc = CloseHandle(hCommPort);
  160. if (!bRc)
  161. {
  162. print("FAIL: cannot close the comm port:%lx\n",bRc);
  163. }
  164. return FALSE;
  165. }
  166. print("Setting the line characteristics on comm: SUCCESS\n");
  167. print("Setting the read/write timeouts to 5 minutes\n");
  168. CommTimeOuts.ReadIntervalTimeout = 0;
  169. CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
  170. CommTimeOuts.ReadTotalTimeoutConstant = 60*(1000)*5; // 5mins
  171. CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
  172. CommTimeOuts.WriteTotalTimeoutConstant = 60*(1000)*5; // 5mins
  173. bRc = SetCommTimeouts(hCommPort, &CommTimeOuts);
  174. printf("bRc from setcommtimeouts = %lx\n",bRc);
  175. if (!bRc)
  176. {
  177. printf("FAIL: setcommtimeouts\n");
  178. }
  179. print("Set the read/write timeouts to 5 minutes\n");
  180. print("Filling the buffer with the known chars \n");
  181. Byte = 0;
  182. for (i=0; i< dwPacketSize; i++)
  183. {
  184. //WrBuffer[i] = 'a';
  185. //WrBuffer[i] = (CHAR)i;
  186. WrBuffer[i] = Byte;
  187. Byte++;
  188. }
  189. print("Filling the buffer with the known chars : SUCCESS\n");
  190. #ifdef COM_DEB
  191. print("Dumping the buffer before sending it to comm\n");
  192. for (i=0; i< dwPacketSize; i++)
  193. {
  194. //print("%c",RdBuffer[i]);
  195. print(" %d ",WrBuffer[i]);
  196. }
  197. print("\nDumping the buffer before sending it to comm SUCCESS\n");
  198. #endif
  199. print("\nLooping %d times doing %d byte packet xfer across %s port\n",
  200. dwLoop,dwPacketSize,lpCom);
  201. for (l=0; l < dwLoop; l++)
  202. {
  203. print("Doing....[%d] iteration in the looped pkt xfer.....\n",l);
  204. print("Filling the Rdbuffer with the known chars (0xFF) to makeit dirty\n");
  205. for (i=0; i< dwPacketSize; i++)
  206. {
  207. RdBuffer[i] = '0xFF';
  208. }
  209. print("Filling the Rdbuffer with the known chars (0xFF): SUCCESS\n");
  210. print("Writting this buffer to the comm port\n");
  211. bRc = WriteFile( hCommPort,
  212. WrBuffer,
  213. dwPacketSize,
  214. &dwNumWritten,
  215. NULL);
  216. if (!bRc)
  217. {
  218. print("FAIL: cannot Write To the comm port:%lx\n",bRc);
  219. bRc = CloseHandle(hCommPort);
  220. if (!bRc)
  221. {
  222. print("FAIL: cannot close the comm port:%lx\n",bRc);
  223. }
  224. return FALSE;
  225. }
  226. print("Writting this buffer to the comm port: SUCCESS rc:%lx, byteswritten:%lx\n",
  227. bRc,dwNumWritten);
  228. if (dwNumWritten < dwPacketSize)
  229. {
  230. print("FAIL: less #bytes written, maybe due to time out, as RC is true\n");
  231. bRc = CloseHandle(hCommPort);
  232. if (!bRc)
  233. {
  234. print("FAIL: cannot close the comm port:%lx\n",bRc);
  235. }
  236. return FALSE;
  237. }
  238. print("Flushing this buffer out of comm port\n");
  239. bRc = FlushFileBuffers(hCommPort);
  240. print("flush file buffers (%lx) rc = %lx\n",hCommPort,bRc);
  241. //bRc = ClearCommError(hCommPort,&dwErrors,NULL);
  242. //print("ClearCommError: rc= %lx and dwErrors=%lx\n",bRc,dwErrors);
  243. Sleep(1000);
  244. print("Calling ReadFile...\n");
  245. bRc = ReadFile( hCommPort,
  246. RdBuffer,
  247. dwPacketSize,
  248. &dwNumRead,
  249. NULL);
  250. if (!bRc)
  251. {
  252. print("FAIL: cannot Read From the comm port:%lx\n",bRc);
  253. bRc = CloseHandle(hCommPort);
  254. if (!bRc)
  255. {
  256. print("FAIL: cannot close the comm port:%lx\n",bRc);
  257. }
  258. return FALSE;
  259. }
  260. print("Reading this buffer from the comm port: SUCCESS rc:%lx, bytesread:%lx\n",
  261. bRc,dwNumRead);
  262. if (dwNumRead < dwPacketSize)
  263. {
  264. print("FAIL: less #bytes read, maybe due to time out, as RC is true\n");
  265. bRc = CloseHandle(hCommPort);
  266. if (!bRc)
  267. {
  268. print("FAIL: cannot close the comm port:%lx\n",bRc);
  269. }
  270. return FALSE;
  271. }
  272. #ifdef COM_DEB
  273. print("Dumping the Rdbuffer with the comm data\n");
  274. for (i=0; i< dwPacketSize; i++)
  275. {
  276. //print("%c",RdBuffer[i]);
  277. print(" %d ",RdBuffer[i]);
  278. }
  279. print("\nDumping the Rdbuffer with the comm data: SUCCESS\n");
  280. #endif
  281. print("Comparing the rd and wr buffers\n");
  282. for (i=0; i< dwPacketSize; i++)
  283. {
  284. if (RdBuffer[i] != WrBuffer[i])
  285. {
  286. print("FAIL: BufferMisMatch: RdBuffer[%d]=%lx,WrBuffer[%d]=%lx\n",
  287. i,RdBuffer[i],i,WrBuffer[i]);
  288. bRc = CloseHandle(hCommPort);
  289. if (!bRc)
  290. {
  291. print("FAIL: cannot close the comm port:%lx\n",bRc);
  292. }
  293. return FALSE;
  294. }
  295. }
  296. print("Comparing the rd and wr buffers: SUCCESS\n");
  297. }
  298. bRc = FlushFileBuffers(hCommPort);
  299. print("flush file buffers (%lx,0) rc = %lx\n",hCommPort,bRc);
  300. bRc = ClearCommError(hCommPort,&dwErrors,NULL);
  301. print("ClearCommError: rc= %lx and dwErrors=%lx\n",bRc,dwErrors);
  302. bRc = PurgeComm(hCommPort,PURGE_TXCLEAR);
  303. print("PurgeComm txclear (%lx,0) rc = %lx\n",hCommPort,bRc);
  304. bRc = PurgeComm(hCommPort,PURGE_RXCLEAR);
  305. print("PurgeComm rxclear (%lx,0) rc = %lx\n",hCommPort,bRc);
  306. bRc = PurgeComm(hCommPort,PURGE_RXABORT);
  307. print("PurgeComm rxabort (%lx,0) rc = %lx\n",hCommPort,bRc);
  308. bRc = PurgeComm(hCommPort,PURGE_TXABORT);
  309. print("PurgeComm txabort (%lx,0) rc = %lx\n",hCommPort,bRc);
  310. print("Closing the comm port\n");
  311. bRc = CloseHandle(hCommPort);
  312. if (!bRc)
  313. {
  314. print("FAIL: cannot close the comm port:%lx\n",bRc);
  315. return FALSE;
  316. }
  317. print("\n\n*** COMM TEST OVER WITHOUT ERRORS *** \n\n");
  318. }