GetCommState failed with error 87.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • uvbaz

    GetCommState failed with error 87.

    Hi,

    i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
    the example code from MSDN "configurin g a communications resource".

    CreateFile pass through, however GetCommState return 0.With
    GetLastError(), i get the error number 87, which means
    "ERROR_INVALID_ PARAMETER The parameter is incorrect. "
    I'm sure the serial port works, because i can use another program to
    read/write through it.

    thx
    Cheng

  • Nick Keighley

    #2
    Re: GetCommState failed with error 87.

    On 14 Apr, 13:52, uvbaz <uv...@stud.u ni-karlsruhe.dewro te:
    i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
    the example code from MSDN "configurin g a communications resource".
    >
    CreateFile pass through, however GetCommState return 0.With
    GetLastError(), i get the error number 87, which means
    "ERROR_INVALID_ PARAMETER The parameter is incorrect. "
    I'm sure the serial port works, because i can use another program to
    read/write through it.
    this isn't topical to comp.lang.c (even if your program
    is written in C!). You need to ask on a Windows related news group.

    Since you have an invalid parameter I strongly suggest
    you post (to an appropriate ng) a short program that
    illustrates your problem.


    --
    Nick Keighley

    Comment

    • uvbaz

      #3
      Re: GetCommState failed with error 87.

      On 14 Apr., 15:47, Nick Keighley <nick_keighley_ nos...@hotmail. com>
      wrote:
      On 14 Apr, 13:52, uvbaz <uv...@stud.u ni-karlsruhe.dewro te:
      >
      i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
      the example code from MSDN "configurin g a communications resource".
      >
      CreateFile pass through, however GetCommState return 0.With
      GetLastError(), i get the error number 87, which means
      "ERROR_INVALID_ PARAMETER The parameter is incorrect. "
      I'm sure the serial port works, because i can use another program to
      read/write through it.
      >
      this isn't topical to comp.lang.c (even if your program
      is written in C!). You need to ask on a Windows related news group.
      >
      Since you have an invalid parameter I strongly suggest
      you post (to an appropriate ng) a short program that
      illustrates your problem.
      >
      --
      Nick Keighley
      O.K.

      Comment

      • Antoninus Twink

        #4
        Re: GetCommState failed with error 87.

        On 14 Apr 2008 at 14:13, uvbaz wrote:
        On 14 Apr., 15:47, Nick Keighley <nick_keighley_ nos...@hotmail. com>
        wrote:
        >On 14 Apr, 13:52, uvbaz <uv...@stud.u ni-karlsruhe.dewro te:
        >>
        i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
        the example code from MSDN "configurin g a communications resource".
        >>
        CreateFile pass through, however GetCommState return 0.With
        GetLastError(), i get the error number 87, which means
        "ERROR_INVALID_ PARAMETER The parameter is incorrect. "
        I'm sure the serial port works, because i can use another program to
        read/write through it.
        >>
        >Since you have an invalid parameter I strongly suggest
        >you post
        [snip]
        > a short program that illustrates your problem.
        >
        O.K.
        You forgot to post the short program!

        Comment

        • uvbaz

          #5
          Re: GetCommState failed with error 87.

          On 14 Apr., 19:14, Antoninus Twink <nos...@nospam. invalidwrote:
          On 14 Apr 2008 at 14:13, uvbaz wrote:
          >
          >
          >
          On 14 Apr., 15:47, Nick Keighley <nick_keighley_ nos...@hotmail. com>
          wrote:
          On 14 Apr, 13:52, uvbaz <uv...@stud.u ni-karlsruhe.dewro te:
          >
          i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
          the example code from MSDN "configurin g a communications resource".
          >
          CreateFile pass through, however GetCommState return 0.With
          GetLastError(), i get the error number 87, which means
          "ERROR_INVALID_ PARAMETER The parameter is incorrect. "
          I'm sure the serial port works, because i can use another program to
          read/write through it.
          >
          Since you have an invalid parameter I strongly suggest
          you post
          [snip]
          a short program that illustrates your problem.
          >
          O.K.
          >
          You forgot to post the short program!
          Sorry.

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

          int main(int argc, char *argv[])
          {
          DCB dcb;
          HANDLE hCom;
          BOOL fSuccess;
          char *pcCommPort = "COM1";

          hCom = CreateFile( pcCommPort,
          GENERIC_READ | GENERIC_WRITE,
          0, // must be opened with exclusive-access
          NULL, // no security attributes
          OPEN_EXISTING, // must use OPEN_EXISTING
          0, // not overlapped I/O
          NULL // hTemplate must be NULL for comm devices
          );

          if (hCom == INVALID_HANDLE_ VALUE)
          {
          // Handle the error.
          printf ("CreateFile failed with error %d.\n", GetLastError()) ;
          return (1);
          }

          // Build on the current configuration, and skip setting the size
          // of the input and output buffers with SetupComm.

          fSuccess = GetCommState(hC om, &dcb);

          if (!fSuccess)
          {
          // Handle the error.
          printf ("GetCommSta te failed with error %d.\n", GetLastError()) ;
          return (2);
          }

          // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.

          dcb.BaudRate = CBR_57600; // set the baud rate
          dcb.ByteSize = 8; // data size, xmit, and rcv
          dcb.Parity = NOPARITY; // no parity bit
          dcb.StopBits = ONESTOPBIT; // one stop bit

          fSuccess = SetCommState(hC om, &dcb);

          if (!fSuccess)
          {
          // Handle the error.
          printf ("SetCommSta te failed with error %d.\n", GetLastError()) ;
          return (3);
          }

          printf ("Serial port %s successfully reconfigured.\n ", pcCommPort);
          return (0);
          }

          Comment

          • jacob navia

            #6
            Re: GetCommState failed with error 87.

            uvbaz wrote:
            On 14 Apr., 19:14, Antoninus Twink <nos...@nospam. invalidwrote:
            >On 14 Apr 2008 at 14:13, uvbaz wrote:
            >>
            >>
            >>
            >>On 14 Apr., 15:47, Nick Keighley <nick_keighley_ nos...@hotmail. com>
            >>wrote:
            >>>On 14 Apr, 13:52, uvbaz <uv...@stud.u ni-karlsruhe.dewro te:
            >>>>i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
            >>>>the example code from MSDN "configurin g a communications resource".
            >>>>CreateFil e pass through, however GetCommState return 0.With
            >>>>GetLastErro r(), i get the error number 87, which means
            >>>>"ERROR_INVA LID_PARAMETER The parameter is incorrect. "
            >>>>I'm sure the serial port works, because i can use another program to
            >>>>read/write through it.
            >>>Since you have an invalid parameter I strongly suggest
            >>>you post
            >[snip]
            >>> a short program that illustrates your problem.
            >>O.K.
            >You forgot to post the short program!
            >
            Sorry.
            >
            #include <windows.h>
            #include <stdio.h>
            >
            int main(int argc, char *argv[])
            {
            DCB dcb;
            HANDLE hCom;
            BOOL fSuccess;
            char *pcCommPort = "COM1";
            >
            hCom = CreateFile( pcCommPort,
            GENERIC_READ | GENERIC_WRITE,
            0, // must be opened with exclusive-access
            NULL, // no security attributes
            OPEN_EXISTING, // must use OPEN_EXISTING
            0, // not overlapped I/O
            NULL // hTemplate must be NULL for comm devices
            );
            >
            if (hCom == INVALID_HANDLE_ VALUE)
            {
            // Handle the error.
            printf ("CreateFile failed with error %d.\n", GetLastError()) ;
            return (1);
            }
            >
            // Build on the current configuration, and skip setting the size
            // of the input and output buffers with SetupComm.
            >
            fSuccess = GetCommState(hC om, &dcb);
            >
            if (!fSuccess)
            {
            // Handle the error.
            printf ("GetCommSta te failed with error %d.\n", GetLastError()) ;
            return (2);
            }
            >
            // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
            >
            dcb.BaudRate = CBR_57600; // set the baud rate
            dcb.ByteSize = 8; // data size, xmit, and rcv
            dcb.Parity = NOPARITY; // no parity bit
            dcb.StopBits = ONESTOPBIT; // one stop bit
            >
            fSuccess = SetCommState(hC om, &dcb);
            >
            if (!fSuccess)
            {
            // Handle the error.
            printf ("SetCommSta te failed with error %d.\n", GetLastError()) ;
            return (3);
            }
            >
            printf ("Serial port %s successfully reconfigured.\n ", pcCommPort);
            return (0);
            }
            I compiled that, and I get
            d:\lcc\mc71\tes t>tport
            Serial port COM1 successfully reconfigured.

            What is the problem?

            Maybe you have a misconfiguratio n in your machine?


            --
            jacob navia
            jacob at jacob point remcomp point fr
            logiciels/informatique

            Comment

            • Flash Gordon

              #7
              Re: GetCommState failed with error 87.

              uvbaz wrote, On 15/04/08 09:09:
              On 14 Apr., 19:14, Antoninus Twink <nos...@nospam. invalidwrote:
              >On 14 Apr 2008 at 14:13, uvbaz wrote:
              >>
              >>
              >>
              >>On 14 Apr., 15:47, Nick Keighley <nick_keighley_ nos...@hotmail. com>
              >>wrote:
              >>>On 14 Apr, 13:52, uvbaz <uv...@stud.u ni-karlsruhe.dewro te:
              >>>>i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
              >>>>the example code from MSDN "configurin g a communications resource".
              >>>>CreateFil e pass through, however GetCommState return 0.With
              >>>>GetLastErro r(), i get the error number 87, which means
              >>>>"ERROR_INVA LID_PARAMETER The parameter is incorrect. "
              >>>>I'm sure the serial port works, because i can use another program to
              >>>>read/write through it.
              >>>Since you have an invalid parameter I strongly suggest
              >>>you post
              >[snip]
              >>> a short program that illustrates your problem.
              >>O.K.
              >You forgot to post the short program!
              >
              Sorry.
              >
              #include <windows.h>
              <snip>

              Now try posting it to a windows group as was suggested in the text that
              Antoninus Twink snipped, something I'm sure he did deliberately to
              misrepresent what Nick Keighley wrote.

              If you post to a Windows group lots of people who know about Windows
              specific problems will see you post and be able to answer, if you post
              here there are far fewer experts on Windows.
              --
              Flash Gordon

              Comment

              Working...