user1963933
user1963933

Reputation: 11

visual basic 6.0 comm port code error

I am actually trying to get data from port1 but the error Invalid port number is generated.

The code is:

Private Sub Command1_Click()
    MsgBox ("The port is open " & MSComm1.PortOpen)
    If (MSComm1.PortOpen = False) Then
    MSComm1.PortOpen = True
    End If
    Command1.Enabled = False
    Command2.Enabled = True
End Sub

Private Sub Command2_Click()
    If (MSComm1.PortOpen = True) Then
    MSComm1.PortOpen = False
    End If
    Command1.Enabled = True
    Command2.Enabled = False
End Sub

Private Sub Form_Load()
    With MSComm1
    .CommPort = 1
    .RThreshold = 1
    .RTSEnable = True
    .Settings = "9600,N,8,1"
    .InputLen = 127
    .SThreshold = 1
    End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If (MSComm1.PortOpen = True) Then
    MSComm1.PortOpen = False
    End If
End Sub

Private Sub MSComm1_OnComm()
    Dim Buffer As String

    Select Case MSComm1.CommEvent
    Case comEvReceive
    'Text1.Text = " "
    Buffer = MSComm1.Input
    Text1.Text = Text1.Text & Buffer
    End Select
End Sub

Upvotes: 0

Views: 7897

Answers (2)

Hrqls
Hrqls

Reputation: 2951

have a look at the code i posted in the following answer :

search for available com ports

running that code will give you a list of available com ports

use it in your code, and let your code select a com port from that list

Upvotes: 0

George
George

Reputation: 2213

Try different COM port. A number between 1 and 16 is acceptable.

' Open the serial port
MSComm1.CommPort = 2
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True

"The CommPort property sets which serial port to open. Assuming that a modem is connected to COM2, the above example sets the value to 2 (COM2) and connects to the modem. You can set the CommPort property value to any number between 1 and 16 (the default is 1). If, however, you set this value to a COM port that does not exist for the system on which your application is run, an error will be generated."

Sauce: dx.eng.uiowa.edu/eedesign/MScomm.doc

Upvotes: 1

Related Questions