
// Sources: http://www.ftdichip.com/Support/Knowledgebase/index.html
// http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/VC.htm
// See also: AN FT_000071 D2XX Programmers Guide Version 1.2
// Compilation: Microsoft Visual C++ .NET Version 7.1.3088 (2003), Win32-Console

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "ftd2xx.h"			// please install FTD2xx-Library before use
#pragma comment(lib,"ftd2xx.lib")

int main(int argc, char* argv[])
{
refresh:
	unsigned long NumDevices = 0, LocID = 0, ChipID = 0;

	printf("\n\nUSB <-> Serial Port\nFTDI-Device Finder\n");
	printf("www.gfai.de/~heinz/techdocs\n");	
	printf("Sources: FTDI-Appnotes and examples for D2XX\n\n");	

	FT_STATUS ftStatus;	
	FT_HANDLE fthandle;						
	FT_STATUS comp;							
	LONG COMPORT;							
	FT_DEVICE_LIST_INFO_NODE *devInfo;
	DWORD numDevs;

	// create the device information list
	ftStatus = FT_CreateDeviceInfoList(&numDevs);
		if (ftStatus == FT_OK) {
			printf("\nConnected USB Serial Port Device(s): %d\n\n",numDevs);
		}

		if (numDevs > 0) {

			// allocate storage for list based on numDevs
			devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs);

			// get the device information list
			ftStatus = FT_GetDeviceInfoList(devInfo,&numDevs);
			if (ftStatus == FT_OK) {
				for (int i = 0; i < (int) numDevs; i++) {
					//printf("Device %d:",i);

					// get the COMPORT-number
					comp = FT_Open(i, &fthandle);
						comp = FT_GetComPortNumber(fthandle,&COMPORT);
						printf("\nCOM%d\t", COMPORT);
					FT_Close(fthandle);

					// get the device informations
					printf("%s\n",devInfo[i].Description);
					printf("\tS/N \t%s\n",devInfo[i].SerialNumber);

					// Serial-Number Code Key:	XY_xxxxxx
					//	Version X:	1/2/3/...
					//				v1:  19	kBaud	FT232RL
					//				v2: 900 kBaud	FT232RL
					//				v3:  12 MBaud	FT232H
					//	Typ Y:	Simplex/Duplex/Uart (S/D/U)
					//	xxxxxx: auto-generated, individual
					// Examples: 1S_, 2U_, 3D_ ...

					//printf("\tFlags \t0x%x\n",devInfo[i].Flags);
					//printf("\tType \t0x%x\n",devInfo[i].Type);
					printf("\tID \t0x%x\n",devInfo[i].ID);
					printf("\tLocId \t0x%x\n",devInfo[i].LocId);
					//printf("\tftHandl\t0x%x\n",devInfo[i].ftHandle);
				}
			}
		free(devInfo);
		}

	printf("\n\nHit any key to refresh the list\n");
	getchar();
	goto refresh;

	return 0;
}