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.

256 lines
6.8 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. clock_t Start;
  17. clock_t Finish;
  18. if (argc > 1) {
  19. sscanf(argv[1],"%d",&NumberToRead);
  20. if (argc > 2) {
  21. sscanf(argv[2],"%d",&UseBaud);
  22. if (argc > 3) {
  23. MyPort = argv[3];
  24. }
  25. }
  26. }
  27. printf("Will try to read %d characters.\n",NumberToRead);
  28. printf("Will try to read a %d baud.\n",UseBaud);
  29. printf("Using port %s\n",MyPort);
  30. if ((hFile = CreateFile(
  31. MyPort,
  32. GENERIC_READ | GENERIC_WRITE,
  33. 0,
  34. NULL,
  35. CREATE_ALWAYS,
  36. FILE_ATTRIBUTE_NORMAL,
  37. NULL
  38. )) != ((HANDLE)-1)) {
  39. printf("We successfully opened the %s port.\n",MyPort);
  40. To.ReadIntervalTimeout = 0;
  41. To.ReadTotalTimeoutMultiplier = ((1000+(((UseBaud+9)/10)-1))/((UseBaud+9)/10));
  42. if (!To.ReadTotalTimeoutMultiplier) {
  43. To.ReadTotalTimeoutMultiplier = 1;
  44. }
  45. printf("Multiplier is: %d\n",To.ReadTotalTimeoutMultiplier);
  46. To.ReadTotalTimeoutConstant = 5000;
  47. To.WriteTotalTimeoutMultiplier = 0;
  48. To.WriteTotalTimeoutConstant = 5000;
  49. if (SetCommTimeouts(
  50. hFile,
  51. &To
  52. )) {
  53. //
  54. // We've successfully opened the file. Set the state of
  55. // the comm device. First we get the old values and
  56. // adjust to our own.
  57. //
  58. if (!GetCommState(
  59. hFile,
  60. &MyDcb
  61. )) {
  62. printf("Couldn't get the comm state: %d\n",GetLastError());
  63. exit(1);
  64. }
  65. MyDcb.BaudRate = UseBaud;
  66. MyDcb.ByteSize = 8;
  67. MyDcb.Parity = NOPARITY;
  68. MyDcb.StopBits = ONESTOPBIT;
  69. MyDcb.fOutxCtsFlow = TRUE;
  70. MyDcb.fOutxDsrFlow = TRUE;
  71. MyDcb.fDtrControl = DTR_CONTROL_ENABLE;
  72. MyDcb.fRtsControl = RTS_CONTROL_ENABLE;
  73. if (SetCommState(
  74. hFile,
  75. &MyDcb
  76. )) {
  77. printf("We successfully set the state of the %s port.\n",MyPort);
  78. Start = clock();
  79. if (ReadFile(
  80. hFile,
  81. readbuff,
  82. NumberToRead,
  83. &NumberActuallyRead,
  84. NULL
  85. )) {
  86. unsigned char j;
  87. DWORD TotalCount;
  88. Finish = clock();
  89. printf("Well we thought the read went ok.\n");
  90. printf("Number actually read %d.\n",NumberActuallyRead);
  91. printf("Now we check the data\n");
  92. // printf("Time to read %f\n",(((double)(Finish-Start))/CLOCKS_PER_SEC));
  93. // printf("Chars per second %f\n",((double)NumberActuallyRead)/(((double)(Finish-Start))/CLOCKS_PER_SEC));
  94. for (
  95. TotalCount = 0;
  96. TotalCount < NumberActuallyRead;
  97. ) {
  98. for (
  99. j = 0;
  100. j <= 9;
  101. j++
  102. ) {
  103. if (readbuff[TotalCount] != j) {
  104. printf("Bad data starting at: %d\n",TotalCount);
  105. goto donewithcheck;
  106. }
  107. TotalCount++;
  108. if (TotalCount >= NumberActuallyRead) {
  109. break;
  110. }
  111. }
  112. }
  113. donewithcheck:;
  114. } else {
  115. DWORD LastError;
  116. LastError = GetLastError();
  117. printf("Couldn't read the %s device.\n",MyPort);
  118. printf("Status of failed read is: %d\n",LastError);
  119. //
  120. // Get the error word from clear comm error.
  121. //
  122. if (!ClearCommError(
  123. hFile,
  124. &LastError,
  125. NULL
  126. )) {
  127. printf("Couldn't call clear comm error: %d\n",GetLastError());
  128. exit(1);
  129. } else {
  130. if (!LastError) {
  131. printf("No LastError\n");
  132. } else {
  133. if (LastError & CE_RXOVER) {
  134. printf("Error: CE_RXOVER\n");
  135. }
  136. if (LastError & CE_OVERRUN) {
  137. printf("Error: CE_OVERRUN\n");
  138. }
  139. if (LastError & CE_RXPARITY) {
  140. printf("Error: CE_RXPARITY\n");
  141. }
  142. if (LastError & CE_FRAME) {
  143. printf("Error: CE_FRAME\n");
  144. }
  145. if (LastError & CE_BREAK) {
  146. printf("Error: CE_BREAK\n");
  147. }
  148. if (LastError & ~(CE_RXOVER |
  149. CE_OVERRUN |
  150. CE_RXPARITY |
  151. CE_FRAME |
  152. CE_BREAK)) {
  153. printf("Unknown errors: %x\n",LastError);
  154. }
  155. }
  156. }
  157. }
  158. } else {
  159. DWORD LastError;
  160. LastError = GetLastError();
  161. printf("Couldn't set the %s device.\n",MyPort);
  162. printf("Status of failed set is: %x\n",LastError);
  163. }
  164. } else {
  165. DWORD LastError;
  166. LastError = GetLastError();
  167. printf("Couldn't set the %s device timeouts.\n",MyPort);
  168. printf("Status of failed timeouts is: %x\n",LastError);
  169. }
  170. CloseHandle(hFile);
  171. } else {
  172. DWORD LastError;
  173. LastError = GetLastError();
  174. printf("Couldn't open the %s device.\n",MyPort);
  175. printf("Status of failed open is: %x\n",LastError);
  176. }
  177. }