Discussion:
SetCommState() fails with "A device attached to the system is not functioning"
(too old to reply)
David Ching
2008-02-15 19:46:45 UTC
Permalink
Hello,

I have successfully opened a COM port, COM6. It is actually a virtual COM
port provided by a Microchip USB device. I can read/write to the device
using the default parameters fine.

But when I try to set the parameters with SetCommState(), the function
returns FALSE, and GetLastError() is 31 which is "A device attached to the
system is not functioning".

Here is the DCB that SetCommState gets, nothing unusual:

DCBlength: 28
BaudRate: 9600
fBinary: 1
fParity: 0
fOutxCtsFlow: 0
fOutxDsrFlow: 0
fDtrControl: 0
fDsrSensitivity: 0
fTXContinueOnXoff: 0
fOutX: 0
fInX: 0
fErrorChar: 0
fNull: 0
fRtsControl: 1
fAbortOnError: 0
fDummy2: 0
wReserved: 0
XonLim: 0
XoffLim: 0
ByteSize: 8
Parity: 0
StopBits: 0
XonChar: 0
XoffChar: 0
ErrorChar: 0
EofChar: 0
EvtChar: 0
wReserved1: 0


The same code works fine for ordinary COM ports, but not for the MicroChip
CDC USB one. This is a pain because most serial port libraries call
SetCommState() after the CreateFile() succeeds, and when SetCommState()
fails, the library reports the port cannot be opened. Thus, we are severely
limited in the serial port libraries we can use.

Is there any way SetCommState() can be fixed?

Thanks,
David
Joseph M. Newcomer
2008-02-16 00:45:39 UTC
Permalink
This post might be inappropriate. Click to display it.
David Ching
2008-02-16 02:40:45 UTC
Permalink
Post by Joseph M. Newcomer
My suspicion would be that the virtual control does not successfully implement all the
IOCTL codes required. There are something like 40 IOCTLs that a serial port must support.
If it would fail to support one of these, the consequence would probably be the error you
are seeing.
It sounds like the fix is not in SetCommState, but in the device driver for the virtual
device.
Short of exploring the DDK documentation in detail, to see what IOCTL is failing, I don't
have any effective way to isolate the failure. Note that SetCommState is nothing more
than a high-level wrapper around a set of DeviceIoControl calls.
I'd suggest downloading the file monitor from www.sysinternals.com and study what IOCTLs
are being sent to your device (look for IRP_MJ_DEVICE_CONTROL requests) and which one is
failing.
Thanks, Joe. The driver is the standard usbser.sys (USB serial port driver
that Windows provides). But the firmware on the device may be suspect. I
just got a new build of the firmware and will let you know if it fixes the
problem.

Thanks,
David
unknown
2008-04-29 09:40:36 UTC
Permalink
im usng com2 for serial communication.. I opened COM2 using createfile().. bt SetCommState() Fails.. how to make it work.. its very urgent..
pls help
Joseph M. Newcomer
2008-04-29 14:35:43 UTC
Permalink
Fails? In what way? How do you know it failed? What was GetLastError()?

I suspect it was error 87, invalid parameter, and this was almost certainly caused by
failing to set proper values in the DCB. However, lacking any code example, it is hard to
guess.

A recommended practice is to use GetCommState to fill the DCB, then change only the
parameters you care about. That way, you don't get invalid parameters in other members.

It is also good practice to open a COM port as \\.\COMn for n any integer. COM1-COM9
will work, but COM10 is not a serial port no matter how hard you try. But \\.\COM10 is
the 10th serial port.
joe
Post by unknown
im usng com2 for serial communication.. I opened COM2 using createfile().. bt SetCommState() Fails.. how to make it work.. its very urgent..
pls help
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
David Ching
2008-04-30 11:25:21 UTC
Permalink
Post by unknown
im usng com2 for serial communication.. I opened COM2 using createfile()..
bt SetCommState() Fails.. how to make it work.. its very urgent..
pls help
FWIW, I had exactly the same issue using a USB serial port for a PIC chip
until the the firmware was updated. Is COM2 your own hardware?

-- David

David Scambler
2008-02-19 00:10:54 UTC
Permalink
"David Ching" <***@remove-this.dcsoft.com> wrote in news:F_ltj.10890$***@newssvr11.news.prodigy.net:

Just curious... Which Microchip device is it? I am currently looking at
Microchip devices for COM port via USB.
Post by David Ching
Hello,
I have successfully opened a COM port, COM6. It is actually a virtual
COM port provided by a Microchip USB device. I can read/write to the
device using the default parameters fine.
snip
David Ching
2008-02-19 20:58:40 UTC
Permalink
Post by David Scambler
Just curious... Which Microchip device is it? I am currently looking at
Microchip devices for COM port via USB.
It's a PIC18F4550. Another team member reports that with a change in this
chip's programming, SetCommState() now works. But I have not been able to
verify it yet.

Thanks again,
David
Loading...