user1803784
user1803784

Reputation: 82

How to resolve runtime error 8015 in VB6?

Good Day

I'm trying to send data to a comport "COM1". A generic printer driver is set up on comport "COM1". When sending data to this port I receive a run-time error '8015: Could not set comm state, there maybe one or more invalid communications parameters'. After clicking ok to that error message i get another error, run-time error '8018: Operation valid only when the port is open'. I believe this issue can be resolve because in Command Prompt I can copy a text file to the port with the command "copy C:\textfile.txt COM1" and it works without any issues. That's my situation.

The methods I have tried are as follow:

I believe the person that can answer this question is someone that knows in depth information about COM Port communication and knows in depth information about windows copy command.

I read on some forum (I've been through too many) that if you remove the plug and plug it in back that it resolve this issue. I haven't tried that yet but i have tried turning off the device and putting it on back which I believe is the same thing.

If anyone can help with this issue it would be very much appreciated

Here is a link that might be interesting:

Thanks,

Jorgen

As requested by Beaner:

Here is the snippet of code that I'm using.

Private Sub Initialize_COMPort()

On Error GoTo COMPort_Error

    If MSComm1.PortOpen Then
        MsgBox "Port is opened"
        MSComm1.PortOpen = False
    End If

    MSComm1.Settings = "9600,n,8,1"
    MSComm1.CommPort = Val(CbCOMPorts.ListIndex) + 1
    MSComm1.PortOpen = True
    MSComm1.RThreshold = 1
    MSComm1.Handshaking = comXOnXoff

    Exit Sub

COMPort_Error:
    MsgBox Err.Number & " - " & Err.Description, vbCritical + vbOKOnly, "Port Tester", Err.HelpFile, Err.HelpContext
End Sub

The error occurs when:

MSComm1.PortOpen = True

is executed.

@Hrqls I appreciate the code however the code you gave me is similar to mind and when executed it the error still persist. The error occurs at the same statement

.PortOpen = True

@Hrqls I haven't try using the printer control before however this device is not actually a text printer it is a card embosser. Its the "Matica Z3i AF" embosser the program I'm currently working on will be used to emboss cards.

The method currently in place is to use the embosser is a batch file that basically runs a 'copy' command of a text file with the card info to the print in a format that the embosser understands.

I shall try using the printer control until then thanks for the responses and I shall keep everyone up to date if the problem is resolved.

Upvotes: 1

Views: 9696

Answers (4)

chunhunghan
chunhunghan

Reputation: 11

Do not set printer on port COM1. COM1 port will be occupied by Printer in COM1 port. When use Visual Basic MSCOMM component, you do not need any printer driver. If you do set Printer in COM1 port. You can use the following code. this code will not impacted by printer setting.

Visual Basic: Open "COM1" For Output AS #1

Upvotes: 1

jac
jac

Reputation: 9726

Does the computer you're working on have the Matica Z3i AF installed and is the printer already using COM1? If so the port is in use and the MSComm control cannot access it. Make sure there is no other hardware or software that already has the port open.

If there is nothing else using the port, does the batch file specifically set the speed, parity, or stop bits? (MODE COM1:9600,N,8,1,P) If so you can try using the settings from the batch file.

If none of that helps, I couldn't find the suggested settings for your embosser so you can try the basic port settings. You can query the port with MODE COM1 /STATUS. The command is not case sensitive and will return something like:

Status for device COM1:
-----------------------
    Baud:            1200
    Parity:          None
    Data Bits:       7
    Stop Bits:       1
    Timeout:         OFF
    XON/XOFF:        OFF
    CTS handshaking: OFF
    DSR handshaking: OFF
    DSR sensitivity: OFF
    DTR circuit:     ON
    RTS circuit:     ON

Upvotes: 1

Hrqls
Hrqls

Reputation: 2951

to check if a port is already in use you can open it, and track the error code

a small project which lists the available ports in a listbox :

'1 form with :
'    1 CommandButton : Name = Command1
'    1 Listbox       : Name = List1
'    1 MSComm        : Name = MSComm1
Option Explicit

Private Enum PortAttr
  PortFree = 0
  PortInUse = 1
  PortUnknown = 2
End Enum

Private Function CheckPort(intPort As Integer) As PortAttr
  On Error GoTo ErrorFound
  With MSComm1
    If .PortOpen Then .PortOpen = False
    .CommPort = intPort
    .PortOpen = True
    CheckPort = PortFree
    If .PortOpen = False Then .PortOpen = True
  End With 'MSComm1
Exit Function
ErrorFound:
  Select Case Err.Number
    Case 8002 'port doesnt exist
      CheckPort = PortUnknown
    Case 8005 'port already in use
      CheckPort = PortInUse
    Case Else
      MsgBox Err.Description, vbCritical, "Error " & CStr(Err.Number) & " on Port " & CStr(intPort)
  End Select
  On Error GoTo 0
End Function

Private Sub ListPorts()
  Dim intIndex As Integer
  Dim intPort As Integer
  Dim intFree As Integer
  On Error GoTo ErrorFound
  With MSComm1
    If .PortOpen Then .PortOpen = False
    intPort = .CommPort
    List1.Clear
    List1.AddItem "--- Not Used ---"
    List1.ItemData(0) = -2 'not possible
    List1.AddItem "---- In Use ----"
    List1.ItemData(1) = -2 'not possible
    intFree = 0
    For intIndex = 1 To 16
      Select Case CheckPort(intIndex)
        Case PortFree
          intFree = intFree + 1
          List1.AddItem "Com" & CStr(intIndex), intFree
          List1.ItemData(intFree) = intIndex
        Case PortInUse
          List1.AddItem "Com" & CStr(intIndex)
      End Select
    Next intIndex
    If .PortOpen Then .PortOpen = False
    .CommPort = intPort
    If CheckPort(intPort) = PortFree Then
      If .PortOpen = False Then .PortOpen = True
    End If
  End With 'MSComm1
  Show
Exit Sub
ErrorFound:
  MsgBox Err.Description, vbCritical, "Error " & CStr(Err.Number)
  On Error GoTo 0
End Sub

Private Sub Command1_Click()
  ListPorts
End Sub

Upvotes: 0

Hrqls
Hrqls

Reputation: 2951

I assume the error comes from the fact that COM1 is already in use by the printer, in that case your MSCOMM control cant use COM1 as well

What do you want to print ?

The easiest, and most straightforward to print something is to use the Printer object

For example to print the current time :

Private Sub Command1_Click()
  Printer.Print CStr(Now)
  Printer.EndDoc
End Sub

Upvotes: 2

Related Questions