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.

398 lines
7.5 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 "windows.h"
  8. #define SIZEOFBUF 1000000
  9. UCHAR writeBuff[SIZEOFBUF];
  10. int __cdecl main(int argc,char *argv[]) {
  11. HANDLE hFile;
  12. DCB myDcb;
  13. char *myPort = "COM1";
  14. unsigned char escapeChar = 0xff;
  15. DWORD numberToWrite;
  16. DWORD numberActuallyWrote;
  17. DWORD useBaud = 1200;
  18. COMMTIMEOUTS to = {0};
  19. int totalCount;
  20. int j;
  21. if (argc > 1) {
  22. sscanf(argv[1],"%d",&numberToWrite);
  23. if (argc > 2) {
  24. sscanf(argv[2],"%d",&useBaud);
  25. if (argc > 3) {
  26. myPort = argv[3];
  27. }
  28. }
  29. }
  30. //
  31. // Fill up the write buff with a known data stream.
  32. //
  33. for (
  34. totalCount = 0;
  35. totalCount < numberToWrite;
  36. ) {
  37. for (
  38. j = 0;
  39. j <= 9;
  40. j++
  41. ) {
  42. writeBuff[totalCount] = j;
  43. totalCount++;
  44. if (totalCount >= numberToWrite) {
  45. break;
  46. }
  47. }
  48. }
  49. if ((hFile = CreateFile(
  50. myPort,
  51. GENERIC_READ | GENERIC_WRITE,
  52. 0,
  53. NULL,
  54. CREATE_ALWAYS,
  55. FILE_ATTRIBUTE_NORMAL,
  56. NULL
  57. )) == ((HANDLE)-1)) {
  58. printf("Couldn't open the comm port\n");
  59. exit(1);
  60. }
  61. if (!SetCommTimeouts(
  62. hFile,
  63. &to
  64. )) {
  65. printf("Couldn't set the timeouts\n");
  66. exit(1);
  67. }
  68. //
  69. // We've successfully opened the file. Set the state of
  70. // the comm device. First we get the old values and
  71. // adjust to our own.
  72. //
  73. if (!GetCommState(
  74. hFile,
  75. &myDcb
  76. )) {
  77. printf("Couldn't get the comm state: %d\n",GetLastError());
  78. exit(1);
  79. }
  80. myDcb.BaudRate = useBaud;
  81. myDcb.ByteSize = 8;
  82. myDcb.Parity = NOPARITY;
  83. myDcb.StopBits = ONESTOPBIT;
  84. //
  85. // Make sure that no flow control is turned on.
  86. //
  87. myDcb.fOutxDsrFlow = FALSE;
  88. myDcb.fOutxCtsFlow = FALSE;
  89. myDcb.fDsrSensitivity = FALSE;
  90. myDcb.fOutX = FALSE;
  91. myDcb.fInX = FALSE;
  92. myDcb.fDtrControl = DTR_CONTROL_DISABLE;
  93. myDcb.fRtsControl = RTS_CONTROL_DISABLE;
  94. if (!SetCommState(
  95. hFile,
  96. &myDcb
  97. )) {
  98. printf("Couldn't set the comm state.\n");
  99. exit(1);
  100. }
  101. //
  102. // Write the number of chars asked to write.
  103. // But for each char we do:
  104. //
  105. // 1) One plain write of the char.
  106. // 2) Cut the baud in half and write the same char.
  107. // 3) Reset the baud.
  108. // 4) Set Break.
  109. // 5) Clear break.
  110. // 6) Enable the DTR Line.
  111. // 7) Enable the RTS Line.
  112. // 8) Clear the RTS Line.
  113. // 9) Clear the DTR Line.
  114. // 10) Set the parity to even.
  115. // 11) Send the char.
  116. // 12) Set the parity to none.
  117. // 13) Send the original char.
  118. //
  119. for (
  120. j = 0;
  121. j < numberToWrite;
  122. j++
  123. ) {
  124. if (!WriteFile(
  125. hFile,
  126. &writeBuff[j],
  127. 1,
  128. &numberActuallyWrote,
  129. NULL
  130. )) {
  131. printf("bad status on write: %d\n",GetLastError());
  132. exit(1);
  133. }
  134. if (numberActuallyWrote != 1) {
  135. printf("Couldn't write plain character number: %d\n",j);
  136. exit(1);
  137. }
  138. myDcb.BaudRate = useBaud / 2;
  139. if (!SetCommState(
  140. hFile,
  141. &myDcb
  142. )) {
  143. printf("Couldn't half the baud rate during write.\n");
  144. exit(1);
  145. }
  146. if (!WriteFile(
  147. hFile,
  148. &writeBuff[j],
  149. 1,
  150. &numberActuallyWrote,
  151. NULL
  152. )) {
  153. printf("bad status on half baud write: %d\n",GetLastError());
  154. exit(1);
  155. }
  156. if (numberActuallyWrote != 1) {
  157. printf("Couldn't write half baud character number: %d\n",j);
  158. exit(1);
  159. }
  160. //
  161. // Give the serial time to do work.
  162. //
  163. Sleep(20);
  164. //
  165. // Reset the baud.
  166. //
  167. myDcb.BaudRate = useBaud;
  168. if (!SetCommState(
  169. hFile,
  170. &myDcb
  171. )) {
  172. printf("Couldn't reset the baud during write.\n");
  173. exit(1);
  174. }
  175. if (!EscapeCommFunction(
  176. hFile,
  177. SETBREAK
  178. )) {
  179. printf("Couldn't set the break during write.\n");
  180. exit(1);
  181. }
  182. Sleep(20);
  183. if (!EscapeCommFunction(
  184. hFile,
  185. CLRBREAK
  186. )) {
  187. printf("Couldn't clr the break during write.\n");
  188. exit(1);
  189. }
  190. Sleep(20);
  191. if (!EscapeCommFunction(
  192. hFile,
  193. SETDTR
  194. )) {
  195. printf("Couldn't set the dtr during write.\n");
  196. exit(1);
  197. }
  198. Sleep(100);
  199. if (!EscapeCommFunction(
  200. hFile,
  201. SETRTS
  202. )) {
  203. printf("Couldn't set the rts during write.\n");
  204. exit(1);
  205. }
  206. Sleep(20);
  207. if (!EscapeCommFunction(
  208. hFile,
  209. CLRRTS
  210. )) {
  211. printf("Couldn't clr the rts during write.\n");
  212. exit(1);
  213. }
  214. Sleep(20);
  215. if (!EscapeCommFunction(
  216. hFile,
  217. CLRDTR
  218. )) {
  219. printf("Couldn't clr the dtr during write.\n");
  220. exit(1);
  221. }
  222. Sleep(20);
  223. myDcb.Parity = EVENPARITY;
  224. if (!SetCommState(
  225. hFile,
  226. &myDcb
  227. )) {
  228. printf("Couldn't adjust the parity during write.\n");
  229. exit(1);
  230. }
  231. if (!WriteFile(
  232. hFile,
  233. &writeBuff[j],
  234. 1,
  235. &numberActuallyWrote,
  236. NULL
  237. )) {
  238. printf("bad status on even parity write: %d\n",GetLastError());
  239. exit(1);
  240. }
  241. Sleep(20);
  242. //
  243. // Reset the parity.
  244. //
  245. myDcb.Parity = NOPARITY;
  246. if (!SetCommState(
  247. hFile,
  248. &myDcb
  249. )) {
  250. printf("Couldn't reset the parity during write.\n");
  251. exit(1);
  252. }
  253. if (!WriteFile(
  254. hFile,
  255. &escapeChar,
  256. 1,
  257. &numberActuallyWrote,
  258. NULL
  259. )) {
  260. printf("bad status escape char write: %d\n",GetLastError());
  261. exit(1);
  262. }
  263. if (numberActuallyWrote != 1) {
  264. printf("Couldn't write escape char number: %d\n",j);
  265. exit(1);
  266. }
  267. if (!WriteFile(
  268. hFile,
  269. &writeBuff[j],
  270. 1,
  271. &numberActuallyWrote,
  272. NULL
  273. )) {
  274. printf("bad status on terminating write: %d\n",GetLastError());
  275. exit(1);
  276. }
  277. if (numberActuallyWrote != 1) {
  278. printf("Couldn't write terminating plain character number: %d\n",j);
  279. exit(1);
  280. }
  281. Sleep(20);
  282. }
  283. }