How to resolve runtime error 8015 in VB6

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:

  • Using the MSComm control (which produces the errors above)
  • Using windows API call (which gave a different error 'Invalid Functions')
    API Link: http://www.thescarms.com/vbasic/CommIO.aspx
  • Using the shell function to shell cmd.exe with the 'copy' command (this method works
    when it wants to)
  • Using VB6e FileCopy() function that doesn't work when copying to ports

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.

Best Answer

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
Related Topic