achar porta serial usada por um dispositivo

Enviado:
06 Set 2008 15:14
por leandrosilva
Pessoal alguem sabe como faço para encontrar qual a porta serial usada por um dispositivo bluetooth? tenho que fazer um programa que quando eu conecto um dispositivo bluetooth no pc o programa fala qual a porta serial que o dispositivo está usando

Enviado:
06 Set 2008 16:53
por joao
Na internet achei este código:
- Código: Selecionar todos
int WinFindCOMPortMappedToBluetoothDevice(
const BLUETOOTH_ADDRESS &btAddr,
cppstring *pCOMPortString)
{
int status = 0;
DWORD reqGuids = 16;
GUID guids[16];
HDEVINFO devs = INVALID_HANDLE_VALUE;
unsigned int ii;
bool bPortFound = false;
DWORD devIndex;
SP_DEVINFO_DATA devInfo;
devInfo.cbSize = sizeof(devInfo);
TCHAR tmpstring[60];
TCHAR hardware_id_string[300];
DWORD propertyType;
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
//Create search string that we are looking for from bluetooth address.
wsprintf(tmpstring, GSTD_S("%02x%02x%02x%02x%02x%02x"),
btAddr.rgBytes[5], btAddr.rgBytes[4], btAddr.rgBytes[3],
btAddr.rgBytes[2], btAddr.rgBytes[1], btAddr.rgBytes[0]);
CString searchString = tmpstring;
searchString.MakeUpper();
CString hwParsingString;
if (SetupDiClassGuidsFromNameEx(GSTD_S("ports"), guids, reqGuids, &reqGuids, NULL, NULL))
{
for (ii = 0; (ii < reqGuids) && (!bPortFound); ii++)
{
devs = SetupDiGetClassDevsEx(&guids[ii],NULL,NULL,DIGCF_PRESENT,NULL,NULL,NULL);
if (devs != INVALID_HANDLE_VALUE)
{
BOOL bSuccess = SetupDiGetDeviceInfoListDetail(devs, &devInfoListDetail);
if (bSuccess)
{
devIndex = 0;
while (SetupDiEnumDeviceInfo(devs,devIndex,&devInfo) && (!bPortFound))
{
status = CM_Get_Device_ID_Ex(devInfo.DevInst, hardware_id_string, 300, 0, devInfoListDetail.RemoteMachineHandle);
if (CR_SUCCESS == status)
{
hwParsingString = hardware_id_string;
if (hwParsingString.Find(searchString) >= 0)
{
bSuccess = SetupDiGetDeviceRegistryProperty(devs, &devInfo,
SPDRP_FRIENDLYNAME, &propertyType,
(PBYTE) hardware_id_string, sizeof(hardware_id_string), NULL);
if (bSuccess)
{
hwParsingString = hardware_id_string;
int firstIndex = hwParsingString.Find(GSTD_S("(COM"));
if (firstIndex >= 0)
{
firstIndex++;
int lastIndex = hwParsingString.Find(GSTD_S(")"), firstIndex);
if (lastIndex > firstIndex)
{
*pCOMPortString = hwParsingString.Mid(firstIndex, lastIndex - firstIndex);
bPortFound = true;
}
}
}
}
}
devIndex++;
}
}
SetupDiDestroyDeviceInfoList(devs);
}
}
}
return (bPortFound ? 0 : -1);
}
retirei ele daqui:[url]
http://www.tech-archive.net/Archive/Dev ... 00025.html[/url]
Acho que ele vai funcionar para o teu caso. A nao ser que tenha mais do que um dispositivo bluetooth ligado ao computador.
[]'s

Enviado:
07 Set 2008 14:03
por leandrosilva
obrigado
vou testá-lo. Eu desenvolvi um hardware que faz comunicação via bluetooth com o pc, e estou fazendo um software para comunicar com o hardware e uma das funções do software é detectar automaticamente qual porta é usada pelo hardware.
vou testar e posto o resultado