Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PC Com Port "Problems" 1

Status
Not open for further replies.

ildenizen

Programmer
Feb 24, 2005
12
US
Good day,

I am an average programmer, but have only just entered the Visual Basic world. I am happy to say it all makes good sense and am progressing quickly.

One problem is that I can't get any comm port to open in my system! I am using the most simple of codes to see if I can simply open and send data. The "dumbed up" code is as follows:

Private Sub Command1_Click()
Dim iPort As Long
iPort = 3 ' I place the port to check here
If COMCheckPort(iPort) Then
MSComm1.CommPort = iPort
MSComm1.Settings = "9600,N,8,1"
MSComm1.DTREnable = False
MSComm1.PortOpen = True
Else
End
End If
End Sub

Private Sub Command2_Click()
MSComm1.Output = Chr$(255)
End Sub

Private Function COMCheckPort(Port As Long) As Boolean
'Returns false if port cannot be opened or does not exist
'Returns true if port is available and can be opened

'Handle all errors
On Error GoTo OpenCom_Error

'Set port number for test
MSComm1.CommPort = Port

If MSComm1.PortOpen = True Then
COMCheckPort = False
Exit Function
Else
'Test the port by opening and closing it
MSComm1.PortOpen = True
MSComm1.PortOpen = False
COMCheckPort = True
Exit Function
End If

OpenCom_Error:
COMCheckPort = False

End Function

I suspect many will give me code improvements. I appreciate that. However I have a more basic question? What could keep my system from successfully opening any Com Port? I know they are not already open because when I try to "Send a byte", I get a run time error saying the port is not open.

Any help/diagnostic method would be much appreciated!

 
Try adding the following lines. Put a Button on your form named Command3. This should tell you the available com ports that you can use.

Private Function SearchForCommPorts() As Variant

Dim i As Long
Dim bError As Boolean
Dim arResults() As Boolean

On Error GoTo SearchForCommPortError

ReDim arResults(7)

MSComm1.Settings = "38400,n,8,1"
If MSComm1.PortOpen Then
MSComm1.PortOpen = False
End If

For i = 1 To 8
bError = False
arResults(i - 1) = False
MSComm1.CommPort = i
If Not bError Then
MSComm1.PortOpen = True
If Not bError Then
MSComm1.PortOpen = False
arResults(i - 1) = True
End If
End If
Next

SearchForCommPorts = arResults

Exit Function

SearchForCommPortError:

bError = True
Resume Next

End Function

Private Sub Command3_Click()

Dim arPorts() As Boolean
Dim i As Integer
Dim cResult As String

arPorts = SearchForCommPorts

cResult = ""
For i = LBound(arPorts) To UBound(arPorts)
cResult = cResult & "Port " & CStr(i + 1) & ": " & IIf(arPorts(i), "TRUE", "FALSE") & vbCrLf
Next

Call MsgBox(cResult)

End Sub
 
Thank you for you help! I have added that and run it. Guess what? Com Ports 1 through 8 are all unavailable.
I am guessing that there is something going on with my Computer, not the code.
I of course at this point am about to throw up my hands, as this seems a little beyond me.
If you can think of anything else that might help me find out why my com ports are all unavailable, I would be MOST grateful. Just FYI, I am running Win 98 SE. Mouse is working from a USB port, and I have a modem/answering machine (works!), that looks like it is on COM port 3.

Again - thank you!
 
If you have a modem on Com 3, then ordinarily you would think that it is available. Except... If you are using your modem for an answering machine, then there must be some answering machine software running. It may be a service, or an app in your startup.

So, here's the thing. In order for answering machine software to function, it must have the com port open so that it can detect when your phone rings.

Try temporarily turning off the answering machine software and then run this code again. You may be pleasantly surprised.
 
Good day. I have already been helped once here today, let's see if I can bat 1000!

I am slowly putting together a com application. I have now been able to successfully open and send a character of information to a USB device configured as a Virtual Serial Port.

However, here is the odd part. Within a command button, I send a single character using MSComm1.Output. It works great. Each click event on my command button sends out a different character successfully. But, if I try to send multiple characters one character at a time (each one using a separate MSComm1.Output line), I lock up my system completely. If I get lucky I can Ctrl-Alt-Del the application, but other times must reboot the entire system.

Personally, I can't see how this is any different except in how fast the characters are being sent, though there may be something else going on I have not considered.

Anyone have any ideas? Thanks in advance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top