Solved

MSCOMM

Posted on 2004-08-17
1
2,385 Views
Last Modified: 2013-12-25
Hello,
I have a vb program where I let the user enter a com port number that they will be using for an rs232 device.
It works well.

If I try to open a comport that I do not have
(com port 4) it gives me an error that says
 invalid com port number.


   MSComm1.CommPort = myComPortNumber
   MSComm1.InputLen = 0
   MSComm1.Settings = "9600,N,8,1"
   MSComm1.PortOpen = True


How do I test for this before the error happens?
0
Comment
Question by:jackjohnson44
1 Comment
 
LVL 11

Accepted Solution

by:
bingie earned 125 total points
Comment Utility
Try this function

Private Function DoesCommPortExist(CommPort As Integer) As Boolean
On Error Resume Next
        MSComm1.CommPort = CommPort
        MSComm1.PortOpen = True
        If Not (Err.Number = 8002) Then  
            MsgBox "CommPort Exists"
            DoesCommPortExist = True
        Else
            MsgBox "CommPort Does Not Exist"
            DoesCommPortExist = False
        End If
        MSComm1.PortOpen = False
End Function


Just pass the comm port number you wish to test with the following:

x = DoesCommPortExist (comm port number)

It will return a boolean true / false based on the result. You can then comment out the msgboxes.

Bingie
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now