Discussion:
GetCommState failed with error 87.
(too old to reply)
uvbaz
2008-04-14 12:59:56 UTC
Permalink
Hi,

i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
the example code from MSDN "configuring 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
Scott McPhillips [MVP]
2008-04-14 15:01:40 UTC
Permalink
Post by uvbaz
Hi,
i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
the example code from MSDN "configuring 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.
Error 87 usually means you have made a simple programming mistake with the
parameters. Can you show the relevant lines of the code? It is very hard
to guess what is wrong if you do not show some code.
--
Scott McPhillips [VC++ MVP]
uvbaz
2008-04-15 08:08:30 UTC
Permalink
On 14 Apr., 17:01, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
Post by Scott McPhillips [MVP]
Post by uvbaz
Hi,
i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
the example code from MSDN "configuring 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.
Error 87 usually means you have made a simple programming mistake with the
parameters. Can you show the relevant lines of the code? It is very hard
to guess what is wrong if you do not show some code.
--
Scott McPhillips [VC++ MVP]
Sorry, i forgot to paste them.

#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(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState 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(hCom, &dcb);

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

printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
Check Abdoul
2008-04-16 06:23:35 UTC
Permalink
Try
char *pcCommPort = "\\\\.\\COM1"

Cheers
Check Abdoul
---------------------
Post by uvbaz
On 14 Apr., 17:01, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
Post by Scott McPhillips [MVP]
Post by uvbaz
Hi,
i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
the example code from MSDN "configuring 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.
Error 87 usually means you have made a simple programming mistake with the
parameters. Can you show the relevant lines of the code? It is very hard
to guess what is wrong if you do not show some code.
--
Scott McPhillips [VC++ MVP]
Sorry, i forgot to paste them.
#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(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState 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(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
Anthony Wieser
2008-04-16 07:32:12 UTC
Permalink
Post by uvbaz
int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM1";
<SNIP>
Post by uvbaz
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
You haven't set dcb.DCBlength = sizeof(dcb), so it will fail, as it doesn't
know what size the strucure is.

Anthony Wieser
Wieser Software Ltd
francois delamotte
2012-01-23 12:01:20 UTC
Permalink
i am using the same code as cheng. Even with the dcb.DCBLENGTH=sizeof(DCB) the program returns error 87. Are there any other possible causes?
Post by Scott McPhillips [MVP]
Error 87 usually means you have made a simple programming mistake with the
parameters. Can you show the relevant lines of the code? It is very hard
to guess what is wrong if you do not show some code.
--
Scott McPhillips [VC++ MVP]
Post by Check Abdoul
Try
char *pcCommPort = "\\\\.\\COM1"
Cheers
Check Abdoul
---------------------
Post by Anthony Wieser
<SNIP>
You have not set dcb.DCBlength = sizeof(dcb), so it will fail, as it does not
know what size the strucure is.
Anthony Wieser
Wieser Software Ltd
Post by uvbaz
Hi,
i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
the example code from MSDN "configuring 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
Post by uvbaz
On 14 Apr., 17:01, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
Sorry, i forgot to paste them.
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(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState 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(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
Post by Joseph M. Newcomer
I presume there was some actual code that caused this error. How are we expected to offer
advice if you don't show the code that failed?
joe
Joseph M. Newcomer [MVP]
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Post by Joseph M. Newcomer
See below...
****
int _tmain(int argc _TCHAR * argv[])
****
****
First, why are you using an obsolete data type like 'char'? Why are you declaring a
variable at all?
The correct code would at the very least be
LPTSTR CommPort = _T("COM1");
but that is not really general; the correct name is actually
_T("\\\\.\\COM1")
because the string you use is only good for COM1..COM9, and fails utterly with COM10.
****
****
Which is only one of about a dozen possible values you could use here; why single out
FILE_FLAG_OVERLAPPED as being the only flag of meaning?
****
*****
Did you actually READ THE DOCUMENTATION about DCB? Error 87 is EXACTLY the correct
response; you failed to initialize the first element, the DCBlength field. This could be
done by writing
DCB dcb = { sizeof(DCB) };
or by explicitly assigning the lenght. The error code is correct; you provided an invalid
parameter!
joe
*****
Joseph M. Newcomer [MVP]
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Ian Semmel
2008-04-16 19:03:40 UTC
Permalink
Try putting a security attributes parameter in your create file

m_securityAttributes.nLength = sizeof ( SECURITY_ATTRIBUTES );
m_securityAttributes.lpSecurityDescriptor = 0;
m_securityAttributes.bInheritHandle = true;

Don't know if it will change anything, but that is what I do. Your code
appears OK.
Post by uvbaz
On 14 Apr., 17:01, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
Post by Scott McPhillips [MVP]
Post by uvbaz
Hi,
i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I
use
Post by Scott McPhillips [MVP]
Post by uvbaz
the example code from MSDN "configuring 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.
Error 87 usually means you have made a simple programming mistake with
the
Post by Scott McPhillips [MVP]
parameters. Can you show the relevant lines of the code? It is very
hard
Post by Scott McPhillips [MVP]
to guess what is wrong if you do not show some code.
--
Scott McPhillips [VC++ MVP]
Sorry, i forgot to paste them.
#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(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState 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(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
Joseph M. Newcomer
2008-04-20 23:47:39 UTC
Permalink
See below...
Post by uvbaz
On 14 Apr., 17:01, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
Post by Scott McPhillips [MVP]
Post by uvbaz
Hi,
i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
the example code from MSDN "configuring 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.
Error 87 usually means you have made a simple programming mistake with the
parameters. Can you show the relevant lines of the code? It is very hard
to guess what is wrong if you do not show some code.
--
Scott McPhillips [VC++ MVP]
Sorry, i forgot to paste them.
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
****
int _tmain(int argc _TCHAR * argv[])
****
Post by uvbaz
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM1";
****
First, why are you using an obsolete data type like 'char'? Why are you declaring a
variable at all?

The correct code would at the very least be
LPTSTR CommPort = _T("COM1");
but that is not really general; the correct name is actually
_T("\\\\.\\COM1")
because the string you use is only good for COM1..COM9, and fails utterly with COM10.
****
Post by uvbaz
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
****
Which is only one of about a dozen possible values you could use here; why single out
FILE_FLAG_OVERLAPPED as being the only flag of meaning?
****
Post by uvbaz
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(hCom, &dcb);
*****
Did you actually READ THE DOCUMENTATION about DCB? Error 87 is EXACTLY the correct
response; you failed to initialize the first element, the DCBlength field. This could be
done by writing
DCB dcb = { sizeof(DCB) };
or by explicitly assigning the lenght. The error code is correct; you provided an invalid
parameter!
joe
*****
Post by uvbaz
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState 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(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer
2008-04-20 23:41:30 UTC
Permalink
I presume there was some actual code that caused this error. How are we expected to offer
advice if you don't show the code that failed?
joe
Post by uvbaz
Hi,
i'm trying to set the serial port.(XP SP2, Visual Studio 2005). I use
the example code from MSDN "configuring 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
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Loading...