Mawg
Mawg

Reputation: 40215

Seeking serial port read example

I want to read from COM1 at 115200,n,8,1 (preferably a blocking call, but I can add that. And I don't need threading).

The only code that I can find is on Stack Overflow in this question (Microsoft also had some useful info).

The author says that his code works, and I don't doubt him, but when I run the code I don't receive any characters, even though the port opened correctly (if I check with a terminal program, data are being sent).

Can someone please post a URL to some example C code? Thanks.

FWIW, here's my code:

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
E_boolean OpenCom1(void)
{
   COMMTIMEOUTS timeouts;

   comPorthandle = CreateFile("COM1",  // Specify port device: default "COM1"
   GENERIC_READ | GENERIC_WRITE,       // Specify mode that open device.
   0,                                  // the device isn't shared.
   NULL,                               // the object gets a default security.
   OPEN_EXISTING,                      // Specify which action to take on file.
   0,                                  // default (not overlapped i/o).
   NULL);                              // default (hTemplate must be NULL for COM devices).

   if (comPorthandle == INVALID_HANDLE_VALUE)
      return False;

   deviceControlBlock.DCBlength = sizeof(deviceControlBlock);

    if((GetCommState(comPorthandle, &deviceControlBlock) == 0))
    {
      // CodeMe: do what?
      return False;
    }

    deviceControlBlock.BaudRate = CBR_115200;
    deviceControlBlock.StopBits = ONESTOPBIT;
    deviceControlBlock.Parity   = NOPARITY;
    deviceControlBlock.ByteSize = DATABITS_8;
    deviceControlBlock.fRtsControl = 0;

    if (!SetCommState(comPorthandle, &deviceControlBlock))
    {
      // CodeMe: do what?
      return False;
    }

#if 0
// alternative to GetCommState() and SetCommState()
// both versions succeed
   if (!BuildCommDCB("115200,n,8,1", &deviceControlBlock))
    {
      // CodeMe: do what?
      return False;
    }
#endif 

    // set short timeouts on the comm port.
    timeouts.ReadIntervalTimeout = 0;
    timeouts.ReadTotalTimeoutMultiplier = 1;
    timeouts.ReadTotalTimeoutConstant = 1;
    timeouts.WriteTotalTimeoutMultiplier = 1;
    timeouts.WriteTotalTimeoutConstant = 1;
    if (!SetCommTimeouts(comPorthandle, &timeouts))
    {
      // CodeMe: do what?
      return False;
    }

   return True;
}//OpenCom1()

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
void      ReadCharacterFromCom1(INPUT char *theCharacter)
{
   DWORD numBytesRead;

   numBytesRead = 0;

   while (numBytesRead == 0)
   {
      ReadFile(comPorthandle,           // handle of file to read
               theCharacter,            // store read data here
               sizeof(theCharacter),    // number of bytes to read
               &numBytesRead,           // pointer to number of bytes actually read
               NULL);
   }
   return;
}//ReadCharacterFromCom1()

Upvotes: 1

Views: 9698

Answers (1)

Alex F
Alex F

Reputation: 43331

I see one problem in this code:

    sizeof(theCharacter),

replace this with

    sizeof(char),

because you want to read one byte, and sizeof(char*) is 4 or 8. Possibly there is something else, but you need to show more code.

Also, use Portmon program http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx to see whether data is received - you can run it together with your program.

Upvotes: 2

Related Questions