Click here to Skip to main content
15,893,161 members
Articles / Programming Languages / C++
Article

W2K BUG: timeout of serial driver uses absolute time

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
28 Sep 2000 60.2K   11   4
The serial driver uses an absolute date/time instead of a relative time for its timeout, causing problems in situations like daylight savings.

Introduction

During testing we uncovered a bug in the standard serial driver of Windows 2000.

If you do a ReadFile() with a certain timeout value and you set the date/time backwards while this timeout has not yet occurred then you can wait a until the specified date/time is reached again before the call returns.

This means that the serial driver uses an absolute date/time instead of a relative time for its timeout. This is a serious problem for everyone doing serial communication especially in daylight saving areas.

This problem was found in Windows 2000 / SP1 with the standard serial port driver.

Just run the following code and change the date/time backwards to see the problem:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   COMMTIMEOUTS timeouts;
   HANDLE handle;
   DCB dcb;
   char buffer[1000] = "abcdefghijklmnopqrstuvwxyz";
   DWORD dwLen;

   handle = CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE,
               0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

   if (handle == INVALID_HANDLE_VALUE)
   {
      printf("CreateFile() Failed\n");
      return 1;
   }

   timeouts.ReadIntervalTimeout        = 300;
   timeouts.ReadTotalTimeoutMultiplier = 0;
   timeouts.ReadTotalTimeoutConstant   = 0;
   timeouts.WriteTotalTimeoutMultiplier= 0;
   timeouts.WriteTotalTimeoutConstant  = 0;
   if (!SetCommTimeouts(handle, &timeouts))
   {
      printf("SetCommTimeouts() Failed\n");
      return 2;
   }

   ZeroMemory(&dcb, sizeof(dcb));

   dcb.DCBlength           = sizeof(dcb);
   dcb.BaudRate            = 19200; 
   dcb.fParity             = TRUE;
   dcb.fOutxCtsFlow        = false;
   dcb.fOutxDsrFlow        = false;
   dcb.fDtrControl         = DTR_CONTROL_ENABLE;
   dcb.fDsrSensitivity     = FALSE;
   dcb.fTXContinueOnXoff   = FALSE;
   dcb.fOutX               = false;
   dcb.fInX                = false;
   dcb.fNull               = FALSE;
   dcb.fRtsControl         = RTS_CONTROL_ENABLE;
   dcb.fAbortOnError       = false;
   dcb.ByteSize            = 8;
   dcb.Parity              = ODDPARITY;
   dcb.StopBits            = ONESTOPBIT;

   if (!SetCommState(handle, &dcb))
   {
      printf("SetCommState() Failed\n");
      return 1;
   }

   while (! _kbhit() )
   {
      printf("W");

      if (!WriteFile(handle, buffer, 40, &dwLen, 0))
         printf("\rWriteFile() returned %d\n", GetLastError());

      printf("R");

      if ( !ReadFile(handle, buffer, 100, &dwLen, 0) )
         printf("\rReadFile() returned %d\n", GetLastError());
   }

   printf("\nDone!\n");

   CloseHandle(handle);

   return 0;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHelp-me ? Pin
Alexandte1-Nov-02 6:37
sussAlexandte1-Nov-02 6:37 
Generali have the problem on one machine ! Pin
24-Apr-01 2:13
suss24-Apr-01 2:13 
GeneralNo way !! Pin
28-Mar-01 2:02
suss28-Mar-01 2:02 
I am glad to read that some of you made this program work.
Actually I didn't. I got an error in the CreateFile call.
I have Win2K SP1.
I have Visualstudio SP4.
Could you please help me ??
It's urgent !!Frown | :(

Riccardo Modesti
GeneralOK under W2K/No SP Pin
S W2-Oct-00 21:34
S W2-Oct-00 21:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.