Why won't this VB 6 code open the serial port?
Dim MSComm1 As MSComm
on error goto cant_open_com1
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSC开发者_开发问答omm1.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 on error handler
When you say "control passes to the error handler", did you forget to add an Exit Sub
? In your comments, you say you added the New declaration, but you still having a problem? Well, I just ran this code and it had no trouble 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
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
精彩评论