Checking if a COM port Exists
Posted
by Dimitrios Papadopoulos
on February 5th, 2004
option Explicit
'*************************************************************
' Module : EnumPorts
' FileName : EnumPorts.BAS
' Author : Dimitrios Papadopoulos
' date Created : 10/08/00 22:07:10
'
' Copyright : 2000, Dimitrios Papadopoulos.
' All Rights Reserved.
'
' Description : Enumerates Existance of COM Ports
'
' Change History :
' 1.0 10 August 2000
' Dimitrios Papadopoulos
' Initial Version
'
'*************************************************************
Type DCB
DCBlength as Long
BaudRate as Long
fBitFields as Long
wReserved as Integer
XonLim as Integer
XoffLim as Integer
ByteSize as Byte
Parity as Byte
StopBits as Byte
XonChar as Byte
XoffChar as Byte
ErrorChar as Byte
EofChar as Byte
EvtChar as Byte
wReserved1 as Integer
End Type
Type COMMCONFIG
dwSize as Long
wVersion as Integer
wReserved as Integer
dcbx as DCB
dwProviderSubType as Long
dwProviderOffset as Long
dwProviderSize as Long
wcProviderData as Byte
End Type
'
Declare Function GetDefaultCommConfig Lib "kernel32" _
Alias "GetDefaultCommConfigA" (byval lpszName as string, _
lpCC as COMMCONFIG, lpdwSize as Long) as Long
'
public Function EnumSerPorts(port as Integer) as Long
'this function returns non-zero value if the port exists
Dim cc as COMMCONFIG, ccsize as Long
'
ccsize = LenB(cc) 'gets the size of COMMCONFIG structure
'
EnumSerPorts = GetDefaultCommConfig("COM" + Trim(Str(port)) + _
Chr(0), cc, ccsize)
'
End Function

Comments
Checking if a COM port Exists
Posted by Rowlama1 on 07/14/2005 06:08pmExcellent code -- just what I needed for a project!
Replydoubt about creating a classmodule
Posted by Legacy on 01/11/2004 12:00amOriginally posted by: suresh
dear sir/madam
I like to know about how connect a database using class module with VB.Kindly clear my doubt
suresh.s
ReplyRead OutBuffer or InBuffer that other Program is using
Posted by Legacy on 01/11/2004 12:00amOriginally posted by: Duong Van Tuan
ReplyRead the Printer Port
Posted by Legacy on 12/28/2003 12:00amOriginally posted by: Rohit
Hi
I have a query that i want to calculate the no of pages that i have printer in a day, ie if i printed 5 DOC pages and 5 text pages then my code should display 10 printouts in a day and the other query is that i want to capture the the no of documents scanned in a day, it would be a great help for me
Thank You
ReplyPeople please don't ask generic questions
Posted by Legacy on 11/18/2003 12:00amOriginally posted by: Dimitrios Papadopoulos
After posting that original source snippet about COM port enumeration, quite a few people were asking me on how to access the COM ports. I soon realized that what they wanted was a way to start with serial port programming. So I provided a half-working example (see http://www.codeguru.com/vb/comments/1743.shtml) for serial port acess in character mode. This is all the help I could offer on questions like "I don't know how to communicate with serial ports, please help". My available time is too limited for answering private questions or general guides to VB programming.
After reading the link above, one should be able to adapt the offered code to his needs. Of course, some elementary skills in VB commands, controls and forms are required.
Reply
how to use mscomm
Posted by Legacy on 11/15/2003 12:00amOriginally posted by: galih
dear sir
I'M VERY HAPPY IF U WOULD LIKE TO HELP ME FOR MY FINAL TASK IN UNIVERSITY. HOW TO CONNECT VISUAL BASIC WITH MICROCONTROLLER BOARD ( MINIMUM SYSTEM BOARD )ATMEL 89C51. I WOULD LIKE TO MAKE A WATER RECHARGE TO THE BOTTLE SIMULATION WITH VISUAL BASIC ,USING THREE MOTOR STEPPER.
THANK VERY MUCH FOR YOUR HELP. I'M WAITING FOR YOUR E-MAIL.THANX
ReplyGALIH
saving data from com port
Posted by Legacy on 10/25/2003 12:00amOriginally posted by: Arif
Replyabout accessing serial ports with VB
Posted by Legacy on 09/22/2003 12:00amOriginally posted by: Dimitrios Papadopoulos
Very often, people ask me how to access serial ports in VB. There are some useful examples in MSDN library cd but one might have to find his way through the complexities of the serial port interface. Novices coming from Quick Basic face quite a few difficulties because acessing the port isn't as straightforward as it was in Quick Basic. First of all, you must put a MSCOMM control in your main form (or in the form where com ports are accessed). Then, you open the port. After that, accessing is similar to that in QB, having in mind that MSCOMM is an object with properties that can be set on either program design on run-time. So, when you want to sent something, instead of assigning values to a numbers that represent a port (e.g. #1), you assign the value to a MSCOMM property. E.g.:
MSComm1.Output = "Hello"
Handling (sending and receiving) characters is much easier than handling bytes (which requires some awful array handling, it's a shame that MS kept that in VB too).
Receiving with MSCOMM is similar to QB. There are two methods: event driven and polling. In the event driven method, you define a handling routine inside the form where MSCOMM control exists (similar to the way ON COMM GOTO was working in QB). See source examples in MSDN for the naming In the polling method, you simply read the port until a character appears.
Here is a simplified example (not complete and ready for usage), where I assume the MSCOMM control is named MSComm1. Handshake method has been left to default (hardware handshake, I think) which means that one must provide the necessary hardware connections:
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "19200,n,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0
End Sub
===============================================
Private Sub SerialOut(chrSerOut As string)
'Call this routine when you want to sent chars to the port
'provide necessary error handling here
MSComm1.Output = chrSerOut
End Sub
===============================================
Private ThrashInput()
'It empties the com input buffer. Use it if needed
Dim chrTrash
MSComm1.InputLen = 0
chrTrash = MSComm1.Input
End Sub
===============================================
Private Function SerialInPolling()
' It returns the characters in the input buffer
' (using the polling method)
Dim Timer1
Timer1 = Timer
Do While (Timer1 - Timer) > 0 And MSComm1.InBufferCount = 0
DoEvents 'For preventing system freeze
Loop
If MSComm1.InBufferCount = 0 Then
MsgBox = "COM receive timeout"
End If
SerialIn = MSComm1.Input
End Function
=================================================
Private On_Comm()
' It must be used when event driven method is needed
' for reading the receiving characters from the buffer
Dim Timer1, SerialInBuff
Timer1 = Timer
Do While (Timer1 - Timer) > 0 And MSComm1.InBufferCount = 0
SerialInBuff = MSComm1.Input
DoEvents 'For preventing system freeze
Loop
End Sub
=================================================
In case one needs to access the MSCOMM control from another module, it must be included in the subroutine arguments. In that case, the above SendSerialOut routine becomes as following:
Public SendSerialOut(MSComm1 as MSComm, chrSerOut As string)
Reply'Call this routine when you want to sent chars to the port
MSComm1.Output = chrSerOut
End Sub
disable com port with vb
Posted by Legacy on 09/20/2003 12:00amOriginally posted by: ashraf
hello to everybody,
Replyhere i need help urgently.does any one know to disable physical comm port by using visual basic?may be someone can guide me by giving sample code.before this i just disable physical comm port manually in device manager.i hope my comment will be hear...
want To Know about PABX and Com port data Exchage
Posted by Legacy on 12/09/2002 12:00amOriginally posted by: rakib
Hai
i am rakib.
i have a problem.
I want to make a software that will work with
com and PABX system.
I have used the ocx that needs for the communication .
But still i have some problem.My data mak 6 or 7% losted
every time.
So any one please help me within short time.
ReplyI will be greatefull to him.
( rakib )
Loading, Please Wait ...