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.

220 lines
4.3 KiB

  1. //
  2. // This test the line status and modem status insertion.
  3. //
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "nt.h"
  8. #include "ntrtl.h"
  9. #include "nturtl.h"
  10. #include "ntddser.h"
  11. #include "windows.h"
  12. #define SIZEOFBUF 1000000
  13. UCHAR readBuff[SIZEOFBUF];
  14. int __cdecl main(int argc,char *argv[]) {
  15. HANDLE hFile;
  16. DCB myDcb;
  17. char *myPort = "COM1";
  18. unsigned char escapeChar = 0xff;
  19. DWORD numberToRead;
  20. DWORD numberActuallyRead;
  21. DWORD useBaud = 1200;
  22. COMMTIMEOUTS to = {0};
  23. int j;
  24. IO_STATUS_BLOCK Iosb1;
  25. NTSTATUS Status1;
  26. if (argc > 1) {
  27. sscanf(argv[1],"%d",&numberToRead);
  28. if (argc > 2) {
  29. sscanf(argv[2],"%d",&useBaud);
  30. if (argc > 3) {
  31. myPort = argv[3];
  32. }
  33. }
  34. }
  35. if ((hFile = CreateFile(
  36. myPort,
  37. GENERIC_READ | GENERIC_WRITE,
  38. 0,
  39. NULL,
  40. CREATE_ALWAYS,
  41. FILE_ATTRIBUTE_NORMAL,
  42. NULL
  43. )) == ((HANDLE)-1)) {
  44. printf("Couldn't open the comm port\n");
  45. exit(1);
  46. }
  47. to.ReadIntervalTimeout = 1000;
  48. to.ReadTotalTimeoutConstant = 20000;
  49. if (!SetCommTimeouts(
  50. hFile,
  51. &to
  52. )) {
  53. printf("Couldn't set the timeouts\n");
  54. exit(1);
  55. }
  56. //
  57. // We've successfully opened the file. Set the state of
  58. // the comm device. First we get the old values and
  59. // adjust to our own.
  60. //
  61. if (!GetCommState(
  62. hFile,
  63. &myDcb
  64. )) {
  65. printf("Couldn't get the comm state: %d\n",GetLastError());
  66. exit(1);
  67. }
  68. myDcb.BaudRate = useBaud;
  69. myDcb.ByteSize = 8;
  70. myDcb.Parity = NOPARITY;
  71. myDcb.StopBits = ONESTOPBIT;
  72. //
  73. // Make sure that no flow control is turned on.
  74. //
  75. myDcb.fOutxDsrFlow = FALSE;
  76. myDcb.fOutxCtsFlow = FALSE;
  77. myDcb.fDsrSensitivity = FALSE;
  78. myDcb.fOutX = FALSE;
  79. myDcb.fInX = FALSE;
  80. myDcb.fDtrControl = DTR_CONTROL_DISABLE;
  81. myDcb.fRtsControl = RTS_CONTROL_DISABLE;
  82. if (!SetCommState(
  83. hFile,
  84. &myDcb
  85. )) {
  86. printf("Couldn't set the comm state.\n");
  87. exit(1);
  88. }
  89. //
  90. // Start up the insert ioctl.
  91. //
  92. Status1 = NtDeviceIoControlFile(
  93. hFile,
  94. NULL,
  95. NULL,
  96. NULL,
  97. &Iosb1,
  98. IOCTL_SERIAL_LSRMST_INSERT,
  99. &escapeChar,
  100. sizeof(unsigned char),
  101. NULL,
  102. 0
  103. );
  104. if (!NT_SUCCESS(Status1)) {
  105. printf("1: Non pending status: %x\n",Status1);
  106. exit(1);
  107. }
  108. if (!ReadFile(
  109. hFile,
  110. &readBuff[0],
  111. 64000,
  112. &numberActuallyRead,
  113. NULL
  114. )) {
  115. printf("bad status on read: %d\n",GetLastError());
  116. exit(1);
  117. }
  118. printf("We actually read %d characters\n",numberActuallyRead);
  119. //
  120. // We got the characters back. Dump each character to the stdout.
  121. //
  122. for (
  123. j = 0;
  124. j < numberActuallyRead;
  125. ) {
  126. if (readBuff[j] != escapeChar) {
  127. printf("Normal Char: %x\n",readBuff[j]);
  128. j++;
  129. } else {
  130. if (j+1 == numberActuallyRead) {
  131. printf("Terminated with the escape char!!!\n");
  132. exit(1);
  133. }
  134. //
  135. // We have the escape char. interpret it.
  136. //
  137. if (readBuff[j+1] == SERIAL_LSRMST_ESCAPE) {
  138. printf("Escaped escape char: %x\n",escapeChar);
  139. j += 2;
  140. } else if (readBuff[j+1] == SERIAL_LSRMST_LSR_DATA) {
  141. printf("LSR reg: %x AND data: %x\n",readBuff[j+2],readBuff[j+3]);
  142. j += 4;
  143. } else if (readBuff[j+1] == SERIAL_LSRMST_LSR_NODATA) {
  144. printf("LSR reg: %x NO DATA\n",readBuff[j+2]);
  145. j += 3;
  146. } else if (readBuff[j+1] == SERIAL_LSRMST_MST) {
  147. printf("MST reg: %x\n",readBuff[j+2]);
  148. j += 3;
  149. } else {
  150. printf("Unknown escape code: %d\n",readBuff[j+1]);
  151. j++;
  152. }
  153. }
  154. }
  155. }