Leaked source code of windows server 2003
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.

198 lines
5.4 KiB

  1. //
  2. // This is special in that it attempts to test the interval timeout.
  3. //
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include "windows.h"
  9. #define BIGREAD 256000
  10. unsigned char readbuff[BIGREAD];
  11. void main(int argc,char *argv[]) {
  12. HANDLE hFile;
  13. DCB MyDcb;
  14. char *MyPort = "COM1";
  15. DWORD NumberActuallyRead;
  16. DWORD NumberToRead = 0;
  17. DWORD UseBaud = 19200;
  18. COMMTIMEOUTS To;
  19. clock_t Start;
  20. clock_t Finish;
  21. if (argc > 1) {
  22. sscanf(argv[1],"%d",&NumberToRead);
  23. if (argc > 2) {
  24. sscanf(argv[2],"%d",&UseBaud);
  25. if (argc > 3) {
  26. MyPort = argv[3];
  27. }
  28. }
  29. }
  30. if (((NumberToRead / 2)*2) != NumberToRead) {
  31. printf("Number to read must be even!\n");
  32. exit(1);
  33. }
  34. printf("Will try to read %d characters.\n",NumberToRead);
  35. printf("Will try to read a %d baud.\n",UseBaud);
  36. printf("Using port %s\n",MyPort);
  37. if ((hFile = CreateFile(
  38. MyPort,
  39. GENERIC_READ | GENERIC_WRITE,
  40. 0,
  41. NULL,
  42. CREATE_ALWAYS,
  43. FILE_ATTRIBUTE_NORMAL,
  44. NULL
  45. )) != ((HANDLE)-1)) {
  46. printf("We successfully opened the %s port.\n",MyPort);
  47. To.ReadIntervalTimeout = 3000; // Three seconds between chars max.
  48. To.ReadTotalTimeoutMultiplier = ((1000+(((UseBaud+9)/10)-1))/((UseBaud+9)/10));
  49. if (!To.ReadTotalTimeoutMultiplier) {
  50. To.ReadTotalTimeoutMultiplier = 1;
  51. }
  52. printf("Multiplier is: %d\n",To.ReadTotalTimeoutMultiplier);
  53. To.ReadTotalTimeoutConstant = NumberToRead * 10000; // Allow ten seconds for each character.
  54. To.WriteTotalTimeoutMultiplier = 0;
  55. To.WriteTotalTimeoutConstant = 5000;
  56. if (SetCommTimeouts(
  57. hFile,
  58. &To
  59. )) {
  60. //
  61. // We've successfully opened the file. Set the state of
  62. // the comm device. First we get the old values and
  63. // adjust to our own.
  64. //
  65. if (!GetCommState(
  66. hFile,
  67. &MyDcb
  68. )) {
  69. printf("Couldn't get the comm state: %d\n",GetLastError());
  70. exit(1);
  71. }
  72. MyDcb.BaudRate = UseBaud;
  73. MyDcb.ByteSize = 8;
  74. MyDcb.Parity = NOPARITY;
  75. MyDcb.StopBits = ONESTOPBIT;
  76. if (SetCommState(
  77. hFile,
  78. &MyDcb
  79. )) {
  80. printf("We successfully set the state of the %s port.\n",MyPort);
  81. Start = clock();
  82. if (ReadFile(
  83. hFile,
  84. readbuff,
  85. NumberToRead,
  86. &NumberActuallyRead,
  87. NULL
  88. )) {
  89. unsigned char j;
  90. DWORD TotalCount;
  91. Finish = clock();
  92. printf("Well we thought the read went ok.\n");
  93. printf("Number actually read %d.\n",NumberActuallyRead);
  94. printf("Now we check the data\n");
  95. // printf("Time to read %f\n",(((double)(Finish-Start))/CLOCKS_PER_SEC));
  96. // printf("Chars per second %f\n",((double)NumberActuallyRead)/(((double)(Finish-Start))/CLOCKS_PER_SEC));
  97. for (
  98. TotalCount = 0;
  99. TotalCount < NumberActuallyRead;
  100. ) {
  101. for (
  102. j = 0;
  103. j <= 9;
  104. j++
  105. ) {
  106. if (readbuff[TotalCount] != j) {
  107. printf("Bad data starting at: %d\n",TotalCount);
  108. goto donewithcheck;
  109. }
  110. TotalCount++;
  111. if (TotalCount >= NumberActuallyRead) {
  112. break;
  113. }
  114. }
  115. }
  116. donewithcheck:;
  117. } else {
  118. DWORD LastError;
  119. LastError = GetLastError();
  120. printf("Couldn't read the %s device.\n",MyPort);
  121. printf("Status of failed read is: %x\n",LastError);
  122. }
  123. } else {
  124. DWORD LastError;
  125. LastError = GetLastError();
  126. printf("Couldn't set the %s device.\n",MyPort);
  127. printf("Status of failed set is: %x\n",LastError);
  128. }
  129. } else {
  130. DWORD LastError;
  131. LastError = GetLastError();
  132. printf("Couldn't set the %s device timeouts.\n",MyPort);
  133. printf("Status of failed timeouts is: %x\n",LastError);
  134. }
  135. CloseHandle(hFile);
  136. } else {
  137. DWORD LastError;
  138. LastError = GetLastError();
  139. printf("Couldn't open the %s device.\n",MyPort);
  140. printf("Status of failed open is: %x\n",LastError);
  141. }
  142. }