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.

348 lines
6.8 KiB

  1. #include <stddef.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "windows.h"
  6. HANDLE WaitEvent;
  7. HANDLE ReadEvent;
  8. HANDLE hFile;
  9. HANDLE IoSemaphore;
  10. DWORD
  11. WaitCommThread(
  12. LPVOID Trash
  13. )
  14. {
  15. DWORD ReasonSatisfied;
  16. DWORD Trash2;
  17. OVERLAPPED Ol;
  18. UNREFERENCED_PARAMETER(Trash);
  19. Ol.hEvent = WaitEvent;
  20. do {
  21. if (!WaitCommEvent(
  22. hFile,
  23. &ReasonSatisfied,
  24. &Ol
  25. )) {
  26. DWORD LastError = GetLastError();
  27. if (LastError == ERROR_IO_PENDING) {
  28. if (!GetOverlappedResult(
  29. hFile,
  30. &Ol,
  31. &Trash2,
  32. TRUE
  33. )) {
  34. WaitForSingleObject(IoSemaphore,-1);
  35. printf("Could not do the getoverlapped on the wait: %d\n",GetLastError());
  36. ReleaseSemaphore(IoSemaphore,1,NULL);
  37. }
  38. } else {
  39. WaitForSingleObject(IoSemaphore,-1);
  40. printf("Could not start the wait: %d\n",LastError);
  41. ReleaseSemaphore(IoSemaphore,1,NULL);
  42. }
  43. }
  44. } while (TRUE);
  45. return 1;
  46. }
  47. DWORD
  48. SetMaskThread(
  49. LPVOID Trash
  50. )
  51. {
  52. UNREFERENCED_PARAMETER(Trash);
  53. do {
  54. //
  55. // Now clear.
  56. //
  57. Sleep(1000);
  58. if (!SetCommMask(
  59. hFile,
  60. 0
  61. )) {
  62. DWORD LastError = GetLastError();
  63. WaitForSingleObject(IoSemaphore,-1);
  64. printf("Could not initiate the 0 set mask: %d\n",LastError);
  65. ReleaseSemaphore(IoSemaphore,1,NULL);
  66. }
  67. //
  68. // Now set.
  69. //
  70. Sleep(1000);
  71. if (!SetCommMask(
  72. hFile,
  73. EV_RXCHAR
  74. )) {
  75. DWORD LastError = GetLastError();
  76. WaitForSingleObject(IoSemaphore,-1);
  77. printf("Could not initiate the EV_RXCHAR set mask: %d\n",LastError);
  78. ReleaseSemaphore(IoSemaphore,1,NULL);
  79. }
  80. } while (TRUE);
  81. return 1;
  82. }
  83. DWORD
  84. ReadThread(
  85. LPVOID Trash
  86. )
  87. {
  88. DWORD Trash2;
  89. OVERLAPPED Ol;
  90. char readbuff[100];
  91. DWORD NumberActuallyRead;
  92. DWORD NumberToRead = 100;
  93. UNREFERENCED_PARAMETER(Trash);
  94. Ol.hEvent = ReadEvent;
  95. do {
  96. Sleep(200);
  97. if (!ReadFile(
  98. hFile,
  99. readbuff,
  100. NumberToRead,
  101. &NumberActuallyRead,
  102. &Ol
  103. )) {
  104. DWORD LastError = GetLastError();
  105. if (LastError == ERROR_IO_PENDING) {
  106. if (!GetOverlappedResult(
  107. hFile,
  108. &Ol,
  109. &Trash2,
  110. TRUE
  111. )) {
  112. WaitForSingleObject(IoSemaphore,-1);
  113. printf("Could not do the getoverlapped on the read: %d\n",GetLastError());
  114. ReleaseSemaphore(IoSemaphore,1,NULL);
  115. }
  116. } else {
  117. WaitForSingleObject(IoSemaphore,-1);
  118. printf("Could not start the wait: %d\n",LastError);
  119. ReleaseSemaphore(IoSemaphore,1,NULL);
  120. }
  121. }
  122. } while (TRUE);
  123. return 1;
  124. }
  125. int __cdecl main(int argc,char *argv[]) {
  126. char *MyPort = "COM1";
  127. DWORD ValueFromEscape = 0;
  128. DWORD CharFunc;
  129. DWORD ThreadId;
  130. DWORD ReadThreadId;
  131. HANDLE ReadHandle;
  132. int scanfval;
  133. if (argc > 1) {
  134. MyPort = argv[1];
  135. }
  136. WaitEvent = CreateEvent(
  137. NULL,
  138. TRUE,
  139. FALSE,
  140. NULL
  141. );
  142. if (!WaitEvent) {
  143. printf("Wait Event could not be created\n");
  144. exit(1);
  145. }
  146. ReadEvent = CreateEvent(
  147. NULL,
  148. TRUE,
  149. FALSE,
  150. NULL
  151. );
  152. if (!ReadEvent) {
  153. printf("Read Event could not be created\n");
  154. exit(1);
  155. }
  156. IoSemaphore = CreateSemaphore(
  157. NULL,
  158. 1,
  159. 1,
  160. NULL
  161. );
  162. if (!IoSemaphore) {
  163. printf("IoSemaphore could not be created\n");
  164. exit(1);
  165. }
  166. if ((hFile = CreateFile(
  167. MyPort,
  168. GENERIC_READ | GENERIC_WRITE,
  169. 0,
  170. NULL,
  171. CREATE_ALWAYS,
  172. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  173. NULL
  174. )) != ((HANDLE)-1)) {
  175. DCB MyDcb;
  176. COMMTIMEOUTS NewTimeouts;
  177. printf("We successfully opened the %s port.\n",MyPort);
  178. //
  179. // We've successfully opened the file. Set the state of
  180. // the comm device. First we get the old values and
  181. // adjust to our own.
  182. //
  183. if (!GetCommState(
  184. hFile,
  185. &MyDcb
  186. )) {
  187. printf("Couldn't get the comm state: %d\n",GetLastError());
  188. exit(1);
  189. }
  190. MyDcb.BaudRate = 19200;
  191. MyDcb.ByteSize = 8;
  192. MyDcb.Parity = NOPARITY;
  193. MyDcb.StopBits = ONESTOPBIT;
  194. MyDcb.EvtChar = 'a';
  195. if (!SetCommState(
  196. hFile,
  197. &MyDcb
  198. )) {
  199. printf("Couldn't set the comm state: %d\n",GetLastError());
  200. exit(1);
  201. }
  202. if (!SetCommTimeouts(
  203. hFile,
  204. &NewTimeouts
  205. )) {
  206. printf("Couldn't set the comm timeouts: %d\n",GetLastError());
  207. exit(1);
  208. }
  209. } else {
  210. printf("Could not open the comm port: %d\n",GetLastError());
  211. }
  212. //
  213. // Create the thread that will wait for the
  214. // comm events.
  215. //
  216. if (!CreateThread(
  217. NULL,
  218. 0,
  219. WaitCommThread,
  220. NULL,
  221. 0,
  222. &ThreadId
  223. )) {
  224. printf("Could not create the wait thread.\n");
  225. exit(1);
  226. }
  227. if (!CreateThread(
  228. NULL,
  229. 0,
  230. SetMaskThread,
  231. NULL,
  232. 0,
  233. &ThreadId
  234. )) {
  235. printf("Could not create the set mask thread.\n");
  236. exit(1);
  237. }
  238. if ((ReadHandle = CreateThread(
  239. NULL,
  240. 0,
  241. ReadThread,
  242. NULL,
  243. 0,
  244. &ThreadId
  245. )) == 0) {
  246. printf("Could not create the read thread.\n");
  247. exit(1);
  248. }
  249. WaitForSingleObject(ReadHandle,-1);
  250. }