PRB: Run-Time Error 8015 If You Try to Open the Serial Port with Microsoft Comm Control


If you try to open the serial port with Microsoft Comm Control, you may receive the following error message:

Run-time error '8015': Could not set comm state, there may be one or more invalid communications parameters.


This behavior can occur if a serial printer driver is using the serial port that you are trying to open.


To resolve this behavior, disable the serial printer or attach it to another port. If you are using Microsoft Windows 2000 or Microsoft Windows XP, create a virtual COM port.


This behavior can occur if the serial printer driver is not working correctly, and is not a problem with the MSComm32 Control.

More Information

To work around this behavior, create a new virtual COM port and use it instead of COM1.

WARNING: If anything else prints on the printer attached to COM1, your program may not work as expected.

  1. Add the following code to the General Declarations section of Form1:
            Private Declare Function DefineDosDevice Lib "kernel32" Alias _
    "DefineDosDeviceA" (ByVal dwFlags As Long, ByVal lpDeviceName _
    As String, ByVal lpTargetPath As String) As Long
  2. Add another Command button to Form1.
  3. Add the following code in the Click event of Command2:
    DefineDosDevice DDD_RAW_TARGET_PATH, "COM5", "\Device\Serial0"
  4. Change the code on Command1 to open COM5:
    MSComm1.CommPort = 5
  5. Run the program, click Command2, and then click Command1.

Steps to Reproduce the Behavior

  1. Install a Generic Text printer, and then connect it to COM1 instead of LPT1.
  2. Start a new Standard EXE project in Microsoft Visual Basic. By Default, Form1 is created.
  3. Add the Microsoft Comm Control to the project.
  4. Add a new MSComm1 control on Form1.
  5. Add a new Command button on Form1, and then insert the following code in the Click event:
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.Handshaking = comNone
    MSComm1.InBufferSize = 1024
    MSComm1.OutBufferSize = 512
    MSComm1.RThreshold = 0
    MSComm1.SThreshold = 0
    MSComm1.InputLen = 0
    MSComm1.PortOpen = True
  6. Run the program, and then click Command1.


For more information, see the DefineDosDevice function documentation in the Microsoft Developer Network (MSDN) under Platform SDK.

ID do Artigo: 318832 - Última Revisão: 19 de jun de 2014 - Revisão: 1