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.

173 lines
4.1 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "windows.h"
  6. #define BIGWRITE 256000
  7. unsigned char writebuff[BIGWRITE];
  8. int __cdecl main(int argc,char *argv[]) {
  9. HANDLE hFile;
  10. DCB MyDcb;
  11. char *MyPort = "COM1";
  12. DWORD NumberActuallyWritten;
  13. DWORD NumberToWrite = 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",&NumberToWrite);
  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 write %d characters.\n",NumberToWrite);
  28. printf("Will try to write at %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. //
  41. // We've successfully opened the file. Set the state of
  42. // the comm device. First we get the old values and
  43. // adjust to our own.
  44. //
  45. if (!GetCommState(
  46. hFile,
  47. &MyDcb
  48. )) {
  49. printf("Couldn't get the comm state: %d\n",GetLastError());
  50. exit(1);
  51. }
  52. MyDcb.BaudRate = UseBaud;
  53. MyDcb.ByteSize = 8;
  54. MyDcb.Parity = NOPARITY;
  55. MyDcb.StopBits = ONESTOPBIT;
  56. MyDcb.fOutxCtsFlow = TRUE;
  57. MyDcb.fOutxDsrFlow = TRUE;
  58. MyDcb.fDtrControl = DTR_CONTROL_ENABLE;
  59. MyDcb.fRtsControl = RTS_CONTROL_ENABLE;
  60. To.ReadIntervalTimeout = 0;
  61. To.ReadTotalTimeoutMultiplier = 0;
  62. To.ReadTotalTimeoutConstant = 0;
  63. To.WriteTotalTimeoutMultiplier = 0;
  64. To.WriteTotalTimeoutConstant = 0;
  65. SetCommTimeouts(
  66. hFile,
  67. &To
  68. );
  69. if (SetCommState(
  70. hFile,
  71. &MyDcb
  72. )) {
  73. unsigned char j;
  74. DWORD TotalCount;
  75. printf("We successfully set the state of the %s port.\n",MyPort);
  76. for (
  77. TotalCount = 0;
  78. TotalCount < NumberToWrite;
  79. ) {
  80. for (
  81. j = 0;
  82. j <= 9;
  83. j++
  84. ) {
  85. writebuff[TotalCount] = j;
  86. TotalCount++;
  87. if (TotalCount >= NumberToWrite) {
  88. break;
  89. }
  90. }
  91. }
  92. Start = clock();
  93. if (WriteFile(
  94. hFile,
  95. writebuff,
  96. NumberToWrite,
  97. &NumberActuallyWritten,
  98. NULL
  99. )) {
  100. Finish = clock();
  101. printf("Time to write %f\n",(((double)(Finish-Start))/CLOCKS_PER_SEC));
  102. printf("Chars per second %f\n",((double)NumberActuallyWritten)/(((double)(Finish-Start))/CLOCKS_PER_SEC));
  103. printf("Well we thought the write went ok.\n");
  104. printf("Number actually written %d.\n",NumberActuallyWritten);
  105. } else {
  106. DWORD LastError;
  107. LastError = GetLastError();
  108. printf("Couldn't write the %s device.\n",MyPort);
  109. printf("Status of failed write is: %x\n",LastError);
  110. }
  111. } else {
  112. DWORD LastError;
  113. LastError = GetLastError();
  114. printf("Couldn't set the %s device.\n",MyPort);
  115. printf("Status of failed set is: %x\n",LastError);
  116. }
  117. CloseHandle(hFile);
  118. } else {
  119. DWORD LastError;
  120. LastError = GetLastError();
  121. printf("Couldn't open the %s device.\n",MyPort);
  122. printf("Status of failed open is: %x\n",LastError);
  123. }
  124. }