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
控制传递给错误处理程序
答案 0 :(得分:2)
当您说“控制传递给错误处理程序”时,您是否忘记添加Exit Sub
?在您的评论中,您说您添加了新声明,但您仍然遇到问题?好吧,我刚刚运行了这段代码,打开端口没问题。
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
答案 1 :(得分:-1)
不要在端口COM1上设置打印机。 COM1端口将被COM1端口中的打印机占用。使用Visual Basic MSCOMM组件时,您不需要任何打印机驱动程序。如果您在COM1端口中设置打印机。您可以使用以下代码。此代码不会受打印机设置的影响。
Visual Basic:为输出AS#1打开“COM1”