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.

304 lines
7.3 KiB

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