Hi,
How does the K8090.exe demo program identify which COM port the board is connected to? Is it able to do this without polling through all possible COM ports?
Hi,
How does the K8090.exe demo program identify which COM port the board is connected to? Is it able to do this without polling through all possible COM ports?
This is what I have come up with for my own project, written in Borland Turbo C++ 2006:
#define CMD_FIRMWARE_VERSION 0x71 // not included in standard header file
#define K8090_FIRMWARE 0x71001006 // K8090 firmware response
HK8090 hRelay; // handle to K8090 relay board
HWND hWnd; // handle to window
int __fastcall ConnectK8090()
{
int return_value = 1; // set return_value for fail by default
BOOL connected = FALSE;
char comport[10];
int i = 0; // start search from COM1
MSG lpMsg;
hWnd = SDIAppForm->Handle;
while((!connected) && (i < 20)) // stop search at COM20
{
i++;
sprintf(comport, "COM%1d", i);
hRelay = NULL;
hRelay = OpenDevice(comport);
if ((hRelay != NULL) && (hRelay != INVALID_HANDLE_VALUE))
{
RegisterListener(hRelay, hWnd);
SendCommand(hRelay, CMD_FIRMWARE_VERSION, 0x00, 0x00, 0x00);
Sleep(500);
if( PeekMessage(&lpMsg, hWnd, WM_K8090_EVENT, WM_K8090_EVENT, PM_REMOVE)
&& (lpMsg.lParam == K8090_FIRMWARE) )
{
connected = TRUE;
return_value = 0;
}
UnregisterListener(hRelay);
}
}
if(!connected) ShowMessage("Unable to connect to K8090 Relay board.");
return return_value;
}
The code polls through the COM ports. The OpenDevice() function does not appear to distinguish the K8090 from other serial devices so, having obtained a valid device handle, the code then sends a request to query the firmware version; waits 500 ms; and sees if a response with the correct firmware data has been obtained.
If anyone has suggestions for improvements please let me know.
It’s quite a complicated bit of code but the demo application uses the Microsoft Setup API to enumerate USB class devices with a specific hardware ID that contains the correct VID/PID combination. We then use this device information to query the port name from the registry.
Functions used are:
SetupDiGetClassDevs
SetupDiEnumDeviceInterfaces
SetupDiGetDeviceInterfaceDetail
SetupDiOpenDevRegKey
SetupDiGetDeviceRegistryProperty
USB class GUID:
A5DCBF10-6530-11D2-901F-00C04FB951ED
String we search for:
USB\Vid_10CF&Pid_8090
I’ll see what I can do to provide some code in C, because finding out how this works from scratch can be quite a pain
I’ve uploaded a piece of source code in C that demonstrates how to detect which K8090 devices are connected to your computer. This piece of code will also work for all other kits that work with USB and create a virtual COM port (VMB1USB, K8090, …).
The example uses the Microsoft Setup API and you will need Microsoft Visual Studio to be able to compile it. It might be able to work in DevCpp also, but i have not tried.
Be weary that this source code is for advanced users.
If you want to detect when a device is plugged in, you can try the following Windows API functions:
RegisterDeviceNotification
UnregisterDeviceNotification
Pass it a window handle and capture DBT_DEVICEREMOVECOMPLETE and DBT_DEVICEARRIVAL messages.
The code in usbfind.c works a treat. Many thanks for that The only change I had to make was to cast the value returned by the HeapAlloc() function to the required type:
DevIntfDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)
HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);