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.

206 lines
5.2 KiB

  1. //
  2. // Read from the comm 1 char at a time.
  3. //
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "windows.h"
  8. #define BIGREAD 256000
  9. unsigned char readbuff[BIGREAD];
  10. void main(int argc,char *argv[]) {
  11. HANDLE hFile;
  12. DCB MyDcb;
  13. char *MyPort = "COM1";
  14. DWORD NumberActuallyRead;
  15. DWORD NumberToRead = 0;
  16. DWORD UseBaud = 19200;
  17. COMMTIMEOUTS To;
  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. if (SetCommState(
  70. hFile,
  71. &MyDcb
  72. )) {
  73. DWORD CurrentReadIt = 0;
  74. DWORD CurrentActualRead = 0;
  75. unsigned char j;
  76. DWORD TotalCount;
  77. NumberActuallyRead = 0;
  78. printf("We successfully set the state of the %s port.\n",MyPort);
  79. printf("Hit any 1 <cr> to start reading: ");
  80. scanf("%d",&CurrentReadIt);
  81. for (
  82. CurrentReadIt = 0;
  83. CurrentReadIt < NumberToRead;
  84. CurrentReadIt++
  85. ) {
  86. if (ReadFile(
  87. hFile,
  88. &readbuff[CurrentReadIt],
  89. 1,
  90. &CurrentActualRead,
  91. NULL
  92. )) {
  93. if (CurrentActualRead != 1) {
  94. printf("Iteration %d read %d\n",CurrentReadIt,CurrentActualRead);
  95. break;
  96. } else {
  97. NumberActuallyRead++;
  98. }
  99. } else {
  100. DWORD LastError;
  101. LastError = GetLastError();
  102. printf("Status of failed %d read is: %x\n",CurrentReadIt,LastError);
  103. break;
  104. }
  105. }
  106. printf("Number actually read %d.\n",NumberActuallyRead);
  107. printf("Now we check the data\n");
  108. for (
  109. TotalCount = 0;
  110. TotalCount < NumberActuallyRead;
  111. ) {
  112. for (
  113. j = 0;
  114. j <= 9;
  115. j++
  116. ) {
  117. if (readbuff[TotalCount] != j) {
  118. printf("Bad data starting at: %d\n",TotalCount);
  119. goto donewithcheck;
  120. }
  121. TotalCount++;
  122. if (TotalCount >= NumberActuallyRead) {
  123. break;
  124. }
  125. }
  126. }
  127. donewithcheck:;
  128. } else {
  129. DWORD LastError;
  130. LastError = GetLastError();
  131. printf("Couldn't set the %s device.\n",MyPort);
  132. printf("Status of failed set is: %x\n",LastError);
  133. }
  134. } else {
  135. DWORD LastError;
  136. LastError = GetLastError();
  137. printf("Couldn't set the %s device timeouts.\n",MyPort);
  138. printf("Status of failed timeouts is: %x\n",LastError);
  139. }
  140. CloseHandle(hFile);
  141. } else {
  142. DWORD LastError;
  143. LastError = GetLastError();
  144. printf("Couldn't open the %s device.\n",MyPort);
  145. printf("Status of failed open is: %x\n",LastError);
  146. }
  147. }