Hi,
I have a weird problem.
I have many applications that use COM port. I use either FTDI Usb to Serial, or STM32 Virtual COM port. I have used both successfully for a long time, suddenly COM port derived from STM32 Virtual COM port is no longer recognized by CVI (but it does by device manager and terminal application such as docklight).
I use code I got from this forum to populate a list of available COM ports (always worked perfectly until now), I will present the code below. The code no longer returns with the STM32 Virtual COM port number (It always did), so I tried to force the code to see the COM port exists (I force "com_port = 4" since it is COM4), and sometimes it works and most of the time OpenComConfig returns with -6 even though the COM port exists.
Any ideas?
Here is the code that lists the COM ports (it populates a menu ring):
if (number_of_found_ports)
{
for(i = 0 ; i < number_of_found_ports ; i++)
{
sprintf(str , "COM %d", portlist[i]) ;
InsertListItem(main_panel_handle , kPANEL_COM_RING , -1 , str , i) ;
}
GetLabelFromIndex(main_panel_handle, kPANEL_COM_RING, 0, selected_com_port);
strcpy(selected_com_port, &selected_com_port[4]);
com_port = atoi(selected_com_port);
com_port_selected = 1;
}
Here is the COM port initialization, OpenComConfig returns with -6
OpenComConfig(com_port, "", COM_BAUDRATE, NO_PARITY, DATA_BITS_8, ONE_STOP_BIT, INPUT_QUEUE, NO_OUTPUT_QUEUE);
SetCTSMode(com_port, LWRS_HWHANDSHAKE_OFF);
This is the code I downloaded from this forum a while back to get a list of available COM ports:
static HDEVINFO BeginEnumeratePorts(VOID)
{
BOOL guidTest = FALSE;
DWORD RequiredSize = 0;
HDEVINFO DeviceInfoSet;
char *buf;
guidTest = SetupDiClassGuidsFromNameA("Ports", 0, 0, &RequiredSize);
if(RequiredSize < 1)return INVALID_HANDLE_VALUE;
buf = malloc(RequiredSize * sizeof(GUID));
guidTest = SetupDiClassGuidsFromNameA("Ports", (GUID *)buf, RequiredSize * sizeof(GUID), &RequiredSize);
if(!guidTest)return (HANDLE) - 1;
DeviceInfoSet = SetupDiGetClassDevs((GUID *)buf, NULL, NULL, DIGCF_PRESENT);
if(DeviceInfoSet == INVALID_HANDLE_VALUE)
return INVALID_HANDLE_VALUE;
free(buf);
return DeviceInfoSet;
}
int LocateCom(char *inval, int *list, int maxnum)
{
DWORD i;
SP_DEVINFO_DATA DeviceInfoData = {0};
int portsfound = 0;
HDEVINFO hDevInfo = BeginEnumeratePorts();
if (INVALID_HANDLE_VALUE == hDevInfo)
return 0;
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,&DataT,
(PBYTE)buffer,
buffersize,&buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer)
free(buffer);
buffer = malloc(buffersize);
}
else
{
if (buffer)
free(buffer);
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
}
if (NULL == buffer)
{
if (buffersize > 0)
{
buffer = malloc(buffersize);
SetupDiGetDeviceRegistryProperty(hDevInfo,&DeviceInfoData,
SPDRP_FRIENDLYNAME,&DataT,
(PBYTE)buffer,
buffersize,&buffersize);
}
}
// We may have found the device
if ((NULL != buffer) && strstr(buffer, inval))
{
char *xval;
xval = strstr(buffer, "COM");
if (xval != NULL)
{
int d;
if ((1 == sscanf(xval, "COM%d", &d)) && (d >= 1) && (d <= 255))
{
if ((NULL == list) || (maxnum <= 0))
{
free(buffer);
SetupDiDestroyDeviceInfoList(hDevInfo);
return d;
}
else
{
if (portsfound < maxnum)
list[portsfound++] = d;
}
}
}
}
if (buffer) free(buffer);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return portsfound;
}
Any help will be greatly appreciated