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.

164 lines
3.6 KiB

  1. //
  2. // Write to the comm one character at a time.
  3. //
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "windows.h"
  8. #define BIGWRITE 256000
  9. unsigned char writebuff[BIGWRITE];
  10. void main(int argc,char *argv[]) {
  11. HANDLE hFile;
  12. DCB MyDcb;
  13. char *MyPort = "COM1";
  14. DWORD NumberActuallyWritten;
  15. DWORD NumberToWrite = 0;
  16. DWORD UseBaud = 19200;
  17. if (argc > 1) {
  18. sscanf(argv[1],"%d",&NumberToWrite);
  19. if (argc > 2) {
  20. sscanf(argv[2],"%d",&UseBaud);
  21. if (argc > 3) {
  22. MyPort = argv[3];
  23. }
  24. }
  25. }
  26. printf("Will try to write %d characters.\n",NumberToWrite);
  27. printf("Will try to write at %d baud.\n",UseBaud);
  28. printf("Using port: %s\n",MyPort);
  29. if ((hFile = CreateFile(
  30. MyPort,
  31. GENERIC_READ | GENERIC_WRITE,
  32. 0,
  33. NULL,
  34. CREATE_ALWAYS,
  35. FILE_ATTRIBUTE_NORMAL,
  36. NULL
  37. )) != ((HANDLE)-1)) {
  38. printf("We successfully opened the %s port.\n",MyPort);
  39. //
  40. // We've successfully opened the file. Set the state of
  41. // the comm device. First we get the old values and
  42. // adjust to our own.
  43. //
  44. if (!GetCommState(
  45. hFile,
  46. &MyDcb
  47. )) {
  48. printf("Couldn't get the comm state: %d\n",GetLastError());
  49. exit(1);
  50. }
  51. MyDcb.BaudRate = UseBaud;
  52. MyDcb.ByteSize = 8;
  53. MyDcb.Parity = NOPARITY;
  54. MyDcb.StopBits = ONESTOPBIT;
  55. if (SetCommState(
  56. hFile,
  57. &MyDcb
  58. )) {
  59. unsigned char j;
  60. DWORD TotalCount;
  61. printf("We successfully set the state of the %s port.\n",MyPort);
  62. for (
  63. TotalCount = 0;
  64. TotalCount < NumberToWrite;
  65. ) {
  66. for (
  67. j = 0;
  68. j <= 9;
  69. j++
  70. ) {
  71. writebuff[TotalCount] = j;
  72. TotalCount++;
  73. if (TotalCount >= NumberToWrite) {
  74. break;
  75. }
  76. }
  77. }
  78. for (
  79. TotalCount = 0;
  80. TotalCount < NumberToWrite;
  81. ) {
  82. if (WriteFile(
  83. hFile,
  84. &writebuff[TotalCount],
  85. 1,
  86. &NumberActuallyWritten,
  87. NULL
  88. )) {
  89. if (NumberActuallyWritten != 1) {
  90. printf("Write iteration %d only wrote %d\n",NumberActuallyWritten);
  91. break;
  92. } else {
  93. TotalCount++;
  94. }
  95. } else {
  96. DWORD LastError;
  97. LastError = GetLastError();
  98. printf("Status of %d write is: %x\n",TotalCount,LastError);
  99. }
  100. }
  101. } else {
  102. DWORD LastError;
  103. LastError = GetLastError();
  104. printf("Couldn't set the %s device.\n",MyPort);
  105. printf("Status of failed set is: %x\n",LastError);
  106. }
  107. CloseHandle(hFile);
  108. } else {
  109. DWORD LastError;
  110. LastError = GetLastError();
  111. printf("Couldn't open the %s device.\n",MyPort);
  112. printf("Status of failed open is: %x\n",LastError);
  113. }
  114. }