Newsgroup: it.comp.lang.visual-basic
Titolo: Porte seriali

Come posso fare per sapere quante e quali porte seriali ho installate su una
macchina?
Grazie mille.

 ha scritto nel messaggio
news:On1_a.238555$Ny5.7...@twister2.libero.it...
: Come posso fare per sapere quante e quali porte seriali ho installate su
una
: macchina?
:
: Grazie mille.
Ho postato ieri in risposta ad una persona che aveva le stesse necessità.
Il thread si chiama "LTP e Vb".
Non ho provato la prima soluzione, ma con le EnumPorts API funziona a
dovere.
Ciao, Luca.

 ha scritto nel messaggio
news:E%5_a.239472$lK4.7...@twister1.libero.it...


Copia, incolla e studia...(da 
allapi.net
)
Private Type PORT_INFO_2
    pPortName As String
    pMonitorName As String
    pDescription As String
    fPortType As Long
    Reserved As Long
End Type
Private Type API_PORT_INFO_2
    pPortName As Long
    pMonitorName As Long
    pDescription As Long
    fPortType As Long
    Reserved As Long
End Type
Private Declare Function EnumPorts Lib "winspool.drv" Alias "EnumPortsA"
(ByVal pName As String, ByVal Level As Long, ByVal lpbPorts As Long, ByVal
cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As
Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As
Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long,
ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal
dwFlags As Long, lpMem As Any) As Long
Dim Ports(0 To 100) As PORT_INFO_2
Public Function TrimStr(strName As String) As String
    'Finds a null then trims the string
    Dim x As Integer
    x = InStr(strName, vbNullChar)
    If x > 0 Then TrimStr = Left(strName, x - 1) Else TrimStr = strName
End Function
Public Function LPSTRtoSTRING(ByVal lngPointer As Long) As String
    Dim lngLength As Long
    'Get number of characters in string
    lngLength = lstrlenW(lngPointer) * 2
    'Initialize string so we have something to copy the string into
    LPSTRtoSTRING = String(lngLength, 0)
    'Copy the string
    CopyMem ByVal StrPtr(LPSTRtoSTRING), ByVal lngPointer, lngLength
    'Convert to Unicode
    LPSTRtoSTRING = TrimStr(StrConv(LPSTRtoSTRING, vbUnicode))
End Function
'Use ServerName to specify the name of a Remote Workstation i.e.
"//WIN95WKST"
'or leave it blank "" to get the ports of the local Machine
Public Function GetAvailablePorts(ServerName As String) As Long
    Dim ret As Long
    Dim PortsStruct(0 To 100) As API_PORT_INFO_2
    Dim pcbNeeded As Long
    Dim pcReturned As Long
    Dim TempBuff As Long
    Dim i As Integer
    'Get the amount of bytes needed to contain the data returned by the API
call
    ret = EnumPorts(ServerName, 2, TempBuff, 0, pcbNeeded, pcReturned)
    'Allocate the Buffer
    TempBuff = HeapAlloc(GetProcessHeap(), 0, pcbNeeded)
    ret = EnumPorts(ServerName, 2, TempBuff, pcbNeeded, pcbNeeded,
pcReturned)
    If ret Then
        'Convert the returned String Pointer Values to VB String Type
        CopyMem PortsStruct(0), ByVal TempBuff, pcbNeeded
        For i = 0 To pcReturned - 1
            Ports(i).pDescription =
LPSTRtoSTRING(PortsStruct(i).pDescription)
            Ports(i).pPortName = LPSTRtoSTRING(PortsStruct(i).pPortName)
            Ports(i).pMonitorName =
LPSTRtoSTRING(PortsStruct(i).pMonitorName)
            Ports(i).fPortType = PortsStruct(i).fPortType
        Next
    End If
    GetAvailablePorts = pcReturned
    'Free the Heap Space allocated for the Buffer
    If TempBuff Then HeapFree GetProcessHeap(), 0, TempBuff
End Function
Private Sub Form_Load()
    Dim NumPorts As Long
    Dim i As Integer
    'Get the Numbers of Ports in the System
    'and Fill the Ports Structure
    NumPorts = GetAvailablePorts("")
    'Show the available Ports
    Me.AutoRedraw = True
    For i = 0 To NumPorts - 1
        Me.Print Ports(i).pPortName
    Next
End Sub
Ciao, Luca.

 ha scritto nel messaggio
news:qPb_a.240108$lK4.7...@twister1.libero.it...


 ha scritto nel messaggio
news:JLe_a.240331$lK4.7...@twister1.libero.it...
: Copia, incolla e studia lo avevo già fatto, ma non ne ricavo un ragno dal
: buco.
: Ma funziona con W2000????
Qui, a casa, non ho un win2000, ma proverò su XP
: Vorrei ricavare soltanto le porte seriali hardware installate, non gli
spool
: del sistema.
Su win98 va che è una meraviglia, mi rileva le 2 porte COM e la LPT.
: Grazie di nuovo.
Prego.
Ciao, Luca.

del sistema.
Con Windows 2000 puoi usare QueryDosDevice, funziona anche in visual
basic:
<code>
Option Explicit
Private Declare Function QueryDosDevice Lib "kernel32.dll" _
	Alias "QueryDosDeviceA" _
 	(ByVal lpDeviceName As String, ByVal lpTargetPath As String, _
 	ByVal ucchMax As Long) _
 	As Long
Sub Main()
    Dim bufDevices As String
    Dim chars As Long
    Dim pos As Long
    Dim lenBuffer As Long
 
    lenBuffer = 65535		' spero che basti, sono 64K
    bufDevices = Space(lenBuffer)
' passando stringa nulla vengono estratte tutte le porte 
' senza distinzione, che verranno scritte nel buffer
    chars = QueryDosDevice(vbNullString, bufDevices, lenBuffer)
 
    If (chars) Then
        pos = 1
        ' esegue un loop sul buffer per cercare solo le COM
	Do
	    pos = InStr(pos, bufDevices, "COM", vbBinaryCompare)
            If (pos = 0) Then Exit Do
            If ((pos + 3) >= lenBuffer) Then Exit Do
            If (IsNumeric(Mid$(bufDevices, pos + 3, 1))) Then
                ' ha trovato una COM
                MsgBox Mid$(bufDevices, pos, _
			InStr(pos, bufDevices, " ", _
			vbBinaryCompare) - pos)
            End If
            ' si porta avanti della lunghezza della parola "COM"
	    pos = pos + 3
        Loop
    End If
 
End Sub
</code>

"Cancers"  ha scritto nel messaggio

Ciao
Con queste poche righe di codice riesci a sapere quante com sono
disponibili sul PC
On Error GoTo nocomm
  For I = 1 To 4
        Me.PortaSeriale.CommPort = I
        Me.PortaSeriale.PortOpen = True
        Me.PortaSeriale.PortOpen = False
        Select Case I
               Case 1
                    ..
               Case 2
                    ..
               Case 3
                    ..
               Case 4
                    ..
        End Select
fineCom:
  Next I
  Exit Sub
nocomm:
    Resume fineCom
ciao