Why does this VB 6 code not open the serial port?

Dim MSComm1 As MSComm

on error goto cant_open_com1

MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.DTREnable = True
MSComm1.Handshaking = comRTS
MSComm1.InBufferSize = 12 + 1  ' +1 for the CR
MSComm1.RThreshold = MSComm1.InBufferSize
MSComm1.RTSEnable = True
MSComm1.InputLen = 0 ' read entire input buffer
MSComm1.InputMode = comInputModeText
MSComm1.NullDiscard = True
MSComm1.OutBufferSize = 0 ' not used, we don't write to the serial port
MSComm1.SThreshold = MSComm1.OutBufferSize
'MSComm1.ParityReplace = ?

MSComm1.PortOpen = True

Control passes to the error handler on

+3
source share
2 answers

When you say "control passes to the error handler," did you forget to add Exit Sub? In your comments, you say you added a new ad, but still have a problem? Well, I just ran this code and it had no problems opening the port.

Private Sub Form_Load()

  Dim MSComm1 As New MSComm

  On Error GoTo cant_open_com1

  MSComm1.CommPort = 1
  MSComm1.Settings = "9600,N,8,1"
  MSComm1.DTREnable = True
  MSComm1.Handshaking = comRTS
  MSComm1.InBufferSize = 12 + 1
  MSComm1.RThreshold = MSComm1.InBufferSize
  MSComm1.RTSEnable = True
  MSComm1.InputLen = 0
  MSComm1.InputMode = comInputModeText
  MSComm1.NullDiscard = True
  MSComm1.OutBufferSize = 0
  MSComm1.SThreshold = MSComm1.OutBufferSize

  MSComm1.PortOpen = True

  Exit Sub

cant_open_com1:
    Debug.Print Err.Description

End Sub
+2
source

Do not install the printer on the COM1 port. Port COM1 will be occupied by the printer on port COM1. When using the Visual Basic MSCOMM component, you do not need a printer driver. If you installed the printer in the COM1 port. You can use the following code. this code will not affect printer settings.

Visual Basic: "COM1" AS # 1

-1

Source: https://habr.com/ru/post/1762864/


All Articles