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.

297 lines
6.3 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "windows.h"
  6. #define BIGREAD 256000
  7. unsigned char readbuff[BIGREAD];
  8. int __cdecl main(int argc,char *argv[]) {
  9. HANDLE hFile;
  10. DCB MyDcb;
  11. char *MyPort = "COM1";
  12. DWORD NumberActuallyRead;
  13. DWORD NumberToRead = 0;
  14. DWORD UseBaud = 19200;
  15. COMMTIMEOUTS To;
  16. COMMPROP MyCommProp;
  17. DWORD numberReadSoFar;
  18. DWORD TotalCount;
  19. DWORD j;
  20. if (argc > 1) {
  21. sscanf(argv[1],"%d",&NumberToRead);
  22. if (argc > 2) {
  23. sscanf(argv[2],"%d",&UseBaud);
  24. if (argc > 3) {
  25. MyPort = argv[3];
  26. }
  27. }
  28. }
  29. printf("Will try to read %d characters.\n",NumberToRead);
  30. printf("Will try to read a %d baud.\n",UseBaud);
  31. printf("Using port %s\n",MyPort);
  32. if ((hFile = CreateFile(
  33. MyPort,
  34. GENERIC_READ | GENERIC_WRITE,
  35. 0,
  36. NULL,
  37. CREATE_ALWAYS,
  38. FILE_ATTRIBUTE_NORMAL,
  39. NULL
  40. )) == ((HANDLE)-1)) {
  41. printf("Couldn't open the port - last error is: %d\n",GetLastError());
  42. exit(1);
  43. }
  44. printf("We successfully opened the %s port.\n",MyPort);
  45. To.ReadIntervalTimeout = 0;
  46. To.ReadTotalTimeoutMultiplier = ((1000+(((UseBaud+9)/10)-1))/((UseBaud+9)/10));
  47. if (!To.ReadTotalTimeoutMultiplier) {
  48. To.ReadTotalTimeoutMultiplier = 1;
  49. }
  50. printf("Multiplier is: %d\n",To.ReadTotalTimeoutMultiplier);
  51. To.ReadTotalTimeoutConstant = 5000;
  52. To.WriteTotalTimeoutMultiplier = 0;
  53. To.WriteTotalTimeoutConstant = 5000;
  54. if (!SetCommTimeouts(
  55. hFile,
  56. &To
  57. )) {
  58. printf("Couldn't set the timeouts - last error: %d\n",GetLastError());
  59. exit(1);
  60. }
  61. //
  62. // We've successfully opened the file. Set the state of
  63. // the comm device. First we get the old values and
  64. // adjust to our own.
  65. //
  66. if (!GetCommState(
  67. hFile,
  68. &MyDcb
  69. )) {
  70. printf("Couldn't get the comm state: %d\n",GetLastError());
  71. exit(1);
  72. }
  73. if (!GetCommProperties(
  74. hFile,
  75. &MyCommProp
  76. )) {
  77. printf("Couldn't get the comm prop: %d\n",GetLastError());
  78. exit(1);
  79. }
  80. //
  81. // Set the Xoff/xon limit so that it lowers the handshake
  82. // whenever we have more than 50 chars in our buffer,
  83. // and raises when we drop below 20.
  84. //
  85. MyDcb.XoffLim = MyCommProp.dwCurrentRxQueue - 50;
  86. MyDcb.XonLim = 20;
  87. MyDcb.BaudRate = UseBaud;
  88. MyDcb.ByteSize = 8;
  89. MyDcb.Parity = NOPARITY;
  90. MyDcb.StopBits = ONESTOPBIT;
  91. //
  92. // Make sure that the only flow control is input RTS.
  93. //
  94. MyDcb.fOutxDsrFlow = FALSE;
  95. MyDcb.fOutxCtsFlow = FALSE;
  96. MyDcb.fDsrSensitivity = FALSE;
  97. MyDcb.fOutX = FALSE;
  98. MyDcb.fInX = FALSE;
  99. MyDcb.fDtrControl = DTR_CONTROL_DISABLE;
  100. MyDcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
  101. if (!SetCommState(
  102. hFile,
  103. &MyDcb
  104. )) {
  105. printf("Couldn't set the comm state - last error: %d\n",GetLastError());
  106. exit(1);
  107. }
  108. //
  109. // Read ten chars at a time until we get all the
  110. // chars or we get some kind of error.
  111. // We delay 100ms after each read to let the buffer
  112. // have a chance to fill up some.
  113. //
  114. for (
  115. numberReadSoFar = 0;
  116. numberReadSoFar < NumberToRead;
  117. ) {
  118. if (ReadFile(
  119. hFile,
  120. &readbuff[numberReadSoFar],
  121. 10,
  122. &NumberActuallyRead,
  123. NULL
  124. )) {
  125. //
  126. // If there was no characters (timed out), then
  127. // we probably won't see anything.
  128. //
  129. if (!NumberActuallyRead) {
  130. printf("No chars - Timeout!\n");
  131. break;
  132. }
  133. numberReadSoFar += NumberActuallyRead;
  134. Sleep(100);
  135. continue;
  136. } else {
  137. //
  138. // Some kind of read error.
  139. //
  140. DWORD LastError;
  141. LastError = GetLastError();
  142. printf("Couldn't read the %s device.\n",MyPort);
  143. printf("Status of failed read is: %d\n",LastError);
  144. //
  145. // Get the error word from clear comm error.
  146. //
  147. if (!ClearCommError(
  148. hFile,
  149. &LastError,
  150. NULL
  151. )) {
  152. printf("Couldn't call clear comm error: %d\n",GetLastError());
  153. exit(1);
  154. } else {
  155. if (!LastError) {
  156. printf("No LastError\n");
  157. } else {
  158. if (LastError & CE_RXOVER) {
  159. printf("Error: CE_RXOVER\n");
  160. }
  161. if (LastError & CE_OVERRUN) {
  162. printf("Error: CE_OVERRUN\n");
  163. }
  164. if (LastError & CE_RXPARITY) {
  165. printf("Error: CE_RXPARITY\n");
  166. }
  167. if (LastError & CE_FRAME) {
  168. printf("Error: CE_FRAME\n");
  169. }
  170. if (LastError & CE_BREAK) {
  171. printf("Error: CE_BREAK\n");
  172. }
  173. if (LastError & ~(CE_RXOVER |
  174. CE_OVERRUN |
  175. CE_RXPARITY |
  176. CE_FRAME |
  177. CE_BREAK)) {
  178. printf("Unknown errors: %x\n",LastError);
  179. }
  180. }
  181. }
  182. exit(1);
  183. }
  184. }
  185. for (
  186. TotalCount = 0;
  187. TotalCount < numberReadSoFar;
  188. ) {
  189. for (
  190. j = 0;
  191. j <= 9;
  192. j++
  193. ) {
  194. if (readbuff[TotalCount] != j) {
  195. printf("Bad data starting at: %d\n",TotalCount);
  196. goto donewithcheck;
  197. }
  198. TotalCount++;
  199. if (TotalCount >= numberReadSoFar) {
  200. break;
  201. }
  202. }
  203. }
  204. donewithcheck:;
  205. exit(1);
  206. }