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.

347 lines
9.0 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "windows.h"
  5. #define BIGREAD 256000
  6. unsigned char readbuff[BIGREAD];
  7. unsigned char writebuff[BIGREAD];
  8. unsigned char DataMask[9] = {0,0,0,0,0x0f,0x1f,0x3f,0x7f,0xff};
  9. void main(int argc,char *argv[]) {
  10. HANDLE hFile;
  11. DCB MyDcb;
  12. char *MyPort = "COM1";
  13. DWORD NumberActuallyRead;
  14. DWORD NumberToRead = 0;
  15. DWORD NumberActuallyWritten;
  16. DWORD NumberToWrite = 0;
  17. DWORD UseBaud = 19200;
  18. DWORD NumberOfDataBits = 8;
  19. COMMTIMEOUTS To;
  20. OVERLAPPED ReadOl = {0};
  21. OVERLAPPED WriteOl = {0};
  22. unsigned char j;
  23. DWORD TotalCount;
  24. BOOL ReadDone;
  25. BOOL WriteDone;
  26. if (argc > 1) {
  27. sscanf(argv[1],"%d",&NumberToRead);
  28. NumberToWrite = NumberToRead;
  29. if (argc > 2) {
  30. sscanf(argv[2],"%d",&UseBaud);
  31. if (argc > 3) {
  32. MyPort = argv[3];
  33. if (argc > 4) {
  34. sscanf(argv[4],"%d",&NumberOfDataBits);
  35. }
  36. }
  37. }
  38. }
  39. printf("Will try to read/write %d characters.\n",NumberToRead);
  40. printf("Will try to read/write at %d baud.\n",UseBaud);
  41. printf("Using port %s\n",MyPort);
  42. for (
  43. TotalCount = 0;
  44. TotalCount < NumberToWrite;
  45. ) {
  46. for (
  47. j = (0xff - 10);
  48. j != 0; // When it wraps around
  49. j++
  50. ) {
  51. writebuff[TotalCount] = j;
  52. TotalCount++;
  53. if (TotalCount >= NumberToWrite) {
  54. break;
  55. }
  56. }
  57. }
  58. if (!(ReadOl.hEvent = CreateEvent(
  59. NULL,
  60. FALSE,
  61. FALSE,
  62. NULL
  63. ))) {
  64. printf("Could not create the read event.\n");
  65. goto cleanup;
  66. } else {
  67. ReadOl.Internal = 0;
  68. ReadOl.InternalHigh = 0;
  69. ReadOl.Offset = 0;
  70. ReadOl.OffsetHigh = 0;
  71. }
  72. if (!(WriteOl.hEvent = CreateEvent(
  73. NULL,
  74. FALSE,
  75. FALSE,
  76. NULL
  77. ))) {
  78. printf("Could not create the write event.\n");
  79. goto cleanup;
  80. } else {
  81. WriteOl.Internal = 0;
  82. WriteOl.InternalHigh = 0;
  83. WriteOl.Offset = 0;
  84. WriteOl.OffsetHigh = 0;
  85. }
  86. if ((hFile = CreateFile(
  87. MyPort,
  88. GENERIC_READ | GENERIC_WRITE,
  89. 0,
  90. NULL,
  91. CREATE_ALWAYS,
  92. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  93. NULL
  94. )) != ((HANDLE)-1)) {
  95. printf("We successfully opened the %s port.\n",MyPort);
  96. To.ReadIntervalTimeout = 0;
  97. To.ReadTotalTimeoutMultiplier = ((1000+(((UseBaud+9)/10)-1))/((UseBaud+9)/10));
  98. if (!To.ReadTotalTimeoutMultiplier) {
  99. To.ReadTotalTimeoutMultiplier = 1;
  100. }
  101. To.WriteTotalTimeoutMultiplier = ((1000+(((UseBaud+9)/10)-1))/((UseBaud+9)/10));
  102. if (!To.WriteTotalTimeoutMultiplier) {
  103. To.WriteTotalTimeoutMultiplier = 1;
  104. }
  105. printf("Multiplier is: %d\n",To.ReadTotalTimeoutMultiplier);
  106. To.ReadTotalTimeoutConstant = 5000;
  107. To.WriteTotalTimeoutConstant = 5000;
  108. if (SetCommTimeouts(
  109. hFile,
  110. &To
  111. )) {
  112. //
  113. // We've successfully opened the file. Set the state of
  114. // the comm device. First we get the old values and
  115. // adjust to our own.
  116. //
  117. if (!GetCommState(
  118. hFile,
  119. &MyDcb
  120. )) {
  121. printf("Couldn't get the comm state: %d\n",GetLastError());
  122. exit(1);
  123. }
  124. MyDcb.BaudRate = UseBaud;
  125. MyDcb.ByteSize = (BYTE)NumberOfDataBits;
  126. MyDcb.Parity = NOPARITY;
  127. MyDcb.StopBits = ONESTOPBIT;
  128. if (SetCommState(
  129. hFile,
  130. &MyDcb
  131. )) {
  132. printf("We successfully set the state of the %s port.\n",MyPort);
  133. //
  134. // Sleep for 5 seconds so both sides can "synchronize".
  135. //
  136. Sleep(5000);
  137. ReadDone = ReadFile(
  138. hFile,
  139. readbuff,
  140. NumberToRead,
  141. &NumberActuallyRead,
  142. &ReadOl
  143. );
  144. if (!ReadDone) {
  145. DWORD LastError;
  146. LastError = GetLastError();
  147. if (LastError != ERROR_IO_PENDING) {
  148. printf("Couldn't read the %s device.\n",MyPort);
  149. printf("Status of failed read is: %x\n",LastError);
  150. goto cleanup;
  151. }
  152. }
  153. WriteDone = WriteFile(
  154. hFile,
  155. writebuff,
  156. NumberToWrite,
  157. &NumberActuallyWritten,
  158. &WriteOl
  159. );
  160. if (!WriteDone) {
  161. DWORD LastError;
  162. LastError = GetLastError();
  163. if (LastError != ERROR_IO_PENDING) {
  164. printf("Couldn't write the %s device.\n",MyPort);
  165. printf("Status of failed write is: %x\n",LastError);
  166. goto cleanup;
  167. }
  168. }
  169. //
  170. // We'll wait for the read to complete.
  171. //
  172. if (!GetOverlappedResult(
  173. hFile,
  174. &ReadOl,
  175. &NumberActuallyRead,
  176. TRUE
  177. )) {
  178. DWORD LastError;
  179. LastError = GetLastError();
  180. printf("Couldn't read the %s device.\n",MyPort);
  181. printf("Status of failed read is: %x\n",LastError);
  182. } else {
  183. printf("Well we thought the read went ok.\n");
  184. printf("Number actually read %d.\n",NumberActuallyRead);
  185. printf("Now we check the data\n");
  186. for (
  187. TotalCount = 0;
  188. TotalCount < NumberActuallyRead;
  189. ) {
  190. for (
  191. j = (0xff - 10);
  192. j != 0; // When it wraps around.
  193. j++
  194. ) {
  195. if (readbuff[TotalCount] != (j & DataMask[NumberOfDataBits])) {
  196. printf("Bad data starting at: %d\n",TotalCount);
  197. printf("readbuff[TotalCount]: %x\n",readbuff[TotalCount]);
  198. printf("(j & DataMask[NumberOfDataBits]): %x\n",(j & DataMask[NumberOfDataBits]));
  199. goto donewithcheck;
  200. }
  201. TotalCount++;
  202. if (TotalCount >= NumberActuallyRead) {
  203. break;
  204. }
  205. }
  206. }
  207. }
  208. donewithcheck: ;
  209. //
  210. // We'll wait for the write to complete.
  211. //
  212. if (!GetOverlappedResult(
  213. hFile,
  214. &WriteOl,
  215. &NumberActuallyWritten,
  216. TRUE
  217. )) {
  218. DWORD LastError;
  219. LastError = GetLastError();
  220. printf("Couldn't write the %s device.\n",MyPort);
  221. printf("Status of failed write is: %x\n",LastError);
  222. goto cleanup;
  223. } else {
  224. printf("Well we thought the write went ok.\n");
  225. printf("Number actually written %d.\n",NumberActuallyWritten);
  226. }
  227. } else {
  228. DWORD LastError;
  229. LastError = GetLastError();
  230. printf("Couldn't set the %s device.\n",MyPort);
  231. printf("Status of failed set is: %x\n",LastError);
  232. }
  233. } else {
  234. DWORD LastError;
  235. LastError = GetLastError();
  236. printf("Couldn't set the %s device timeouts.\n",MyPort);
  237. printf("Status of failed timeouts is: %x\n",LastError);
  238. }
  239. CloseHandle(hFile);
  240. } else {
  241. DWORD LastError;
  242. LastError = GetLastError();
  243. printf("Couldn't open the %s device.\n",MyPort);
  244. printf("Status of failed open is: %x\n",LastError);
  245. }
  246. cleanup:;
  247. }