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.

183 lines
4.8 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "windows.h"
  6. void main(int argc,char *argv[]) {
  7. HANDLE hFile;
  8. DCB MyDcb;
  9. DCB GottenDcb;
  10. char *MyPort = "COM1";
  11. if (argc > 1) {
  12. MyPort = argv[1];
  13. }
  14. printf("Using port %s\n",MyPort);
  15. if ((hFile = CreateFile(
  16. MyPort,
  17. GENERIC_READ | GENERIC_WRITE,
  18. 0,
  19. NULL,
  20. CREATE_ALWAYS,
  21. FILE_ATTRIBUTE_NORMAL,
  22. NULL
  23. )) != ((HANDLE)-1)) {
  24. printf("We successfully opened the %s port.\n",MyPort);
  25. //
  26. // Get the state of the comm port and then
  27. // adjust the values to our liking.
  28. //
  29. if (!GetCommState(
  30. hFile,
  31. &MyDcb
  32. )) {
  33. printf("Couldn't get comm state: %d\n",GetLastError());
  34. exit(1);
  35. } else {
  36. //
  37. // We immediately to a setcomm state on what we got
  38. // back. This will test that GetCommState can *ONLY*
  39. // return good data.
  40. //
  41. printf("Sanity check on GetCommState()\n");
  42. if (!SetCommState(
  43. hFile,
  44. &MyDcb
  45. )) {
  46. printf("Bad GetCommState: %d\n",GetLastError());
  47. exit(1);
  48. }
  49. printf("GetCommState is ok\n");
  50. //
  51. // We've successfully opened the file. Set the state of
  52. // the comm device.
  53. //
  54. MyDcb.BaudRate = 19200;
  55. MyDcb.ByteSize = 8;
  56. MyDcb.Parity = NOPARITY;
  57. MyDcb.StopBits = ONESTOPBIT;
  58. MyDcb.XonChar = 20;
  59. MyDcb.XoffChar = 25;
  60. MyDcb.ErrorChar = 1;
  61. MyDcb.EofChar = 2;
  62. MyDcb.EvtChar = 3;
  63. if (SetCommState(
  64. hFile,
  65. &MyDcb
  66. )) {
  67. printf("We successfully set the state of the %s port.\n",MyPort);
  68. //
  69. // Now we get the comm state again and make sure
  70. // the adjusted values are as we like them.
  71. //
  72. if (!GetCommState(
  73. hFile,
  74. &GottenDcb
  75. )) {
  76. printf("Couldn't get the adjusted dcb: %d\n",GetLastError());
  77. exit(1);
  78. } else {
  79. printf("Got the new state - checking\n");
  80. if (MyDcb.BaudRate != GottenDcb.BaudRate) {
  81. printf("MyDcb.BaudRate != GottenDcb.BaudRate: %d %d\n",MyDcb.BaudRate,GottenDcb.BaudRate);
  82. }
  83. if (MyDcb.ByteSize != GottenDcb.ByteSize) {
  84. printf("MyDcb.ByteSize != GottenDcb.ByteSize: %d %d\n",MyDcb.ByteSize,GottenDcb.ByteSize);
  85. }
  86. if (MyDcb.Parity != GottenDcb.Parity) {
  87. printf("MyDcb.Parity != GottenDcb.Parity: %d %d\n",MyDcb.Parity,GottenDcb.Parity);
  88. }
  89. if (MyDcb.StopBits != GottenDcb.StopBits) {
  90. printf("MyDcb.StopBits != GottenDcb.StopBits: %d %d\n",MyDcb.StopBits,GottenDcb.StopBits);
  91. }
  92. if (MyDcb.XonChar != GottenDcb.XonChar) {
  93. printf("MyDcb.XonChar != GottenDcb.XonChar: %d %d\n",MyDcb.XonChar,GottenDcb.XonChar);
  94. }
  95. if (MyDcb.XoffChar != GottenDcb.XoffChar) {
  96. printf("MyDcb.XoffChar != GottenDcb.XoffChar: %d %d\n",MyDcb.XoffChar,GottenDcb.XoffChar);
  97. }
  98. if (MyDcb.ErrorChar != GottenDcb.ErrorChar) {
  99. printf("MyDcb.ErrorChar != GottenDcb.ErrorChar: %d %d\n",MyDcb.ErrorChar,GottenDcb.ErrorChar);
  100. }
  101. if (MyDcb.EofChar != GottenDcb.EofChar) {
  102. printf("MyDcb.EofChar != GottenDcb.EofChar: %d %d\n",MyDcb.EofChar,GottenDcb.EofChar);
  103. }
  104. if (MyDcb.EvtChar != GottenDcb.EvtChar) {
  105. printf("MyDcb.EvtChar != GottenDcb.EvtChar: %d %d\n",MyDcb.EvtChar,GottenDcb.EvtChar);
  106. }
  107. }
  108. } else {
  109. DWORD LastError;
  110. LastError = GetLastError();
  111. printf("Couldn't set the %s device.\n",MyPort);
  112. printf("Status of failed set is: %d\n",LastError);
  113. }
  114. }
  115. } else {
  116. DWORD LastError;
  117. LastError = GetLastError();
  118. printf("Couldn't open the %s device.\n",MyPort);
  119. printf("Status of failed open is: %d\n",LastError);
  120. }
  121. }