| Determining Information on Communications Ports
and Modems I had a letter asking me how can you return the number of Data-Bits, Stop-Bits and the parity of a specified port, i.e. the Communications port in which your modem is connected to. This can be simply done using the CreateFile API to open up the port and then the GetCommState API to query information about the COM port. Below is an example showing you how you can receive these three properties. There are actually more that you can return like the baud rate and more. Option ExplicitType 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 Const NOPARITY = 0 Const ODDPARITY = 1 Const EVENPARITY = 2 Const MARKPARITY = 3 Const SPACEPARITY = 4 Const ONESTOPBIT = 0 Const ONE5STOPBITS = 1 Const TWOSTOPBITS = 2 Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, _ ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile _ As Long) As Long Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Dim tDCB As DCB Function GetPortInfo(sCommPort As String) As Long Dim lHandle As Long, RetVal As Long lHandle = CreateFile(sCommPort, &HC0000000, 0, 0, 3, 0, 0) If lHandle = -1 Then GetPortInfo = 0: Exit Function Call GetCommState(lHandle, tDCB) lHandle = CloseHandle(lHandle) With tDCB Debug.Print "--- Properties ---" Debug.Print "Data Bits: " & .ByteSize Select Case .Parity Case NOPARITY: Debug.Print "Parity: No Parity" Case ODDPARITY: Debug.Print "Parity: Odd" Case EVENPARITY: Debug.Print "Parity: Even" Case MARKPARITY: Debug.Print "Parity: Mark" Case SPACEPARITY: Debug.Print "Parity: Space" End Select Select Case .StopBits Case ONESTOPBIT: Debug.Print "Stop Bits: 1" Case ONE5STOPBITS: Debug.Print "Stop Bits 1 1/2" Case TWOSTOPBITS: Debug.Print "Stop Bits: 2" End Select End With GetPortInfo = 1 End Function Call the function like the following If GetPortInfo("<PortName>") = 0 then Msgbox "Could not open port", vbCritical |
___________________________________________ Send Bugs, comments to the webmaster at webmaster@mwe.8m.com © 1999 MW Software, all rights reserved |