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.

121 lines
3.0 KiB

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <process.h>
  4. #include <string.h>
  5. #include <windows.h>
  6. #define PERR(bSuccess, api) {if (!(bSuccess)) printf("%s: Error %d from %s \
  7. on line %d\n", __FILE__, GetLastError(), api, __LINE__);}
  8. char cr = '\r';
  9. char lf = '\n';
  10. char ctrlC = 3;
  11. int addLf = 0;
  12. HANDLE hfSerialPort;
  13. void readPort()
  14. {
  15. DWORD cbBytesRead;
  16. char inBuf[256];
  17. int i;
  18. BOOL bSuccess;
  19. SYSTEMTIME sysTimeStart;
  20. SYSTEMTIME sysTimeEnd;
  21. while (1)
  22. {
  23. GetSystemTime(&sysTimeStart);
  24. bSuccess = ReadFile(hfSerialPort, inBuf, sizeof(inBuf), &cbBytesRead,
  25. NULL);
  26. GetSystemTime(&sysTimeEnd);
  27. printf("StartingTime: Yr: %d Mn: %d DayofWeek: %d Day: %d Hr: %d Min: %d Sec: %d Milli: %d\n",
  28. sysTimeStart.wYear,
  29. sysTimeStart.wMonth,
  30. sysTimeStart.wDayOfWeek,
  31. sysTimeStart.wDay,
  32. sysTimeStart.wHour,
  33. sysTimeStart.wMinute,
  34. sysTimeStart.wSecond,
  35. sysTimeStart.wMilliseconds
  36. );
  37. printf("EndingTime: Yr: %d Mn: %d DayofWeek: %d Day: %d Hr: %d Min: %d Sec: %d Milli: %d\n",
  38. sysTimeEnd.wYear,
  39. sysTimeEnd.wMonth,
  40. sysTimeEnd.wDayOfWeek,
  41. sysTimeEnd.wDay,
  42. sysTimeEnd.wHour,
  43. sysTimeEnd.wMinute,
  44. sysTimeEnd.wSecond,
  45. sysTimeEnd.wMilliseconds
  46. );
  47. PERR(bSuccess, "ReadFile");
  48. if (bSuccess && (cbBytesRead == 0))
  49. puts("total timeout");
  50. else
  51. if (cbBytesRead < sizeof(inBuf))
  52. printf("\ainterval timeout, ");
  53. else
  54. printf("buffer full, ");
  55. printf("bytes read: %d\n", cbBytesRead);
  56. for (i = 0; i < (int) cbBytesRead; i++)
  57. {
  58. switch(inBuf[i])
  59. {
  60. case '\r':
  61. if (addLf)
  62. putchar(lf);
  63. putchar(inBuf[i]);
  64. break;
  65. default:
  66. putchar(inBuf[i]);
  67. break;
  68. }
  69. }
  70. }
  71. return;
  72. }
  73. int __cdecl main(int argc,char *argv[])
  74. {
  75. char szTemp[512];
  76. BOOL bSuccess;
  77. COMMTIMEOUTS ctmo;
  78. DCB dcb;
  79. hfSerialPort = CreateFile("COM2", GENERIC_READ | GENERIC_WRITE, 0,
  80. NULL, OPEN_EXISTING, 0, NULL );
  81. PERR(hfSerialPort != INVALID_HANDLE_VALUE, "CreateFile");
  82. bSuccess = GetCommState(hfSerialPort, &dcb);
  83. PERR(bSuccess, "GetCommState");
  84. dcb.BaudRate = 9600;
  85. dcb.fBinary = 1;
  86. dcb.fParity = 0;
  87. dcb.fOutxCtsFlow = 0;
  88. dcb.fOutxDsrFlow = 0;
  89. dcb.fDtrControl = DTR_CONTROL_DISABLE;
  90. dcb.fDsrSensitivity = 0;
  91. dcb.fTXContinueOnXoff = 0;
  92. dcb.fOutX = 1;
  93. dcb.fInX = 1;
  94. dcb.fErrorChar = 0;
  95. dcb.fNull = 1;
  96. dcb.fRtsControl = RTS_CONTROL_DISABLE;
  97. dcb.fAbortOnError = 0;
  98. dcb.Parity = NOPARITY;
  99. dcb.StopBits = ONESTOPBIT;
  100. bSuccess = SetCommState(hfSerialPort, &dcb);
  101. PERR(bSuccess, "SetCommState");
  102. ctmo.ReadIntervalTimeout = 1000;
  103. ctmo.ReadTotalTimeoutMultiplier = 0;
  104. ctmo.ReadTotalTimeoutConstant = 30000;
  105. ctmo.WriteTotalTimeoutMultiplier = 0;
  106. ctmo.WriteTotalTimeoutConstant = 0;
  107. bSuccess = SetCommTimeouts(hfSerialPort, &ctmo);
  108. PERR(bSuccess, "SetCommTimeouts");
  109. readPort();
  110. return(0);
  111. }