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.

182 lines
4.3 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 MyTimeouts = {0};
  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. // Make sure that there is no timeouts left around.
  42. //
  43. if (!SetCommTimeouts(
  44. hFile,
  45. &MyTimeouts
  46. )) {
  47. printf("Coundn't set the timeouts\n");
  48. exit(1);
  49. }
  50. //
  51. // We've successfully opened the file. Set the state of
  52. // the comm device. First we get the old values and
  53. // adjust to our own.
  54. //
  55. if (!GetCommState(
  56. hFile,
  57. &MyDcb
  58. )) {
  59. printf("Couldn't get the comm state: %d\n",GetLastError());
  60. exit(1);
  61. }
  62. MyDcb.BaudRate = UseBaud;
  63. MyDcb.ByteSize = 8;
  64. MyDcb.Parity = NOPARITY;
  65. MyDcb.StopBits = ONESTOPBIT;
  66. //
  67. // Make sure that the only flow control is output cts.
  68. //
  69. MyDcb.fOutxDsrFlow = FALSE;
  70. MyDcb.fOutxCtsFlow = TRUE;
  71. MyDcb.fDsrSensitivity = FALSE;
  72. MyDcb.fOutX = FALSE;
  73. MyDcb.fInX = FALSE;
  74. MyDcb.fDtrControl = DTR_CONTROL_DISABLE;
  75. MyDcb.fRtsControl = RTS_CONTROL_DISABLE;
  76. if (SetCommState(
  77. hFile,
  78. &MyDcb
  79. )) {
  80. unsigned char j;
  81. DWORD TotalCount;
  82. printf("We successfully set the state of the %s port.\n",MyPort);
  83. for (
  84. TotalCount = 0;
  85. TotalCount < NumberToWrite;
  86. ) {
  87. for (
  88. j = 0;
  89. j <= 9;
  90. j++
  91. ) {
  92. writebuff[TotalCount] = j;
  93. TotalCount++;
  94. if (TotalCount >= NumberToWrite) {
  95. break;
  96. }
  97. }
  98. }
  99. Start = clock();
  100. if (WriteFile(
  101. hFile,
  102. writebuff,
  103. NumberToWrite,
  104. &NumberActuallyWritten,
  105. NULL
  106. )) {
  107. Finish = clock();
  108. printf("Time to write %f\n",(((double)(Finish-Start))/CLOCKS_PER_SEC));
  109. printf("Chars per second %f\n",((double)NumberActuallyWritten)/(((double)(Finish-Start))/CLOCKS_PER_SEC));
  110. printf("Well we thought the write went ok.\n");
  111. printf("Number actually written %d.\n",NumberActuallyWritten);
  112. } else {
  113. DWORD LastError;
  114. LastError = GetLastError();
  115. printf("Couldn't write the %s device.\n",MyPort);
  116. printf("Status of failed write is: %x\n",LastError);
  117. }
  118. } else {
  119. DWORD LastError;
  120. LastError = GetLastError();
  121. printf("Couldn't set the %s device.\n",MyPort);
  122. printf("Status of failed set is: %x\n",LastError);
  123. }
  124. CloseHandle(hFile);
  125. } else {
  126. DWORD LastError;
  127. LastError = GetLastError();
  128. printf("Couldn't open the %s device.\n",MyPort);
  129. printf("Status of failed open is: %x\n",LastError);
  130. }
  131. }