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.

248 lines
5.7 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "windows.h"
  5. #define FAILURE printf("FAIL: %d\n",__LINE__);exit(1)
  6. VOID
  7. DumpCommState(
  8. IN DCB MyDcb
  9. ) {
  10. printf("Current comm state: \n");
  11. printf("Size of DCB: %d\n",MyDcb.DCBlength);
  12. printf("Baud rate: %d\n",MyDcb.BaudRate);
  13. printf("Binary mode: %s\n",MyDcb.fBinary?"TRUE":"FALSE");
  14. printf("Parity checking: %s\n",MyDcb.fParity?"TRUE":"FALSE");
  15. printf("Out CTS Flow: %s\n",MyDcb.fOutxCtsFlow?"TRUE":"FALSE");
  16. printf("Out DSR Flow: %s\n",MyDcb.fOutxDsrFlow?"TRUE":"FALSE");
  17. printf("DTR control: %s\n",MyDcb.fDtrControl==DTR_CONTROL_DISABLE?"DISABLE":
  18. MyDcb.fDtrControl==DTR_CONTROL_ENABLE?"ENABLE":
  19. MyDcb.fDtrControl==DTR_CONTROL_HANDSHAKE?"HANDSHAKE":"INVALID!");
  20. printf("Dsr sensitive: %s\n",MyDcb.fDsrSensitivity?"TRUE":"FALSE");
  21. printf("Tx Contin xoff: %s\n",MyDcb.fTXContinueOnXoff?"TRUE":"FALSE");
  22. printf("Output X on/off: %s\n",MyDcb.fOutX?"ON":"OFF");
  23. printf("Input X on/off: %s\n",MyDcb.fInX?"ON":"OFF");
  24. printf("Error replace: %s\n",MyDcb.fErrorChar?"ON":"OFF");
  25. printf("Null stripping: %s\n",MyDcb.fNull?"ON":"OFF");
  26. printf("RTS control: %s\n",MyDcb.fRtsControl==RTS_CONTROL_DISABLE?"DISABLE":
  27. MyDcb.fRtsControl==RTS_CONTROL_ENABLE?"ENABLE":
  28. MyDcb.fRtsControl==RTS_CONTROL_HANDSHAKE?"HANDSHAKE":"TOGGLE");
  29. printf("Abort on error: %s\n",MyDcb.fAbortOnError?"ON":"OFF");
  30. printf("Xon Limit: %d\n",MyDcb.XonLim);
  31. printf("Xoff Limit: %d\n",MyDcb.XoffLim);
  32. printf("Valid bits/byte: %d\n",MyDcb.ByteSize);
  33. printf("Parity: %s\n",MyDcb.Parity==EVENPARITY?"EVEN":
  34. MyDcb.Parity==ODDPARITY?"ODD":
  35. MyDcb.Parity==MARKPARITY?"MARK":
  36. MyDcb.Parity==NOPARITY?"NO":"INVALID");
  37. printf("Stop bits: %s\n",MyDcb.StopBits==ONESTOPBIT?"1":
  38. MyDcb.StopBits==TWOSTOPBITS?"2":
  39. MyDcb.StopBits==ONE5STOPBITS?"1.5":"INVALID");
  40. printf("Xoff char: %x\n",MyDcb.XoffChar);
  41. printf("Xon char: %x\n",MyDcb.XonChar);
  42. printf("Error char: %x\n",MyDcb.ErrorChar);
  43. printf("EOF char: %x\n",MyDcb.EofChar);
  44. printf("Evt char: %x\n",MyDcb.EvtChar);
  45. }
  46. int __cdecl main(int argc,char *argv[]) {
  47. HANDLE hFile;
  48. char *MyPort = "COM1";
  49. DCB MyDcb;
  50. char firstString[] = "1200,n,8,1";
  51. char secondString[] = "1200,n,8,1,x";
  52. char thirdString[] = "1200,n,8,1,p";
  53. if (argc > 1) {
  54. MyPort = argv[1];
  55. }
  56. if ((hFile = CreateFile(
  57. MyPort,
  58. GENERIC_READ | GENERIC_WRITE,
  59. 0,
  60. NULL,
  61. CREATE_ALWAYS,
  62. FILE_ATTRIBUTE_NORMAL,
  63. NULL
  64. )) != ((HANDLE)-1)) {
  65. if (!GetCommState(
  66. hFile,
  67. &MyDcb
  68. )) {
  69. FAILURE;
  70. }
  71. if (!BuildCommDCB(
  72. firstString,
  73. &MyDcb
  74. )) {
  75. FAILURE;
  76. }
  77. if (!SetCommState(
  78. hFile,
  79. &MyDcb
  80. )) {
  81. FAILURE;
  82. }
  83. if (!GetCommState(
  84. hFile,
  85. &MyDcb
  86. )) {
  87. FAILURE;
  88. }
  89. printf("mode string: %s\n",firstString);
  90. DumpCommState(MyDcb);
  91. if (!BuildCommDCB(
  92. secondString,
  93. &MyDcb
  94. )) {
  95. FAILURE;
  96. }
  97. if (!SetCommState(
  98. hFile,
  99. &MyDcb
  100. )) {
  101. FAILURE;
  102. }
  103. if (!GetCommState(
  104. hFile,
  105. &MyDcb
  106. )) {
  107. FAILURE;
  108. }
  109. printf("mode string: %s\n",secondString);
  110. DumpCommState(MyDcb);
  111. if (!BuildCommDCB(
  112. firstString,
  113. &MyDcb
  114. )) {
  115. FAILURE;
  116. }
  117. if (!SetCommState(
  118. hFile,
  119. &MyDcb
  120. )) {
  121. FAILURE;
  122. }
  123. if (!GetCommState(
  124. hFile,
  125. &MyDcb
  126. )) {
  127. FAILURE;
  128. }
  129. printf("mode string: %s\n",firstString);
  130. DumpCommState(MyDcb);
  131. if (!BuildCommDCB(
  132. thirdString,
  133. &MyDcb
  134. )) {
  135. FAILURE;
  136. }
  137. if (!SetCommState(
  138. hFile,
  139. &MyDcb
  140. )) {
  141. FAILURE;
  142. }
  143. if (!GetCommState(
  144. hFile,
  145. &MyDcb
  146. )) {
  147. FAILURE;
  148. }
  149. printf("mode string: %s\n",thirdString);
  150. DumpCommState(MyDcb);
  151. if (!BuildCommDCB(
  152. firstString,
  153. &MyDcb
  154. )) {
  155. FAILURE;
  156. }
  157. if (!SetCommState(
  158. hFile,
  159. &MyDcb
  160. )) {
  161. FAILURE;
  162. }
  163. if (!GetCommState(
  164. hFile,
  165. &MyDcb
  166. )) {
  167. FAILURE;
  168. }
  169. printf("mode string: %s\n",firstString);
  170. DumpCommState(MyDcb);
  171. CloseHandle(hFile);
  172. } else {
  173. DWORD LastError;
  174. LastError = GetLastError();
  175. printf("Couldn't open the %s device.\n",MyPort);
  176. printf("Status of failed open is: %x\n",LastError);
  177. }
  178. return 1;
  179. }