2011-12-19 9 views
6

Sto cercando un modo per determinare se una COM è una COM standard o se è una COM SPP, nota anche come adattatore bluetooth sostitutivo via cavo per un dispositivo COM.Determinare se la porta seriale è normale COM o SPP

Ho un dispositivo che funziona sia in USB (COM -> USB) e Bluetooth, e l'interfaccia Bluetooth funziona con SPP.

Attualmente sto usando System.IO.Ports.SerialPort.GetPortNames() per ottenere i COM.

C'è un modo per determinare se è connesso o meno con Bluetooth o USB?

SOLUZIONE:

System.Management.ManagementObjectSearcher Searcher = new System.Management.ManagementObjectSearcher("Select * from WIN32_SerialPort"); 
foreach (System.Management.ManagementObject Port in Searcher.Get()) 
{ 
    foreach (System.Management.PropertyData Property in Port.Properties) 
    { 
     Console.WriteLine(Property.Name + " " + (Property.Value == null ? null : Property.Value.ToString())); 
    } 
} 

e l'uscita è qualcosa di simile:

Availability 2 
Binary True 
Capabilities 
CapabilityDescriptions 
Caption Standard Serial over Bluetooth link (COM10) 
ConfigManagerErrorCode 0 
ConfigManagerUserConfig False 
CreationClassName Win32_SerialPort 
Description Standard Serial over Bluetooth link 
DeviceID COM10 
ErrorCleared 
ErrorDescription 
InstallDate 
LastErrorCode 
MaxBaudRate 9600 
MaximumInputBufferSize 0 
MaximumOutputBufferSize 0 
MaxNumberControlled 
Name Standard Serial over Bluetooth link (COM10) 
OSAutoDiscovered True 
PNPDeviceID BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\8&3062A492&0&000000000000_0000001C 
PowerManagementCapabilities System.UInt16[] 
PowerManagementSupported False 
ProtocolSupported 
ProviderType RS232 Serial Port 
SettableBaudRate True 
SettableDataBits True 
SettableFlowControl True 
SettableParity True 
SettableParityCheck False 
SettableRLSD True 
SettableStopBits True 
Status OK 
StatusInfo 3 
Supports16BitMode False 
SupportsDTRDSR True 
SupportsElapsedTimeouts True 
SupportsIntTimeouts True 
SupportsParityCheck False 
SupportsRLSD True 
SupportsRTSCTS True 
SupportsSpecialCharacters False 
SupportsXOnXOff False 
SupportsXOnXOffSet False 
SystemCreationClassName Win32_ComputerSystem 
SystemName JVALDRON-PC 
TimeOfLastReset 
+0

Guarda i miei risposte a http://stackoverflow.com/questions/2085179/ how-can-i-find-out-a-com-port-number-of-a-bluetooth-device-in-c/2096728 # 2096728 e http://stackoverflow.com/questions/6850965/how-come- getdefaultcommconfig-doesnt-work-with-bluetooth-spp-devices/6997320 # 6997320 – alanjmcf

risposta

6

non si riesce a trovare queste informazioni tramite la classe SerialPort. Dovresti eseguire una query WMI.

fare qualcosa sulla falsariga di questo può portare ad esso

ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * 
             from WIN32_SerialPort"); 

foreach(ManagementObject Port in searcher.Get()) { 

     string a = (string) Port.GetPropertyValue("Name"); 

} 

non ho il codice caricato in modo da non so quali altre proprietà che è possibile ottenere. Tuttavia, se ci fosse comunque, WMI sarebbe il modo per farlo.

+0

Perfetto! Pubblicherò il codice che ho usato e l'output se qualcun altro ha bisogno. – jValdron

+0

Fantastico vederlo funzionare. –

0

vedo il tuo guardando un dispositivo Bluetooth collegato:

Query il Win32_PnPSignedDriver e guardare la proprietà InfName. Il valore dovrebbe essere bthspp.inf

Non posso dire con certezza che il file inf sarà SEMPRE questo nome per il dispositivo Bluetooth di ogni produttore che supporta il protocollo SPP, ma questo è l'impostazione predefinita.

Classe GUID per COM & porte LPT è: {4d36e978-E325-11CE-BFC1-08002BE10318} Rif: https://msdn.microsoft.com/en-us/library/windows/hardware/ff553426

ManagementObjectSearcher Searcher = new ManagementObjectSearcher(computer + @"root\cimv2", 
       "SELECT * FROM Win32_PnPSignedDriver " 
      + "WHERE ClassGuid = '{4d36e978-e325-11ce-bfc1-08002be10318}' " 
      +  AND DeviceID LIKE 'BTHENUM%' 
      );