Serial Port Detection

Discussion in 'Software' started by ceal21c, Jun 20, 2006.

  1. ceal21c

    ceal21c Private E-2

    Hey Guys,

    Can someone help me with VB6 code that will detect all available serial com ports and their associated speeds?
     
  2. ceal21c

    ceal21c Private E-2

    Hey Everyone,

    I'm still fighting with this code...Here is what I managed to come up with so far but it does not quite solve my problem.

    What happens is all ports (1 thru 16) return a false state with .PortOpen eventhough the only Com Port (COM1) that I have installed works fine. Shouldn't COM1 at least return a True VALUE?

    Private Sub Form_Load()
    txtPort.Text = ""
    End Sub

    Private Sub ocxTimer_Timer()
    Static intPortNumber As Integer

    intPortNumber = intPortNumber + 1

    If intPortNumber <= 16 Then
    ocxPort.CommPort = intPortNumber

    'Write current port name to txtPort
    txtPort.Text = "COM" & intPortNumber

    If ocxPort.PortOpen Then
    chkPortOpen.Value = vbChecked
    Else
    chkPortOpen.Value = vbUnchecked
    End If

    End If

    End Sub

    DOES ANYONE SEE THE PROBLEM WITH THIS CODE, OR WITH ME? LOL!
     
  3. ceal21c

    ceal21c Private E-2

    Hey Everyone,

    I can't take full credit for this code, I received help from a guru in another forum, THANX BILL. What you want to do is add a combo box to your form that will be used to display the list of available COM ports.

    'I wanted this code to process when the form is loaded you may not...

    Private Sub Form_Load()

    Dim intComPortExist As Long
    Dim i As Integer

    For i = 1 To 16

    intComPortExist = EnumComPorts(i)

    If intComPortExist > 0 Then

    cboPortName.AddItem "COM" & Trim(Str(i))

    End If

    Next i

    End Sub


    'Add this function to your form also:


    Public Function EnumComPorts(port As Integer) As Long

    Dim cp As COMMCONFIG, cpsize As Long

    cpsize = LenB(cp) 'gets the size of COMMCONFIG structure

    EnumComPorts = GetDefaultCommConfig("COM" + Trim(Str(port)) + _
    Chr(0), cp, cpsize)
    End Function


    'Finaly, create a module and add this code to it...


    Option Explicit

    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

    Hope this helps someone, LATA!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds