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.

168 lines
4.2 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. void main(int argc,char *argv[]) {
  9. HANDLE hFile;
  10. DCB MyDcb;
  11. char *MyPort = "COM1";
  12. DWORD NumberToRead = 0;
  13. DWORD UseBaud = 19200;
  14. COMMTIMEOUTS To;
  15. if (argc > 1) {
  16. sscanf(argv[1],"%d",&NumberToRead);
  17. if (argc > 2) {
  18. sscanf(argv[2],"%d",&UseBaud);
  19. if (argc > 3) {
  20. MyPort = argv[3];
  21. }
  22. }
  23. }
  24. printf("Will try to read %d characters.\n",NumberToRead);
  25. printf("Will try to read a %d baud.\n",UseBaud);
  26. printf("Using port %s\n",MyPort);
  27. if ((hFile = CreateFile(
  28. MyPort,
  29. GENERIC_READ | GENERIC_WRITE,
  30. 0,
  31. NULL,
  32. CREATE_ALWAYS,
  33. FILE_ATTRIBUTE_NORMAL,
  34. NULL
  35. )) != ((HANDLE)-1)) {
  36. printf("We successfully opened the %s port.\n",MyPort);
  37. To.ReadIntervalTimeout = 0;
  38. To.ReadTotalTimeoutMultiplier = ((1000+(((UseBaud+9)/10)-1))/((UseBaud+9)/10));
  39. if (!To.ReadTotalTimeoutMultiplier) {
  40. To.ReadTotalTimeoutMultiplier = 1;
  41. }
  42. printf("Multiplier is: %d\n",To.ReadTotalTimeoutMultiplier);
  43. To.ReadTotalTimeoutConstant = 5000;
  44. To.WriteTotalTimeoutMultiplier = 0;
  45. To.WriteTotalTimeoutConstant = 5000;
  46. if (SetCommTimeouts(
  47. hFile,
  48. &To
  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 = 19200;
  63. MyDcb.ByteSize = 8;
  64. MyDcb.Parity = NOPARITY;
  65. MyDcb.StopBits = ONESTOPBIT;
  66. if (SetCommState(
  67. hFile,
  68. &MyDcb
  69. )) {
  70. printf("We successfully set the state of the %s port.\n",MyPort);
  71. if (PurgeComm(
  72. hFile,
  73. PURGE_TXABORT
  74. )) {
  75. printf("We succesfully purged\n");
  76. } else {
  77. DWORD LastError;
  78. LastError = GetLastError();
  79. printf("Couldn't purge the %s device.\n",MyPort);
  80. printf("Status of failed purge is: %x\n",LastError);
  81. }
  82. if (!PurgeComm(
  83. hFile,
  84. 0
  85. )) {
  86. printf("Purge correctly detects bad parameter: %d\n",GetLastError());
  87. } else {
  88. printf("Purge should have rejected!!!!!\n");
  89. }
  90. if (FlushFileBuffers(hFile)) {
  91. printf("We succesfully flushed\n");
  92. } else {
  93. DWORD LastError;
  94. LastError = GetLastError();
  95. printf("Couldn't flush the %s device.\n",MyPort);
  96. printf("Status of failed flush is: %x\n",LastError);
  97. }
  98. } else {
  99. DWORD LastError;
  100. LastError = GetLastError();
  101. printf("Couldn't set the %s device.\n",MyPort);
  102. printf("Status of failed set is: %x\n",LastError);
  103. }
  104. } else {
  105. DWORD LastError;
  106. LastError = GetLastError();
  107. printf("Couldn't set the %s device timeouts.\n",MyPort);
  108. printf("Status of failed timeouts is: %x\n",LastError);
  109. }
  110. CloseHandle(hFile);
  111. } else {
  112. DWORD LastError;
  113. LastError = GetLastError();
  114. printf("Couldn't open the %s device.\n",MyPort);
  115. printf("Status of failed open is: %x\n",LastError);
  116. }
  117. }