Burhanuddin Saifuddin
Burhanuddin Saifuddin

Reputation: 11

usbser.sys mdmcpq.inf issues with cable detach

I have designed an embedded system using PIC18F67J94 and have used a random VID and PID. Then I used Zadig to generate a USB CDC driver for the hardware. It uses mdmcpq.inf and usbser.sys. The following is the .inf file made by Zadig with a few modifications in vendor name

[Strings]
SNMPMOD = "SNMP-Modbus Converter"
VendorName = "ABCD"
SourceName = "ABCD Install Disk"
SNMPMODID   = "VID_8474&PID_0005"
DeviceGUID = "{6097438F-67AB-4C8C-A8C5-AF92EE79D3DF}"
Service    = "USB COM Port"

[Version]
Signature   = "$Windows NT$"
Provider = "ABCD"
Class       = Ports
ClassGuid   = {4D36E978-E325-11CE-BFC1-08002BE10318}
CatalogFile = ABCD.cat
DriverVer   = 04/11/2018, 1.0.0.6

[ControlFlags]
ExcludeFromSelect=*

[Manufacturer]
%VendorName% = DeviceList,NTx86,NTamd64

[DeviceList.NTx86]
%SNMPMOD% = UsbSer_Install, USB\%SNMPMODID%

[DeviceList.NTamd64]
%SNMPMOD% = UsbSer_Install, USB\%SNMPMODID%

[UsbSer_Install]
include     = mdmcpq.inf
CopyFiles   = FakeModemCopyFileSection
AddReg      = UsbSer_Install.AddReg
AddProperty = UsbSer_AddProperty

[UsbSer_Install.AddReg]
HKR,,DevLoader,,*ntkern
HKR,,NTMPDriver,,usbser.sys
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"

[UsbSer_Install.Services]
AddService = usbser,0x00000002,UsbSer_Service

[UsbSer_Service]
DisplayName   = %Service%
ServiceType   = 1
StartType     = 3
ErrorControl  = 1
ServiceBinary = %12%\usbser.sys
LoadOrderGroup = Base

[UsbSer_AddProperty]
GenericDriverInstalled,,,,1

[DestinationDirs]
DefaultDestDir = 12

[SourceDisksNames]
1 = %SourceName%

It makes a COM port and works fine, but if I remove the device without closing the COM Port and then reconnect the device, the device manager shows the device to be connected and the com port number along with it but that com port is no more accessible using a terminal software.

I then have to remove the device again and then reconnect the device after which it creates a valid com port.

I also have used FTDI devices, and also Prolific Technology device, they both dont have these issues.

Prolific uses ser2pl.sys where as FTDI has its own .sys files.

I want to know if it is possible to make the windows somehow close the COM port when the device is disconnected.

Upvotes: 1

Views: 772

Answers (1)

David Grayson
David Grayson

Reputation: 87396

As far as I can tell, this is an unavoidable problem with any device that uses usbser.sys on versions of Windows prior to Windows 10. (It's good that you posted the details about your driver but I don't think it matters in this case.)

You can use the WinObj program to inspect the COM port objects that are created by drivers like usbser.sys, and you will see that the driver cannot destroy the COM port until the program that is using it closes its handle. So when the device is plugged in again before the old COM port is closed, the driver is unable to create a COM port with the right name, because the old object is still hanging around.

They actually fixed this in Windows 10 when they rewrote usbser.sys, so you could just upgrade to Windows 10. If I recall correctly, all you have to do to get the COM port working is to close handles to the old instance of the COM port; you don't have to disconnect the device anymore. Also, Windows 10 ships with a Microsoft driver named usbser.inf so you won't have to generate your own driver.

(I hope you buy your own vendor ID if you intend to ship this device to users, by the way. Using a random vendor ID is a recipe for collisions that will interfere with the function of your device or another device if a user has both.)

Upvotes: 2

Related Questions